1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod tests {
5
    use core::cmp::Ordering;
6

            
7
    use super::*;
8

            
9
    #[test]
10
1
    fn hit_test_edges_match_contains() {
11
1
        let r = LogicalRect::new(
12
1
            LogicalPosition::new(10.0, 20.0),
13
1
            LogicalSize::new(30.0, 40.0),
14
        );
15
        // left/top edge: inclusive in both
16
1
        let tl = LogicalPosition::new(10.0, 20.0);
17
1
        assert!(r.contains(tl));
18
1
        assert!(r.hit_test(&tl).is_some());
19
        // just inside
20
1
        let inside = LogicalPosition::new(11.0, 21.0);
21
1
        assert!(r.contains(inside));
22
1
        assert!(r.hit_test(&inside).is_some());
23
        // right/bottom edge: exclusive in both
24
1
        let br = LogicalPosition::new(40.0, 60.0);
25
1
        assert!(!r.contains(br));
26
1
        assert!(r.hit_test(&br).is_none());
27
        // outside left
28
1
        let out = LogicalPosition::new(9.0, 20.0);
29
1
        assert!(!r.contains(out));
30
1
        assert!(r.hit_test(&out).is_none());
31
1
    }
32

            
33
    #[test]
34
1
    fn hit_test_offset_is_from_top_left() {
35
1
        let r = LogicalRect::new(
36
1
            LogicalPosition::new(10.0, 20.0),
37
1
            LogicalSize::new(30.0, 40.0),
38
        );
39
1
        let hit = r.hit_test(&LogicalPosition::new(15.0, 25.0)).unwrap();
40
1
        assert_eq!(hit, LogicalPosition::new(5.0, 5.0));
41
1
    }
42

            
43
    #[test]
44
1
    fn quantize_nan_is_distinct_from_zero() {
45
1
        assert_eq!(quantize(f32::NAN), i64::MIN);
46
1
        assert_ne!(quantize(f32::NAN), quantize(0.0));
47
1
    }
48

            
49
    #[test]
50
1
    fn partial_eq_agrees_with_ord_and_hash() {
51
        use core::hash::{Hash, Hasher};
52
        // Two values within the same quantization bucket must be == AND cmp==Equal.
53
1
        let a = LogicalPosition::new(1.00000, 2.00000);
54
1
        let b = LogicalPosition::new(1.00004, 2.00004); // < 0.001 apart
55
1
        assert_eq!(a, b);
56
1
        assert_eq!(a.cmp(&b), Ordering::Equal);
57

            
58
2
        let hash_of = |p: &LogicalPosition| {
59
2
            let mut h = std::collections::hash_map::DefaultHasher::new();
60
2
            p.hash(&mut h);
61
2
            h.finish()
62
2
        };
63
1
        assert_eq!(hash_of(&a), hash_of(&b));
64

            
65
        // NaN equals NaN under the quantized PartialEq (i64::MIN bucket) — this
66
        // is what stops the every-frame Resize loop upstream.
67
1
        let n1 = LogicalSize::new(f32::NAN, 1.0);
68
1
        let n2 = LogicalSize::new(f32::NAN, 1.0);
69
1
        assert_eq!(n1, n2);
70
1
    }
71

            
72
    #[test]
73
1
    fn quantize_saturates_instead_of_wrapping() {
74
        // Huge coordinate must saturate, not wrap to a small/negative bucket.
75
1
        assert_eq!(quantize(f32::INFINITY), i64::MAX);
76
1
        assert_eq!(quantize(f32::NEG_INFINITY), i64::MIN);
77
1
    }
78
}
79

            
80
#[cfg(test)]
81
#[allow(clippy::float_cmp)]
82
mod autotest_generated {
83
    use core::{
84
        cmp::Ordering,
85
        hash::{Hash, Hasher},
86
    };
87

            
88
    use azul_css::props::layout::LayoutWritingMode;
89
    use proptest::{prelude::*, proptest};
90

            
91
    use super::*;
92

            
93
    /// Hostile float grid: every class that can reach a coordinate field.
94
    const HOSTILE: [f32; 8] = [
95
        f32::NAN,
96
        f32::NEG_INFINITY,
97
        f32::MIN,
98
        -1.0,
99
        0.0,
100
        1.0,
101
        f32::MAX,
102
        f32::INFINITY,
103
    ];
104

            
105
    const WMS: [LayoutWritingMode; 3] = [
106
        LayoutWritingMode::HorizontalTb,
107
        LayoutWritingMode::VerticalRl,
108
        LayoutWritingMode::VerticalLr,
109
    ];
110

            
111
    fn hash_of<T: Hash>(v: &T) -> u64 {
112
        let mut h = std::collections::hash_map::DefaultHasher::new();
113
        v.hash(&mut h);
114
        h.finish()
115
    }
116

            
117
    // ---------------------------------------------------------------------
118
    // quantize: numeric / saturation / NaN
119
    // ---------------------------------------------------------------------
120

            
121
    #[test]
122
    fn quantize_zero_and_negative_zero_share_a_bucket() {
123
        assert_eq!(quantize(0.0), 0);
124
        assert_eq!(quantize(-0.0), 0);
125
        // Sign of zero must not split the bucket, or (0,0) and (-0,-0) would be
126
        // distinct HashMap keys for the same visual origin.
127
        assert_eq!(quantize(0.0), quantize(-0.0));
128
    }
129

            
130
    #[test]
131
    fn quantize_applies_the_decimal_multiplier() {
132
        assert_eq!(quantize(1.0), DECIMAL_MULTIPLIER as i64);
133
        assert_eq!(quantize(-1.0), -(DECIMAL_MULTIPLIER as i64));
134
        assert_eq!(quantize(1.5), 1500);
135
        assert_eq!(quantize(-1.5), -1500);
136
    }
137

            
138
    #[test]
139
    fn quantize_truncates_toward_zero_below_precision() {
140
        // Sub-millipixel deltas collapse into the same bucket (truncation IS the
141
        // documented rounding step) — and truncation is toward zero, not floor.
142
        assert_eq!(quantize(0.0004), 0);
143
        assert_eq!(quantize(-0.0004), 0);
144
        assert_eq!(quantize(1.0004), 1000);
145
        assert_eq!(quantize(-1.0004), -1000);
146
    }
147

            
148
    #[test]
149
    fn quantize_extremes_saturate_and_never_wrap() {
150
        // f32::MAX * 1000 overflows to +inf before the cast; the cast must clamp.
151
        assert_eq!(quantize(f32::MAX), i64::MAX);
152
        assert_eq!(quantize(f32::MIN), i64::MIN);
153
        assert_eq!(quantize(f32::INFINITY), i64::MAX);
154
        assert_eq!(quantize(f32::NEG_INFINITY), i64::MIN);
155
        // Denormal-ish tiny values must land on 0, not on a garbage bucket.
156
        assert_eq!(quantize(f32::MIN_POSITIVE), 0);
157
        assert_eq!(quantize(-f32::MIN_POSITIVE), 0);
158
    }
159

            
160
    #[test]
161
    fn quantize_nan_never_aliases_the_origin() {
162
        // The historical bug: `NaN as isize == 0` put NaN on top of (0.0, 0.0).
163
        assert_eq!(quantize(f32::NAN), i64::MIN);
164
        assert_eq!(quantize(-f32::NAN), i64::MIN);
165
        assert_ne!(quantize(f32::NAN), quantize(0.0));
166
    }
167

            
168
    #[test]
169
    fn quantize_saturation_aliases_nan_with_the_bottom_of_the_range() {
170
        // KNOWN, INTENTIONAL LOSSINESS: NaN, -inf and f32::MIN all collapse onto
171
        // the i64::MIN bucket, so they compare Equal. This keeps Ord/Eq total and
172
        // consistent (which is what the type contract needs), but callers cannot
173
        // use == to distinguish "no value" (NaN) from a huge negative coordinate.
174
        assert_eq!(quantize(f32::NAN), quantize(f32::NEG_INFINITY));
175
        assert_eq!(quantize(f32::NAN), quantize(f32::MIN));
176
        assert_eq!(
177
            LogicalPosition::new(f32::NAN, 0.0),
178
            LogicalPosition::new(f32::NEG_INFINITY, 0.0)
179
        );
180
    }
181

            
182
    #[test]
183
    fn quantize_is_monotonic_over_finite_inputs() {
184
        let ascending = [-1.0e6_f32, -1.0, -0.001, 0.0, 0.001, 1.0, 1.0e6];
185
        for w in ascending.windows(2) {
186
            assert!(
187
                quantize(w[0]) <= quantize(w[1]),
188
                "quantize inverted the order of {} and {}",
189
                w[0],
190
                w[1]
191
            );
192
        }
193
    }
194

            
195
    proptest! {
196
        #[test]
197
        fn quantize_is_deterministic_across_calls(v in proptest::num::f32::ANY) {
198
            assert_eq!(quantize(v), quantize(v));
199
        }
200
    }
201

            
202
    // ---------------------------------------------------------------------
203
    // Ord / Eq / Hash total-order contract over the hostile grid
204
    // ---------------------------------------------------------------------
205

            
206
    fn hostile_positions() -> [LogicalPosition; 64] {
207
        let mut out = [LogicalPosition::zero(); 64];
208
        let mut i = 0;
209
        for x in HOSTILE {
210
            for y in HOSTILE {
211
                out[i] = LogicalPosition::new(x, y);
212
                i += 1;
213
            }
214
        }
215
        out
216
    }
217

            
218
    proptest! {
219
        #[test]
220
        fn ord_is_reflexive_and_antisymmetric_even_with_nan(
221
            ax in proptest::num::f32::ANY, ay in proptest::num::f32::ANY,
222
            bx in proptest::num::f32::ANY, by in proptest::num::f32::ANY
223
        ) {
224
            let a = LogicalPosition::new(ax, ay);
225
            let b = LogicalPosition::new(bx, by);
226
            assert_eq!(a.cmp(&a), Ordering::Equal);
227
            assert_eq!(a, a);
228
            assert_eq!(a.cmp(&b), b.cmp(&a).reverse());
229
        }
230
    }
231

            
232
    proptest! {
233
        #[test]
234
        fn ord_is_transitive_over_the_hostile_grid(
235
            ax in proptest::num::f32::ANY, ay in proptest::num::f32::ANY,
236
            bx in proptest::num::f32::ANY, by in proptest::num::f32::ANY,
237
            cx in proptest::num::f32::ANY, cy in proptest::num::f32::ANY
238
        ) {
239
            let a = LogicalPosition::new(ax, ay);
240
            let b = LogicalPosition::new(bx, by);
241
            let c = LogicalPosition::new(cx, cy);
242
            if a.cmp(&b) == Ordering::Less && b.cmp(&c) == Ordering::Less {
243
                assert_eq!(a.cmp(&c), Ordering::Less);
244
            }
245
        }
246
    }
247

            
248
    proptest! {
249
        #[test]
250
        fn partial_eq_ord_and_hash_agree_over_the_hostile_grid(
251
            ax in proptest::num::f32::ANY, ay in proptest::num::f32::ANY,
252
            bx in proptest::num::f32::ANY, by in proptest::num::f32::ANY
253
        ) {
254
            let a = LogicalPosition::new(ax, ay);
255
            let b = LogicalPosition::new(bx, by);
256
            let eq = a == b;
257
            assert_eq!(eq, a.cmp(&b) == Ordering::Equal);
258
            assert_eq!(Some(a.cmp(&b)), a.partial_cmp(&b));
259
            if eq {
260
                assert_eq!(hash_of(&a), hash_of(&b));
261
            }
262
        }
263
    }
264

            
265
    proptest! {
266
        #[test]
267
        fn logical_size_eq_and_hash_agree_including_nan(
268
            w in proptest::num::f32::ANY, h in proptest::num::f32::ANY
269
        ) {
270
            let a = LogicalSize::new(w, h);
271
            let b = LogicalSize::new(w, h);
272
            assert_eq!(a, b);
273
            assert_eq!(a.cmp(&b), Ordering::Equal);
274
            assert_eq!(hash_of(&a), hash_of(&b));
275
        }
276
    }
277

            
278
    #[test]
279
    fn logical_rect_eq_and_hash_are_quantized_through_its_fields() {
280
        // LogicalRect's derived PartialEq/Hash must inherit the quantized field
281
        // impls — a NaN-sized rect has to be a stable HashMap key.
282
        let a = LogicalRect::new(
283
            LogicalPosition::new(f32::NAN, 1.0),
284
            LogicalSize::new(f32::NAN, 2.0),
285
        );
286
        let b = a;
287
        assert_eq!(a, b);
288
        assert_eq!(hash_of(&a), hash_of(&b));
289

            
290
        // Sub-millipixel jitter must not create a new key.
291
        let c = LogicalRect::new(LogicalPosition::new(1.0, 2.0), LogicalSize::new(3.0, 4.0));
292
        let d = LogicalRect::new(
293
            LogicalPosition::new(1.00004, 2.00004),
294
            LogicalSize::new(3.00004, 4.00004),
295
        );
296
        assert_eq!(c, d);
297
        assert_eq!(hash_of(&c), hash_of(&d));
298
    }
299

            
300
    // ---------------------------------------------------------------------
301
    // Constructors / zero neutrality
302
    // ---------------------------------------------------------------------
303

            
304
    proptest! {
305
        #[test]
306
        fn constructors_preserve_fields_for_extreme_arguments(
307
            x in proptest::num::f32::ANY, y in proptest::num::f32::ANY
308
        ) {
309
            let p = LogicalPosition::new(x, y);
310
            assert_eq!(p.x.to_bits(), x.to_bits());
311
            assert_eq!(p.y.to_bits(), y.to_bits());
312

            
313
            let s = LogicalSize::new(x, y);
314
            assert_eq!(s.width.to_bits(), x.to_bits());
315
            assert_eq!(s.height.to_bits(), y.to_bits());
316

            
317
            let r = LogicalRect::new(p, s);
318
            assert_eq!(r.origin.x.to_bits(), x.to_bits());
319
            assert_eq!(r.size.height.to_bits(), y.to_bits());
320

            
321
            assert_eq!(ScreenPosition::new(x, y).x.to_bits(), x.to_bits());
322
            assert_eq!(CursorNodePosition::new(x, y).y.to_bits(), y.to_bits());
323
            assert_eq!(PhysicalPosition::new(x, y).x.to_bits(), x.to_bits());
324
            assert_eq!(PhysicalSize::new(x, y).height.to_bits(), y.to_bits());
325
        }
326
    }
327

            
328
    #[test]
329
    fn zero_constructors_are_neutral_and_match_default() {
330
        assert_eq!(LogicalPosition::zero(), LogicalPosition::default());
331
        assert_eq!(LogicalSize::zero(), LogicalSize::default());
332
        assert_eq!(LogicalRect::zero(), LogicalRect::default());
333
        assert_eq!(LogicalRect::zero().origin, LogicalPosition::zero());
334
        assert_eq!(LogicalRect::zero().size, LogicalSize::zero());
335

            
336
        assert_eq!(ScreenPosition::zero(), ScreenPosition::default());
337
        assert_eq!(CursorNodePosition::zero(), CursorNodePosition::default());
338

            
339
        assert_eq!(PhysicalPosition::<i32>::zero(), PhysicalPosition::new(0, 0));
340
        assert_eq!(
341
            PhysicalPosition::<f64>::zero(),
342
            PhysicalPosition::new(0.0_f64, 0.0_f64)
343
        );
344
        assert_eq!(PhysicalSize::<u32>::zero(), PhysicalSize::new(0, 0));
345

            
346
        // A zero rect is degenerate: it contains no point at all, not even its
347
        // own origin, and it does not intersect itself.
348
        let z = LogicalRect::zero();
349
        assert!(!z.contains(LogicalPosition::zero()));
350
        assert!(!z.intersects(z));
351
        assert_eq!(z.min_x(), 0.0);
352
        assert_eq!(z.max_x(), 0.0);
353
        assert_eq!(z.min_y(), 0.0);
354
        assert_eq!(z.max_y(), 0.0);
355
    }
356

            
357
    // ---------------------------------------------------------------------
358
    // LogicalRect getters
359
    // ---------------------------------------------------------------------
360

            
361
    #[test]
362
    fn rect_getters_return_the_constructed_edges() {
363
        let r = LogicalRect::new(
364
            LogicalPosition::new(10.0, 20.0),
365
            LogicalSize::new(30.0, 40.0),
366
        );
367
        assert_eq!(r.min_x(), 10.0);
368
        assert_eq!(r.max_x(), 40.0);
369
        assert_eq!(r.min_y(), 20.0);
370
        assert_eq!(r.max_y(), 60.0);
371
    }
372

            
373
    proptest! {
374
        #[test]
375
        fn rect_getters_do_not_panic_on_extreme_geometry(
376
            x in proptest::num::f32::ANY, w in proptest::num::f32::ANY
377
        ) {
378
            let r = LogicalRect::new(LogicalPosition::new(x, x), LogicalSize::new(w, w));
379
            let _ = r.min_x();
380
            let _ = r.max_x();
381
            let _ = r.min_y();
382
            let _ = r.max_y();
383
        }
384
    }
385
    #[test]
386
    fn rect_getters_nan_propagation() {
387
        let r = LogicalRect::new(
388
            LogicalPosition::new(f32::INFINITY, f32::INFINITY),
389
            LogicalSize::new(f32::NEG_INFINITY, f32::NEG_INFINITY),
390
        );
391
        assert!(r.max_x().is_nan());
392
        assert!(r.max_y().is_nan());
393
    }
394

            
395
    // ---------------------------------------------------------------------
396
    // contains / hit_test / intersects
397
    // ---------------------------------------------------------------------
398

            
399
    #[test]
400
    fn contains_is_half_open_left_top_inclusive_right_bottom_exclusive() {
401
        let r = LogicalRect::new(
402
            LogicalPosition::new(10.0, 20.0),
403
            LogicalSize::new(30.0, 40.0),
404
        );
405
        assert!(r.contains(LogicalPosition::new(10.0, 20.0))); // top-left: in
406
        assert!(!r.contains(LogicalPosition::new(40.0, 59.0))); // right edge: out
407
        assert!(!r.contains(LogicalPosition::new(39.0, 60.0))); // bottom edge: out
408
        assert!(!r.contains(LogicalPosition::new(40.0, 60.0))); // bottom-right: out
409
        assert!(r.contains(LogicalPosition::new(39.999, 59.999)));
410
    }
411

            
412
    proptest! {
413
        #[test]
414
        fn contains_and_hit_test_agree_on_the_hostile_grid(
415
            x in proptest::num::f32::ANY, y in proptest::num::f32::ANY
416
        ) {
417
            // The invariant the hit_test comment promises: identical edge semantics.
418
            let rects = [
419
                LogicalRect::zero(),
420
                LogicalRect::new(
421
                    LogicalPosition::new(10.0, 20.0),
422
                    LogicalSize::new(30.0, 40.0),
423
                ),
424
                LogicalRect::new(
425
                    LogicalPosition::new(-5.0, -5.0),
426
                    LogicalSize::new(10.0, 10.0),
427
                ),
428
                // Negative extent: max < min, so it can never contain anything.
429
                LogicalRect::new(
430
                    LogicalPosition::new(0.0, 0.0),
431
                    LogicalSize::new(-10.0, -10.0),
432
                ),
433
                LogicalRect::new(
434
                    LogicalPosition::new(f32::NAN, f32::NAN),
435
                    LogicalSize::new(f32::NAN, f32::NAN),
436
                ),
437
                LogicalRect::new(
438
                    LogicalPosition::zero(),
439
                    LogicalSize::new(f32::INFINITY, f32::INFINITY),
440
                ),
441
            ];
442
            for r in rects {
443
                let p = LogicalPosition::new(x, y);
444
                assert_eq!(
445
                    r.contains(p),
446
                    r.hit_test(&p).is_some(),
447
                    "contains/hit_test disagree for {r:?} at {p:?}"
448
                );
449
            }
450
        }
451
    }
452

            
453
    #[test]
454
    fn contains_rejects_nan_points_and_nan_rects() {
455
        let r = LogicalRect::new(
456
            LogicalPosition::new(0.0, 0.0),
457
            LogicalSize::new(100.0, 100.0),
458
        );
459
        // Every comparison against NaN is false, so a NaN point is never inside.
460
        assert!(!r.contains(LogicalPosition::new(f32::NAN, 50.0)));
461
        assert!(!r.contains(LogicalPosition::new(50.0, f32::NAN)));
462
        assert!(!r.contains(LogicalPosition::new(f32::NAN, f32::NAN)));
463

            
464
        let nan_rect = LogicalRect::new(
465
            LogicalPosition::new(f32::NAN, f32::NAN),
466
            LogicalSize::new(f32::NAN, f32::NAN),
467
        );
468
        assert!(!nan_rect.contains(LogicalPosition::zero()));
469
        assert!(nan_rect.hit_test(&LogicalPosition::zero()).is_none());
470
    }
471

            
472
    #[test]
473
    fn contains_handles_negative_extent_rects_without_panicking() {
474
        // A negative width puts max_x below min_x: nothing can satisfy both bounds.
475
        let r = LogicalRect::new(
476
            LogicalPosition::new(0.0, 0.0),
477
            LogicalSize::new(-10.0, -10.0),
478
        );
479
        assert!(!r.contains(LogicalPosition::zero()));
480
        assert!(!r.contains(LogicalPosition::new(-5.0, -5.0)));
481
        assert!(r.hit_test(&LogicalPosition::new(-5.0, -5.0)).is_none());
482
    }
483

            
484
    #[test]
485
    fn contains_at_the_coordinate_extremes() {
486
        let huge = LogicalRect::new(
487
            LogicalPosition::new(f32::MIN, f32::MIN),
488
            LogicalSize::new(f32::MAX, f32::MAX),
489
        );
490
        // f32::MIN + f32::MAX == 0.0 exactly, so the rect spans [MIN, 0).
491
        assert_eq!(huge.max_x(), 0.0);
492
        assert!(huge.contains(LogicalPosition::new(-1.0, -1.0)));
493
        assert!(!huge.contains(LogicalPosition::zero()));
494

            
495
        let unbounded = LogicalRect::new(
496
            LogicalPosition::new(f32::NEG_INFINITY, f32::NEG_INFINITY),
497
            LogicalSize::new(f32::INFINITY, f32::INFINITY),
498
        );
499
        // -inf + inf == NaN, so the "infinite" rect contains nothing. Surprising,
500
        // but defined and panic-free.
501
        assert!(unbounded.max_x().is_nan());
502
        assert!(!unbounded.contains(LogicalPosition::zero()));
503
    }
504

            
505
    #[test]
506
    fn hit_test_returns_the_offset_from_the_top_left_corner() {
507
        let r = LogicalRect::new(
508
            LogicalPosition::new(10.0, 20.0),
509
            LogicalSize::new(30.0, 40.0),
510
        );
511
        assert_eq!(
512
            r.hit_test(&LogicalPosition::new(10.0, 20.0)),
513
            Some(LogicalPosition::new(0.0, 0.0))
514
        );
515
        assert_eq!(
516
            r.hit_test(&LogicalPosition::new(25.0, 45.0)),
517
            Some(LogicalPosition::new(15.0, 25.0))
518
        );
519
        // Right/bottom edges are exclusive.
520
        assert_eq!(r.hit_test(&LogicalPosition::new(40.0, 30.0)), None);
521
        assert_eq!(r.hit_test(&LogicalPosition::new(30.0, 60.0)), None);
522
    }
523

            
524
    #[test]
525
    fn hit_test_offset_is_always_non_negative_when_it_hits() {
526
        let r = LogicalRect::new(
527
            LogicalPosition::new(-100.0, -100.0),
528
            LogicalSize::new(200.0, 200.0),
529
        );
530
        // Dyadic values only: `origin + offset` must reconstruct the point exactly,
531
        // so the assertion tests hit_test's arithmetic and not f32 rounding.
532
        for x in [-100.0_f32, -50.0, 0.0, 50.0, 99.5] {
533
            for y in [-100.0_f32, -50.0, 0.0, 50.0, 99.5] {
534
                let hit = r.hit_test(&LogicalPosition::new(x, y)).expect("inside");
535
                assert!(hit.x >= 0.0 && hit.y >= 0.0, "negative offset {hit:?}");
536
                assert_eq!(r.origin.x + hit.x, x);
537
                assert_eq!(r.origin.y + hit.y, y);
538
            }
539
        }
540
    }
541

            
542
    #[test]
543
    fn intersects_is_symmetric_even_for_degenerate_and_nan_rects() {
544
        let rects = [
545
            LogicalRect::zero(),
546
            LogicalRect::new(LogicalPosition::new(0.0, 0.0), LogicalSize::new(10.0, 10.0)),
547
            LogicalRect::new(LogicalPosition::new(5.0, 5.0), LogicalSize::new(10.0, 10.0)),
548
            LogicalRect::new(
549
                LogicalPosition::new(10.0, 0.0),
550
                LogicalSize::new(10.0, 10.0),
551
            ),
552
            LogicalRect::new(
553
                LogicalPosition::new(0.0, 0.0),
554
                LogicalSize::new(-10.0, -10.0),
555
            ),
556
            LogicalRect::new(
557
                LogicalPosition::new(f32::NAN, f32::NAN),
558
                LogicalSize::new(f32::NAN, f32::NAN),
559
            ),
560
            LogicalRect::new(
561
                LogicalPosition::new(f32::MIN, f32::MIN),
562
                LogicalSize::new(f32::MAX, f32::MAX),
563
            ),
564
        ];
565
        for a in rects {
566
            for b in rects {
567
                assert_eq!(
568
                    a.intersects(b),
569
                    b.intersects(a),
570
                    "intersects is asymmetric for {a:?} / {b:?}"
571
                );
572
            }
573
        }
574
    }
575

            
576
    #[test]
577
    fn intersects_touching_edges_do_not_count_as_overlap() {
578
        let a = LogicalRect::new(LogicalPosition::new(0.0, 0.0), LogicalSize::new(10.0, 10.0));
579
        let touching = LogicalRect::new(
580
            LogicalPosition::new(10.0, 0.0),
581
            LogicalSize::new(10.0, 10.0),
582
        );
583
        let overlapping = LogicalRect::new(
584
            LogicalPosition::new(9.99, 0.0),
585
            LogicalSize::new(10.0, 10.0),
586
        );
587
        assert!(!a.intersects(touching));
588
        assert!(a.intersects(overlapping));
589
        assert!(a.intersects(a));
590
        // Zero-area rects never overlap anything, including themselves.
591
        assert!(!LogicalRect::zero().intersects(a));
592
    }
593

            
594
    #[test]
595
    fn intersects_with_nan_rect_is_permissive_current_behavior() {
596
        // DOCUMENTS A REAL QUIRK (reported, not worked around): every `<=` guard
597
        // in `intersects` is false against NaN, so all four early-outs are skipped
598
        // and a fully-NaN rect reports that it intersects EVERYTHING — while
599
        // `contains` on the same rect correctly reports false for every point.
600
        // Locked down here so a future fix has to change this deliberately.
601
        let nan_rect = LogicalRect::new(
602
            LogicalPosition::new(f32::NAN, f32::NAN),
603
            LogicalSize::new(f32::NAN, f32::NAN),
604
        );
605
        let normal = LogicalRect::new(LogicalPosition::new(0.0, 0.0), LogicalSize::new(10.0, 10.0));
606
        assert!(nan_rect.intersects(normal));
607
        assert!(normal.intersects(nan_rect));
608
        assert!(!nan_rect.contains(LogicalPosition::zero()));
609
    }
610

            
611
    // ---------------------------------------------------------------------
612
    // scale_for_dpi
613
    // ---------------------------------------------------------------------
614

            
615
    #[test]
616
    fn scale_for_dpi_by_one_is_the_identity() {
617
        let mut p = LogicalPosition::new(1.5, -2.5);
618
        p.scale_for_dpi(1.0);
619
        assert_eq!(p, LogicalPosition::new(1.5, -2.5));
620

            
621
        let mut s = LogicalSize::new(3.5, 4.5);
622
        assert_eq!(s.scale_for_dpi(1.0), LogicalSize::new(3.5, 4.5));
623

            
624
        let mut r = LogicalRect::new(LogicalPosition::new(1.0, 2.0), LogicalSize::new(3.0, 4.0));
625
        r.scale_for_dpi(1.0);
626
        assert_eq!(
627
            r,
628
            LogicalRect::new(LogicalPosition::new(1.0, 2.0), LogicalSize::new(3.0, 4.0))
629
        );
630
    }
631

            
632
    #[test]
633
    fn scale_for_dpi_by_zero_collapses_to_the_origin() {
634
        let mut r = LogicalRect::new(
635
            LogicalPosition::new(10.0, 20.0),
636
            LogicalSize::new(30.0, 40.0),
637
        );
638
        r.scale_for_dpi(0.0);
639
        assert_eq!(r, LogicalRect::zero());
640
    }
641

            
642
    #[test]
643
    fn scale_for_dpi_by_negative_factor_mirrors_deterministically() {
644
        let mut r = LogicalRect::new(
645
            LogicalPosition::new(10.0, 20.0),
646
            LogicalSize::new(30.0, 40.0),
647
        );
648
        r.scale_for_dpi(-2.0);
649
        assert_eq!(
650
            r,
651
            LogicalRect::new(
652
                LogicalPosition::new(-20.0, -40.0),
653
                LogicalSize::new(-60.0, -80.0)
654
            )
655
        );
656
        // A mirrored rect has an inverted extent, so it contains nothing.
657
        assert!(!r.contains(LogicalPosition::new(-30.0, -50.0)));
658
    }
659

            
660
    #[test]
661
    fn scale_for_dpi_overflows_to_infinity_rather_than_panicking() {
662
        let mut s = LogicalSize::new(f32::MAX, f32::MAX);
663
        let out = s.scale_for_dpi(2.0);
664
        assert!(out.width.is_infinite() && out.width.is_sign_positive());
665
        assert!(out.height.is_infinite());
666
        // scale_for_dpi mutates in place AND returns a copy: they must match.
667
        assert_eq!(out, s);
668
    }
669

            
670
    proptest! {
671
        #[test]
672
        fn scale_for_dpi_with_nan_or_inf_does_not_panic(factor in proptest::num::f32::ANY) {
673
            let mut p = LogicalPosition::new(1.0, -1.0);
674
            p.scale_for_dpi(factor);
675

            
676
            let mut s = LogicalSize::new(1.0, -1.0);
677
            let _ = s.scale_for_dpi(factor);
678

            
679
            let mut r =
680
                LogicalRect::new(LogicalPosition::new(1.0, -1.0), LogicalSize::new(2.0, -2.0));
681
            r.scale_for_dpi(factor);
682
        }
683
    }
684
    #[test]
685
    fn scale_for_dpi_with_inf_produces_nan() {
686
        let mut r = LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(1.0, 1.0));
687
        r.scale_for_dpi(f32::INFINITY);
688
        assert!(r.origin.x.is_nan());
689
        assert!(r.size.width.is_infinite());
690
    }
691

            
692
    // ---------------------------------------------------------------------
693
    // DPI conversion: to_physical / to_logical
694
    // ---------------------------------------------------------------------
695

            
696
    #[test]
697
    fn to_physical_rounds_half_away_from_zero() {
698
        assert_eq!(
699
            LogicalPosition::new(0.5, 1.5).to_physical(1.0),
700
            PhysicalPosition::new(1, 2)
701
        );
702
        // 2.5 -> 3 (round-half-away), NOT 2 (banker's rounding).
703
        assert_eq!(
704
            LogicalSize::new(2.5, 3.5).to_physical(1.0),
705
            PhysicalSize::new(3, 4)
706
        );
707
    }
708

            
709
    #[test]
710
    fn to_physical_clamps_negatives_to_zero_instead_of_wrapping() {
711
        // `as u32` saturates (Rust >= 1.45), so -1.0 must become 0, NOT u32::MAX.
712
        assert_eq!(
713
            LogicalPosition::new(-1.0, -1000.0).to_physical(1.0),
714
            PhysicalPosition::new(0, 0)
715
        );
716
        assert_eq!(
717
            LogicalSize::new(-0.6, -1.0).to_physical(2.0),
718
            PhysicalSize::new(0, 0)
719
        );
720
        assert_eq!(
721
            LogicalPosition::new(1.0, 1.0).to_physical(-1.0),
722
            PhysicalPosition::new(0, 0)
723
        );
724
    }
725

            
726
    #[test]
727
    fn to_physical_saturates_at_u32_max_on_overflow() {
728
        assert_eq!(
729
            LogicalSize::new(f32::MAX, f32::INFINITY).to_physical(1.0),
730
            PhysicalSize::new(u32::MAX, u32::MAX)
731
        );
732
        // x: 1e30 * 1e30 overflows f32 to +inf, then saturates at the cast.
733
        // y: 0.0 * 1e30 stays 0 — saturation must not smear across components.
734
        assert_eq!(
735
            LogicalPosition::new(1.0e30, 0.0).to_physical(1.0e30),
736
            PhysicalPosition::new(u32::MAX, 0)
737
        );
738
    }
739

            
740
    #[test]
741
    fn to_physical_maps_nan_to_zero() {
742
        // `NaN as u32` == 0 by the saturating-cast rules. Defined, not UB.
743
        assert_eq!(
744
            LogicalPosition::new(f32::NAN, f32::NAN).to_physical(1.0),
745
            PhysicalPosition::new(0, 0)
746
        );
747
        assert_eq!(
748
            LogicalSize::new(f32::NAN, 5.0).to_physical(f32::NAN),
749
            PhysicalSize::new(0, 0)
750
        );
751
        // 0.0 * inf == NaN -> 0
752
        assert_eq!(
753
            LogicalSize::new(0.0, 0.0).to_physical(f32::INFINITY),
754
            PhysicalSize::new(0, 0)
755
        );
756
    }
757

            
758
    proptest! {
759
        #[test]
760
        fn to_physical_never_panics_on_the_hostile_grid(
761
            v in proptest::num::f32::ANY, f in proptest::num::f32::ANY
762
        ) {
763
            let _ = LogicalPosition::new(v, v).to_physical(f);
764
            let _ = LogicalSize::new(v, v).to_physical(f);
765
        }
766
    }
767

            
768
    #[test]
769
    fn to_logical_divides_by_the_dpi_factor() {
770
        assert_eq!(
771
            PhysicalSize::new(200_u32, 100).to_logical(2.0),
772
            LogicalSize::new(100.0, 50.0)
773
        );
774
        assert_eq!(
775
            PhysicalPosition::new(-10_i32, 20).to_logical(2.0),
776
            LogicalPosition::new(-5.0, 10.0)
777
        );
778
        assert_eq!(
779
            PhysicalPosition::new(-10.0_f64, 20.0).to_logical(2.0),
780
            LogicalPosition::new(-5.0, 10.0)
781
        );
782
    }
783

            
784
    #[test]
785
    fn to_logical_with_zero_dpi_yields_infinity_not_a_panic() {
786
        // Float division by zero is defined: no divide-by-zero panic here.
787
        let s = PhysicalSize::new(100_u32, 100).to_logical(0.0);
788
        assert!(s.width.is_infinite() && s.width.is_sign_positive());
789

            
790
        // 0 / 0 == NaN.
791
        let z = PhysicalSize::<u32>::zero().to_logical(0.0);
792
        assert!(z.width.is_nan() && z.height.is_nan());
793

            
794
        let p = PhysicalPosition::new(-5_i32, 5).to_logical(0.0);
795
        assert!(p.x.is_infinite() && p.x.is_sign_negative());
796
        assert!(p.y.is_infinite() && p.y.is_sign_positive());
797
    }
798

            
799
    #[test]
800
    fn to_logical_at_the_integer_limits() {
801
        let p = PhysicalPosition::new(i32::MIN, i32::MAX).to_logical(1.0);
802
        assert_eq!(p.x, i32::MIN as f32);
803
        assert_eq!(p.y, i32::MAX as f32);
804

            
805
        let s = PhysicalSize::new(u32::MAX, 0_u32).to_logical(1.0);
806
        assert_eq!(s.width, u32::MAX as f32);
807
        assert_eq!(s.height, 0.0);
808

            
809
        // f64 -> f32 narrowing saturates to inf rather than wrapping.
810
        let big = PhysicalPosition::new(f64::MAX, f64::MIN).to_logical(1.0);
811
        assert!(big.x.is_infinite() && big.x.is_sign_positive());
812
        assert!(big.y.is_infinite() && big.y.is_sign_negative());
813
    }
814

            
815
    proptest! {
816
        #[test]
817
        fn to_logical_never_panics_for_hostile_dpi_factors(f in proptest::num::f32::ANY) {
818
            let _ = PhysicalPosition::new(i32::MIN, i32::MAX).to_logical(f);
819
            let _ = PhysicalPosition::new(f64::MAX, f64::MIN).to_logical(f);
820
            let _ = PhysicalSize::new(u32::MAX, 0_u32).to_logical(f);
821
        }
822
    }
823

            
824
    // ---------------------------------------------------------------------
825
    // Round-trips
826
    // ---------------------------------------------------------------------
827

            
828
    #[test]
829
    fn logical_size_physical_round_trip_is_lossless_for_integral_pixels() {
830
        for factor in [1.0_f32, 2.0, 4.0] {
831
            for (w, h) in [
832
                (0.0_f32, 0.0_f32),
833
                (1.0, 1.0),
834
                (100.0, 50.0),
835
                (1920.0, 1080.0),
836
            ] {
837
                let original = LogicalSize::new(w, h);
838
                let round_tripped = original.to_physical(factor).to_logical(factor);
839
                assert_eq!(
840
                    original, round_tripped,
841
                    "round-trip lost {original:?} at dpi {factor}"
842
                );
843
            }
844
        }
845
    }
846

            
847
    #[test]
848
    fn physical_size_logical_round_trip_preserves_the_pixel_count() {
849
        for factor in [1.0_f32, 1.5, 2.0, 3.0] {
850
            for (w, h) in [(0_u32, 0_u32), (1, 1), (1920, 1080), (3840, 2160)] {
851
                let original = PhysicalSize::new(w, h);
852
                let round_tripped = original.to_logical(factor).to_physical(factor);
853
                assert_eq!(
854
                    original, round_tripped,
855
                    "round-trip lost {original:?} at dpi {factor}"
856
                );
857
            }
858
        }
859
    }
860

            
861
    proptest! {
862
        #[test]
863
        fn screen_and_cursor_position_logical_round_trip_bit_for_bit(
864
            x in proptest::num::f32::ANY, y in proptest::num::f32::ANY
865
        ) {
866
            let p = LogicalPosition::new(x, y);
867

            
868
            let screen = ScreenPosition::from_logical(p).to_logical();
869
            assert_eq!(screen.x.to_bits(), x.to_bits());
870
            assert_eq!(screen.y.to_bits(), y.to_bits());
871

            
872
            let cursor = CursorNodePosition::from_logical(p).to_logical();
873
            assert_eq!(cursor.x.to_bits(), x.to_bits());
874
            assert_eq!(cursor.y.to_bits(), y.to_bits());
875
        }
876
    }
877

            
878
    #[test]
879
    fn add_sub_are_inverse_for_finite_positions() {
880
        let a = LogicalPosition::new(10.0, -20.0);
881
        let b = LogicalPosition::new(2.5, 7.5);
882
        assert_eq!((a + b) - b, a);
883

            
884
        let mut c = a;
885
        c += b;
886
        assert_eq!(c, a + b);
887
        c -= b;
888
        assert_eq!(c, a);
889
    }
890

            
891
    // ---------------------------------------------------------------------
892
    // Writing-mode axis mapping
893
    // ---------------------------------------------------------------------
894

            
895
    proptest! {
896
        #[test]
897
        fn position_main_cross_round_trip_for_every_writing_mode(
898
            main in proptest::num::f32::ANY, cross in proptest::num::f32::ANY
899
        ) {
900
            for wm in WMS {
901
                let p = LogicalPosition::from_main_cross(main, cross, wm);
902
                assert_eq!(p.main(wm).to_bits(), main.to_bits());
903
                assert_eq!(p.cross(wm).to_bits(), cross.to_bits());
904
            }
905
        }
906

            
907
        #[test]
908
        fn size_main_cross_round_trip_for_every_writing_mode(
909
            main in proptest::num::f32::ANY, cross in proptest::num::f32::ANY
910
        ) {
911
            for wm in WMS {
912
                let s = LogicalSize::from_main_cross(main, cross, wm);
913
                assert_eq!(s.main(wm).to_bits(), main.to_bits());
914
                assert_eq!(s.cross(wm).to_bits(), cross.to_bits());
915
            }
916
        }
917
    }
918

            
919
    #[test]
920
    fn horizontal_tb_maps_main_to_the_block_axis() {
921
        // In horizontal-tb the block (main) axis is vertical: main == y / height.
922
        let wm = LayoutWritingMode::HorizontalTb;
923
        let p = LogicalPosition::new(3.0, 7.0);
924
        assert_eq!(p.main(wm), 7.0);
925
        assert_eq!(p.cross(wm), 3.0);
926

            
927
        let s = LogicalSize::new(30.0, 70.0);
928
        assert_eq!(s.main(wm), 70.0);
929
        assert_eq!(s.cross(wm), 30.0);
930
    }
931

            
932
    #[test]
933
    fn vertical_modes_map_main_to_the_horizontal_axis() {
934
        for wm in [LayoutWritingMode::VerticalRl, LayoutWritingMode::VerticalLr] {
935
            let p = LogicalPosition::new(3.0, 7.0);
936
            assert_eq!(p.main(wm), 3.0);
937
            assert_eq!(p.cross(wm), 7.0);
938

            
939
            let s = LogicalSize::new(30.0, 70.0);
940
            assert_eq!(s.main(wm), 30.0);
941
            assert_eq!(s.cross(wm), 70.0);
942
        }
943
    }
944

            
945
    proptest! {
946
        #[test]
947
        fn with_main_and_with_cross_only_touch_their_own_axis(v in proptest::num::f32::ANY) {
948
            for wm in WMS {
949
                let s = LogicalSize::new(10.0, 20.0);
950

            
951
                let m = s.with_main(wm, v);
952
                assert_eq!(m.main(wm).to_bits(), v.to_bits());
953
                assert_eq!(
954
                    m.cross(wm),
955
                    s.cross(wm),
956
                    "with_main clobbered the cross axis"
957
                );
958

            
959
                let c = s.with_cross(wm, v);
960
                assert_eq!(c.cross(wm).to_bits(), v.to_bits());
961
                assert_eq!(c.main(wm), s.main(wm), "with_cross clobbered the main axis");
962
            }
963
        }
964
    }
965

            
966
    #[test]
967
    fn with_main_then_with_cross_reconstructs_from_main_cross() {
968
        for wm in WMS {
969
            let built = LogicalSize::zero().with_main(wm, 5.0).with_cross(wm, 9.0);
970
            assert_eq!(built, LogicalSize::from_main_cross(5.0, 9.0, wm));
971
        }
972
    }
973

            
974
    // ---------------------------------------------------------------------
975
    // Display / Debug (serializers)
976
    // ---------------------------------------------------------------------
977

            
978
    #[test]
979
    fn display_formats_are_well_formed_for_representative_values() {
980
        let p = LogicalPosition::new(1.5, -2.5);
981
        assert_eq!(format!("{p}"), "(1.5, -2.5)");
982
        assert_eq!(format!("{p:?}"), "(1.5, -2.5)");
983

            
984
        let s = LogicalSize::new(30.0, 40.0);
985
        assert_eq!(format!("{s}"), "30x40");
986
        assert_eq!(format!("{s:?}"), "30x40");
987

            
988
        let r = LogicalRect::new(p, s);
989
        assert_eq!(format!("{r}"), "30x40 @ (1.5, -2.5)");
990
        assert_eq!(format!("{r:?}"), "30x40 @ (1.5, -2.5)");
991

            
992
        assert_eq!(format!("{:?}", PhysicalPosition::new(1_i32, 2)), "(1, 2)");
993
        assert_eq!(format!("{:?}", PhysicalSize::new(1_u32, 2)), "1x2");
994
    }
995

            
996
    #[test]
997
    fn display_of_zero_values_is_non_empty() {
998
        assert!(!format!("{}", LogicalPosition::zero()).is_empty());
999
        assert!(!format!("{}", LogicalSize::zero()).is_empty());
        assert!(!format!("{}", LogicalRect::zero()).is_empty());
        assert_eq!(format!("{}", LogicalRect::zero()), "0x0 @ (0, 0)");
    }
    proptest! {
        #[test]
        fn display_does_not_panic_on_nan_or_infinite_coordinates(
            x in proptest::num::f32::ANY, y in proptest::num::f32::ANY
        ) {
            let r = LogicalRect::new(LogicalPosition::new(x, y), LogicalSize::new(x, y));
            let shown = format!("{r}");
            assert!(!shown.is_empty());
            assert_eq!(shown, format!("{r:?}"));
        }
    }
    #[test]
    fn display_nan_explicit_formatting() {
        let nan = LogicalRect::new(
            LogicalPosition::new(f32::NAN, f32::INFINITY),
            LogicalSize::new(f32::NEG_INFINITY, f32::NAN),
        );
        assert_eq!(format!("{nan}"), "-infxNaN @ (NaN, inf)");
    }
}