]>
git.proxmox.com Git - rustc.git/blob - library/core/tests/cmp.rs
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
);
138 // Test default methods in PartialOrd and PartialEq
143 impl PartialEq
for Fool
{
144 fn eq(&self, other
: &Fool
) -> bool
{
145 let Fool(this
) = *self;
146 let Fool(other
) = *other
;
153 impl PartialEq
for Int
{
154 fn eq(&self, other
: &Int
) -> bool
{
155 let Int(this
) = *self;
156 let Int(other
) = *other
;
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
)
169 struct RevInt(isize);
171 impl PartialEq
for RevInt
{
172 fn eq(&self, other
: &RevInt
) -> bool
{
173 let RevInt(this
) = *self;
174 let RevInt(other
) = *other
;
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
)
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));
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));
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));