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
use crate::{integer::Integer, rational::Rational};
use core::cmp::Ordering;

macro_rules! impl_partial_eq {
    ($rhs:ty, $func:path) => {
        impl PartialEq<$rhs> for Rational {
            fn eq(&self, other: &$rhs) -> bool {
                $func(&self, other) == Ordering::Equal
            }
        }
    };
    ($rhs:ty, $func:path, into rhs) => {
        impl PartialEq<$rhs> for Rational {
            fn eq(&self, other: &$rhs) -> bool {
                $func(&self, &Rational::from(other)) == Ordering::Equal
            }
        }

        impl PartialEq<Rational> for $rhs {
            fn eq(&self, other: &Rational) -> bool {
                // This doesn't need to be reversed as it simply checks equality
                $func(&other, &Rational::from(self)) == Ordering::Equal
            }
        }

        impl PartialEq<($rhs, $rhs)> for Rational {
            fn eq(&self, other: &($rhs, $rhs)) -> bool {
                $func(&self, &Rational::from(other)) == Ordering::Equal
            }
        }

        impl PartialEq<Rational> for ($rhs, $rhs) {
            fn eq(&self, other: &Rational) -> bool {
                // This doesn't need to be reversed as it simply checks equality
                $func(&other, &Rational::from(self)) == Ordering::Equal
            }
        }
    };
}

impl_partial_eq!(Rational, Rational::compare);
impl_partial_eq!(Integer, Rational::compare, into rhs);
impl_partial_eq!(u8, Rational::compare, into rhs);
impl_partial_eq!(i8, Rational::compare, into rhs);
impl_partial_eq!(u16, Rational::compare, into rhs);
impl_partial_eq!(i16, Rational::compare, into rhs);
impl_partial_eq!(i32, Rational::compare, into rhs);
impl_partial_eq!(u32, Rational::compare, into rhs);
impl_partial_eq!(i64, Rational::compare, into rhs);
impl_partial_eq!(u64, Rational::compare, into rhs);
impl_partial_eq!(i128, Rational::compare, into rhs);
impl_partial_eq!(u128, Rational::compare, into rhs);

impl Eq for Rational {}

macro_rules! impl_partial_ord {
    ($rhs:ty, $func:path) => {
        impl PartialOrd<$rhs> for Rational {
            fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
                Some($func(self, other))
            }
        }
    };
    ($rhs:ty, $func:path, into rhs) => {
        impl PartialOrd<$rhs> for Rational {
            fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
                Some($func(self, &Rational::from(other)))
            }
        }

        impl PartialOrd<Rational> for $rhs {
            fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
                // This implies that:
                // (a cmp b) <=> (b cmp a).reverse()
                // I don't know if thats always true
                Some($func(other, &Rational::from(self)).reverse())
            }
        }

        impl PartialOrd<($rhs, $rhs)> for Rational {
            fn partial_cmp(&self, other: &($rhs, $rhs)) -> Option<Ordering> {
                Some($func(self, &Rational::from(other)))
            }
        }

        impl PartialOrd<Rational> for ($rhs, $rhs) {
            fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
                // This implies that:
                // (a cmp b) <=> (b cmp a).reverse()
                // I don't know if thats always true
                Some($func(other, &Rational::from(self)).reverse())
            }
        }
    };
}

impl_partial_ord!(Rational, Rational::compare);
impl_partial_ord!(Integer, Rational::compare, into rhs);
impl_partial_ord!(u8, Rational::compare, into rhs);
impl_partial_ord!(i8, Rational::compare, into rhs);
impl_partial_ord!(u16, Rational::compare, into rhs);
impl_partial_ord!(i16, Rational::compare, into rhs);
impl_partial_ord!(i32, Rational::compare, into rhs);
impl_partial_ord!(u32, Rational::compare, into rhs);
impl_partial_ord!(i64, Rational::compare, into rhs);
impl_partial_ord!(u64, Rational::compare, into rhs);
impl_partial_ord!(i128, Rational::compare, into rhs);
impl_partial_ord!(u128, Rational::compare, into rhs);

impl Ord for Rational {
    fn cmp(&self, other: &Self) -> Ordering {
        Rational::compare(self, other)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn compare_rationals() {
        let a = Rational::from(50505);
        let b = Rational::from(5050);

        assert_eq!(a.cmp(&b), Ordering::Greater);
        assert_eq!(a.cmp(&a), Ordering::Equal);
        assert_eq!(b.cmp(&a), Ordering::Less);
    }

    #[test]
    fn compare_rationals_with_operands() {
        let a = Rational::from(12345);
        let b = Rational::from(1234);

        assert!(a != b);
        assert!(a > b);
        assert!(!(a < b));
        // assert!((&a - &b) > b);

        assert!(b != a);
        assert!(b < a);
        assert!(!(b > a));
        // assert!((&b - &a) < 0);
    }

    #[test]
    fn compare_big_rationals_with_small() {
        let a = Rational::from_string_repr("1234567890").unwrap();

        assert!(a > 0);
        assert!(a != 987_654_321);
        assert!(a < 9_999_999_990u128);

        assert!(9_999_999_990u128 > a);
        assert!(987_654_321 != a);
        assert!(0 < a);
    }
}