ijson 0.1.7

A more memory efficient replacement for serde_json::Value
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
//! Shared edge-case inputs for exhaustive testing of the numeric code paths.
//!
//! Blind fuzzing tends to miss the values that actually matter, because the
//! interesting behaviour lives at the *discontinuities* in how a number is
//! represented. This module enumerates those discontinuities and provides one
//! list per input flavour (`i64`, `u64`, `f64`, and raw JSON strings) covering
//! each of them. A number is stored as `mantissa * 10^exp`:
//!
//! - **Inline mantissa fit** — an integer fits inline only if `|value| < 2^55`
//!   (64-bit) or `< 2^23` (32-bit); otherwise it spills to a heap scalar.
//! - **f64 exact-representability** — an integer is exactly an `f64` iff it has
//!   at most 53 significant bits (the `2^53` cliff); this gates `to_f64`.
//! - **Integer type limits** — `i8..i64`, `u8..u64`, `i32`/`u32`, and the
//!   `i64`/`u64` seam at `2^63` (`i64::MAX + 1`).
//! - **float→int range** — `to_i64` accepts `x < i64::MAX as f64` (`== 2^63`)
//!   and `to_u64` accepts `x < u64::MAX as f64` (`== 2^64`).
//! - **Inline decimal fractions** — dyadic short decimals at `exp -1..=-7`; the
//!   deepest exactly-representable one is `2^-7 = 0.0078125`.
//! - **Positive-exponent factoring** — integer-valued *floats* factor trailing
//!   zeros into `exp 1..=7` (so `1e18` stays inline), while plain integers do
//!   not (they spill to the heap).
//! - **int-vs-float distinction** — `has_decimal_point` (which drives
//!   serialization) separates `1e18` from `1000000000000000000`.
//! - **Special floats** — `0.0`/`-0.0`, NaN/±Inf, subnormals, `MIN`/`MAX`, and
//!   `2^127` (the exact-comparison limit in `cmp_int_f64`).
//! - **Big-int overflow** — an integer literal beyond `u64::MAX` is reparsed by
//!   `serde_json` as an `f64`, so it becomes a float.
#![allow(
    clippy::unreadable_literal,
    clippy::excessive_precision,
    clippy::approx_constant
)]

use std::f64;

/// `i64` inputs. Covers both mantissa-fit boundaries (`2^23`, `2^55`), the
/// `2^53` f64 cliff, every signed-integer type limit, powers of ten (which the
/// factoring logic is sensitive to), and assorted "awkward" magnitudes with no
/// trailing zeros.
pub(crate) fn i64_cases() -> Vec<i64> {
    let mut v = vec![
        // Tiny values and sign boundaries.
        0,
        1,
        -1,
        2,
        -2,
        5,
        -5,
        9,
        10,
        -10,
        11,
        -11,
        99,
        100,
        -100,
        101,
        127,
        128,
        -128,
        -129,
        255,
        256,
        1000,
        -1000,
        1_000_000,
        -1_000_000,
        // i32 / u32 limits (relevant to `to_i32`/`to_u32`).
        i32::MAX as i64,
        i32::MIN as i64,
        i32::MAX as i64 + 1,
        i32::MIN as i64 - 1,
        u32::MAX as i64,
        u32::MAX as i64 + 1,
        1i64 << 31,
        -(1i64 << 31),
        // The 2^53 f64 cliff: `to_f64` must switch from exact to `None` here.
        (1i64 << 53) - 1,
        1i64 << 53,
        (1i64 << 53) + 1,
        (1i64 << 53) + 2,
        -((1i64 << 53) + 1),
        1i64 << 52,
        1i64 << 54,
        // Inline mantissa fit at 2^23 (the 32-bit boundary).
        (1i64 << 23) - 1,
        1i64 << 23,
        (1i64 << 23) + 1,
        -(1i64 << 23),
        -(1i64 << 23) - 1,
        // Inline mantissa fit at 2^55 (the 64-bit boundary).
        (1i64 << 55) - 1,
        1i64 << 55,
        (1i64 << 55) + 1,
        -(1i64 << 55),
        -(1i64 << 55) - 1,
        -(1i64 << 55) + 1,
        // i64 limits.
        i64::MIN,
        i64::MIN + 1,
        i64::MAX,
        i64::MAX - 1,
        // Large magnitudes with no trailing zeros (must not factor, and are not
        // f64-exact) — several are prime.
        9_007_199_254_740_881,
        9_999_999_999_999_937,
        9_223_372_036_854_775_783,
        6_148_914_691_236_517_205, // 0x5555_5555_5555_5555
        -6_148_914_691_236_517_205,
        // Round magnitudes above the mantissa (previously factored as integers,
        // now spill to the heap).
        36_000_000_000_000_000,
        -36_000_000_000_000_000,
    ];
    // Powers of ten, positive and negative (10^18 < i64::MAX < 10^19).
    let mut p: i64 = 1;
    for _ in 0..=18 {
        v.push(p);
        v.push(-p);
        p = p.saturating_mul(10);
    }
    v
}

/// `u64` inputs. Covers the `i64`/`u64` seam at `2^63`, the top of the `u64`
/// range, the shared mantissa/f64 boundaries, and powers of ten up to `10^19`.
pub(crate) fn u64_cases() -> Vec<u64> {
    let mut v = vec![
        0,
        1,
        2,
        u32::MAX as u64,
        u32::MAX as u64 + 1,
        // 2^53 f64 cliff.
        (1u64 << 53) - 1,
        1u64 << 53,
        (1u64 << 53) + 1,
        // Inline mantissa fit at 2^55.
        (1u64 << 55) - 1,
        1u64 << 55,
        (1u64 << 55) + 1,
        // The i64/u64 seam: i64::MAX, then the first value that needs `u64`.
        i64::MAX as u64,
        i64::MAX as u64 + 1, // == 2^63
        (1u64 << 63) - 1,
        1u64 << 63,
        (1u64 << 63) + 1,
        // Top of the u64 range.
        u64::MAX,
        u64::MAX - 1,
        18_446_744_073_709_551_557, // largest u64 prime
        12_297_829_382_473_034_410, // 0xAAAA_AAAA_AAAA_AAAA
        6_148_914_691_236_517_205,  // 0x5555_5555_5555_5555
    ];
    // Powers of ten up to 10^19 (10^20 > u64::MAX).
    let mut p: u64 = 1;
    for _ in 0..=19 {
        v.push(p);
        p = p.saturating_mul(10);
    }
    v
}

/// Finite `f64` inputs. Covers exact dyadic short decimals (inline), decimals
/// that merely *look* short but are not exactly representable (heap), the
/// integer-valued floats that factor into positive exponents, the float→int
/// conversion thresholds at `2^63`/`2^64`, and the extremes of the `f64` range.
pub(crate) fn f64_cases() -> Vec<f64> {
    let mut v = vec![
        // Zero (and negative zero, which canonicalises to +0.0).
        0.0,
        -0.0,
        // Small integers as floats ("N.0").
        1.0,
        -1.0,
        2.0,
        -2.0,
        10.0,
        100.0,
        -100.0,
        // Exact dyadic short decimals (inline, exp -1..=-7).
        0.5,
        -0.5,
        0.25,
        0.75,
        0.125,
        0.375,
        0.0625,
        0.1875,
        2.5,
        63.5,
        -63.5,
        0.0078125,  // 2^-7, the deepest inline fraction
        -0.0078125, // 2^-7, negative
        0.00390625, // 2^-8, just too deep -> heap
        // Fractions that are exact f64s but whose inline *scaled* mantissa exceeds
        // 2^53, so a naive `mantissa as f64 * 10^exp` decode rounds (found by
        // fuzzing `to_f64_lossy`). `949288156749637.5 == 9492881567496375 * 10^-1`.
        949288156749637.5,
        -949288156749637.5,
        3000000000000000.5, // 30000000000000005 * 10^-1
        123456789012345.75, // 12345678901234575 * 10^-2
        // "Short-looking" decimals that are not exactly representable (heap).
        0.1,
        0.2,
        0.3,
        0.7,
        1.1,
        3.14,
        0.30000000000000004, // 0.1 + 0.2
        // Irrational-ish constants (heap f64).
        f64::consts::PI,
        f64::consts::E,
        f64::consts::TAU,
        f64::consts::SQRT_2,
        -f64::consts::PI,
        // Integer-valued floats spanning the factoring range (some inline via a
        // positive exponent, the largest ones spill to the heap).
        1e7,
        1e8,
        1e15,
        1e16,
        1e17,
        1e18,
        1e19,
        1e20,
        1e21,
        1e22, // largest power of ten that is exactly an f64
        1e23, // not exactly an f64 -> heap
        -1e18,
        // Powers of two as floats (all exact integers).
        (1u64 << 52) as f64,
        (1u64 << 53) as f64,
        (1u64 << 54) as f64,
        (1u64 << 55) as f64,
        // f32 exactness cliff at 2^24 (relevant to `to_f32`).
        16777216.0, // 2^24
        16777217.0, // 2^24 + 1
        16777218.0,
        // float -> int conversion thresholds.
        9223372036854774784.0,  // 2^63 - 1024, largest f64 int below i64::MAX
        9223372036854775808.0,  // 2^63 == i64::MAX as f64 (not convertible to i64)
        18446744073709549568.0, // 2^64 - 2048, largest f64 int below u64::MAX
        18446744073709551616.0, // 2^64 == u64::MAX as f64 (not convertible to u64)
        // The exact-comparison limit in `cmp_int_f64`.
        1.7014118346046923e38, // 2^127
        // Extremes of the f64 range.
        f64::MAX,
        f64::MIN, // == -f64::MAX
        f64::MIN_POSITIVE,
        f64::from_bits(1), // smallest positive subnormal (~5e-324)
        -f64::from_bits(1),
        f64::EPSILON,
    ];
    // Negatives of the integer-valued and extreme floats, for symmetry.
    let extra: Vec<f64> = [1e15, 1e16, 1e17, 1e19, 1e20, 1e22, 1e23, f64::MAX]
        .iter()
        .map(|x| -x)
        .collect();
    v.extend(extra);
    v
}

/// Non-finite `f64` inputs, which cannot be stored in an `INumber` and must be
/// rejected by `try_from` (and turned into `null` by `IValue::from`).
pub(crate) fn f64_nonfinite_cases() -> Vec<f64> {
    vec![f64::NAN, -f64::NAN, f64::INFINITY, f64::NEG_INFINITY]
}

/// Raw JSON number strings to deserialize via `serde_json`. Covers the
/// integer/float syntactic distinction, the `i64`/`u64`/overflow parsing seams,
/// e-notation in every form, trailing-zero decimals, and both the shallowest and
/// deepest inline fractions.
pub(crate) fn json_number_cases() -> Vec<&'static str> {
    vec![
        // Plain integers, including both signs of zero.
        "0",
        "-0",
        "1",
        "-1",
        "10",
        "-10",
        "100",
        "255",
        "256",
        "1000000000000000000",
        "-1000000000000000000",
        // Integer parsing seams: i64::MAX, i64::MAX+1 (u64), u64::MAX, overflow.
        "9223372036854775807",
        "9223372036854775808",
        "18446744073709551615",
        "18446744073709551616",  // u64::MAX + 1 -> f64
        "-9223372036854775808",  // i64::MIN
        "-9223372036854775809",  // i64::MIN - 1 -> f64
        "100000000000000000000", // 1e20 written out -> f64
        "-100000000000000000000",
        // Mantissa-fit boundaries as integers.
        "8388608",           // 2^23
        "8388607",           // 2^23 - 1
        "36028797018963968", // 2^55
        "36028797018963967", // 2^55 - 1
        "9007199254740992",  // 2^53
        "9007199254740993",  // 2^53 + 1 (exact integer, not f64-exact)
        // No-trailing-zero large integers.
        "12345678901234567",
        "9999999999999937",
        // Floats with an explicit decimal point.
        "0.0",
        "-0.0",
        "1.0",
        "2.0",
        "1.5",
        "-1.5",
        "0.5",
        "0.25",
        "0.1",
        "3.141592653589793",
        "0.0078125",  // 2^-7 (inline)
        "0.00390625", // 2^-8 (heap)
        "0.30000000000000004",
        // Trailing-zero decimals (re-serialise to a shorter form but stay equal).
        "1.50",
        "2.000",
        "100.00",
        "9007199254740992.0", // 2^53 as a float
        // e-notation in every spelling.
        "1e0",
        "0e0",
        "1e1",
        "2e2",
        "1e18", // same magnitude as the integer "1000000000000000000"
        "1E18",
        "1e+18",
        "1.5e3",
        "1.5E-3",
        "1e-7",
        "1e7",  // == 10000000.0, a float despite being integer-valued
        "1e22", // factors inline
        "1e23", // spills to the heap
        // Extremes of the f64 range (all finite and parseable).
        "1e308",
        "1e-308",
        "5e-324",                 // smallest positive subnormal
        "1e-400",                 // underflows to 0.0
        "1.7976931348623157e308", // f64::MAX
    ]
}

/// Number strings for `INumber`'s own [`FromStr`](std::str::FromStr) parser,
/// mirroring the other lists: the curated JSON strings above plus every `i64`,
/// `u64` and `f64` boundary rendered as text. The parser must accept them all and
/// agree with both direct construction and `serde_json`.
pub(crate) fn string_number_cases() -> Vec<String> {
    let mut v: Vec<String> = json_number_cases()
        .iter()
        .map(|s| (*s).to_owned())
        .collect();
    v.extend(i64_cases().iter().map(|x| x.to_string()));
    v.extend(u64_cases().iter().map(|x| x.to_string()));
    // Render floats via `serde_json` so each keeps float syntax (a decimal point
    // or exponent), exactly as it would appear in JSON.
    v.extend(
        f64_cases()
            .iter()
            .map(|x| serde_json::to_string(x).expect("a finite f64 serialises")),
    );
    v
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::IValue;
    use std::collections::hash_map::DefaultHasher;
    use std::convert::TryFrom;
    use std::hash::{Hash, Hasher};

    // An integer is exactly representable as an f64 iff it has at most 53
    // significant bits, i.e. `leading_zeros + trailing_zeros >= 11` over 64 bits.
    fn f64_exact_u64(a: u64) -> bool {
        a.leading_zeros() + a.trailing_zeros() >= 11
    }

    fn hash_of(v: &IValue) -> u64 {
        let mut h = DefaultHasher::new();
        v.hash(&mut h);
        h.finish()
    }

    fn json(s: &str) -> IValue {
        serde_json::from_str(s).unwrap_or_else(|e| panic!("parse {:?}: {}", s, e))
    }

    // Parses a number string through `INumber`'s own `FromStr` parser.
    fn inum(s: &str) -> IValue {
        IValue::from(
            s.parse::<crate::INumber>()
                .unwrap_or_else(|e| panic!("parse {:?}: {}", s, e)),
        )
    }

    // Every number from the edge-case lists, as `IValue`s — including the string
    // list parsed through `INumber::from_str`. Deliberately contains duplicates
    // (the same value reached via different types/representations/parsers), which
    // is exactly what the equality/hash/order invariants must tolerate.
    // Only the two O(n^2) pool tests use this, and both are skipped under Miri.
    #[cfg(not(miri))]
    fn number_pool() -> Vec<IValue> {
        let mut pool = Vec::new();
        pool.extend(i64_cases().into_iter().map(IValue::from));
        pool.extend(u64_cases().into_iter().map(IValue::from));
        pool.extend(f64_cases().into_iter().map(IValue::from));
        pool.extend(string_number_cases().iter().map(|s| inum(s)));
        pool
    }

    /// `deserialize_any` picks a visitor method from `has_decimal_point`, then has to
    /// actually produce a number of that shape. The accessors it reaches for are the
    /// *exact* ones, which are partial by design: with `arbitrary_precision` a number
    /// need be neither an exact `f64` nor an `i64`/`u64` — `0.1` is exactly a decimal,
    /// and a 30-digit integer fits nothing — so every one of them can return `None`.
    ///
    /// Deserializing must still work for any number the library can hold, so this walks
    /// the whole edge-case pool through `deserialize_any` (which is what an untyped
    /// target like `serde_json::Value` uses).
    #[test]
    fn deserialize_any_is_total_over_every_number() {
        for s in string_number_cases() {
            let v = inum(&s);
            let back: serde_json::Value = crate::from_value(&v)
                .unwrap_or_else(|e| panic!("deserialize_any on {:?}: {}", s, e));
            assert!(back.is_number(), "{:?} did not come back a number", s);
        }
    }

    /// Serializing a number must reproduce it: same value, same JSON shape. This is
    /// what stops an exact decimal from being quietly rounded on the way out — writing
    /// it through an `f64` would turn `1e-400` into `0.0` and round any long fraction —
    /// and what stops an integer held as an `f64` from coming back as a float.
    #[test]
    fn numbers_round_trip_through_json() {
        let mut cases = string_number_cases();
        cases.extend(
            [
                // Beyond every fixed-width representation, in both directions.
                "123456789012345678901234567890123",
                "-123456789012345678901234567890123",
                "0.1234567890123456789012345678901234567890",
                "1e400",
                "-1e400",
                "1e-400",
                // An integer that is exactly an `f64` but past `u64` — the case where
                // "stored as a float" and "written as a float" come apart.
                "100000000000000000000",
                "18446744073709551616",
                // A float whose value is a whole number, but which must stay a float.
                "1.2345678901234567891e19",
                "1e19",
            ]
            .map(str::to_owned),
        );

        for s in &cases {
            // Only `arbitrary_precision` can hold these exactly; without it the parser
            // rejects the ones that overflow an `f64` and rounds the rest, so a
            // round-trip is not expected to be exact.
            let Ok(n) = s.parse::<crate::INumber>() else {
                assert!(
                    !cfg!(feature = "arbitrary_precision"),
                    "{:?} should be representable",
                    s
                );
                continue;
            };

            let text = serde_json::to_string(&n).unwrap_or_else(|e| panic!("write {:?}: {}", s, e));
            let back: crate::INumber = text.parse().unwrap_or_else(|e| {
                panic!("{:?} wrote {:?}, which does not re-parse: {}", s, text, e)
            });

            assert_eq!(back, n, "{:?} wrote {:?}, a different value", s, text);
            assert_eq!(
                back.has_decimal_point(),
                n.has_decimal_point(),
                "{:?} wrote {:?}, which changed integer/float shape",
                s,
                text
            );
            assert_eq!(hash_of(&back.into()), hash_of(&n.into()), "{:?} hash", s);
        }
    }

    #[test]
    fn i64_inputs_are_consistent() {
        for &x in &i64_cases() {
            let v = IValue::from(x);
            assert!(v.is_number(), "{} not a number", x);
            // Round-trips exactly and is an integer (no decimal point).
            assert_eq!(v.to_i64(), Some(x), "{} to_i64", x);
            assert!(!v.as_number().unwrap().has_decimal_point(), "{} dot", x);
            // `to_u64` succeeds exactly when the value is non-negative.
            assert_eq!(v.to_u64(), u64::try_from(x).ok(), "{} to_u64", x);
            // `to_f64` is `Some` exactly when the value is f64-exact.
            assert_eq!(
                v.to_f64().is_some(),
                f64_exact_u64(x.unsigned_abs()),
                "{} to_f64 exactness",
                x
            );
            // Narrowing conversions agree with the standard library.
            assert_eq!(v.to_i32(), i32::try_from(x).ok(), "{} to_i32", x);
            assert_eq!(v.to_u32(), u32::try_from(x).ok(), "{} to_u32", x);
            // serde round-trip preserves the value.
            let s = serde_json::to_string(&v).unwrap();
            let back: IValue = serde_json::from_str(&s).unwrap();
            assert_eq!(v, back, "{} serde round-trip ({})", x, s);
        }
    }

    #[test]
    fn u64_inputs_are_consistent() {
        for &x in &u64_cases() {
            let v = IValue::from(x);
            assert!(v.is_number(), "{} not a number", x);
            assert_eq!(v.to_u64(), Some(x), "{} to_u64", x);
            assert!(!v.as_number().unwrap().has_decimal_point(), "{} dot", x);
            assert_eq!(v.to_i64(), i64::try_from(x).ok(), "{} to_i64", x);
            assert_eq!(v.to_f64().is_some(), f64_exact_u64(x), "{} to_f64", x);
            let s = serde_json::to_string(&v).unwrap();
            let back: IValue = serde_json::from_str(&s).unwrap();
            assert_eq!(v, back, "{} serde round-trip ({})", x, s);
        }
    }

    #[test]
    fn f64_inputs_are_consistent() {
        for &x in &f64_cases() {
            let v = IValue::from(x);
            assert!(v.is_number(), "{} not a number", x);
            // Every f64 keeps a decimal point, and reconstructs exactly (lossy
            // conversion is only lossy for values we cannot store, and those
            // still store their own bits). `0.0 == -0.0`, so this holds for -0.0.
            assert!(v.as_number().unwrap().has_decimal_point(), "{} dot", x);
            assert_eq!(v.to_f64_lossy(), Some(x), "{} to_f64_lossy", x);
            // When integer conversion succeeds the recovered integer equals x.
            if let Some(i) = v.to_i64() {
                assert_eq!(i as f64, x, "{} to_i64 value", x);
            }
            if let Some(u) = v.to_u64() {
                assert_eq!(u as f64, x, "{} to_u64 value", x);
            }
            // A round trip through JSON text must not lose the value.
            let out = serde_json::to_string(&v).unwrap();
            let back: IValue = serde_json::from_str(&out).unwrap();
            #[cfg(feature = "arbitrary_precision")]
            {
                // Deserialization is exact: every `f64` is an exact decimal, written out in
                // full and read back through the same parser, so it reconstructs perfectly.
                // (ijson canonicalises `-0.0` to `+0.0`; `0.0 == -0.0`.)
                assert_eq!(back.to_f64_lossy(), Some(x), "{} exact serde round-trip", x);
            }
            #[cfg(not(feature = "arbitrary_precision"))]
            {
                // serde_json's default float *parser* is not exact-round-trip for all
                // magnitudes (its precise parser is behind `float_roundtrip`). Require only
                // that ijson loses no more than serde_json's own `f64` pipeline does.
                let baseline: f64 =
                    serde_json::from_str(&serde_json::to_string(&x).unwrap()).unwrap();
                assert_eq!(
                    back.to_f64_lossy(),
                    Some(baseline),
                    "{} serde round-trip",
                    x
                );
            }
        }
    }

    #[test]
    fn round_trips_through_inumber_exactly() {
        // Converting a number into an `INumber` and back out with the matching
        // accessor must return exactly the original value.
        for &x in &i64_cases() {
            assert_eq!(crate::INumber::from(x).to_i64(), Some(x), "i64 {}", x);
        }
        for &x in &u64_cases() {
            assert_eq!(crate::INumber::from(x).to_u64(), Some(x), "u64 {}", x);
        }
        for &x in &f64_cases() {
            let n = crate::INumber::try_from(x).unwrap();
            // The *exact* accessor round-trips (not merely the lossy one). `0.0 ==
            // -0.0`, so the canonicalisation of -0.0 to +0.0 still satisfies this.
            assert_eq!(n.to_f64(), Some(x), "f64 {} exact", x);
            assert_eq!(n.to_f64_lossy(), x, "f64 {} lossy", x);
        }
    }

    #[test]
    fn nonfinite_f64_inputs_are_rejected() {
        for &x in &f64_nonfinite_cases() {
            assert!(crate::INumber::try_from(x).is_err(), "{} accepted", x);
            assert!(IValue::from(x).is_null(), "{} not null", x);
        }
    }

    #[test]
    fn json_inputs_are_consistent() {
        for &s in &json_number_cases() {
            let v: IValue =
                serde_json::from_str(s).unwrap_or_else(|e| panic!("parse {:?}: {}", s, e));
            assert!(v.is_number(), "{:?} not a number", s);

            // A number has a decimal point iff it was *written* as a float — with `.`/`e`/
            // `E`. Without `arbitrary_precision`, deserialization also rounds anything it
            // cannot hold to an `f64`, which gains a point: a bare integer beyond `i64`/`u64`,
            // and the token "-0" (which serde reads as `-0.0`). With the feature,
            // deserialization is exact — both of those stay integers — so only the syntax
            // decides. (This mirrors `string_inputs_are_consistent`, which reaches the same
            // split through `INumber::from_str`; deserialization now shares that parser.)
            let syntactic_float = s.bytes().any(|b| matches!(b, b'.' | b'e' | b'E'));
            let fits_int = s.parse::<i64>().is_ok() || s.parse::<u64>().is_ok();
            let expect_dot = if cfg!(feature = "arbitrary_precision") {
                syntactic_float
            } else {
                syntactic_float || !fits_int || s == "-0"
            };
            assert_eq!(
                v.as_number().unwrap().has_decimal_point(),
                expect_dot,
                "{:?} decimal-point",
                s
            );

            // ijson's number deserialization agrees with serde_json's own
            // `Value` -> `IValue` path (both use serde_json's parser, so this is
            // exact regardless of that parser's float precision).
            let via_value: serde_json::Value = serde_json::from_str(s).unwrap();
            assert_eq!(v, IValue::from(via_value), "{:?} vs serde Value", s);

            // Serialising then reparsing is consistent between ijson and serde
            // (both share the same parser, so any float imprecision is identical).
            let out = serde_json::to_string(&v).unwrap();
            let back: IValue = serde_json::from_str(&out).unwrap();
            let back_value: serde_json::Value = serde_json::from_str(&out).unwrap();
            assert_eq!(back, IValue::from(back_value), "{:?} reparse agreement", s);
        }
    }

    #[test]
    fn string_inputs_are_consistent() {
        for s in &string_number_cases() {
            let s = s.as_str();
            let v = inum(s);
            assert!(v.is_number(), "{:?} not a number", s);

            // A number has a decimal point iff it is written as a float: with a
            // fraction/exponent, or — without `arbitrary_precision` — as a bare integer
            // too large for `i64`/`u64`, which then has nowhere to go but an `f64`. With
            // the feature, such an integer is stored exactly and stays an integer.
            // Unlike serde_json — which parses "-0" as -0.0 to keep the sign — the
            // string parser treats the integer token "-0" as the integer `0`, faithfully
            // to the JSON grammar (so no dot).
            let exact_big_integers = cfg!(feature = "arbitrary_precision");
            let syntactic_float = s.bytes().any(|b| matches!(b, b'.' | b'e' | b'E'));
            let fits_int = s.parse::<i64>().is_ok() || s.parse::<u64>().is_ok();
            let expect_dot = syntactic_float || (!fits_int && !exact_big_integers);
            let dot = v.as_number().unwrap().has_decimal_point();
            assert_eq!(dot, expect_dot, "{:?} decimal-point", s);

            if !expect_dot && fits_int {
                // An exact integer both parsers can hold: they agree on the value.
                // (An integer beyond `u64` is excluded: only *this* parser holds it
                // exactly, and serde_json has already rounded it to an `f64`, so the two
                // legitimately differ.)
                let js = json(s);
                assert_eq!(v, js, "{:?} vs serde_json", s);
                // When serde also stored it as an integer they land on identical
                // bits. (serde stores the token "-0" as the float -0.0 — a
                // different decimal-point class — so that one is skipped.)
                if !js.as_number().unwrap().has_decimal_point() {
                    assert_eq!(v.number_repr_key(), js.number_repr_key(), "{:?} repr", s);
                }
            } else if expect_dot {
                // Stored as a float. With `arbitrary_precision` this may be an
                // *exact decimal* more precise than any f64 (from_str keeps e.g.
                // 0.1 as 1*10^-1, unlike serde_json), so it is not equal to the f64
                // and is not compared as such — but its nearest f64 always matches
                // a direct `std` parse, in either configuration.
                assert_eq!(
                    v.to_f64_lossy(),
                    Some(s.parse::<f64>().unwrap()),
                    "{:?} nearest f64",
                    s
                );
            }

            // Serialising and reparsing preserves the value up to f64 precision.
            // (It is not exact for a heap f64 whose shortest decimal `serde_json`
            // emits then parses back as an exact decimal — e.g. 2^64 renders as
            // "1.8446744073709552e19", which denotes a slightly different integer.)
            let out = serde_json::to_string(&v).unwrap();
            assert_eq!(
                inum(&out).to_f64_lossy(),
                v.to_f64_lossy(),
                "{:?} round-trip ({})",
                s,
                out
            );
        }
    }

    #[test]
    fn string_parsing_matches_direct_construction() {
        // Rendering each numeric edge case to text and parsing it back through
        // `INumber::from_str` reproduces the directly-constructed number.
        for &x in &i64_cases() {
            let v = inum(&x.to_string());
            assert_eq!(v, IValue::from(x), "i64 {}", x);
            assert!(!v.as_number().unwrap().has_decimal_point(), "i64 {} dot", x);
        }
        for &x in &u64_cases() {
            let v = inum(&x.to_string());
            assert_eq!(v, IValue::from(x), "u64 {}", x);
            assert!(!v.as_number().unwrap().has_decimal_point(), "u64 {} dot", x);
        }
        for &x in &f64_cases() {
            let v = inum(&serde_json::to_string(&x).unwrap());
            // With `arbitrary_precision`, from_str may store the shortest decimal
            // exactly rather than the f64 (e.g. "0.1"), so compare via the nearest
            // f64 rather than the value.
            assert_eq!(v.to_f64_lossy(), Some(x), "f64 {}", x);
            assert!(v.as_number().unwrap().has_decimal_point(), "f64 {} dot", x);
        }
    }

    #[test]
    fn equal_magnitudes_agree_across_representations() {
        // A value written as an integer and as an e-notation float must compare
        // equal and hash equal, even though one is inline and the other heap and
        // they disagree on `has_decimal_point`.
        let pairs = [
            ("100", "1e2"),
            ("10000000", "1e7"),
            ("1000000000000000000", "1e18"),
        ];
        for (int_str, float_str) in pairs {
            let int = json(int_str);
            let float = json(float_str);
            assert_eq!(int, float, "{} == {}", int_str, float_str);
            assert!(!int.as_number().unwrap().has_decimal_point());
            assert!(float.as_number().unwrap().has_decimal_point());
            assert_eq!(
                hash_of(&int),
                hash_of(&float),
                "{} hash {}",
                int_str,
                float_str
            );
        }
    }

    #[test]
    fn normalisation_makes_equal_values_indistinguishable() {
        // Each group is the SAME mathematical number reached through different
        // types and representations. Within a group every value must compare
        // equal and hash equal (the crux the caller called out: -0.0, +0.0 and
        // integer 0 all normalise together, as do 1 and 1.0); representatives of
        // different groups must differ.
        let groups: Vec<Vec<IValue>> = vec![
            // Zero: both signed zeros and integer zero.
            vec![
                IValue::from(0_i64),
                IValue::from(0_u64),
                IValue::from(0.0_f64),
                IValue::from(-0.0_f64),
                json("0"),
                json("-0"),
                json("0.0"),
                json("-0.0"),
                json("0e0"),
            ],
            // One.
            vec![
                IValue::from(1_i64),
                IValue::from(1_u64),
                IValue::from(1.0_f64),
                json("1"),
                json("1.0"),
                json("1e0"),
            ],
            // Negative one.
            vec![
                IValue::from(-1_i64),
                IValue::from(-1.0_f64),
                json("-1"),
                json("-1.0"),
            ],
            // A hundred, integer and float spellings.
            vec![
                IValue::from(100_i64),
                IValue::from(1e2_f64),
                json("100"),
                json("1e2"),
                json("100.0"),
            ],
            // 2^55: exactly representable as i64, u64 and f64.
            vec![
                IValue::from(1_i64 << 55),
                IValue::from(1_u64 << 55),
                IValue::from((1_u64 << 55) as f64),
            ],
            // 10^18: heap integer vs inline factored e-notation float.
            vec![
                IValue::from(1_000_000_000_000_000_000_i64),
                IValue::from(1e18_f64),
                json("1000000000000000000"),
                json("1e18"),
            ],
            // 2^63: u64 vs float (both heap), straddling the i64/u64 seam.
            vec![
                IValue::from(1_u64 << 63),
                IValue::from(9223372036854775808.0_f64),
                json("9223372036854775808"),
            ],
            // A dyadic fraction.
            vec![IValue::from(0.5_f64), json("0.5"), json("5e-1")],
        ];

        for g in &groups {
            for a in g {
                for b in g {
                    assert_eq!(a, b, "same group not equal: {:?} vs {:?}", a, b);
                    assert_eq!(
                        hash_of(a),
                        hash_of(b),
                        "same group hashes differ: {:?} vs {:?}",
                        a,
                        b
                    );
                }
            }
        }
        for (i, g1) in groups.iter().enumerate() {
            for (j, g2) in groups.iter().enumerate() {
                if i != j {
                    assert_ne!(
                        g1[0], g2[0],
                        "different groups equal: {:?} vs {:?}",
                        g1[0], g2[0]
                    );
                }
            }
        }
    }

    #[test]
    fn sorting_matches_reference_order() {
        // A strictly-increasing sequence spanning representations and the
        // precision-trap adjacencies (a large integer just above the float it
        // rounds to, and the i64/u64/float seams).
        let ordered: Vec<IValue> = vec![
            IValue::from(f64::MIN), // -f64::MAX
            IValue::from(-1e300_f64),
            IValue::from(i64::MIN),
            IValue::from(-1e18_f64),
            IValue::from(-(1_i64 << 55)),
            IValue::from(-1_000_000_i64),
            IValue::from(-2.5_f64),
            IValue::from(-1_i64),
            IValue::from(-0.5_f64),
            IValue::from(-0.0078125_f64),
            IValue::from(0_i64),
            IValue::from(0.0078125_f64),
            IValue::from(0.5_f64),
            IValue::from(1_i64),
            IValue::from(1.5_f64),
            IValue::from(2_i64),
            IValue::from(100_i64),
            IValue::from(9007199254740992.0_f64), // 2^53 as a float
            IValue::from(9007199254740993_i64),   // 2^53 + 1 (> the float above)
            IValue::from(1e17_f64),
            IValue::from(1e18_f64),
            IValue::from(i64::MAX),    // 2^63 - 1
            IValue::from(1_u64 << 63), // 2^63
            IValue::from(u64::MAX),    // 2^64 - 1
            IValue::from(1e20_f64),    // > u64::MAX
            IValue::from(1e300_f64),
            IValue::from(f64::MAX),
        ];

        // Strictly increasing, so `cmp` never conflates two distinct values.
        for w in ordered.windows(2) {
            assert!(
                w[0] < w[1],
                "not strictly increasing: {:?} !< {:?}",
                w[0],
                w[1]
            );
        }

        // Sorting a shuffled copy recovers exactly this order.
        let mut shuffled = ordered.clone();
        shuffled.reverse();
        shuffled.sort_by(|a, b| a.partial_cmp(b).unwrap());
        assert_eq!(shuffled, ordered);
    }

    #[test]
    fn representation_is_canonical() {
        // Canonicalisation: the same number, reached different ways, must have the
        // identical internal representation. The *only* allowed difference is the
        // decimal point — an integer and the float form of the same value differ
        // (and larger values differ further in inline-vs-heap storage). Within one
        // decimal-point class the representation must be unique.

        // Integers reached via `i64`, `u64` and JSON all land on the same bits.
        for &x in &i64_cases() {
            let base = IValue::from(x).number_repr_key();
            assert_eq!(
                json(&x.to_string()).number_repr_key(),
                base,
                "int {} JSON",
                x
            );
            if x >= 0 {
                let via_u64 = IValue::from(x as u64).number_repr_key();
                assert_eq!(via_u64, base, "int {} via u64", x);
            }
        }
        for &x in &u64_cases() {
            let base = IValue::from(x).number_repr_key();
            assert_eq!(
                json(&x.to_string()).number_repr_key(),
                base,
                "uint {} JSON",
                x
            );
            if let Ok(i) = i64::try_from(x) {
                assert_eq!(
                    IValue::from(i).number_repr_key(),
                    base,
                    "uint {} via i64",
                    x
                );
            }
        }

        // Grouped equal magnitudes: within a decimal-point class the bits match
        // exactly. In particular -0.0, +0.0 and integer 0 collapse as expected
        // (the two signed zeros are literally the same bits).
        let groups: Vec<Vec<IValue>> = vec![
            vec![
                IValue::from(0_i64),
                IValue::from(0_u64),
                IValue::from(0.0_f64),
                IValue::from(-0.0_f64),
                json("0"),
                json("-0"),
                json("0.0"),
                json("-0.0"),
                json("0e0"),
            ],
            vec![
                IValue::from(1_i64),
                IValue::from(1.0_f64),
                json("1"),
                json("1.0"),
                json("1e0"),
            ],
            vec![
                IValue::from(100_i64),
                IValue::from(1e2_f64),
                json("100"),
                json("1e2"),
                json("100.0"),
            ],
            vec![
                IValue::from(1_000_000_000_000_000_000_i64),
                IValue::from(1e18_f64),
                json("1000000000000000000"),
                json("1e18"),
            ],
            vec![IValue::from(0.5_f64), json("0.5"), json("5e-1")],
        ];
        for g in &groups {
            for a in g {
                for b in g {
                    if a.as_number().unwrap().has_decimal_point()
                        == b.as_number().unwrap().has_decimal_point()
                    {
                        assert_eq!(
                            a.number_repr_key(),
                            b.number_repr_key(),
                            "same value & decimal-point, different representation: {:?} vs {:?}",
                            a,
                            b
                        );
                    }
                }
            }
        }
    }

    // Exhaustive pairwise checks over the whole pool are O(n^2); skip them under
    // Miri (which only needs to see the unsafe paths, covered by the tests above).
    #[cfg(not(miri))]
    #[test]
    fn equal_values_share_hash_and_representation() {
        let pool = number_pool();
        for a in &pool {
            for b in &pool {
                if a != b {
                    continue;
                }
                // The Hash/Eq contract: equal values must hash equal. This is the
                // cross-check between `inline::number::hash` and `number_hash`.
                assert_eq!(
                    hash_of(a),
                    hash_of(b),
                    "equal values hash differently: {:?} vs {:?}",
                    a,
                    b
                );
                // Canonicalisation: equal values with the same decimal point must
                // be bit-for-bit identical, no matter how they were constructed.
                if a.as_number().unwrap().has_decimal_point()
                    == b.as_number().unwrap().has_decimal_point()
                {
                    assert_eq!(
                        a.number_repr_key(),
                        b.number_repr_key(),
                        "equal values, same decimal-point, different representation: {:?} vs {:?}",
                        a,
                        b
                    );
                }
            }
        }
    }

    #[cfg(not(miri))]
    #[test]
    fn comparison_is_a_consistent_total_order() {
        use std::cmp::Ordering;
        let pool = number_pool();
        for a in &pool {
            for b in &pool {
                let ab = a
                    .partial_cmp(b)
                    .unwrap_or_else(|| panic!("no ordering: {:?} vs {:?}", a, b));
                let ba = b
                    .partial_cmp(a)
                    .unwrap_or_else(|| panic!("no ordering: {:?} vs {:?}", b, a));
                // Antisymmetry and agreement between `==` and `cmp == Equal`.
                assert_eq!(ab, ba.reverse(), "antisymmetry: {:?} vs {:?}", a, b);
                assert_eq!(
                    a == b,
                    ab == Ordering::Equal,
                    "eq/cmp disagree: {:?} vs {:?}",
                    a,
                    b
                );
            }
        }
    }
}