]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | use core::cmp::{ |
2 | self, | |
3 | Ordering::{self, *}, | |
4 | }; | |
1a4d82fc JJ |
5 | |
6 | #[test] | |
7 | fn test_int_totalord() { | |
85aaf69f SL |
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); | |
1a4d82fc JJ |
13 | } |
14 | ||
60c5eb7d XL |
15 | #[test] |
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 | ||
1a4d82fc JJ |
23 | #[test] |
24 | fn test_mut_int_totalord() { | |
85aaf69f SL |
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); | |
1a4d82fc JJ |
30 | } |
31 | ||
041b39d2 XL |
32 | #[test] |
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 | ||
e1599b0c XL |
42 | #[test] |
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] | |
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 | ||
1a4d82fc JJ |
64 | #[test] |
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] | |
72 | fn test_ordering_order() { | |
73 | assert!(Less < Equal); | |
74 | assert_eq!(Greater.cmp(&Less), Greater); | |
75 | } | |
76 | ||
c30ab7b3 SL |
77 | #[test] |
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] | |
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 | ||
1a4d82fc JJ |
103 | #[test] |
104 | fn test_user_defined_eq() { | |
1a4d82fc JJ |
105 | // Our type. |
106 | struct SketchyNum { | |
dfeec247 | 107 | num: isize, |
1a4d82fc JJ |
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! | |
dfeec247 XL |
119 | assert!(SketchyNum { num: 37 } == SketchyNum { num: 34 }); |
120 | assert!(SketchyNum { num: 25 } != SketchyNum { num: 57 }); | |
1a4d82fc | 121 | } |
1b1a35ee XL |
122 | |
123 | #[test] | |
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 | } | |
fc512014 XL |
135 | |
136 | #[test] | |
137 | fn cmp_default() { | |
138 | // Test default methods in PartialOrd and PartialEq | |
139 | ||
140 | #[derive(Debug)] | |
141 | struct Fool(bool); | |
142 | ||
143 | impl PartialEq for Fool { | |
144 | fn eq(&self, other: &Fool) -> bool { | |
145 | let Fool(this) = *self; | |
146 | let Fool(other) = *other; | |
147 | this != other | |
148 | } | |
149 | } | |
150 | ||
151 | struct Int(isize); | |
152 | ||
153 | impl PartialEq for Int { | |
154 | fn eq(&self, other: &Int) -> bool { | |
155 | let Int(this) = *self; | |
156 | let Int(other) = *other; | |
157 | this == other | |
158 | } | |
159 | } | |
160 | ||
161 | impl PartialOrd for Int { | |
162 | fn partial_cmp(&self, other: &Int) -> Option<Ordering> { | |
163 | let Int(this) = *self; | |
164 | let Int(other) = *other; | |
165 | this.partial_cmp(&other) | |
166 | } | |
167 | } | |
168 | ||
169 | struct RevInt(isize); | |
170 | ||
171 | impl PartialEq for RevInt { | |
172 | fn eq(&self, other: &RevInt) -> bool { | |
173 | let RevInt(this) = *self; | |
174 | let RevInt(other) = *other; | |
175 | this == other | |
176 | } | |
177 | } | |
178 | ||
179 | impl PartialOrd for RevInt { | |
180 | fn partial_cmp(&self, other: &RevInt) -> Option<Ordering> { | |
181 | let RevInt(this) = *self; | |
182 | let RevInt(other) = *other; | |
183 | other.partial_cmp(&this) | |
184 | } | |
185 | } | |
186 | ||
187 | assert!(Int(2) > Int(1)); | |
188 | assert!(Int(2) >= Int(1)); | |
189 | assert!(Int(1) >= Int(1)); | |
190 | assert!(Int(1) < Int(2)); | |
191 | assert!(Int(1) <= Int(2)); | |
192 | assert!(Int(1) <= Int(1)); | |
193 | ||
194 | assert!(RevInt(2) < RevInt(1)); | |
195 | assert!(RevInt(2) <= RevInt(1)); | |
196 | assert!(RevInt(1) <= RevInt(1)); | |
197 | assert!(RevInt(1) > RevInt(2)); | |
198 | assert!(RevInt(1) >= RevInt(2)); | |
199 | assert!(RevInt(1) >= RevInt(1)); | |
200 | ||
201 | assert_eq!(Fool(true), Fool(false)); | |
202 | assert!(Fool(true) != Fool(true)); | |
203 | assert!(Fool(false) != Fool(false)); | |
204 | assert_eq!(Fool(false), Fool(true)); | |
205 | } |