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);
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);
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);
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);
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);
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);
65 fn test_ordering_reverse() {
66 assert_eq!(Less.reverse(), Greater);
67 assert_eq!(Equal.reverse(), Equal);
68 assert_eq!(Greater.reverse(), Less);
72 fn test_ordering_order() {
73 assert!(Less < Equal);
74 assert_eq!(Greater.cmp(&Less), Greater);
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);
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);
104 fn test_user_defined_eq() {
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
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 });
124 fn ordering_const() {
125 // test that the methods of `Ordering` are usable in a const context
127 const ORDERING: Ordering = Greater;
129 const REVERSE: Ordering = ORDERING.reverse();
130 assert_eq!(REVERSE, Less);
132 const THEN: Ordering = Equal.then(ORDERING);
133 assert_eq!(THEN, Greater);
137 fn ordering_structural_eq() {
138 // test that consts of type `Ordering` are usable in patterns
140 const ORDERING: Ordering = Greater;
142 const REVERSE: Ordering = ORDERING.reverse();
143 match Ordering::Less {
151 // Test default methods in PartialOrd and PartialEq
156 impl PartialEq for Fool {
157 fn eq(&self, other: &Fool) -> bool {
158 let Fool(this) = *self;
159 let Fool(other) = *other;
166 impl PartialEq for Int {
167 fn eq(&self, other: &Int) -> bool {
168 let Int(this) = *self;
169 let Int(other) = *other;
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)
182 struct RevInt(isize);
184 impl PartialEq for RevInt {
185 fn eq(&self, other: &RevInt) -> bool {
186 let RevInt(this) = *self;
187 let RevInt(other) = *other;
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)
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));
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));
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));
225 impl const PartialEq for S {
226 fn eq(&self, other: &Self) -> bool {
231 impl const PartialOrd for S {
232 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
233 let ret = match (self.0, other.0) {
234 (a, b) if a > b => Ordering::Greater,
235 (a, b) if a < b => Ordering::Less,
236 _ => Ordering::Equal,
243 const _: () = assert!(S(1) == S(1));
244 const _: () = assert!(S(0) != S(1));
246 const _: () = assert!(S(1) <= S(1));
247 const _: () = assert!(S(1) >= S(1));
248 const _: () = assert!(S(0) < S(1));
249 const _: () = assert!(S(1) > S(0));