]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/cmp.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / libcoretest / 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_ordering_reverse() {
33     assert_eq!(Less.reverse(), Greater);
34     assert_eq!(Equal.reverse(), Equal);
35     assert_eq!(Greater.reverse(), Less);
36 }
37
38 #[test]
39 fn test_ordering_order() {
40     assert!(Less < Equal);
41     assert_eq!(Greater.cmp(&Less), Greater);
42 }
43
44 #[test]
45 fn test_user_defined_eq() {
46     // Our type.
47     struct SketchyNum {
48         num : isize
49     }
50
51     // Our implementation of `PartialEq` to support `==` and `!=`.
52     impl PartialEq for SketchyNum {
53         // Our custom eq allows numbers which are near each other to be equal! :D
54         fn eq(&self, other: &SketchyNum) -> bool {
55             (self.num - other.num).abs() < 5
56         }
57     }
58
59     // Now these binary operators will work when applied!
60     assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
61     assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
62 }