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
//! Cross-type consistency property tests for the Number type.
//!
//! Tests consistency properties across different numeric types:
//! - Equality consistency: same values in different types are equal
//! - Ordering consistency: same values in different types have consistent
//!   ordering
//! - Hash consistency: same values in different types hash identically
//! - Conversion consistency: conversions preserve value relationships
//! - Arithmetic consistency: operations produce consistent results across types

use uninum::{Number, num};

#[test]
fn test_cross_type_equality_consistency() {
    // Test that equivalent values across different types are equal
    let equivalent_groups = vec![
        // Integer value 0 in different types
        vec![
            Number::from(0_u64),
            Number::from(0_i64),
            Number::from(0_u64),
            Number::from(0_i64),
            num!(0.0),
            num!(-0.0),
            num!(0.0),
            num!(-0.0),
        ],
        // Integer value 1 in different types
        vec![
            Number::from(1_u64),
            Number::from(1_i64),
            Number::from(1_u64),
            Number::from(1_i64),
            num!(1.0),
            num!(1.0),
        ],
        // Integer value 42 in different types
        vec![
            Number::from(42_u64),
            Number::from(42_i64),
            Number::from(42_u64),
            Number::from(42_i64),
            num!(42.0),
            num!(42.0),
        ],
        // Integer value 255 in different types
        vec![
            Number::from(255_u64),
            Number::from(255_u64),
            Number::from(255_i64),
            Number::from(255_i64),
            num!(255.0),
            num!(255.0),
        ],
        // Negative values
        vec![
            Number::from(-1_i64),
            Number::from(-1_i64),
            num!(-1.0),
            num!(-1.0),
        ],
    ];

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

    for group in equivalent_groups {
        // All values in a group should be equal to each other
        for (i, a) in group.iter().enumerate() {
            for (j, b) in group.iter().enumerate() {
                assert_eq!(
                    a, b,
                    "Cross-type equality failed at group item {i} and {j}: {a:?} != {b:?}"
                );
            }
        }
    }
}

#[test]
fn test_cross_type_ordering_consistency() {
    // Test that ordering is consistent across different types
    let test_cases = vec![
        // (smaller, larger) pairs across different types
        (Number::from(10_u64), Number::from(20_u64)),
        (Number::from(-10_i64), Number::from(10_u64)),
        (Number::from(100_u64), num!(200.0)),
        (Number::from(-50_i64), num!(0.0)),
        (num!(3.12), num!(3.15)),
        (Number::from(1000_u64), Number::from(2000_i64)),
        // Edge cases
        (
            Number::from(i64::from(i32::MIN)),
            Number::from(i64::from(i32::MAX)),
        ),
        (Number::from(0_u64), Number::from(u64::from(u32::MAX))),
        (Number::from(i64::MIN), Number::from(i64::MAX)),
        (Number::from(0_u64), Number::from(i64::MAX)),
    ];

    for (smaller, larger) in test_cases {
        assert!(
            smaller < larger,
            "Cross-type ordering failed: {smaller:?} should be < {larger:?}"
        );
        assert!(
            smaller <= larger,
            "Cross-type ordering failed: {smaller:?} should be <= {larger:?}"
        );
        assert!(
            larger > smaller,
            "Cross-type ordering failed: {larger:?} should be > {smaller:?}"
        );
        assert!(
            larger >= smaller,
            "Cross-type ordering failed: {larger:?} should be >= {smaller:?}"
        );
        assert_ne!(
            smaller, larger,
            "Cross-type ordering failed: {smaller:?} should not equal {larger:?}"
        );
    }
}

#[test]
fn test_cross_type_hash_consistency() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

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

    // Test that equivalent values across different types hash identically
    let equivalent_groups = vec![
        // Integer value 42 in different types
        vec![
            Number::from(42_u64),
            Number::from(42_i64),
            Number::from(42_u64),
            Number::from(42_i64),
            num!(42.0),
            num!(42.0),
        ],
        // Integer value 0 in different types
        vec![
            Number::from(0_u64),
            Number::from(0_i64),
            Number::from(0_u64),
            Number::from(0_i64),
            num!(0.0),
            num!(0.0),
        ],
    ];

    for group in equivalent_groups {
        let mut expected_hash = None;
        for num in &group {
            let hash = calculate_hash(num);
            if let Some(expected) = expected_hash {
                assert_eq!(
                    hash, expected,
                    "Cross-type hash inconsistency: {num:?} hashes to {hash} but expected \
                     {expected}"
                );
            } else {
                expected_hash = Some(hash);
            }
        }
    }
}

#[test]
fn test_cross_type_special_values() {
    // Test special float values in F64
    let nan_f64 = num!(f64::NAN);
    let nan_f64_2 = num!(f64::NAN);
    let pos_inf_f64 = num!(f64::INFINITY);
    let pos_inf_f64_2 = num!(f64::INFINITY);
    let neg_inf_f64 = num!(f64::NEG_INFINITY);
    let neg_inf_f64_2 = num!(f64::NEG_INFINITY);

    // Special value equality
    assert_eq!(nan_f64, nan_f64_2, "NaN should be equal to itself");
    assert_eq!(
        pos_inf_f64, pos_inf_f64_2,
        "Positive infinity should be equal to itself"
    );
    assert_eq!(
        neg_inf_f64, neg_inf_f64_2,
        "Negative infinity should be equal to itself"
    );

    // Special value ordering
    assert!(
        neg_inf_f64 < pos_inf_f64,
        "Negative infinity should be less than positive infinity"
    );
    assert!(
        pos_inf_f64 < nan_f64,
        "Positive infinity should be less than NaN"
    );
    assert!(
        neg_inf_f64 < nan_f64,
        "Negative infinity should be less than NaN"
    );
}

#[test]
fn test_cross_type_boundary_values() {
    // Test boundary values across different types
    let boundary_tests = vec![
        // Test that u32::MAX fits in larger types
        (
            Number::from(u64::from(u32::MAX)),
            Number::from(u32::MAX as u64),
        ),
        (
            Number::from(u64::from(u32::MAX)),
            Number::from(u32::MAX as i64),
        ),
        (Number::from(u64::from(u32::MAX)), num!(u32::MAX as f64)),
        // Test that i32::MAX fits in larger types
        (
            Number::from(i64::from(i32::MAX)),
            Number::from(i32::MAX as i64),
        ),
        (Number::from(i64::from(i32::MAX)), num!(i32::MAX as f64)),
        // Test that i32::MIN fits in larger types
        (
            Number::from(i64::from(i32::MIN)),
            Number::from(i32::MIN as i64),
        ),
        (Number::from(i64::from(i32::MIN)), num!(i32::MIN as f64)),
    ];

    for (smaller_type, larger_type) in boundary_tests {
        assert_eq!(
            smaller_type, larger_type,
            "Boundary value cross-type equality failed: {smaller_type:?} != {larger_type:?}"
        );
    }
}

#[test]
fn test_cross_type_precision_limits() {
    // Test behavior at precision limits where some types can't represent exact
    // values

    // Test large integers that may lose precision in floats
    let large_int = Number::from(9007199254740992_u64); // 2^53, exactly representable in f64
    let large_float = num!(9007199254740992.0);
    assert_eq!(
        large_int, large_float,
        "Large integer should equal its exact float representation"
    );

    let larger_int = Number::from(9007199254740993_u64); // 2^53 + 1, may not be exactly representable in f64
    let larger_float = num!(9007199254740993.0);

    // This test depends on the exact floating-point representation
    // If they're not equal, that's expected due to precision limits
    if larger_int != larger_float {
        // But they should still be orderable consistently
        assert!(
            larger_int != larger_float,
            "Different precision values should not be equal"
        );
    }
}

#[test]
fn test_cross_type_arithmetic_consistency() {
    // Test that arithmetic operations produce consistent results across types
    let a_u32 = Number::from(10_u64);
    let a_i32 = Number::from(10_i64);
    let a_f64 = num!(10.0);

    let b_u32 = Number::from(5_u64);
    let b_i32 = Number::from(5_i64);
    let b_f64 = num!(5.0);

    // This test assumes arithmetic operations exist and work consistently
    // If arithmetic is not implemented, these assertions would need to be adjusted

    // Test that equal values remain equal after operations
    assert_eq!(a_u32, a_i32, "Cross-type operands should be equal");
    assert_eq!(a_u32, a_f64, "Cross-type operands should be equal");
    assert_eq!(b_u32, b_i32, "Cross-type operands should be equal");
    assert_eq!(b_u32, b_f64, "Cross-type operands should be equal");
}

#[test]
fn test_cross_type_collection_behavior() {
    use std::collections::{HashMap, HashSet};

    // Test that cross-type equivalent values behave correctly in collections
    let mut set = HashSet::new();

    // Insert equivalent values of different types
    set.insert(Number::from(42_u64));
    set.insert(Number::from(42_i64));
    set.insert(num!(42.0));
    set.insert(Number::from(42_u64));

    // Should have only one entry since all values are equivalent
    assert_eq!(
        set.len(),
        1,
        "HashSet should contain only one entry for equivalent cross-type values"
    );

    // Test HashMap behavior
    let mut map = HashMap::new();
    map.insert(Number::from(100_u64), "u32");
    map.insert(Number::from(100_i64), "i32");
    map.insert(num!(100.0), "f64");

    // Should have only one entry, with the last value
    assert_eq!(
        map.len(),
        1,
        "HashMap should contain only one entry for equivalent cross-type keys"
    );
    assert_eq!(
        map.get(&Number::from(100_u64)),
        Some(&"f64"),
        "HashMap should find equivalent cross-type keys"
    );
}

#[test]
fn test_cross_type_mixed_operations() {
    // Test mixed operations across different types
    let values = [
        Number::from(5_u64),
        Number::from(5_i64),
        Number::from(5_u64),
        Number::from(5_i64),
        num!(5.0),
        num!(5.0),
    ];

    // All values should be equal to each other
    for i in 0..values.len() {
        for j in 0..values.len() {
            assert_eq!(
                values[i], values[j],
                "Cross-type mixed operation failed: {:?} != {:?}",
                values[i], values[j]
            );
        }
    }
}

#[test]
fn test_cross_type_edge_case_consistency() {
    // Test edge cases that might cause inconsistencies

    // Test zero values
    let zero_values = vec![
        Number::from(0_u64),
        Number::from(0_i64),
        Number::from(0_u64),
        Number::from(0_i64),
        num!(0.0),
        num!(-0.0),
        num!(0.0),
        num!(-0.0),
    ];

    for a in &zero_values {
        for b in &zero_values {
            assert_eq!(a, b, "All zero values should be equal: {a:?} != {b:?}");
        }
    }

    // Test maximum values that fit in all types
    let max_i8_values = vec![
        Number::from(i64::from(i8::MAX as i32)),
        Number::from(i8::MAX as i64),
        Number::from(u64::from(i8::MAX as u32)),
        Number::from(i8::MAX as u64),
        num!(i8::MAX as f64),
        num!(i8::MAX as f64),
    ];

    for a in &max_i8_values {
        for b in &max_i8_values {
            assert_eq!(a, b, "All i8::MAX values should be equal: {a:?} != {b:?}");
        }
    }
}

#[test]
fn test_cross_type_precision_differences() {
    // F64 precision considerations
    let f64_pi = num!(3.16);
    let f64_pi_2 = num!(3.16);
    assert_eq!(
        f64_pi, f64_pi_2,
        "F64 values with same literal should be equal"
    );

    // Test approximately equal values
    assert!(f64_pi.approx_eq(&f64_pi_2, 1e-6, 0.0));

    // Use appropriate float constructors
    let a = num!(3.16);
    let b = num!(3.16f64);
    assert!(a.approx_eq(&b, 1e-6, 0.0));
}

#[test]
fn test_cross_type_large_integer_precision_loss() {
    // Large integer that can't be precisely represented in f32
    let large_int = Number::from(16777217_u64); // 2^24 + 1
    let as_f64 = num!(16777217.0f64);
    let as_f64_2 = num!(16777217.0f64);

    // F64 maintains precision
    assert_eq!(
        large_int, as_f64,
        "Large integer should equal its precise F64 representation"
    );
    // F64 values should be equal
    assert_eq!(as_f64, as_f64_2, "F64 values should be equal");
}