num_bigint_dig/algorithms/
cmp.rs

1use core::cmp::Ordering::{self, Equal, Greater, Less};
2
3use crate::big_digit::BigDigit;
4
5pub fn cmp_slice(a: &[BigDigit], b: &[BigDigit]) -> Ordering {
6    debug_assert!(a.last() != Some(&0));
7    debug_assert!(b.last() != Some(&0));
8
9    let (a_len, b_len) = (a.len(), b.len());
10    if a_len < b_len {
11        return Less;
12    }
13    if a_len > b_len {
14        return Greater;
15    }
16
17    for (&ai, &bi) in a.iter().rev().zip(b.iter().rev()) {
18        if ai < bi {
19            return Less;
20        }
21        if ai > bi {
22            return Greater;
23        }
24    }
25    Equal
26}
27
28#[cfg(test)]
29mod tests {
30    use crate::BigUint;
31
32    use num_traits::Num;
33
34    #[test]
35    fn test_eq() {
36        let a = BigUint::from_str_radix("265252859812191058636308480000000", 10).unwrap();
37        let b = BigUint::from_str_radix("26525285981219105863630848000000", 10).unwrap();
38        assert!(a != b);
39        assert_ne!(a, b);
40
41        let a = BigUint::from_str_radix("138995801145388806366366393471481216294", 10).unwrap();
42        let b = BigUint::from_str_radix("168653801169012228514850424976871974699", 10).unwrap();
43
44        assert!(a != b);
45        assert_ne!(a, b);
46        assert!(&a != &b);
47        assert_ne!(&a, &b);
48    }
49}