]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/cmp.rs
check: Reword the warning to be more prescriptive
[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::{partial_min, partial_max};
12 use core::cmp::Ordering::{Less, Greater, Equal};
13
14 #[test]
15 fn test_int_totalord() {
16     assert_eq!(5.cmp(&10), Less);
17     assert_eq!(10.cmp(&5), Greater);
18     assert_eq!(5.cmp(&5), Equal);
19     assert_eq!((-5).cmp(&12), Less);
20     assert_eq!(12.cmp(&-5), Greater);
21 }
22
23 #[test]
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);
30 }
31
32 #[test]
33 fn test_ordering_reverse() {
34     assert_eq!(Less.reverse(), Greater);
35     assert_eq!(Equal.reverse(), Equal);
36     assert_eq!(Greater.reverse(), Less);
37 }
38
39 #[test]
40 fn test_ordering_order() {
41     assert!(Less < Equal);
42     assert_eq!(Greater.cmp(&Less), Greater);
43 }
44
45 #[test]
46 fn test_partial_min() {
47     use core::f64::NAN;
48     let data_integer = [
49         // a, b, result
50         (0, 0, Some(0)),
51         (1, 0, Some(0)),
52         (0, 1, Some(0)),
53         (-1, 0, Some(-1)),
54         (0, -1, Some(-1))
55     ];
56
57     let data_float = [
58         // a, b, result
59         (0.0f64, 0.0f64, Some(0.0f64)),
60         (1.0f64, 0.0f64, Some(0.0f64)),
61         (0.0f64, 1.0f64, Some(0.0f64)),
62         (-1.0f64, 0.0f64, Some(-1.0f64)),
63         (0.0f64, -1.0f64, Some(-1.0f64)),
64         (NAN, NAN, None),
65         (NAN, 1.0f64, None),
66         (1.0f64, NAN, None)
67     ];
68
69     for &(a, b, result) in &data_integer {
70         assert!(partial_min(a, b) == result);
71     }
72
73     for &(a, b, result) in &data_float {
74         assert!(partial_min(a, b) == result);
75     }
76 }
77
78 #[test]
79 fn test_partial_max() {
80     use core::f64::NAN;
81     let data_integer = [
82         // a, b, result
83         (0, 0, Some(0)),
84         (1, 0, Some(1)),
85         (0, 1, Some(1)),
86         (-1, 0, Some(0)),
87         (0, -1, Some(0))
88     ];
89
90     let data_float = [
91         // a, b, result
92         (0.0f64, 0.0f64, Some(0.0f64)),
93         (1.0f64, 0.0f64, Some(1.0f64)),
94         (0.0f64, 1.0f64, Some(1.0f64)),
95         (-1.0f64, 0.0f64, Some(0.0f64)),
96         (0.0f64, -1.0f64, Some(0.0f64)),
97         (NAN, NAN, None),
98         (NAN, 1.0f64, None),
99         (1.0f64, NAN, None)
100     ];
101
102     for &(a, b, result) in &data_integer {
103         assert!(partial_max(a, b) == result);
104     }
105
106     for &(a, b, result) in &data_float {
107         assert!(partial_max(a, b) == result);
108     }
109 }
110
111 #[test]
112 fn test_user_defined_eq() {
113     use core::num::SignedInt;
114
115     // Our type.
116     struct SketchyNum {
117         num : int
118     }
119
120     // Our implementation of `PartialEq` to support `==` and `!=`.
121     impl PartialEq for SketchyNum {
122         // Our custom eq allows numbers which are near each other to be equal! :D
123         fn eq(&self, other: &SketchyNum) -> bool {
124             (self.num - other.num).abs() < 5
125         }
126     }
127
128     // Now these binary operators will work when applied!
129     assert!(SketchyNum {num: 37} == SketchyNum {num: 34});
130     assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
131 }