uninum 0.1.1

A robust, ergonomic unified number type for Rust with automatic overflow handling, type promotion, and cross-type consistency.
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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! Hash property tests for the Number type.
//!
//! Tests fundamental hash properties:
//! - Consistency: if a == b, then hash(a) == hash(b)
//! - Distribution quality: different values should hash differently
//! - Cross-type consistency: equivalent values across types hash the same
//! - Special value handling: NaN, infinity, zero values

use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[cfg(feature = "decimal")]
use std::str::FromStr;

use proptest::prelude::*;
use uninum::{Number, num};

fn calculate_hash<T: Hash>(t: &T) -> u64 {
    let mut hasher = DefaultHasher::new();
    t.hash(&mut hasher);
    hasher.finish()
}

/// Strategy for generating arbitrary Number values
fn number_strategy() -> impl Strategy<Value = Number> {
    prop_oneof![
        // Integer variants
        any::<u32>().prop_map(|x| Number::from(u64::from(x))),
        any::<i32>().prop_map(|x| Number::from(i64::from(x))),
        any::<u32>().prop_map(|x| Number::from(u64::from(x))),
        any::<i32>().prop_map(|x| Number::from(i64::from(x))),
        any::<u32>().prop_map(|x| Number::from(u64::from(x))),
        any::<i32>().prop_map(|x| Number::from(i64::from(x))),
        any::<u64>().prop_map(Number::from),
        any::<i64>().prop_map(Number::from),
        // Float variants - use finite values to avoid NaN/infinity for most tests
        any::<f64>()
            .prop_filter("finite f64", |f| f.is_finite())
            .prop_map(|f| num!(f)),
        any::<f64>()
            .prop_filter("finite f64", |f| f.is_finite())
            .prop_map(|f| num!(f)),
    ]
}

/// Strategy for generating equivalent Number pairs across different types
fn equivalent_number_pairs() -> impl Strategy<Value = (Number, Number)> {
    prop_oneof![
        // Small integers that fit in all types
        (0i32..=127i32).prop_map(|i| {
            let variants = [
                Number::from(u64::from(i as u32)),
                Number::from(i64::from(i)),
                Number::from(u64::from(i as u32)),
                Number::from(i64::from(i)),
                Number::from(u64::from(i as u32)),
                Number::from(i64::from(i)),
                Number::from(i as u64),
                Number::from(i as i64),
                num!(i as f64),
                num!(i as f64),
            ];
            let idx1 = (i as usize) % variants.len();
            let idx2 = ((i as usize) + 1) % variants.len();
            (variants[idx1].clone(), variants[idx2].clone())
        }),
        // Zero values
        Just((Number::from(0i64), num!(0.0f64))),
        Just((Number::from(0u64), num!(0.0f64))),
        Just((Number::from(0i64), Number::from(0u64))),
        // One values
        Just((Number::from(1i64), num!(1.0f64))),
        Just((Number::from(1u64), num!(1.0f64))),
        Just((Number::from(1i64), Number::from(1u64))),
    ]
}

/// Strategy for generating special float values
fn special_float_strategy() -> impl Strategy<Value = Number> {
    prop_oneof![
        Just(num!(f64::NAN)),
        Just(num!(f64::NAN)),
        Just(num!(f64::INFINITY)),
        Just(num!(f64::INFINITY)),
        Just(num!(f64::NEG_INFINITY)),
        Just(num!(f64::NEG_INFINITY)),
        Just(num!(0.0f64)),
        Just(num!(-0.0f64)),
        Just(num!(0.0f64)),
        Just(num!(-0.0f64)),
    ]
}

#[cfg(feature = "decimal")]
/// Strategy for generating decimal values
fn decimal_strategy() -> impl Strategy<Value = Number> {
    use rust_decimal::Decimal;

    prop_oneof![
        // Simple decimal values
        any::<i32>().prop_map(|i| Number::from(Decimal::new(i.abs() as i64, 0))),
        // Fractional decimals
        (any::<i32>(), 0u32..=28u32)
            .prop_map(|(i, scale)| Number::from(Decimal::new(i.abs() as i64, scale))),
    ]
}

// Property-based tests
proptest! {
    #[test]
    fn prop_hash_consistency(a in number_strategy(), b in number_strategy()) {
        // If two numbers are equal, their hashes must be equal
        if a == b {
            prop_assert_eq!(calculate_hash(&a), calculate_hash(&b));
        }
    }

    #[test]
    fn prop_equivalent_pairs_hash_same((a, b) in equivalent_number_pairs()) {
        // Numbers that are equivalent across types should hash the same
        if a == b {
            prop_assert_eq!(
                calculate_hash(&a),
                calculate_hash(&b),
                "Equivalent numbers {:?} and {:?} should have the same hash",
                a, b
            );
        }
    }

    #[test]
    fn prop_cross_type_integer_consistency(val in 0i32..=127i32) {
        // All integer types representing the same value should hash the same
        let numbers = [Number::from(u64::from(val as u32)),
            Number::from(i64::from(val)),
            Number::from(u64::from(val as u32)),
            Number::from(i64::from(val)),
            Number::from(u64::from(val as u32)),
            Number::from(i64::from(val)),
            Number::from(val as u64),
            Number::from(val as i64),
            num!(val as f64),
            num!(val as f64)];

        let first_hash = calculate_hash(&numbers[0]);
        for num in &numbers[1..] {
            if numbers[0] == *num {
                prop_assert_eq!(
                    first_hash,
                    calculate_hash(num),
                    "All equivalent representations of {} should hash the same",
                    val
                );
            }
        }
    }

    #[test]
    fn prop_special_float_consistency(special in special_float_strategy()) {
        // Test that special float values are handled correctly
        let hash1 = calculate_hash(&special);
        let hash2 = calculate_hash(&special);
        prop_assert_eq!(hash1, hash2, "Hash should be consistent for special value {:?}", special);
    }

    #[test]
    fn prop_hashmap_consistency(pairs in prop::collection::vec(equivalent_number_pairs(), 1..10)) {
        use std::collections::HashMap;

        for (a, b) in pairs {
            if a == b {
                let mut map = HashMap::new();
                map.insert(a.clone(), "first");
                map.insert(b.clone(), "second");

                // Should only have one entry since they're equal
                prop_assert_eq!(map.len(), 1, "Equal values should result in one HashMap entry");

                // Both should resolve to the same value
                prop_assert_eq!(map.get(&a), map.get(&b), "Equal keys should resolve to same value");
            }
        }
    }

    #[test]
    #[cfg(feature = "decimal")]
    fn prop_decimal_consistency(dec in decimal_strategy()) {
        // Test that decimal values hash consistently with equivalent integer/float values
        if let Some(arc_dec) = dec.try_get_decimal() {
            let decimal_val = arc_dec.as_ref();

            // If the decimal represents an integer, test against integer types
            if decimal_val.scale() == 0
                && let Ok(int_val) = decimal_val.to_string().parse::<i64>()
                    && int_val >= 0 && int_val <= i32::MAX as i64 {
                        let int_num = Number::from(i64::from(int_val as i32));
                        if dec == int_num {
                            prop_assert_eq!(
                                calculate_hash(&dec),
                                calculate_hash(&int_num),
                                "Decimal {} should hash the same as integer equivalent",
                                decimal_val
                            );
                        }
                    }
        }
    }
}

#[test]
fn prop_nan_consistency() {
    let nan_f64 = num!(f64::NAN);

    assert_eq!(
        calculate_hash(&nan_f64),
        calculate_hash(&nan_f64),
        "All NaN values should hash the same"
    );
}

#[test]
fn prop_infinity_consistency() {
    // Positive infinities should hash the same
    let pos_inf_f64 = num!(f64::INFINITY);
    assert_eq!(
        calculate_hash(&pos_inf_f64),
        calculate_hash(&pos_inf_f64),
        "Positive infinities should hash the same"
    );

    // Negative infinities should hash the same
    let neg_inf_f64 = num!(f64::NEG_INFINITY);
    assert_eq!(
        calculate_hash(&neg_inf_f64),
        calculate_hash(&neg_inf_f64),
        "Negative infinities should hash the same"
    );

    // But positive and negative should be different
    assert_ne!(
        calculate_hash(&pos_inf_f64),
        calculate_hash(&neg_inf_f64),
        "Positive and negative infinities should hash differently"
    );
}

#[test]
fn prop_zero_consistency() {
    // +0 and -0 should hash the same
    let pos_zero_f64_2 = num!(0.0f64);
    let neg_zero_f64_2 = num!(-0.0f64);
    let pos_zero_f64 = num!(0.0f64);
    let neg_zero_f64 = num!(-0.0f64);

    assert_eq!(
        calculate_hash(&pos_zero_f64_2),
        calculate_hash(&neg_zero_f64_2),
        "+0.0 and -0.0 f64 should hash the same"
    );

    assert_eq!(
        calculate_hash(&pos_zero_f64),
        calculate_hash(&neg_zero_f64),
        "+0.0 and -0.0 f64 should hash the same"
    );
}

/// Generate a comprehensive set of test numbers covering all variants and edge
/// cases
fn generate_test_numbers() -> Vec<Number> {
    let numbers = vec![
        // Basic integers
        Number::from(0u64),
        Number::from(1u64),
        Number::from(42u64),
        Number::from(255u64),
        Number::from(-128i64),
        Number::from(-1i64),
        Number::from(0i64),
        Number::from(1i64),
        Number::from(127i64),
        Number::from(0u64),
        Number::from(256u64),
        Number::from(65535u64),
        Number::from(-32768i64),
        Number::from(0i64),
        Number::from(32767i64),
        Number::from(0u64),
        Number::from(65536u64),
        Number::from(4294967295u64),
        Number::from(-2147483648i64),
        Number::from(0i64),
        Number::from(2147483647i64),
        Number::from(0u64),
        Number::from(4294967296u64),
        Number::from(u64::MAX),
        Number::from(i64::MIN),
        Number::from(0i64),
        Number::from(i64::MAX),
        // Floats
        num!(0.0f64),
        num!(-0.0f64),
        num!(1.0f64),
        num!(-1.0f64),
        num!(3.15159f64),
        num!(-3.15159f64),
        num!(f64::INFINITY),
        num!(f64::NEG_INFINITY),
        num!(f64::NAN),
        num!(0.0f64),
        num!(-0.0f64),
        num!(1.0f64),
        num!(-1.0f64),
        num!(std::f64::consts::PI),
        Number::from(-std::f64::consts::PI),
        num!(f64::INFINITY),
        num!(f64::NEG_INFINITY),
        num!(f64::NAN),
        // Cross-type equivalents
        Number::from(42u64),
        Number::from(42i64),
        num!(42.0f64),
        num!(42.0f64),
    ];

    #[cfg(feature = "decimal")]
    let numbers = {
        use rust_decimal::Decimal;
        let mut numbers = numbers;
        numbers.extend([
            Number::from(Decimal::new(0, 0)),
            Number::from(Decimal::new(1, 0)),
            Number::from(Decimal::new(42, 0)),
            Number::from(Decimal::new(314159, 5)),  // 3.14159
            Number::from(Decimal::new(-314159, 5)), // -3.14159
        ]);
        numbers
    };

    numbers
}

#[test]
fn test_hash_consistency_property() {
    let numbers = generate_test_numbers();

    // Test hash consistency: if a == b, then hash(a) == hash(b)
    for (i, a) in numbers.iter().enumerate() {
        for (j, b) in numbers.iter().enumerate() {
            if i != j && a == b {
                let hash_a = calculate_hash(a);
                let hash_b = calculate_hash(b);
                assert_eq!(
                    hash_a, hash_b,
                    "Hash consistency violated: {a:?} == {b:?} but hash({a:?}) = {hash_a} != \
                     {hash_b} = hash({b:?})"
                );
            }
        }
    }
}

#[test]
fn test_hash_distribution_quality() {
    // Test that different values produce different hashes (no guarantee, but should
    // be likely)
    let numbers = generate_test_numbers();
    let mut hashes = std::collections::HashMap::new();
    let mut collisions = 0;

    for num in &numbers {
        let hash = calculate_hash(num);
        if let Some(existing) = hashes.get(&hash) {
            if existing != num {
                collisions += 1;
            }
        } else {
            hashes.insert(hash, num.clone());
        }
    }

    // Allow some collisions, but not too many
    let collision_rate = collisions as f64 / numbers.len() as f64;
    assert!(
        collision_rate < 0.1,
        "Too many hash collisions: {} out of {} values ({}%)",
        collisions,
        numbers.len(),
        collision_rate * 100.0
    );
}

#[test]
fn test_cross_type_hash_consistency() {
    // Test that cross-type equivalent values hash consistently
    let equivalent_groups = vec![
        // Integer 1 in different types
        vec![
            Number::from(1u64),
            Number::from(1i64),
            Number::from(1u64),
            Number::from(1i64),
            Number::from(1u64),
            Number::from(1i64),
            Number::from(1u64),
            Number::from(1i64),
            num!(1.0f64),
            num!(1.0f64),
        ],
        // Zero in different types
        vec![
            Number::from(0u64),
            Number::from(0i64),
            Number::from(0u64),
            Number::from(0i64),
            Number::from(0u64),
            Number::from(0i64),
            Number::from(0u64),
            Number::from(0i64),
            num!(0.0f64),
            num!(0.0f64),
        ],
        // 42 in different types
        vec![
            Number::from(42u64),
            Number::from(42i64),
            Number::from(42u64),
            Number::from(42i64),
            Number::from(42u64),
            Number::from(42i64),
            Number::from(42u64),
            Number::from(42i64),
            num!(42.0f64),
            num!(42.0f64),
        ],
    ];

    #[cfg(feature = "decimal")]
    let equivalent_groups = {
        use rust_decimal::Decimal;
        let mut equivalent_groups = equivalent_groups;
        equivalent_groups[0].push(Number::from(Decimal::new(1, 0)));
        equivalent_groups[1].push(Number::from(Decimal::new(0, 0)));
        equivalent_groups[2].push(Number::from(Decimal::new(42, 0)));
        equivalent_groups
    };

    for group in equivalent_groups {
        // All values in a group should be equal and hash the same
        for a in &group {
            for b in &group {
                if a == b {
                    let hash_a = calculate_hash(a);
                    let hash_b = calculate_hash(b);
                    assert_eq!(
                        hash_a, hash_b,
                        "Cross-type hash consistency violated: {a:?} == {b:?} but hash({a:?}) = \
                         {hash_a} != {hash_b} = hash({b:?})"
                    );
                }
            }
        }
    }
}

#[test]
fn test_special_value_hashing() {
    // Test NaN hashing - all NaN values should hash the same
    let nan_f64 = num!(f64::NAN);

    assert_eq!(
        calculate_hash(&nan_f64),
        calculate_hash(&nan_f64),
        "All NaN values should hash the same"
    );

    // Test infinity hashing
    let pos_inf_f64 = num!(f64::INFINITY);
    let neg_inf_f64 = num!(f64::NEG_INFINITY);

    assert_eq!(
        calculate_hash(&pos_inf_f64),
        calculate_hash(&pos_inf_f64),
        "Positive infinities should hash the same"
    );
    assert_eq!(
        calculate_hash(&neg_inf_f64),
        calculate_hash(&neg_inf_f64),
        "Negative infinities should hash the same"
    );
    assert_ne!(
        calculate_hash(&pos_inf_f64),
        calculate_hash(&neg_inf_f64),
        "Positive and negative infinities should hash differently"
    );

    // Test zero hashing - +0 and -0 should hash the same
    let pos_zero = num!(0.0f64);
    let neg_zero = num!(-0.0f64);
    assert_eq!(
        calculate_hash(&pos_zero),
        calculate_hash(&neg_zero),
        "+0 and -0 should hash the same"
    );
}

#[test]
fn test_large_u64_eq_hash_consistency() {
    // Test with a large U64 value that exceeds i64::MAX
    let large_u64_val = (i64::MAX as u64) + 1;
    let u64_num = Number::from(large_u64_val);
    let f64_num = num!(large_u64_val as f64);

    // Both should be integers, and both should have as_i128() succeed
    assert!(u64_num.is_integer());
    assert!(f64_num.is_integer());
    assert!(u64_num.as_i128().is_some());
    assert!(f64_num.as_i128().is_some());

    // Due to the PartialEq implementation, these will be compared using as_i128()
    let u64_i128 = u64_num.as_i128().unwrap();
    let f64_i128 = f64_num.as_i128().unwrap();

    if u64_i128 == f64_i128 {
        // They should be equal
        assert_eq!(
            u64_num, f64_num,
            "Large U64 and equivalent F64 should be equal"
        );

        // And their hash values should also be equal
        let u64_hash = calculate_hash(&u64_num);
        let f64_hash = calculate_hash(&f64_num);

        assert_eq!(
            u64_hash, f64_hash,
            "Hash values must be equal for equal items. u64 hash: {u64_hash}, f64 hash: {f64_hash}"
        );
    } else {
        // If they don't have the same i128 representation, they should not be equal
        assert_ne!(
            u64_num, f64_num,
            "Numbers with different i128 representations should not be equal"
        );
    }
}

#[test]
fn test_other_large_u64_values() {
    // Test other large U64 values that exceed i64::MAX
    let large_values = [
        u64::MAX,
        u64::MAX - 1,
        (i64::MAX as u64) + 1,
        (i64::MAX as u64) + 100,
    ];

    for &val in &large_values {
        let u64_num = Number::from(val);
        let f64_num = num!(val as f64);

        // Check if they're equal
        let are_equal = u64_num == f64_num;

        // Check if their hashes are equal
        let hash_equal = calculate_hash(&u64_num) == calculate_hash(&f64_num);

        // If they're equal, their hashes must be equal
        if are_equal {
            assert!(
                hash_equal,
                "For value {}: numbers are equal but hashes differ. u64 hash: {}, f64 hash: {}",
                val,
                calculate_hash(&u64_num),
                calculate_hash(&f64_num)
            );
        }
    }
}

#[test]
fn test_hashmap_usage() {
    use std::collections::HashMap;

    let mut map = HashMap::new();

    // Different types with same value map to same entry
    map.insert(Number::from(100u64), "hundred");
    assert_eq!(map.get(&Number::from(100i64)), Some(&"hundred"));
    assert_eq!(map.get(&num!(100.0f64)), Some(&"hundred"));

    // Update with different type
    map.insert(num!(100.0f64), "one hundred");
    assert_eq!(map.get(&Number::from(100u64)), Some(&"one hundred"));
    assert_eq!(map.len(), 1);

    // Different values map to different entries
    map.insert(Number::from(200u64), "two hundred");
    assert_eq!(map.len(), 2);
}

#[test]
fn test_hashset_usage() {
    use std::collections::HashSet;

    let mut set = HashSet::new();

    // All these represent the value 42
    set.insert(Number::from(42u64));
    set.insert(Number::from(42u64));
    set.insert(Number::from(42u64));
    set.insert(Number::from(42u64));
    set.insert(Number::from(42i64));
    set.insert(Number::from(42i64));
    set.insert(Number::from(42i64));
    set.insert(Number::from(42i64));
    set.insert(num!(42.0f64));

    // All should hash to the same value
    assert_eq!(set.len(), 1);

    // Non-integer float hashes differently
    set.insert(num!(42.5f64));
    assert_eq!(set.len(), 2);
}

#[test]
fn test_large_u64_hashmap_behavior() {
    use std::collections::HashMap;

    // Test HashMap behavior with values that should be equal and hash the same
    let large_u64_val = (i64::MAX as u64) + 1;
    let u64_num = Number::from(large_u64_val);
    let f64_num = num!(large_u64_val as f64);

    // Only test if they are actually equal
    if u64_num == f64_num {
        let mut map = HashMap::new();
        map.insert(u64_num.clone(), "u64_num");
        map.insert(f64_num.clone(), "f64_num");

        // Since the values are equal and should hash the same, HashMap should contain
        // only one entry
        assert_eq!(
            map.len(),
            1,
            "HashMap should contain only one entry for equal values"
        );

        // Test lookup behavior
        assert_eq!(
            map.get(&u64_num),
            map.get(&f64_num),
            "Lookups should return the same value"
        );
    } else {
        // If they're not equal, they should have different entries
        let mut map = HashMap::new();
        map.insert(u64_num.clone(), "u64_num");
        map.insert(f64_num.clone(), "f64_num");

        assert_eq!(
            map.len(),
            2,
            "HashMap should contain two entries for different values"
        );
    }
}

#[test]
fn test_decimal_hash_consistency() {
    #[cfg(feature = "decimal")]
    {
        use std::collections::HashSet;

        use rust_decimal::Decimal;

        let mut set = HashSet::new();
        // Decimal(1) should hash the same as U64(1) and F64(1.0)
        set.insert(Number::from(Decimal::from(1)));
        set.insert(Number::from(1u64));
        set.insert(Number::from(1i64));
        set.insert(num!(1.0f64));
        assert_eq!(
            set.len(),
            1,
            "Decimal(1), U64(1), I64(1), and F64(1.0) should hash to same value"
        );
    }
}

#[test]
fn test_hash_canonical_integer_path() {
    // Test that the integer canonical path is used for integers
    // This covers the (is_negative, true, int_value, _) branch in Hash impl

    // Test positive integers
    let u32_val = Number::from(42u64);
    let i32_val = Number::from(42i64);
    let u64_val = Number::from(42u64);
    let i64_val = Number::from(42i64);

    // All should hash the same as they're equivalent integers
    let u32_hash = calculate_hash(&u32_val);
    let i32_hash = calculate_hash(&i32_val);
    let u64_hash = calculate_hash(&u64_val);
    let i64_hash = calculate_hash(&i64_val);

    assert_eq!(
        u32_hash, i32_hash,
        "U64(42) and I64(42) should hash the same"
    );
    assert_eq!(
        u32_hash, u64_hash,
        "U64(42) and U64(42) should hash the same"
    );
    assert_eq!(
        u32_hash, i64_hash,
        "U64(42) and I64(42) should hash the same"
    );

    // Test negative integers
    let neg_i32 = Number::from(-42i64);
    let neg_i64 = Number::from(-42i64);

    let neg_i32_hash = calculate_hash(&neg_i32);
    let neg_i64_hash = calculate_hash(&neg_i64);

    assert_eq!(
        neg_i32_hash, neg_i64_hash,
        "I64(-42) and I64(-42) should hash the same"
    );

    // Positive and negative should hash differently
    assert_ne!(u32_hash, neg_i32_hash, "42 and -42 should hash differently");
}

#[test]
fn test_hash_canonical_float_path() {
    // Test that the float canonical path is used for non-integers
    // This covers the (_, false, _, float_value) branch in Hash impl

    // Test fractional values
    let f64_frac = num!(42.5);
    let f64_frac_hash = calculate_hash(&f64_frac);

    // Test that integer and fractional values hash differently
    let f64_int = num!(42.0);
    let f64_int_hash = calculate_hash(&f64_int);

    assert_ne!(
        f64_frac_hash, f64_int_hash,
        "42.5 and 42.0 should hash differently"
    );

    // Test U64 values that exceed i64::MAX (these use float path)
    let large_u64 = Number::from((i64::MAX as u64) + 1);
    let large_u64_hash = calculate_hash(&large_u64);

    // This should use the float path because the value > i64::MAX
    // It should hash differently from smaller integers
    let small_u64 = Number::from(42u64);
    let small_u64_hash = calculate_hash(&small_u64);

    assert_ne!(
        large_u64_hash, small_u64_hash,
        "Large U64 and small U64 should hash differently"
    );
}

#[test]
fn test_hash_special_float_values() {
    // Test special float values in the float hash path

    // Test NaN - should use "NaN".hash(state)
    let nan = num!(f64::NAN);
    let nan_hash = calculate_hash(&nan);

    // All NaN values should hash the same
    let nan2 = num!(f64::NAN);
    let nan2_hash = calculate_hash(&nan2);

    assert_eq!(nan_hash, nan2_hash, "All NaN values should hash the same");

    // Test that NaN hashes differently from finite values
    let finite = num!(42.5);
    let finite_hash = calculate_hash(&finite);

    assert_ne!(
        nan_hash, finite_hash,
        "NaN and finite values should hash differently"
    );

    // Test infinity values
    let pos_inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    let pos_inf_hash = calculate_hash(&pos_inf);
    let neg_inf_hash = calculate_hash(&neg_inf);

    assert_ne!(
        pos_inf_hash, neg_inf_hash,
        "Positive and negative infinity should hash differently"
    );
    assert_ne!(
        pos_inf_hash, nan_hash,
        "Infinity and NaN should hash differently"
    );
}

#[cfg(feature = "decimal")]
#[test]
fn test_decimal_hash_distinguishes_close_values() {
    use rust_decimal::Decimal;

    // Two decimals that differ beyond f64 precision must still hash distinctly.
    let a = Number::from(Decimal::from_str("0.1000000000000000000000000000").unwrap());
    let b = Number::from(Decimal::from_str("0.1000000000000000000000000001").unwrap());

    let mut set = HashSet::new();
    set.insert(a.clone());
    set.insert(b.clone());

    assert_eq!(
        set.len(),
        2,
        "HashSet should retain both distinct decimal values"
    );
    assert!(set.contains(&a));
    assert!(set.contains(&b));
}

#[test]
fn test_hash_zero_canonical_handling() {
    // Test that zero values are handled correctly in canonical form
    // Both +0.0 and -0.0 should use the same canonical hash

    let pos_zero_f64 = num!(0.0);
    let neg_zero_f64 = num!(-0.0);

    let pos_zero_hash = calculate_hash(&pos_zero_f64);
    let neg_zero_hash = calculate_hash(&neg_zero_f64);

    // Both should hash the same because they're canonically equivalent
    assert_eq!(
        pos_zero_hash, neg_zero_hash,
        "+0.0 and -0.0 should hash the same"
    );

    // Test that all zero integer types hash the same
    let zero_u32 = Number::from(0u64);
    let zero_i32 = Number::from(0i64);
    let zero_u64 = Number::from(0u64);
    let zero_i64 = Number::from(0i64);

    let zero_u32_hash = calculate_hash(&zero_u32);
    let zero_i32_hash = calculate_hash(&zero_i32);
    let zero_u64_hash = calculate_hash(&zero_u64);
    let zero_i64_hash = calculate_hash(&zero_i64);

    assert_eq!(
        zero_u32_hash, zero_i32_hash,
        "U64(0) and I64(0) should hash the same"
    );
    assert_eq!(
        zero_u32_hash, zero_u64_hash,
        "U64(0) and U64(0) should hash the same"
    );
    assert_eq!(
        zero_u32_hash, zero_i64_hash,
        "U64(0) and I64(0) should hash the same"
    );

    // All zero values should hash the same
    assert_eq!(
        zero_u32_hash, pos_zero_hash,
        "Integer zero and float zero should hash the same"
    );
}

#[test]
fn test_hash_integer_canonical_path_coverage() {
    // Test the specific (is_negative, true, int_value, _) path in Hash impl
    // This ensures the integer canonical hashing path is exercised

    // Test cases that definitely use the integer canonical path:
    // - All U64/I64/U64/I64 that fit in i64
    // - F64 values that are integers
    // - Decimal values that are integers (when decimal feature enabled)

    // Positive integers - should use (false, true, value, _)
    let _integers = vec![
        Number::from(0u64),
        Number::from(42u64),
        Number::from(u64::from(u32::MAX)),
        Number::from(0i64),
        Number::from(42i64),
        Number::from(i64::from(i32::MAX)),
        Number::from(0u64),
        Number::from(42u64),
        Number::from(i64::MAX as u64), // within i64 range
        Number::from(0i64),
        Number::from(42i64),
        Number::from(i64::MAX),
        num!(0.0),  // integer F64
        num!(42.0), // integer F64
    ];

    // Negative integers - should use (true, true, value, _)
    let _negative_integers = [
        Number::from(-1i64),
        Number::from(-42i64),
        Number::from(i64::from(i32::MIN)),
        Number::from(-1i64),
        Number::from(-42i64),
        Number::from(i64::MIN),
        num!(-1.0),  // negative integer F64
        num!(-42.0), // negative integer F64
    ];

    // Test that all positive integers with same value hash the same
    let same_42 = [
        Number::from(42u64),
        Number::from(42i64),
        Number::from(42u64),
        Number::from(42i64),
        num!(42.0),
    ];

    let hash_42 = calculate_hash(&same_42[0]);
    for num in &same_42[1..] {
        assert_eq!(
            hash_42,
            calculate_hash(num),
            "All representations of 42 should use integer canonical path and hash the same"
        );
    }

    // Test that negative and positive versions hash differently
    let pos_42 = Number::from(42i64);
    let neg_42 = Number::from(-42i64);

    assert_ne!(
        calculate_hash(&pos_42),
        calculate_hash(&neg_42),
        "Positive and negative integers should hash differently via integer canonical path"
    );

    // Test zero special case (should be (false, true, 0, _))
    let zeros = [
        Number::from(0u64),
        Number::from(0i64),
        Number::from(0u64),
        Number::from(0i64),
        num!(0.0),
        num!(-0.0), // -0.0 should be treated same as 0.0
    ];

    let hash_zero = calculate_hash(&zeros[0]);
    for num in &zeros[1..] {
        assert_eq!(
            hash_zero,
            calculate_hash(num),
            "All zero representations should use integer canonical path and hash the same"
        );
    }

    #[cfg(feature = "decimal")]
    {
        use rust_decimal::Decimal;

        // Test integer decimals use the integer canonical path
        let decimal_42 = Number::from(Decimal::new(42, 0));
        let decimal_neg42 = Number::from(Decimal::new(-42, 0));
        let decimal_zero = Number::from(Decimal::ZERO);

        // Should hash the same as integer equivalents
        assert_eq!(
            hash_42,
            calculate_hash(&decimal_42),
            "Decimal(42) should use integer canonical path"
        );

        assert_eq!(
            calculate_hash(&neg_42),
            calculate_hash(&decimal_neg42),
            "Decimal(-42) should use integer canonical path"
        );

        assert_eq!(
            hash_zero,
            calculate_hash(&decimal_zero),
            "Decimal(0) should use integer canonical path"
        );
    }
}

#[test]
fn test_hash_float_canonical_path_coverage() {
    // Test the specific (_, false, _, float_value) path in Hash impl
    // This covers non-integer values and large U64s

    // U64 values larger than i64::MAX should use float path
    let large_u64_1 = Number::from((i64::MAX as u64) + 1);
    let large_u64_2 = Number::from(u64::MAX);

    // These should use (false, false, 0, float_value) since they exceed i64::MAX
    let hash_large_1 = calculate_hash(&large_u64_1);
    let hash_large_2 = calculate_hash(&large_u64_2);

    // Should be different from each other and from smaller integers
    assert_ne!(
        hash_large_1, hash_large_2,
        "Different large U64s should hash differently"
    );
    assert_ne!(
        hash_large_1,
        calculate_hash(&Number::from(42u64)),
        "Large U64 should hash differently from small U64"
    );

    // Non-integer F64 values should use float path
    let _float_fractionals = [num!(3.16), num!(-3.16), num!(0.5), num!(-0.5), num!(42.001)];

    // These should all hash differently from their integer counterparts
    assert_ne!(
        calculate_hash(&num!(3.16)),
        calculate_hash(&Number::from(3i64)),
        "Fractional float should hash differently from integer"
    );

    // Special F64 values should use float path
    let _special_floats = [num!(f64::NAN), num!(f64::INFINITY), num!(f64::NEG_INFINITY)];

    // NaN should hash consistently
    let nan1 = num!(f64::NAN);
    let nan2 = num!(f64::NAN);
    assert_eq!(
        calculate_hash(&nan1),
        calculate_hash(&nan2),
        "All NaN values should hash the same via float canonical path"
    );

    // Infinities should hash consistently but differently
    let pos_inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);

    assert_eq!(
        calculate_hash(&pos_inf),
        calculate_hash(&num!(f64::INFINITY)),
        "Positive infinities should hash the same"
    );

    assert_ne!(
        calculate_hash(&pos_inf),
        calculate_hash(&neg_inf),
        "Positive and negative infinity should hash differently via float canonical path"
    );

    #[cfg(feature = "decimal")]
    {
        use rust_decimal::Decimal;

        // Test fractional decimals use the float canonical path
        let decimal_frac = Number::from(Decimal::new(316, 2)); // 3.16
        let float_equiv = num!(3.16);

        // Should hash the same as float equivalent
        assert_eq!(
            calculate_hash(&decimal_frac),
            calculate_hash(&float_equiv),
            "Decimal(3.16) and F64(3.16) should use float canonical path and hash the same"
        );
    }
}

#[test]
fn test_hash_negative_value_handling() {
    // Test that negative values are handled correctly in the is_negative flag

    // Test I64 negative values
    let neg_i32 = Number::from(-1i64);
    let pos_i32 = Number::from(1i64);

    let neg_i32_hash = calculate_hash(&neg_i32);
    let pos_i32_hash = calculate_hash(&pos_i32);

    assert_ne!(
        neg_i32_hash, pos_i32_hash,
        "Negative and positive I64 should hash differently"
    );

    // Test I64 negative values
    let neg_i64 = Number::from(-1i64);
    let pos_i64 = Number::from(1i64);

    let neg_i64_hash = calculate_hash(&neg_i64);
    let pos_i64_hash = calculate_hash(&pos_i64);

    assert_ne!(
        neg_i64_hash, pos_i64_hash,
        "Negative and positive I64 should hash differently"
    );

    // Test that equivalent negative values hash the same
    assert_eq!(
        neg_i32_hash, neg_i64_hash,
        "I64(-1) and I64(-1) should hash the same"
    );
    assert_eq!(
        pos_i32_hash, pos_i64_hash,
        "I64(1) and I64(1) should hash the same"
    );

    // Test F64 negative values
    let neg_f64 = num!(-1.0);
    let pos_f64 = num!(1.0);

    let neg_f64_hash = calculate_hash(&neg_f64);
    let pos_f64_hash = calculate_hash(&pos_f64);

    assert_ne!(
        neg_f64_hash, pos_f64_hash,
        "Negative and positive F64 should hash differently"
    );

    // Integer and float equivalents should hash the same
    assert_eq!(
        neg_i32_hash, neg_f64_hash,
        "I64(-1) and F64(-1.0) should hash the same"
    );
    assert_eq!(
        pos_i32_hash, pos_f64_hash,
        "I64(1) and F64(1.0) should hash the same"
    );
}

#[test]
fn test_large_f64_integer_hash_coverage() {
    // Test the specific case where an F64 is an integer but too large for i128
    // This exercises the (is_negative, true, int_value, _) branch in Hash impl
    let large_positive = num!(1e40);
    let large_negative = num!(-1e40);

    // Verify they are integers
    assert!(large_positive.is_integer());
    assert!(large_negative.is_integer());

    // Verify as_i128() returns None (outside range)
    assert!(large_positive.as_i128().is_none());
    assert!(large_negative.as_i128().is_none());

    // Test that they can be hashed and used in collections
    let mut set = HashSet::new();
    set.insert(large_positive);
    set.insert(large_negative);

    // Verify the values are in the set
    assert!(set.contains(&num!(1e40)));
    assert!(set.contains(&num!(-1e40)));

    // Test equality preservation
    let large_positive_2 = num!(1e40);
    assert_eq!(num!(1e40), large_positive_2);
    assert!(set.contains(&large_positive_2));
}