]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/cmp.rs
Rollup merge of #42496 - Razaekel:feature/integer_max-min, r=BurntSushi
[rust.git] / src / libcore / tests / cmp.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use core::cmp::Ordering::{Less, Greater, Equal};
12
13 #[test]
14 fn test_int_totalord() {
15     assert_eq!(5.cmp(&10), Less);
16     assert_eq!(10.cmp(&5), Greater);
17     assert_eq!(5.cmp(&5), Equal);
18     assert_eq!((-5).cmp(&12), Less);
19     assert_eq!(12.cmp(&-5), Greater);
20 }
21
22 #[test]
23 fn test_mut_int_totalord() {
24     assert_eq!((&mut 5).cmp(&&mut 10), Less);
25     assert_eq!((&mut 10).cmp(&&mut 5), Greater);
26     assert_eq!((&mut 5).cmp(&&mut 5), Equal);
27     assert_eq!((&mut -5).cmp(&&mut 12), Less);
28     assert_eq!((&mut 12).cmp(&&mut -5), Greater);
29 }
30
31 #[test]
32 fn test_ord_max_min() {
33     assert_eq!(1.max(2), 2);
34     assert_eq!(2.max(1), 2);
35     assert_eq!(1.min(2), 1);
36     assert_eq!(2.min(1), 1);
37     assert_eq!(1.max(1), 1);
38     assert_eq!(1.min(1), 1);
39 }
40
41 #[test]
42 fn test_ordering_reverse() {
43     assert_eq!(Less.reverse(), Greater);
44     assert_eq!(Equal.reverse(), Equal);
45     assert_eq!(Greater.reverse(), Less);
46 }
47
48 #[test]
49 fn test_ordering_order() {
50     assert!(Less < Equal);
51     assert_eq!(Greater.cmp(&Less), Greater);
52 }
53
54 #[test]
55 fn test_ordering_then() {
56     assert_eq!(Equal.then(Less), Less);
57     assert_eq!(Equal.then(Equal), Equal);
58     assert_eq!(Equal.then(Greater), Greater);
59     assert_eq!(Less.then(Less), Less);
60     assert_eq!(Less.then(Equal), Less);
61     assert_eq!(Less.then(Greater), Less);
62     assert_eq!(Greater.then(Less), Greater);
63     assert_eq!(Greater.then(Equal), Greater);
64     assert_eq!(Greater.then(Greater), Greater);
65 }
66
67 #[test]
68 fn test_ordering_then_with() {
69     assert_eq!(Equal.then_with(|| Less), Less);
70     assert_eq!(Equal.then_with(|| Equal), Equal);
71     assert_eq!(Equal.then_with(|| Greater), Greater);
72     assert_eq!(Less.then_with(|| Less), Less);
73     assert_eq!(Less.then_with(|| Equal), Less);
74     assert_eq!(Less.then_with(|| Greater), Less);
75     assert_eq!(Greater.then_with(|| Less), Greater);
76     assert_eq!(Greater.then_with(|| Equal), Greater);
77     assert_eq!(Greater.then_with(|| Greater), Greater);
78 }
79
80 #[test]
81 fn test_user_defined_eq() {
82     // Our type.
83     struct SketchyNum {
84         num : isize
85     }
86
87     // Our implementation of `PartialEq` to support `==` and `!=`.
88     impl PartialEq for SketchyNum {
89         // Our custom eq allows numbers which are near each other to be equal! :D
90         fn eq(&self, other: &SketchyNum) -> bool {
91             (self.num - other.num).abs() < 5
92         }
93     }
94
95     // Now these binary operators will work when applied!
96     assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
97     assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
98 }