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
//! Comprehensive Rounding Function Tests
//!
//! This module contains well-organized tests for all rounding strategies:
//! - Basic integer and float rounding (0 decimal places)
//! - Decimal precision rounding (non-zero decimal places)
//! - Special values (NaN, infinity, zero)
//! - Type preservation and consistency
//! - All 7 rounding strategies with comprehensive coverage

use uninum::{Number, RoundingStrategy, num};

// =============================================================================
// Basic Strategy Tests - 0 Decimal Places
// =============================================================================

#[test]
fn test_midpoint_nearest_even_basic() {
    // Integer types should return unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::MidpointNearestEven)
    );

    // True midpoints (x.5) - round to even
    assert_eq!(
        num!(0.0f64),
        num!(0.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // 0.5 -> 0 (even)
    assert_eq!(
        num!(2.0f64),
        num!(1.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // 1.5 -> 2 (even)
    assert_eq!(
        num!(2.0f64),
        num!(2.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // 2.5 -> 2 (even)
    assert_eq!(
        num!(4.0f64),
        num!(3.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // 3.5 -> 4 (even)

    // Negative midpoints
    assert_eq!(
        num!(-0.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // -0.5 -> 0 (even)
    assert_eq!(
        num!(-2.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // -1.5 -> -2 (even)
    assert_eq!(
        num!(-2.0f64),
        num!(-2.5f64).round(0, RoundingStrategy::MidpointNearestEven)
    ); // -2.5 -> -2 (even)

    // Non-midpoints
    assert_eq!(
        num!(3.0f64),
        num!(3.16f64).round(0, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        num!(4.0f64),
        num!(3.86f64).round(0, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        num!(-3.0f64),
        num!(-3.16f64).round(0, RoundingStrategy::MidpointNearestEven)
    );
}

#[test]
fn test_midpoint_away_from_zero_basic() {
    // Integer types should return unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::MidpointAwayFromZero)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::MidpointAwayFromZero)
    );

    // True midpoints (x.5) - round away from zero
    assert_eq!(
        num!(1.0f64),
        num!(0.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 0.5 -> 1
    assert_eq!(
        num!(2.0f64),
        num!(1.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.5 -> 2
    assert_eq!(
        num!(3.0f64),
        num!(2.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 2.5 -> 3
    assert_eq!(
        num!(4.0f64),
        num!(3.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 3.5 -> 4

    // Negative midpoints - away from zero means toward negative infinity
    assert_eq!(
        num!(-1.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // -0.5 -> -1
    assert_eq!(
        num!(-2.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // -1.5 -> -2
    assert_eq!(
        num!(-3.0f64),
        num!(-2.5f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // -2.5 -> -3

    // Non-midpoints
    assert_eq!(
        num!(3.0f64),
        num!(3.2f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    );
    assert_eq!(
        num!(4.0f64),
        num!(3.7f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    );
    assert_eq!(
        num!(-3.0f64),
        num!(-3.2f64).round(0, RoundingStrategy::MidpointAwayFromZero)
    );
}

#[test]
fn test_midpoint_toward_zero_basic() {
    // True midpoints (x.5) - round toward zero
    assert_eq!(
        num!(0.0f64),
        num!(0.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // 0.5 -> 0
    assert_eq!(
        num!(1.0f64),
        num!(1.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // 1.5 -> 1
    assert_eq!(
        num!(2.0f64),
        num!(2.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // 2.5 -> 2

    // Negative midpoints - toward zero means toward positive
    assert_eq!(
        num!(-0.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // -0.5 -> 0
    assert_eq!(
        num!(-1.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // -1.5 -> -1
    assert_eq!(
        num!(-2.0f64),
        num!(-2.5f64).round(0, RoundingStrategy::MidpointTowardZero)
    ); // -2.5 -> -2

    // Non-midpoints round normally (not affected by TowardZero rule)
    assert_eq!(
        num!(4.0f64),
        num!(3.7f64).round(0, RoundingStrategy::MidpointTowardZero)
    );
    assert_eq!(
        num!(-4.0f64),
        num!(-3.7f64).round(0, RoundingStrategy::MidpointTowardZero)
    );
}

#[test]
fn test_to_zero_basic() {
    // Truncation toward zero
    assert_eq!(
        num!(3.0f64),
        num!(3.16f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(3.0f64),
        num!(3.99f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(-3.0f64),
        num!(-3.16f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(-3.0f64),
        num!(-3.99f64).round(0, RoundingStrategy::ToZero)
    );

    // Midpoints truncate toward zero
    assert_eq!(
        num!(0.0f64),
        num!(0.5f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(1.0f64),
        num!(1.5f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(-0.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        num!(-1.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::ToZero)
    );

    // Integer types unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::ToZero)
    );
}

#[test]
fn test_away_from_zero_basic() {
    // Always round away from zero
    assert_eq!(
        num!(4.0f64),
        num!(3.01f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(4.0f64),
        num!(3.99f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(-4.0f64),
        num!(-3.01f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(-4.0f64),
        num!(-3.99f64).round(0, RoundingStrategy::AwayFromZero)
    );

    // Midpoints round away from zero
    assert_eq!(
        num!(1.0f64),
        num!(0.5f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(2.0f64),
        num!(1.5f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(-1.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        num!(-2.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::AwayFromZero)
    );

    // Integer types unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::AwayFromZero)
    );
}

#[test]
fn test_floor_basic() {
    // Floor always rounds toward negative infinity
    assert_eq!(
        num!(3.0f64),
        num!(3.16f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(3.0f64),
        num!(3.99f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(-4.0f64),
        num!(-3.16f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(-4.0f64),
        num!(-3.01f64).round(0, RoundingStrategy::Floor)
    );

    // Midpoints floor toward negative infinity
    assert_eq!(num!(0.0f64), num!(0.5f64).round(0, RoundingStrategy::Floor));
    assert_eq!(num!(1.0f64), num!(1.5f64).round(0, RoundingStrategy::Floor));
    assert_eq!(
        num!(-1.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(-2.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::Floor)
    );

    // Integer types unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::Floor)
    );
}

#[test]
fn test_ceil_basic() {
    // Ceil always rounds toward positive infinity
    assert_eq!(num!(4.0f64), num!(3.01f64).round(0, RoundingStrategy::Ceil));
    assert_eq!(num!(4.0f64), num!(3.99f64).round(0, RoundingStrategy::Ceil));
    assert_eq!(
        num!(-3.0f64),
        num!(-3.99f64).round(0, RoundingStrategy::Ceil)
    );
    assert_eq!(
        num!(-3.0f64),
        num!(-3.01f64).round(0, RoundingStrategy::Ceil)
    );

    // Midpoints ceil toward positive infinity
    assert_eq!(num!(1.0f64), num!(0.5f64).round(0, RoundingStrategy::Ceil));
    assert_eq!(num!(2.0f64), num!(1.5f64).round(0, RoundingStrategy::Ceil));
    assert_eq!(
        num!(-0.0f64),
        num!(-0.5f64).round(0, RoundingStrategy::Ceil)
    );
    assert_eq!(
        num!(-1.0f64),
        num!(-1.5f64).round(0, RoundingStrategy::Ceil)
    );

    // Integer types unchanged
    assert_eq!(
        Number::from(42_u64),
        Number::from(42_u64).round(0, RoundingStrategy::Ceil)
    );
    assert_eq!(
        Number::from(-42_i64),
        Number::from(-42_i64).round(0, RoundingStrategy::Ceil)
    );
}

// =============================================================================
// Decimal Precision Tests - Non-Zero Decimal Places
// =============================================================================

#[test]
fn test_decimal_precision_midpoint_nearest_even() {
    // Test with 2 decimal places - banker's rounding
    assert_eq!(
        num!(1.12f64),
        num!(1.125f64).round(2, RoundingStrategy::MidpointNearestEven)
    ); // 1.125 -> 1.12 (even)
    assert_eq!(
        num!(1.14f64),
        num!(1.135f64).round(2, RoundingStrategy::MidpointNearestEven)
    ); // 1.135 -> 1.14 (even)
    assert_eq!(
        num!(1.12f64),
        num!(1.115f64).round(2, RoundingStrategy::MidpointNearestEven)
    ); // 1.115 -> 1.12 (even)

    // Test with 1 decimal place
    assert_eq!(
        num!(1.2f64),
        num!(1.25f64).round(1, RoundingStrategy::MidpointNearestEven)
    ); // 1.25 -> 1.2 (even)
    assert_eq!(
        num!(1.4f64),
        num!(1.35f64).round(1, RoundingStrategy::MidpointNearestEven)
    ); // 1.35 -> 1.4 (even)

    // Negative values
    assert_eq!(
        num!(-1.12f64),
        num!(-1.125f64).round(2, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        num!(-1.14f64),
        num!(-1.135f64).round(2, RoundingStrategy::MidpointNearestEven)
    );
}

#[test]
fn test_decimal_precision_midpoint_away_from_zero() {
    // Test with 2 decimal places - round away from zero
    assert_eq!(
        num!(1.13f64),
        num!(1.125f64).round(2, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.125 -> 1.13
    assert_eq!(
        num!(1.14f64),
        num!(1.135f64).round(2, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.135 -> 1.14

    // Test with 1 decimal place
    assert_eq!(
        num!(1.3f64),
        num!(1.25f64).round(1, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.25 -> 1.3
    assert_eq!(
        num!(1.4f64),
        num!(1.35f64).round(1, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.35 -> 1.4

    // Negative values - away from zero means more negative
    assert_eq!(
        num!(-1.13f64),
        num!(-1.125f64).round(2, RoundingStrategy::MidpointAwayFromZero)
    );
    assert_eq!(
        num!(-1.14f64),
        num!(-1.135f64).round(2, RoundingStrategy::MidpointAwayFromZero)
    );
}

#[test]
fn test_decimal_precision_other_strategies() {
    let test_val = num!(1.125f64);

    // Test all strategies with 2 decimal places
    assert_eq!(
        num!(1.12f64),
        test_val.round(2, RoundingStrategy::MidpointTowardZero)
    );
    assert_eq!(num!(1.12f64), test_val.round(2, RoundingStrategy::ToZero));
    assert_eq!(
        num!(1.13f64),
        test_val.round(2, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(num!(1.12f64), test_val.round(2, RoundingStrategy::Floor));
    assert_eq!(num!(1.13f64), test_val.round(2, RoundingStrategy::Ceil));
}

// =============================================================================
// Special Values Tests
// =============================================================================

#[test]
fn test_special_float_values() {
    // Test all strategies with special float values
    let strategies = [
        RoundingStrategy::MidpointNearestEven,
        RoundingStrategy::MidpointAwayFromZero,
        RoundingStrategy::MidpointTowardZero,
        RoundingStrategy::ToZero,
        RoundingStrategy::AwayFromZero,
        RoundingStrategy::Floor,
        RoundingStrategy::Ceil,
    ];

    for strategy in strategies {
        // NaN should remain NaN
        assert!(num!(f64::NAN).round(0, strategy).is_nan());
        assert!(num!(f64::NAN).round(2, strategy).is_nan());

        // Infinity should remain infinity
        assert_eq!(num!(f64::INFINITY), num!(f64::INFINITY).round(0, strategy));
        assert_eq!(
            num!(f64::NEG_INFINITY),
            num!(f64::NEG_INFINITY).round(0, strategy)
        );
        assert_eq!(num!(f64::INFINITY), num!(f64::INFINITY).round(2, strategy));
        assert_eq!(
            num!(f64::NEG_INFINITY),
            num!(f64::NEG_INFINITY).round(2, strategy)
        );

        // Zero should remain zero
        assert_eq!(num!(0.0f64), num!(0.0f64).round(0, strategy));
        assert_eq!(num!(-0.0f64), num!(-0.0f64).round(0, strategy));
        assert_eq!(num!(0.0f64), num!(0.0f64).round(2, strategy));
    }
}

#[test]
fn test_type_preservation() {
    // Test that integer types are preserved for 0 decimal places
    let u32_val = Number::from(42_u64);
    let i32_val = Number::from(-42_i64);
    let u64_val = Number::from(1000_u64);
    let i64_val = Number::from(-1000_i64);

    let strategies = [
        RoundingStrategy::MidpointNearestEven,
        RoundingStrategy::MidpointAwayFromZero,
        RoundingStrategy::MidpointTowardZero,
        RoundingStrategy::ToZero,
        RoundingStrategy::AwayFromZero,
        RoundingStrategy::Floor,
        RoundingStrategy::Ceil,
    ];

    for strategy in strategies {
        assert_eq!(u32_val, u32_val.round(0, strategy));
        assert_eq!(i32_val, i32_val.round(0, strategy));
        assert_eq!(u64_val, u64_val.round(0, strategy));
        assert_eq!(i64_val, i64_val.round(0, strategy));
    }

    // Test that F64 type is preserved
    let f64_val = num!(3.16f64);
    for strategy in strategies {
        if f64_val.round(0, strategy).try_get_f64().is_some() {
            // Type preserved
        } else {
            panic!("F64 type not preserved for strategy {strategy:?}");
        }
    }
}

#[test]
fn test_integer_rounding_with_fractional_places_promotes_to_f64() {
    let unsigned = Number::from(42_u64);
    let rounded_unsigned = unsigned.round(2, RoundingStrategy::MidpointNearestEven);
    assert!(rounded_unsigned.try_get_f64().is_some());
    assert_eq!(rounded_unsigned.try_get_f64().unwrap(), 42.0);

    let signed = Number::from(-7_i64);
    let rounded_signed = signed.round(3, RoundingStrategy::Floor);
    assert!(rounded_signed.try_get_f64().is_some());
    assert_eq!(rounded_signed.try_get_f64().unwrap(), -7.0);
}

// =============================================================================
// Decimal Type Tests (feature-gated)
// =============================================================================

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

    // Test non-midpoint value 1.25 (rust_decimal treats this as closer to 1 than 2)
    let decimal_125 = Number::from(Decimal::new(125, 2)); // 1.25
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_125.round(0, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_125.round(0, RoundingStrategy::MidpointAwayFromZero)
    );
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_125.round(0, RoundingStrategy::MidpointTowardZero)
    );
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_125.round(0, RoundingStrategy::ToZero)
    );
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_125.round(0, RoundingStrategy::AwayFromZero)
    );
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_125.round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_125.round(0, RoundingStrategy::Ceil)
    );

    // Test true midpoint value 1.5
    let decimal_15 = Number::from(Decimal::new(15, 1)); // 1.5
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_15.round(0, RoundingStrategy::MidpointNearestEven)
    ); // 1.5 -> 2 (even)
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_15.round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.5 -> 2
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_15.round(0, RoundingStrategy::MidpointTowardZero)
    ); // 1.5 -> 1
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_15.round(0, RoundingStrategy::ToZero)
    ); // 1.5 -> 1
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_15.round(0, RoundingStrategy::AwayFromZero)
    ); // 1.5 -> 2
    assert_eq!(
        Number::from(Decimal::new(1, 0)),
        decimal_15.round(0, RoundingStrategy::Floor)
    ); // 1.5 -> 1
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_15.round(0, RoundingStrategy::Ceil)
    ); // 1.5 -> 2

    // Test true midpoint value 2.5 (should round to 2 for MidpointNearestEven)
    let decimal_25 = Number::from(Decimal::new(25, 1)); // 2.5
    assert_eq!(
        Number::from(Decimal::new(2, 0)),
        decimal_25.round(0, RoundingStrategy::MidpointNearestEven)
    ); // 2.5 -> 2 (even)
    assert_eq!(
        Number::from(Decimal::new(3, 0)),
        decimal_25.round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // 2.5 -> 3

    // Test negative values
    let neg_decimal_15 = Number::from(Decimal::new(-15, 1)); // -1.5
    assert_eq!(
        Number::from(Decimal::new(-2, 0)),
        neg_decimal_15.round(0, RoundingStrategy::MidpointNearestEven)
    ); // -1.5 -> -2 (even)
    assert_eq!(
        Number::from(Decimal::new(-2, 0)),
        neg_decimal_15.round(0, RoundingStrategy::MidpointAwayFromZero)
    ); // -1.5 -> -2 (away from zero)
    assert_eq!(
        Number::from(Decimal::new(-1, 0)),
        neg_decimal_15.round(0, RoundingStrategy::MidpointTowardZero)
    ); // -1.5 -> -1 (toward zero)
    assert_eq!(
        Number::from(Decimal::new(-2, 0)),
        neg_decimal_15.round(0, RoundingStrategy::Floor)
    ); // -1.5 -> -2 (floor)
    assert_eq!(
        Number::from(Decimal::new(-1, 0)),
        neg_decimal_15.round(0, RoundingStrategy::Ceil)
    ); // -1.5 -> -1 (ceil)

    // Test decimal precision
    let decimal_1125 = Number::from(Decimal::new(1125, 3)); // 1.125
    assert_eq!(
        Number::from(Decimal::new(112, 2)),
        decimal_1125.round(2, RoundingStrategy::MidpointNearestEven)
    ); // 1.125 -> 1.12 (even)
    assert_eq!(
        Number::from(Decimal::new(113, 2)),
        decimal_1125.round(2, RoundingStrategy::MidpointAwayFromZero)
    ); // 1.125 -> 1.13
}

// =============================================================================
// Consistency and Edge Case Tests
// =============================================================================

#[test]
fn test_consistency_across_types() {
    // Test that equivalent values across different types produce consistent results
    let f64_val = num!(3.5f64);
    let strategies = [
        RoundingStrategy::MidpointNearestEven,
        RoundingStrategy::MidpointAwayFromZero,
        RoundingStrategy::Floor,
        RoundingStrategy::Ceil,
    ];

    for strategy in strategies {
        let f64_result = f64_val.round(0, strategy);

        // Extract the numeric value for comparison
        if let Some(f64_wrapper) = f64_result.try_get_f64() {
            let val = f64_wrapper;
            // 3.5 should round to 4 for all strategies except floor
            match strategy {
                RoundingStrategy::Floor => assert_eq!(val, 3.0),
                _ => assert_eq!(val, 4.0),
            }
        }
    }
}

#[test]
fn test_boundary_cases() {
    // Test values very close to midpoints
    assert_eq!(
        num!(1.0f64),
        num!(1.4999999f64).round(0, RoundingStrategy::MidpointNearestEven)
    );
    assert_eq!(
        num!(2.0f64),
        num!(1.5000001f64).round(0, RoundingStrategy::MidpointNearestEven)
    );

    // Test very small values
    assert_eq!(
        num!(0.0f64),
        num!(0.0001f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(1.0f64),
        num!(0.0001f64).round(0, RoundingStrategy::Ceil)
    );

    // Test very large values
    assert_eq!(
        num!(1000000.0f64),
        num!(1000000.1f64).round(0, RoundingStrategy::Floor)
    );
    assert_eq!(
        num!(1000001.0f64),
        num!(1000000.1f64).round(0, RoundingStrategy::Ceil)
    );
}

#[test]
fn test_zero_decimal_places_edge_cases() {
    // Ensure 0 decimal places works correctly for all strategies
    let test_values = [
        0.1, 0.5, 0.9, 1.1, 1.5, 1.9, -0.1, -0.5, -0.9, -1.1, -1.5, -1.9,
    ];

    for val in test_values {
        let num_val = num!(val);

        // All strategies should return valid results for 0 decimal places
        let _ = num_val.round(0, RoundingStrategy::MidpointNearestEven);
        let _ = num_val.round(0, RoundingStrategy::MidpointAwayFromZero);
        let _ = num_val.round(0, RoundingStrategy::MidpointTowardZero);
        let _ = num_val.round(0, RoundingStrategy::ToZero);
        let _ = num_val.round(0, RoundingStrategy::AwayFromZero);
        let _ = num_val.round(0, RoundingStrategy::Floor);
        let _ = num_val.round(0, RoundingStrategy::Ceil);
    }
}