• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::cmp::{
2     self,
3     Ordering::{self, *},
4 };
5 
6 #[test]
test_int_totalord()7 fn test_int_totalord() {
8     assert_eq!(5.cmp(&10), Less);
9     assert_eq!(10.cmp(&5), Greater);
10     assert_eq!(5.cmp(&5), Equal);
11     assert_eq!((-5).cmp(&12), Less);
12     assert_eq!(12.cmp(&-5), Greater);
13 }
14 
15 #[test]
test_bool_totalord()16 fn test_bool_totalord() {
17     assert_eq!(true.cmp(&false), Greater);
18     assert_eq!(false.cmp(&true), Less);
19     assert_eq!(true.cmp(&true), Equal);
20     assert_eq!(false.cmp(&false), Equal);
21 }
22 
23 #[test]
test_mut_int_totalord()24 fn test_mut_int_totalord() {
25     assert_eq!((&mut 5).cmp(&&mut 10), Less);
26     assert_eq!((&mut 10).cmp(&&mut 5), Greater);
27     assert_eq!((&mut 5).cmp(&&mut 5), Equal);
28     assert_eq!((&mut -5).cmp(&&mut 12), Less);
29     assert_eq!((&mut 12).cmp(&&mut -5), Greater);
30 }
31 
32 #[test]
test_ord_max_min()33 fn test_ord_max_min() {
34     assert_eq!(1.max(2), 2);
35     assert_eq!(2.max(1), 2);
36     assert_eq!(1.min(2), 1);
37     assert_eq!(2.min(1), 1);
38     assert_eq!(1.max(1), 1);
39     assert_eq!(1.min(1), 1);
40 }
41 
42 #[test]
test_ord_min_max_by()43 fn test_ord_min_max_by() {
44     let f = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
45     assert_eq!(cmp::min_by(1, -1, f), 1);
46     assert_eq!(cmp::min_by(1, -2, f), 1);
47     assert_eq!(cmp::min_by(2, -1, f), -1);
48     assert_eq!(cmp::max_by(1, -1, f), -1);
49     assert_eq!(cmp::max_by(1, -2, f), -2);
50     assert_eq!(cmp::max_by(2, -1, f), 2);
51 }
52 
53 #[test]
test_ord_min_max_by_key()54 fn test_ord_min_max_by_key() {
55     let f = |x: &i32| x.abs();
56     assert_eq!(cmp::min_by_key(1, -1, f), 1);
57     assert_eq!(cmp::min_by_key(1, -2, f), 1);
58     assert_eq!(cmp::min_by_key(2, -1, f), -1);
59     assert_eq!(cmp::max_by_key(1, -1, f), -1);
60     assert_eq!(cmp::max_by_key(1, -2, f), -2);
61     assert_eq!(cmp::max_by_key(2, -1, f), 2);
62 }
63 
64 #[test]
test_ordering_reverse()65 fn test_ordering_reverse() {
66     assert_eq!(Less.reverse(), Greater);
67     assert_eq!(Equal.reverse(), Equal);
68     assert_eq!(Greater.reverse(), Less);
69 }
70 
71 #[test]
test_ordering_order()72 fn test_ordering_order() {
73     assert!(Less < Equal);
74     assert_eq!(Greater.cmp(&Less), Greater);
75 }
76 
77 #[test]
test_ordering_then()78 fn test_ordering_then() {
79     assert_eq!(Equal.then(Less), Less);
80     assert_eq!(Equal.then(Equal), Equal);
81     assert_eq!(Equal.then(Greater), Greater);
82     assert_eq!(Less.then(Less), Less);
83     assert_eq!(Less.then(Equal), Less);
84     assert_eq!(Less.then(Greater), Less);
85     assert_eq!(Greater.then(Less), Greater);
86     assert_eq!(Greater.then(Equal), Greater);
87     assert_eq!(Greater.then(Greater), Greater);
88 }
89 
90 #[test]
test_ordering_then_with()91 fn test_ordering_then_with() {
92     assert_eq!(Equal.then_with(|| Less), Less);
93     assert_eq!(Equal.then_with(|| Equal), Equal);
94     assert_eq!(Equal.then_with(|| Greater), Greater);
95     assert_eq!(Less.then_with(|| Less), Less);
96     assert_eq!(Less.then_with(|| Equal), Less);
97     assert_eq!(Less.then_with(|| Greater), Less);
98     assert_eq!(Greater.then_with(|| Less), Greater);
99     assert_eq!(Greater.then_with(|| Equal), Greater);
100     assert_eq!(Greater.then_with(|| Greater), Greater);
101 }
102 
103 #[test]
test_user_defined_eq()104 fn test_user_defined_eq() {
105     // Our type.
106     struct SketchyNum {
107         num: isize,
108     }
109 
110     // Our implementation of `PartialEq` to support `==` and `!=`.
111     impl PartialEq for SketchyNum {
112         // Our custom eq allows numbers which are near each other to be equal! :D
113         fn eq(&self, other: &SketchyNum) -> bool {
114             (self.num - other.num).abs() < 5
115         }
116     }
117 
118     // Now these binary operators will work when applied!
119     assert!(SketchyNum { num: 37 } == SketchyNum { num: 34 });
120     assert!(SketchyNum { num: 25 } != SketchyNum { num: 57 });
121 }
122 
123 #[test]
ordering_const()124 fn ordering_const() {
125     // test that the methods of `Ordering` are usable in a const context
126 
127     const ORDERING: Ordering = Greater;
128 
129     const REVERSE: Ordering = ORDERING.reverse();
130     assert_eq!(REVERSE, Less);
131 
132     const THEN: Ordering = Equal.then(ORDERING);
133     assert_eq!(THEN, Greater);
134 }
135 
136 #[test]
ordering_structural_eq()137 fn ordering_structural_eq() {
138     // test that consts of type `Ordering` are usable in patterns
139 
140     const ORDERING: Ordering = Greater;
141 
142     const REVERSE: Ordering = ORDERING.reverse();
143     match Ordering::Less {
144         REVERSE => {}
145         _ => unreachable!(),
146     };
147 }
148 
149 #[test]
cmp_default()150 fn cmp_default() {
151     // Test default methods in PartialOrd and PartialEq
152 
153     #[derive(Debug)]
154     struct Fool(bool);
155 
156     impl PartialEq for Fool {
157         fn eq(&self, other: &Fool) -> bool {
158             let Fool(this) = *self;
159             let Fool(other) = *other;
160             this != other
161         }
162     }
163 
164     struct Int(isize);
165 
166     impl PartialEq for Int {
167         fn eq(&self, other: &Int) -> bool {
168             let Int(this) = *self;
169             let Int(other) = *other;
170             this == other
171         }
172     }
173 
174     impl PartialOrd for Int {
175         fn partial_cmp(&self, other: &Int) -> Option<Ordering> {
176             let Int(this) = *self;
177             let Int(other) = *other;
178             this.partial_cmp(&other)
179         }
180     }
181 
182     struct RevInt(isize);
183 
184     impl PartialEq for RevInt {
185         fn eq(&self, other: &RevInt) -> bool {
186             let RevInt(this) = *self;
187             let RevInt(other) = *other;
188             this == other
189         }
190     }
191 
192     impl PartialOrd for RevInt {
193         fn partial_cmp(&self, other: &RevInt) -> Option<Ordering> {
194             let RevInt(this) = *self;
195             let RevInt(other) = *other;
196             other.partial_cmp(&this)
197         }
198     }
199 
200     assert!(Int(2) > Int(1));
201     assert!(Int(2) >= Int(1));
202     assert!(Int(1) >= Int(1));
203     assert!(Int(1) < Int(2));
204     assert!(Int(1) <= Int(2));
205     assert!(Int(1) <= Int(1));
206 
207     assert!(RevInt(2) < RevInt(1));
208     assert!(RevInt(2) <= RevInt(1));
209     assert!(RevInt(1) <= RevInt(1));
210     assert!(RevInt(1) > RevInt(2));
211     assert!(RevInt(1) >= RevInt(2));
212     assert!(RevInt(1) >= RevInt(1));
213 
214     assert_eq!(Fool(true), Fool(false));
215     assert!(Fool(true) != Fool(true));
216     assert!(Fool(false) != Fool(false));
217     assert_eq!(Fool(false), Fool(true));
218 }
219 
220 /* FIXME(#110395)
221 mod const_cmp {
222     use super::*;
223 
224     struct S(i32);
225 
226     impl PartialEq for S {
227         fn eq(&self, other: &Self) -> bool {
228             self.0 == other.0
229         }
230     }
231 
232     impl PartialOrd for S {
233         fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
234             let ret = match (self.0, other.0) {
235                 (a, b) if a > b => Ordering::Greater,
236                 (a, b) if a < b => Ordering::Less,
237                 _ => Ordering::Equal,
238             };
239 
240             Some(ret)
241         }
242     }
243 
244     const _: () = assert!(S(1) == S(1));
245     const _: () = assert!(S(0) != S(1));
246 
247     const _: () = assert!(S(1) <= S(1));
248     const _: () = assert!(S(1) >= S(1));
249     const _: () = assert!(S(0) < S(1));
250     const _: () = assert!(S(1) > S(0));
251 }
252 */
253