1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod autotest_generated {
5
    #![allow(clippy::float_cmp)]
6

            
7
    use super::*;
8

            
9
    /// Every hostile `f32` a caller can hand to these constructors.
10
    const HOSTILE_F32: [f32; 12] = [
11
        0.0,
12
        -0.0,
13
        1.0,
14
        -1.0,
15
        f32::MAX,
16
        f32::MIN,
17
        f32::MIN_POSITIVE,
18
        -f32::MIN_POSITIVE,
19
        f32::EPSILON,
20
        f32::INFINITY,
21
        f32::NEG_INFINITY,
22
        f32::NAN,
23
    ];
24

            
25
    /// Bit-exact float compare: distinguishes `0.0` from `-0.0`, and treats any
26
    /// NaN as equal to any other NaN (so it can be used on the hostile list).
27
    fn same_f32(a: f32, b: f32) -> bool {
28
        if a.is_nan() && b.is_nan() {
29
            return true;
30
        }
31
        a.to_bits() == b.to_bits()
32
    }
33

            
34
    // --- FragmentationContext::new_continuous / new_paged (constructor) -------
35

            
36
    #[test]
37
    fn new_continuous_stores_width_bit_exactly_for_hostile_input() {
38
        for w in HOSTILE_F32 {
39
            let ctx = FragmentationContext::new_continuous(w);
40
            match ctx {
41
                FragmentationContext::Continuous { width } => {
42
                    assert!(same_f32(width, w), "width mangled: {w:?} -> {width:?}");
43
                }
44
                FragmentationContext::Paged { .. } => {
45
                    panic!("new_continuous produced a Paged variant for width {w:?}")
46
                }
47
            }
48
        }
49
    }
50

            
51
    #[test]
52
    fn new_paged_stores_page_size_bit_exactly_for_hostile_input() {
53
        for w in HOSTILE_F32 {
54
            for h in HOSTILE_F32 {
55
                let ctx = FragmentationContext::new_paged(LogicalSize::new(w, h));
56
                match ctx {
57
                    FragmentationContext::Paged { page_size } => {
58
                        assert!(same_f32(page_size.width, w));
59
                        assert!(same_f32(page_size.height, h));
60
                    }
61
                    FragmentationContext::Continuous { .. } => {
62
                        panic!("new_paged produced a Continuous variant for {w:?}x{h:?}")
63
                    }
64
                }
65
            }
66
        }
67
    }
68

            
69
    /// The `const fn` marker is part of the public contract: a caller may put
70
    /// these in a `const` item. If constness regresses, this stops compiling.
71
    #[test]
72
    fn constructors_and_accessors_are_usable_in_const_context() {
73
        const CONT: FragmentationContext = FragmentationContext::new_continuous(1024.0);
74
        const A4: FragmentationContext =
75
            FragmentationContext::new_paged(LogicalSize::new(595.0, 842.0));
76
        const CONT_H: f32 = CONT.page_content_height();
77
        const A4_H: f32 = A4.page_content_height();
78
        const CONT_PAGED: bool = CONT.is_paged();
79
        const A4_PAGED: bool = A4.is_paged();
80
        const UNIFORM: PageMargins = PageMargins::uniform(10.0);
81
        const EXPLICIT: PageMargins = PageMargins::new(1.0, 2.0, 3.0, 4.0);
82

            
83
        assert_eq!(CONT_H, f32::MAX);
84
        assert_eq!(A4_H, 842.0);
85
        const _: () = assert!(!CONT_PAGED && A4_PAGED);
86
        assert_eq!(UNIFORM.top, 10.0);
87
        assert_eq!(EXPLICIT.left, 4.0);
88
    }
89

            
90
    // --- FragmentationContext::page_content_height (getter) ------------------
91

            
92
    #[test]
93
    fn page_content_height_is_f32_max_for_every_continuous_width() {
94
        // Continuous ignores `width` entirely — even NaN/inf must not leak out.
95
        for w in HOSTILE_F32 {
96
            let h = FragmentationContext::new_continuous(w).page_content_height();
97
            assert!(
98
                same_f32(h, f32::MAX),
99
                "continuous width {w:?} leaked into page_content_height: {h:?}"
100
            );
101
        }
102
    }
103

            
104
    #[test]
105
    fn page_content_height_returns_paged_height_verbatim_and_ignores_width() {
106
        for h in HOSTILE_F32 {
107
            // Width must never contaminate the result, however hostile it is.
108
            for w in HOSTILE_F32 {
109
                let got =
110
                    FragmentationContext::new_paged(LogicalSize::new(w, h)).page_content_height();
111
                assert!(
112
                    same_f32(got, h),
113
                    "page {w:?}x{h:?} -> page_content_height {got:?}, expected {h:?}"
114
                );
115
            }
116
        }
117
    }
118

            
119
    #[test]
120
    fn page_content_height_does_not_saturate_or_clamp_degenerate_pages() {
121
        // A zero/negative page height is nonsense for print, but the getter is a
122
        // plain accessor: it must report what it was given, not silently repair it.
123
        let zero = FragmentationContext::new_paged(LogicalSize::zero());
124
        assert_eq!(zero.page_content_height(), 0.0);
125

            
126
        let negative = FragmentationContext::new_paged(LogicalSize::new(595.0, -842.0));
127
        assert_eq!(negative.page_content_height(), -842.0);
128

            
129
        let nan = FragmentationContext::new_paged(LogicalSize::new(595.0, f32::NAN));
130
        assert!(nan.page_content_height().is_nan());
131
    }
132

            
133
    /// `f32::MAX` is the sentinel for "infinitely tall". A paged context whose page
134
    /// is exactly `f32::MAX` tall is therefore indistinguishable from a continuous
135
    /// one *by height alone* — `is_paged()` is the only safe discriminator.
136
    #[test]
137
    fn f32_max_tall_page_collides_with_continuous_sentinel_but_is_paged_disambiguates() {
138
        let continuous = FragmentationContext::new_continuous(595.0);
139
        let max_page = FragmentationContext::new_paged(LogicalSize::new(595.0, f32::MAX));
140

            
141
        assert_eq!(
142
            continuous.page_content_height(),
143
            max_page.page_content_height()
144
        );
145
        assert!(!continuous.is_paged());
146
        assert!(max_page.is_paged());
147
    }
148

            
149
    // --- FragmentationContext::is_paged (predicate) --------------------------
150

            
151
    #[test]
152
    fn is_paged_is_true_only_for_paged_regardless_of_hostile_fields() {
153
        for v in HOSTILE_F32 {
154
            assert!(
155
                !FragmentationContext::new_continuous(v).is_paged(),
156
                "continuous({v:?}) reported as paged"
157
            );
158
            assert!(
159
                FragmentationContext::new_paged(LogicalSize::new(v, v)).is_paged(),
160
                "paged({v:?}x{v:?}) reported as continuous"
161
            );
162
        }
163
        // Fully degenerate page: still paged. The predicate inspects the variant,
164
        // never the payload.
165
        assert!(FragmentationContext::new_paged(LogicalSize::zero()).is_paged());
166
    }
167

            
168
    #[test]
169
    fn is_paged_is_deterministic_across_repeated_calls_and_copies() {
170
        let ctx = FragmentationContext::new_paged(LogicalSize::new(f32::NAN, f32::NAN));
171
        let copied = ctx; // Copy
172
        assert!(ctx.is_paged());
173
        assert!(ctx.is_paged());
174
        assert!(copied.is_paged());
175
    }
176

            
177
    // --- PageMargins::new (constructor) --------------------------------------
178

            
179
    #[test]
180
    fn new_assigns_each_argument_to_its_own_field_no_transposition() {
181
        // Distinct values in every slot: catches any top/right/bottom/left swap.
182
        let m = PageMargins::new(1.0, 2.0, 3.0, 4.0);
183
        assert_eq!(m.top, 1.0);
184
        assert_eq!(m.right, 2.0);
185
        assert_eq!(m.bottom, 3.0);
186
        assert_eq!(m.left, 4.0);
187
    }
188

            
189
    #[test]
190
    fn new_stores_hostile_floats_bit_exactly() {
191
        for v in HOSTILE_F32 {
192
            // Rotate the hostile value through each slot; the other three stay sane
193
            // so a misassignment shows up as a mismatch rather than cancelling out.
194
            let m = PageMargins::new(v, 2.0, 3.0, 4.0);
195
            assert!(same_f32(m.top, v));
196
            assert_eq!(m.right, 2.0);
197

            
198
            let m = PageMargins::new(1.0, v, 3.0, 4.0);
199
            assert!(same_f32(m.right, v));
200
            assert_eq!(m.bottom, 3.0);
201

            
202
            let m = PageMargins::new(1.0, 2.0, v, 4.0);
203
            assert!(same_f32(m.bottom, v));
204
            assert_eq!(m.left, 4.0);
205

            
206
            let m = PageMargins::new(1.0, 2.0, 3.0, v);
207
            assert!(same_f32(m.left, v));
208
            assert_eq!(m.top, 1.0);
209
        }
210
    }
211

            
212
    #[test]
213
    fn default_margins_are_positive_zero_and_sum_to_zero() {
214
        let d = PageMargins::default();
215
        for f in [d.top, d.right, d.bottom, d.left] {
216
            assert!(same_f32(f, 0.0), "default field is not +0.0: {f:?}");
217
        }
218
        assert_eq!(d.horizontal(), 0.0);
219
        assert_eq!(d.vertical(), 0.0);
220
    }
221

            
222
    // --- PageMargins::uniform (numeric) --------------------------------------
223

            
224
    #[test]
225
    fn uniform_zero_and_negative_zero_preserve_sign() {
226
        let z = PageMargins::uniform(0.0);
227
        assert!(same_f32(z.top, 0.0) && same_f32(z.left, 0.0));
228
        assert_eq!(z.horizontal(), 0.0);
229
        assert_eq!(z.vertical(), 0.0);
230

            
231
        // -0.0 + -0.0 == -0.0, so the sign must survive all the way through.
232
        let nz = PageMargins::uniform(-0.0);
233
        assert!(same_f32(nz.top, -0.0), "uniform(-0.0) lost the sign bit");
234
        assert!(same_f32(nz.horizontal(), -0.0));
235
        assert!(same_f32(nz.vertical(), -0.0));
236
    }
237

            
238
    #[test]
239
    fn uniform_replicates_hostile_value_into_all_four_fields() {
240
        for v in HOSTILE_F32 {
241
            let m = PageMargins::uniform(v);
242
            assert!(same_f32(m.top, v), "top != {v:?}");
243
            assert!(same_f32(m.right, v), "right != {v:?}");
244
            assert!(same_f32(m.bottom, v), "bottom != {v:?}");
245
            assert!(same_f32(m.left, v), "left != {v:?}");
246
        }
247
    }
248

            
249
    #[test]
250
    fn uniform_negative_margins_are_kept_not_clamped() {
251
        // Negative margins are legal CSS (`margin: -10pt`); nothing here may clamp.
252
        let m = PageMargins::uniform(-10.0);
253
        assert_eq!(m.top, -10.0);
254
        assert_eq!(m.horizontal(), -20.0);
255
        assert_eq!(m.vertical(), -20.0);
256
    }
257

            
258
    #[test]
259
    fn uniform_min_max_do_not_panic_and_overflow_to_infinity_not_wrap() {
260
        // f32::MAX + f32::MAX overflows. IEEE-754 says +inf; it must not panic,
261
        // wrap to a negative, or saturate back to f32::MAX.
262
        let hi = PageMargins::uniform(f32::MAX);
263
        assert_eq!(hi.horizontal(), f32::INFINITY);
264
        assert_eq!(hi.vertical(), f32::INFINITY);
265

            
266
        let lo = PageMargins::uniform(f32::MIN);
267
        assert_eq!(lo.horizontal(), f32::NEG_INFINITY);
268
        assert_eq!(lo.vertical(), f32::NEG_INFINITY);
269
    }
270

            
271
    #[test]
272
    fn uniform_exactly_at_the_overflow_boundary_stays_finite() {
273
        // MAX/2 is exactly representable, so doubling it must land on MAX exactly
274
        // — the last finite step before the overflow tested above.
275
        let edge = PageMargins::uniform(f32::MAX / 2.0);
276
        assert_eq!(edge.horizontal(), f32::MAX);
277
        assert!(edge.horizontal().is_finite());
278
    }
279

            
280
    #[test]
281
    fn uniform_smallest_normal_doubles_exactly_without_flushing_to_zero() {
282
        let tiny = PageMargins::uniform(f32::MIN_POSITIVE);
283
        assert_eq!(tiny.horizontal(), f32::MIN_POSITIVE * 2.0);
284
        assert!(tiny.horizontal() > 0.0, "tiny margin flushed to zero");
285
    }
286

            
287
    #[test]
288
    fn uniform_nan_and_inf_propagate_without_panicking() {
289
        let nan = PageMargins::uniform(f32::NAN);
290
        assert!(nan.horizontal().is_nan());
291
        assert!(nan.vertical().is_nan());
292

            
293
        let inf = PageMargins::uniform(f32::INFINITY);
294
        assert_eq!(inf.horizontal(), f32::INFINITY);
295
        assert_eq!(inf.vertical(), f32::INFINITY);
296

            
297
        let neg_inf = PageMargins::uniform(f32::NEG_INFINITY);
298
        assert_eq!(neg_inf.horizontal(), f32::NEG_INFINITY);
299
        assert_eq!(neg_inf.vertical(), f32::NEG_INFINITY);
300
    }
301

            
302
    #[test]
303
    fn uniform_agrees_with_new_given_the_same_value() {
304
        for v in HOSTILE_F32 {
305
            let u = PageMargins::uniform(v);
306
            let n = PageMargins::new(v, v, v, v);
307
            assert!(same_f32(u.top, n.top));
308
            assert!(same_f32(u.right, n.right));
309
            assert!(same_f32(u.bottom, n.bottom));
310
            assert!(same_f32(u.left, n.left));
311
            assert!(same_f32(u.horizontal(), n.horizontal()));
312
            assert!(same_f32(u.vertical(), n.vertical()));
313
        }
314
    }
315

            
316
    // --- PageMargins::horizontal / vertical (getters) ------------------------
317

            
318
    #[test]
319
    fn horizontal_sums_left_and_right_vertical_sums_top_and_bottom() {
320
        // new(top, right, bottom, left) = (1, 2, 3, 4)
321
        //   horizontal = left + right   = 4 + 2 = 6
322
        //   vertical   = top  + bottom  = 1 + 3 = 4
323
        // Distinct values, so picking the wrong pair cannot produce these sums.
324
        let m = PageMargins::new(1.0, 2.0, 3.0, 4.0);
325
        assert_eq!(m.horizontal(), 6.0);
326
        assert_eq!(m.vertical(), 4.0);
327
    }
328

            
329
    #[test]
330
    fn horizontal_and_vertical_are_axis_independent() {
331
        // Loading one axis must not move the other.
332
        let h_only = PageMargins::new(0.0, 7.0, 0.0, 11.0);
333
        assert_eq!(h_only.horizontal(), 18.0);
334
        assert_eq!(h_only.vertical(), 0.0);
335

            
336
        let v_only = PageMargins::new(7.0, 0.0, 11.0, 0.0);
337
        assert_eq!(v_only.vertical(), 18.0);
338
        assert_eq!(v_only.horizontal(), 0.0);
339
    }
340

            
341
    #[test]
342
    fn opposing_infinities_yield_nan_not_a_panic() {
343
        // +inf + -inf is NaN by IEEE-754. The getter must return it, not trap.
344
        let m = PageMargins::new(
345
            f32::INFINITY,
346
            f32::INFINITY,
347
            f32::NEG_INFINITY,
348
            f32::NEG_INFINITY,
349
        );
350
        assert!(m.vertical().is_nan(), "inf + -inf should be NaN");
351
        assert!(m.horizontal().is_nan(), "-inf + inf should be NaN");
352
    }
353

            
354
    #[test]
355
    fn opposing_extremes_cancel_to_zero_rather_than_overflowing() {
356
        // MAX + MIN == MAX + (-MAX) == 0.0, exactly. No overflow, no panic.
357
        let m = PageMargins::new(f32::MAX, f32::MAX, f32::MIN, f32::MIN);
358
        assert_eq!(m.vertical(), 0.0);
359
        assert_eq!(m.horizontal(), 0.0);
360
    }
361

            
362
    #[test]
363
    fn getters_absorb_a_tiny_operand_next_to_a_huge_one_without_error() {
364
        // Classic float-precision trap: 1e30 + 1.0 == 1e30. Documented, not a bug —
365
        // pin it so nobody "fixes" the sum into something lossier.
366
        // horizontal() = left + right, vertical() = top + bottom -- so each axis has
367
        // to MIX magnitudes for absorption to happen at all. (top, right, bottom, left)
368
        let m = PageMargins::new(1.0e30, 1.0, 1.0, 1.0e30);
369
        assert_eq!(m.horizontal(), 1.0e30);
370
        assert_eq!(m.vertical(), 1.0e30);
371
    }
372

            
373
    #[test]
374
    fn getters_do_not_panic_on_any_hostile_field_combination() {
375
        for a in HOSTILE_F32 {
376
            for b in HOSTILE_F32 {
377
                // new(top=a, right=b, bottom=b, left=a): both axes sum the same pair,
378
                // so horizontal() and vertical() must agree bit-for-bit for every one
379
                // of the 144 hostile combinations — and neither may panic.
380
                let m = PageMargins::new(a, b, b, a);
381
                assert!(
382
                    same_f32(m.horizontal(), m.vertical()),
383
                    "axes disagree for {a:?}/{b:?}: h={:?} v={:?}",
384
                    m.horizontal(),
385
                    m.vertical()
386
                );
387
            }
388
        }
389
    }
390

            
391
    #[test]
392
    fn getters_are_pure_and_do_not_mutate_the_receiver() {
393
        let m = PageMargins::new(1.0, 2.0, 3.0, 4.0);
394
        let first_h = m.horizontal();
395
        let first_v = m.vertical();
396
        // Repeat calls: same answer, fields untouched.
397
        assert_eq!(m.horizontal(), first_h);
398
        assert_eq!(m.vertical(), first_v);
399
        assert_eq!(m.top, 1.0);
400
        assert_eq!(m.right, 2.0);
401
        assert_eq!(m.bottom, 3.0);
402
        assert_eq!(m.left, 4.0);
403
    }
404

            
405
    #[test]
406
    fn page_margins_copy_does_not_alias_the_original() {
407
        let original = PageMargins::uniform(5.0);
408
        let mut copy = original; // Copy, not a move
409
        copy.top = 99.0;
410
        assert_eq!(
411
            original.top, 5.0,
412
            "mutating a copy wrote through to the original"
413
        );
414
        assert_eq!(original.vertical(), 10.0);
415
        assert_eq!(copy.vertical(), 104.0);
416
    }
417

            
418
    // --- round-trip ----------------------------------------------------------
419

            
420
    #[test]
421
    fn round_trip_new_fields_reconstruct_an_identical_margin() {
422
        for v in HOSTILE_F32 {
423
            let a = PageMargins::new(v, 1.0, 2.0, 3.0);
424
            let b = PageMargins::new(a.top, a.right, a.bottom, a.left);
425
            assert!(same_f32(a.top, b.top));
426
            assert!(same_f32(a.right, b.right));
427
            assert!(same_f32(a.bottom, b.bottom));
428
            assert!(same_f32(a.left, b.left));
429
        }
430
    }
431

            
432
    #[test]
433
    fn round_trip_paged_context_returns_the_height_it_was_built_from() {
434
        for h in [
435
            0.0_f32,
436
            1.0,
437
            842.0,
438
            -842.0,
439
            f32::MAX,
440
            f32::MIN,
441
            f32::MIN_POSITIVE,
442
        ] {
443
            let ctx = FragmentationContext::new_paged(LogicalSize::new(595.0, h));
444
            assert!(same_f32(ctx.page_content_height(), h));
445
            assert!(ctx.is_paged());
446
        }
447
    }
448
}