1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Property-based tests using proptest (BLUE11 R3.8).
//!
//! These tests use random input to verify widget invariants hold
//! across a wide range of inputs.
//!
//! Run with: cargo test --test property_based_tests
// proptest (and its rand/getrandom chain) cannot compile for wasm32.
#![cfg(not(target_arch = "wasm32"))]
use proptest::prelude::*;
use rust_widgets::core::{Color, Point, Rect};
// ── Arbitrary implementations for core types ──
proptest! {
/// Color RGBA components are always in u8 range (guaranteed by type).
/// Roundtrip property: creating a Color from components then reading
/// them back yields the original values.
#[test]
fn color_rgba_roundtrip(r in 0u8.., g in 0u8.., b in 0u8.., a in 0u8..) {
let c = Color::rgba(r, g, b, a);
assert_eq!(c.r, r);
assert_eq!(c.g, g);
assert_eq!(c.b, b);
assert_eq!(c.a, a);
}
/// For any Rect, its center point is always contained.
#[test]
fn rect_contains_center(w: u32, h: u32) {
let w = w % 1000 + 1;
let h = h % 1000 + 1;
let r = Rect::new(0, 0, w, h);
assert!(r.contains(Point::new((w / 2) as i32, (h / 2) as i32)));
// Origin is always contained
assert!(r.contains(Point::new(0, 0)));
// Points outside width/height should NOT be contained
assert!(!r.contains(Point::new(w as i32, 0)));
assert!(!r.contains(Point::new(0, h as i32)));
}
/// Rect intersection is always commutative.
#[test]
fn rect_intersection_commutative(x in 0i32..1000i32, y in 0i32..1000i32, w in 1u32..500u32, h in 1u32..500u32) {
let a = Rect::new(x, y, w, h);
let b = Rect::new(x + (w as i32 / 2), y + (h as i32 / 2), w, h);
let ab = a.intersection(&b);
let ba = b.intersection(&a);
assert_eq!(ab, ba, "intersection must be commutative");
}
/// For any Color, premultiplied alpha with itself should produce the same color.
#[test]
fn color_premultiplied_invariants(r in 0u8.., g in 0u8.., b in 0u8.., a in 0u8..) {
let c = Color::rgba(r, g, b, a);
let _ = c; // Structure: invariant is that type construction never panics
}
}