1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
#[allow(clippy::float_cmp, clippy::eq_op, clippy::unusual_byte_groupings)]
5
mod autotest_generated {
6
    use core::{
7
        cmp::Ordering,
8
        hash::{Hash, Hasher},
9
    };
10

            
11
    use super::*;
12

            
13
    // ---------------------------------------------------------------
14
    // helpers
15
    // ---------------------------------------------------------------
16

            
17
    /// A fix with every optional field *present* (no NaN anywhere), so a
18
    /// test can knock out exactly one field and watch only that getter flip.
19
    const fn fix_all_present() -> LocationFix {
20
        LocationFix {
21
            latitude_deg: 48.208_8,
22
            longitude_deg: 16.372_1,
23
            accuracy_m: 5.0,
24
            altitude_m: 171.0,
25
            altitude_accuracy_m: 3.0,
26
            heading_deg: 90.0,
27
            speed_mps: 1.5,
28
            timestamp_ms: 1_234,
29
        }
30
    }
31

            
32
    /// The "platform reported nothing but lat/lon" fix the module docs describe.
33
    const fn fix_all_absent() -> LocationFix {
34
        LocationFix {
35
            latitude_deg: 0.0,
36
            longitude_deg: 0.0,
37
            accuracy_m: 0.0,
38
            altitude_m: f32::NAN,
39
            altitude_accuracy_m: f32::NAN,
40
            heading_deg: f32::NAN,
41
            speed_mps: f32::NAN,
42
            timestamp_ms: 0,
43
        }
44
    }
45

            
46
    const fn cfg(
47
        high_accuracy: bool,
48
        background: bool,
49
        max_accuracy_m: f32,
50
        min_interval_ms: u32,
51
    ) -> GeolocationProbeConfig {
52
        GeolocationProbeConfig {
53
            high_accuracy,
54
            background,
55
            max_accuracy_m,
56
            min_interval_ms,
57
        }
58
    }
59

            
60
    /// Bit-exact float identity (so `-0.0 != 0.0`), but NaN-tolerant: any NaN
61
    /// matches any NaN. A plain `==` would report `NaN != NaN` and make every
62
    /// round-trip assertion below vacuously fail; comparing raw payload bits
63
    /// would over-constrain (Rust does not guarantee NaN payload propagation).
64
    fn same_f32(a: f32, b: f32) -> bool {
65
        if a.is_nan() {
66
            b.is_nan()
67
        } else {
68
            a.to_bits() == b.to_bits()
69
        }
70
    }
71

            
72
    fn same_f64(a: f64, b: f64) -> bool {
73
        if a.is_nan() {
74
            b.is_nan()
75
        } else {
76
            a.to_bits() == b.to_bits()
77
        }
78
    }
79

            
80
    fn same_fix(a: &LocationFix, b: &LocationFix) -> bool {
81
        same_f64(a.latitude_deg, b.latitude_deg)
82
            && same_f64(a.longitude_deg, b.longitude_deg)
83
            && same_f32(a.accuracy_m, b.accuracy_m)
84
            && same_f32(a.altitude_m, b.altitude_m)
85
            && same_f32(a.altitude_accuracy_m, b.altitude_accuracy_m)
86
            && same_f32(a.heading_deg, b.heading_deg)
87
            && same_f32(a.speed_mps, b.speed_mps)
88
            && a.timestamp_ms == b.timestamp_ms
89
    }
90

            
91
    /// FNV-1a — a `no_std`-safe, deterministic stand-in for `DefaultHasher`
92
    /// (this crate is `no_std` without the `std` feature).
93
    struct Fnv1a(u64);
94

            
95
    impl Fnv1a {
96
        const fn new() -> Self {
97
            Self(0xcbf2_9ce4_8422_2325)
98
        }
99
    }
100

            
101
    impl Hasher for Fnv1a {
102
        fn finish(&self) -> u64 {
103
            self.0
104
        }
105
        fn write(&mut self, bytes: &[u8]) {
106
            for b in bytes {
107
                self.0 ^= u64::from(*b);
108
                self.0 = self.0.wrapping_mul(0x0000_0100_0000_01b3);
109
            }
110
        }
111
    }
112

            
113
    fn hash_of(c: &GeolocationProbeConfig) -> u64 {
114
        let mut h = Fnv1a::new();
115
        c.hash(&mut h);
116
        h.finish()
117
    }
118

            
119
    /// Every interesting `f32` class, as raw bits, including four distinct NaN
120
    /// encodings (quiet, negative-quiet, signalling, all-ones payload).
121
    const F32_PATTERNS: [u32; 16] = [
122
        0x0000_0000, // +0.0
123
        0x8000_0000, // -0.0
124
        0x0000_0001, // smallest positive subnormal
125
        0x8000_0001, // smallest negative subnormal
126
        0x007f_ffff, // largest subnormal
127
        0x0080_0000, // f32::MIN_POSITIVE
128
        0x3f80_0000, // 1.0
129
        0xbf80_0000, // -1.0
130
        0x7f7f_ffff, // f32::MAX
131
        0xff7f_ffff, // f32::MIN
132
        0x7f80_0000, // +inf
133
        0xff80_0000, // -inf
134
        0x7fc0_0000, // quiet NaN
135
        0xffc0_0000, // negative quiet NaN
136
        0x7f80_0001, // signalling NaN
137
        0x7fff_ffff, // NaN, all-ones payload
138
    ];
139

            
140
    // ---------------------------------------------------------------
141
    // LocationFix getters — the documented contract is
142
    // "NaN (and only NaN) means the platform did not report the field"
143
    // ---------------------------------------------------------------
144

            
145
    /// The core invariant, swept over every float class: a getter returns
146
    /// `None` **iff** its backing field is NaN, and otherwise hands back the
147
    /// value bit-for-bit — including infinities, subnormals and `-0.0`, which
148
    /// are all "reported" values and must NOT be swallowed like NaN is.
149
    #[test]
150
    fn getter_is_none_exactly_when_field_is_nan() {
151
        for bits in F32_PATTERNS {
152
            let v = f32::from_bits(bits);
153

            
154
            // Each getter is exercised on a fix where *only* its own field
155
            // carries the pattern, so a getter reading the wrong field is caught.
156
            let mut alt = fix_all_present();
157
            alt.altitude_m = v;
158
            let mut alt_acc = fix_all_present();
159
            alt_acc.altitude_accuracy_m = v;
160
            let mut head = fix_all_present();
161
            head.heading_deg = v;
162
            let mut spd = fix_all_present();
163
            spd.speed_mps = v;
164

            
165
            let got = [
166
                ("altitude", alt.altitude()),
167
                ("altitude_accuracy", alt_acc.altitude_accuracy()),
168
                ("heading", head.heading()),
169
                ("speed", spd.speed()),
170
            ];
171

            
172
            for (name, out) in got {
173
                if v.is_nan() {
174
                    assert!(
175
                        out.is_none(),
176
                        "{name}() must be None for NaN bits {bits:#010x}, got {out:?}"
177
                    );
178
                } else {
179
                    let inner = out.unwrap_or_else(|| {
180
                        panic!("{name}() must be Some for non-NaN bits {bits:#010x}")
181
                    });
182
                    assert_eq!(
183
                        inner.to_bits(),
184
                        bits,
185
                        "{name}() must return the field verbatim for {bits:#010x}"
186
                    );
187
                }
188
            }
189

            
190
            // The other three getters are untouched by the pattern under test.
191
            assert_eq!(alt.heading(), Some(90.0));
192
            assert_eq!(alt_acc.speed(), Some(1.5));
193
            assert_eq!(head.altitude(), Some(171.0));
194
            assert_eq!(spd.altitude_accuracy(), Some(3.0));
195
        }
196
    }
197

            
198
    /// `-0.0` is a legitimate reported value (a stationary device at sea level,
199
    /// a southbound heading rounded to zero). It must survive the getter with
200
    /// its sign bit intact rather than being normalised to `+0.0`.
201
    #[test]
202
    fn getters_preserve_negative_zero() {
203
        let mut fix = fix_all_present();
204
        fix.altitude_m = -0.0;
205
        fix.altitude_accuracy_m = -0.0;
206
        fix.heading_deg = -0.0;
207
        fix.speed_mps = -0.0;
208

            
209
        for out in [
210
            fix.altitude(),
211
            fix.altitude_accuracy(),
212
            fix.heading(),
213
            fix.speed(),
214
        ] {
215
            let v = out.expect("-0.0 is not NaN, so the field is 'reported'");
216
            assert_eq!(v.to_bits(), 0x8000_0000, "sign bit of -0.0 was dropped");
217
            assert!(v == 0.0, "-0.0 must still compare equal to 0.0");
218
        }
219
    }
220

            
221
    /// Infinities are not NaN, so per the documented contract they are handed
222
    /// through as `Some(inf)` — the getters must not "helpfully" filter them.
223
    #[test]
224
    fn getters_pass_infinities_through() {
225
        let mut fix = fix_all_absent();
226
        fix.altitude_m = f32::INFINITY;
227
        fix.altitude_accuracy_m = f32::NEG_INFINITY;
228
        fix.heading_deg = f32::INFINITY;
229
        fix.speed_mps = f32::NEG_INFINITY;
230

            
231
        assert_eq!(fix.altitude(), Some(f32::INFINITY));
232
        assert_eq!(fix.altitude_accuracy(), Some(f32::NEG_INFINITY));
233
        assert_eq!(fix.heading(), Some(f32::INFINITY));
234
        assert_eq!(fix.speed(), Some(f32::NEG_INFINITY));
235
    }
236

            
237
    /// The "platform reported nothing" fix, plus a saturated timestamp and
238
    /// out-of-range WGS-84 coordinates: nothing here may panic, and every
239
    /// optional getter must report absence.
240
    #[test]
241
    fn getters_on_extreme_and_all_absent_fix() {
242
        let mut fix = fix_all_absent();
243
        fix.latitude_deg = f64::MAX;
244
        fix.longitude_deg = f64::MIN;
245
        fix.accuracy_m = f32::MAX;
246
        fix.timestamp_ms = u64::MAX;
247

            
248
        assert_eq!(fix.altitude(), None);
249
        assert_eq!(fix.altitude_accuracy(), None);
250
        assert_eq!(fix.heading(), None);
251
        assert_eq!(fix.speed(), None);
252

            
253
        // Getters are `&self` — they must not have mutated the receiver.
254
        assert_eq!(fix.timestamp_ms, u64::MAX);
255
        assert!(fix.latitude_deg == f64::MAX);
256

            
257
        // Default-ish zeroed fix: 0.0 is not NaN, so everything is "reported".
258
        let zeroed = LocationFix {
259
            latitude_deg: 0.0,
260
            longitude_deg: 0.0,
261
            accuracy_m: 0.0,
262
            altitude_m: 0.0,
263
            altitude_accuracy_m: 0.0,
264
            heading_deg: 0.0,
265
            speed_mps: 0.0,
266
            timestamp_ms: 0,
267
        };
268
        assert_eq!(zeroed.altitude(), Some(0.0));
269
        assert_eq!(zeroed.altitude_accuracy(), Some(0.0));
270
        assert_eq!(zeroed.heading(), Some(0.0));
271
        assert_eq!(zeroed.speed(), Some(0.0));
272
    }
273

            
274
    /// The getters are declared `const fn`; that is part of the public API, so
275
    /// pin it — a non-const rewrite would be a silent breaking change.
276
    #[test]
277
    fn getters_are_const_evaluable() {
278
        const PRESENT: LocationFix = fix_all_present();
279
        const ABSENT: LocationFix = fix_all_absent();
280

            
281
        const ALT: Option<f32> = PRESENT.altitude();
282
        const ALT_ACC: Option<f32> = PRESENT.altitude_accuracy();
283
        const HEADING: Option<f32> = PRESENT.heading();
284
        const SPEED: Option<f32> = PRESENT.speed();
285
        const NO_ALT: Option<f32> = ABSENT.altitude();
286
        const NO_HEADING: Option<f32> = ABSENT.heading();
287

            
288
        assert_eq!(ALT, Some(171.0));
289
        assert_eq!(ALT_ACC, Some(3.0));
290
        assert_eq!(HEADING, Some(90.0));
291
        assert_eq!(SPEED, Some(1.5));
292
        assert_eq!(NO_ALT, None);
293
        assert_eq!(NO_HEADING, None);
294
    }
295

            
296
    /// `LocationFix` derives `PartialEq` over raw floats, so a fix carrying an
297
    /// unreported (NaN) field is *not* equal to itself. Pinned deliberately:
298
    /// the type is (correctly) not `Eq`/`Hash`, and any future use as a map key
299
    /// or in a `!=`-based change check would be broken by this. Callers must
300
    /// compare via the getters, as `same_fix` does.
301
    #[test]
302
    fn location_fix_partial_eq_is_not_reflexive_when_fields_are_absent() {
303
        let absent = fix_all_absent();
304
        assert!(absent != absent, "IEEE semantics: NaN != NaN");
305
        assert!(same_fix(&absent, &absent), "but it is the same fix");
306

            
307
        let present = fix_all_present();
308
        assert!(present == present, "a fully-reported fix is self-equal");
309
        assert!(same_fix(&present, &present));
310

            
311
        // `-0.0 == 0.0` under PartialEq, but the two are distinguishable.
312
        let mut neg_zero = fix_all_present();
313
        neg_zero.altitude_m = -0.0;
314
        let mut pos_zero = fix_all_present();
315
        pos_zero.altitude_m = 0.0;
316
        assert!(neg_zero == pos_zero);
317
        assert!(!same_fix(&neg_zero, &pos_zero));
318
    }
319

            
320
    // ---------------------------------------------------------------
321
    // OptionLocationFix — the FFI wrapper: encode == decode
322
    // ---------------------------------------------------------------
323

            
324
    /// Round-trip `Option<LocationFix>` -> `OptionLocationFix` -> back, for both
325
    /// variants, plus the full wrapper surface (`replace` must return the
326
    /// *previous* value, `map`/`and_then` must short-circuit on `None`).
327
    #[test]
328
    fn option_location_fix_roundtrips() {
329
        assert!(OptionLocationFix::default().is_none());
330
        assert!(!OptionLocationFix::default().is_some());
331
        assert_eq!(
332
            Option::<LocationFix>::from(OptionLocationFix::default()),
333
            None
334
        );
335

            
336
        let fix = fix_all_present();
337
        let wrapped: OptionLocationFix = Some(fix).into();
338
        assert!(wrapped.is_some());
339
        assert!(!wrapped.is_none());
340
        assert_eq!(wrapped.as_ref(), Some(&fix));
341
        assert_eq!(wrapped.as_option(), Some(&fix));
342
        assert_eq!(wrapped.into_option(), Some(fix));
343

            
344
        let decoded = Option::<LocationFix>::from(wrapped).expect("Some in, Some out");
345
        assert!(same_fix(&decoded, &fix), "encode == decode");
346

            
347
        let none: OptionLocationFix = Option::<LocationFix>::None.into();
348
        assert!(none.is_none());
349
        assert_eq!(none.as_ref(), None);
350
        assert_eq!(none.map(|f| f.timestamp_ms), None);
351
        assert_eq!(none.and_then(|f| f.altitude()), None);
352
        assert_eq!(wrapped.map(|f| f.timestamp_ms), Some(1_234));
353
        assert_eq!(wrapped.and_then(|f| f.altitude()), Some(171.0));
354

            
355
        // `replace` has mem::replace semantics: it yields the OLD value.
356
        let mut slot = OptionLocationFix::None;
357
        assert!(slot.replace(fix).is_none());
358
        assert_eq!(slot.as_option(), Some(&fix));
359

            
360
        let other = fix_all_absent();
361
        let prev = slot.replace(other);
362
        assert!(prev.is_some());
363
        assert!(same_fix(
364
            &Option::<LocationFix>::from(prev).expect("previous fix"),
365
            &fix
366
        ));
367
        assert!(same_fix(
368
            slot.as_option().expect("current fix"),
369
            &fix_all_absent()
370
        ));
371

            
372
        // `as_mut` hands out the live payload.
373
        if let Some(inner) = slot.as_mut() {
374
            inner.timestamp_ms = u64::MAX;
375
        }
376
        assert_eq!(slot.as_option().expect("still Some").timestamp_ms, u64::MAX);
377
    }
378

            
379
    /// The wrapper must survive the exact payload the platform layer produces
380
    /// most often: a fix whose optional fields are all NaN. `PartialEq` on the
381
    /// payload is useless here (NaN != NaN), so the round-trip is checked
382
    /// field-wise — this is the case a naive `assert_eq!` would hide.
383
    #[test]
384
    fn option_location_fix_roundtrip_survives_absent_fields() {
385
        let mut fix = fix_all_absent();
386
        fix.latitude_deg = -89.999_999_9;
387
        fix.longitude_deg = 179.999_999_9;
388
        fix.accuracy_m = f32::MIN_POSITIVE;
389
        fix.timestamp_ms = u64::MAX;
390

            
391
        let decoded = Option::<LocationFix>::from(OptionLocationFix::Some(fix))
392
            .expect("Some survives the round-trip");
393

            
394
        assert!(same_fix(&decoded, &fix), "encode == decode for a NaN-y fix");
395
        assert_eq!(decoded.altitude(), None);
396
        assert_eq!(decoded.altitude_accuracy(), None);
397
        assert_eq!(decoded.heading(), None);
398
        assert_eq!(decoded.speed(), None);
399
        assert_eq!(decoded.timestamp_ms, u64::MAX);
400

            
401
        // The wrapper's own derived PartialEq inherits the NaN quirk; assert it
402
        // rather than trusting `assert_eq!` to mean anything here.
403
        assert!(OptionLocationFix::Some(fix) != OptionLocationFix::Some(fix));
404
        assert!(OptionLocationFix::None == OptionLocationFix::None);
405
    }
406

            
407
    /// `#[repr(C, u8)]` means the wrapper is tag + payload with no niche
408
    /// packing — it must be strictly larger than the payload, or the C ABI
409
    /// header generated for it would be wrong.
410
    #[test]
411
    fn option_location_fix_is_tagged_not_niche_packed() {
412
        use core::mem::{align_of, size_of};
413
        assert!(
414
            size_of::<OptionLocationFix>() > size_of::<LocationFix>(),
415
            "repr(C, u8) must carry an explicit discriminant"
416
        );
417
        assert!(align_of::<OptionLocationFix>() >= align_of::<LocationFix>());
418
        assert!(size_of::<LocationFix>() >= 2 * size_of::<f64>() + 5 * size_of::<f32>());
419
    }
420

            
421
    // ---------------------------------------------------------------
422
    // GeolocationProbeConfig — Default / Ord / Hash
423
    // ---------------------------------------------------------------
424

            
425
    /// The documented default: no high accuracy, no background, no accuracy
426
    /// filter (`0` = "deliver every sample"), no throttle.
427
    #[test]
428
    fn probe_config_default_is_permissive_zero() {
429
        let d = GeolocationProbeConfig::default();
430
        assert!(!d.high_accuracy);
431
        assert!(!d.background);
432
        assert!(d.max_accuracy_m == 0.0);
433
        assert_eq!(
434
            d.max_accuracy_m.to_bits(),
435
            0,
436
            "default must be +0.0, not -0.0"
437
        );
438
        assert_eq!(d.min_interval_ms, 0);
439
        assert_eq!(d, cfg(false, false, 0.0, 0));
440
        assert_eq!(d.cmp(&GeolocationProbeConfig::default()), Ordering::Equal);
441
    }
442

            
443
    /// Field precedence of the hand-written `Ord`: high_accuracy, then
444
    /// background, then accuracy bits, then interval. Each earlier field must
445
    /// dominate *every* later one, so the loser is given maximal later fields.
446
    #[test]
447
    fn probe_config_ord_field_precedence() {
448
        let low_but_maxed = cfg(false, true, f32::from_bits(u32::MAX), u32::MAX);
449
        let high_but_minimal = cfg(true, false, 0.0, 0);
450
        assert!(low_but_maxed < high_but_minimal, "high_accuracy dominates");
451

            
452
        let fg_maxed = cfg(true, false, f32::from_bits(u32::MAX), u32::MAX);
453
        let bg_minimal = cfg(true, true, 0.0, 0);
454
        assert!(
455
            fg_maxed < bg_minimal,
456
            "background dominates the numeric fields"
457
        );
458

            
459
        let small_acc = cfg(true, true, 1.0, u32::MAX);
460
        let big_acc = cfg(true, true, 2.0, 0);
461
        assert!(small_acc < big_acc, "accuracy bits dominate the interval");
462

            
463
        assert!(cfg(true, true, 1.0, 0) < cfg(true, true, 1.0, 1));
464
        assert!(cfg(true, true, 1.0, u32::MAX - 1) < cfg(true, true, 1.0, u32::MAX));
465
        assert_eq!(
466
            cfg(true, true, 1.0, u32::MAX).cmp(&cfg(true, true, 1.0, u32::MAX)),
467
            Ordering::Equal
468
        );
469
    }
470

            
471
    /// The ordering is over `to_bits`, NOT over numeric value: sign-magnitude
472
    /// bits mean every negative accuracy sorts *above* every positive one, and
473
    /// NaN sorts above +inf. Pinned because it is load-bearing (`NodeType`
474
    /// dedup/sort) and because it is exactly the trap a reader assumes away.
475
    #[test]
476
    fn probe_config_ord_is_bitwise_not_numeric() {
477
        let neg = cfg(false, false, -1.0, 0);
478
        let pos = cfg(false, false, 1.0, 0);
479
        assert!(
480
            neg.max_accuracy_m < pos.max_accuracy_m,
481
            "numerically: -1 < 1"
482
        );
483
        assert_eq!(
484
            neg.cmp(&pos),
485
            Ordering::Greater,
486
            "bitwise: 0xbf80_0000 > 0x3f80_0000"
487
        );
488

            
489
        let inf = cfg(false, false, f32::INFINITY, 0);
490
        let nan = cfg(false, false, f32::NAN, 0);
491
        assert_eq!(nan.cmp(&inf), Ordering::Greater, "NaN bits sort above +inf");
492
        assert_eq!(inf.cmp(&pos), Ordering::Greater);
493
    }
494

            
495
    /// A NaN accuracy must not break the total order: `cmp` stays reflexive,
496
    /// antisymmetric and transitive, and sorting a NaN-containing slice
497
    /// terminates without panicking (an inconsistent comparator can make
498
    /// `sort_unstable` misbehave).
499
    #[test]
500
    fn probe_config_ord_is_a_total_order_with_nan() {
501
        let nan = cfg(false, false, f32::NAN, 7);
502
        assert_eq!(nan.cmp(&nan), Ordering::Equal, "cmp must be reflexive");
503
        assert_eq!(nan.partial_cmp(&nan), Some(Ordering::Equal));
504

            
505
        let mut list = [
506
            cfg(true, true, f32::NAN, u32::MAX),
507
            cfg(false, false, -0.0, 0),
508
            cfg(false, false, f32::INFINITY, 1),
509
            nan,
510
            cfg(true, false, f32::NEG_INFINITY, 3),
511
            cfg(false, true, 0.0, u32::MAX),
512
            GeolocationProbeConfig::default(),
513
        ];
514
        list.sort_unstable();
515
        for w in list.windows(2) {
516
            assert_ne!(
517
                w[0].cmp(&w[1]),
518
                Ordering::Greater,
519
                "sort_unstable must leave the slice ordered under cmp"
520
            );
521
        }
522

            
523
        // Antisymmetry + transitivity across every pair/triple in the slice.
524
        for a in list {
525
            for b in list {
526
                assert_eq!(a.cmp(&b), b.cmp(&a).reverse(), "cmp must be antisymmetric");
527
                for c in list {
528
                    if a.cmp(&b) != Ordering::Greater && b.cmp(&c) != Ordering::Greater {
529
                        assert_ne!(a.cmp(&c), Ordering::Greater, "cmp must be transitive");
530
                    }
531
                }
532
            }
533
        }
534
    }
535

            
536
    /// `PartialOrd` must agree with `Ord`, and — for the ordinary (finite,
537
    /// non-negative) configs a user actually writes — `==`, `cmp == Equal` and
538
    /// equal hashes must all coincide. This is the Eq/Ord/Hash contract holding
539
    /// on the reachable domain; the two regression tests below pin the -0.0/+0.0
540
    /// and NaN corners where a naive derived `PartialEq` would break it.
541
    #[test]
542
    fn probe_config_eq_ord_hash_agree_on_ordinary_configs() {
543
        let configs = [
544
            GeolocationProbeConfig::default(),
545
            cfg(false, false, 25.0, 1_000),
546
            cfg(true, false, 5.0, 0),
547
            cfg(true, true, 0.5, u32::MAX),
548
            cfg(false, true, f32::MAX, 1),
549
            cfg(true, true, f32::MIN_POSITIVE, 16),
550
        ];
551

            
552
        for a in configs {
553
            for b in configs {
554
                assert_eq!(
555
                    a.partial_cmp(&b),
556
                    Some(a.cmp(&b)),
557
                    "PartialOrd must mirror Ord"
558
                );
559
                assert_eq!(
560
                    a == b,
561
                    a.cmp(&b) == Ordering::Equal,
562
                    "`==` must agree with `cmp == Equal`"
563
                );
564
                if a == b {
565
                    assert_eq!(hash_of(&a), hash_of(&b), "Eq implies equal hashes");
566
                }
567
            }
568
            // Reflexivity of the whole trio.
569
            assert_eq!(a, a);
570
            assert_eq!(a.cmp(&a), Ordering::Equal);
571
            assert_eq!(hash_of(&a), hash_of(&a));
572
        }
573
    }
574

            
575
    /// Hashing must be a pure function of the four fields, must survive NaN
576
    /// (`to_bits` cannot panic), and must actually distinguish configs that
577
    /// differ only in a late field — otherwise `NodeType` dedup collapses
578
    /// distinct probes.
579
    #[test]
580
    fn probe_config_hash_is_stable_and_discriminating() {
581
        let a = cfg(true, false, 12.5, 250);
582
        assert_eq!(hash_of(&a), hash_of(&cfg(true, false, 12.5, 250)));
583

            
584
        let nan = cfg(false, false, f32::NAN, 0);
585
        assert_eq!(hash_of(&nan), hash_of(&nan), "hashing NaN must be stable");
586

            
587
        for other in [
588
            cfg(false, false, 12.5, 250),
589
            cfg(true, true, 12.5, 250),
590
            cfg(true, false, 12.75, 250),
591
            cfg(true, false, 12.5, 251),
592
        ] {
593
            assert_ne!(a, other);
594
            assert_ne!(
595
                hash_of(&a),
596
                hash_of(&other),
597
                "configs differing in one field must not collide: {other:?}"
598
            );
599
        }
600
    }
601

            
602
    // ---------------------------------------------------------------
603
    // Regression guards for Eq/Ord/Hash consistency of the f32 config field
604
    // (canonicalized bits: -0.0 == +0.0, NaN == NaN). The derived PartialEq used
605
    // to disagree with the hand-written Ord/Hash.
606
    // ---------------------------------------------------------------
607

            
608
    /// `impl Eq` + `impl Hash` promise: `a == b` implies `hash(a) == hash(b)`
609
    /// and `a.cmp(&b) == Equal`. A derived `PartialEq` would compare
610
    /// `max_accuracy_m` numerically (`-0.0 == 0.0`) while `Hash`/`Ord` compared
611
    /// raw `to_bits` (where they differ), so a `HashMap`/`BTreeMap` keyed on a
612
    /// `NodeType` carrying this config could fail to find an entry that compares
613
    /// equal. The hand-written `PartialEq`/`Ord`/`Hash` all route through
614
    /// `canon_bits`, keeping them consistent — this pins that.
615
    #[test]
616
    fn probe_config_eq_implies_equal_hash_and_ordering() {
617
        let pos = cfg(false, false, 0.0, 0);
618
        let neg = cfg(false, false, -0.0, 0);
619

            
620
        assert_eq!(pos, neg, "IEEE: -0.0 == 0.0");
621
        assert_eq!(hash_of(&pos), hash_of(&neg), "Eq/Hash contract");
622
        assert_eq!(pos.cmp(&neg), Ordering::Equal, "Eq/Ord contract");
623
    }
624

            
625
    /// `impl Eq for GeolocationProbeConfig {}` asserts reflexivity. A derived
626
    /// `PartialEq` comparing `max_accuracy_m` with float `==` would make a
627
    /// config with a NaN accuracy unequal to itself — while `Ord` (via
628
    /// `canon_bits`) reports `Equal` for the very same pair. The hand-written
629
    /// `PartialEq` uses `canon_bits` too, so both agree; this pins it.
630
    #[test]
631
    #[allow(clippy::eq_op)]
632
    fn probe_config_eq_is_reflexive_with_nan() {
633
        let c = cfg(false, false, f32::NAN, 0);
634

            
635
        assert_eq!(c.cmp(&c), Ordering::Equal, "Ord says equal");
636
        assert_eq!(c, c, "so Eq must too");
637
    }
638
}