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.
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.
11 use core::cmp::{partial_min, partial_max};
12 use core::cmp::Ordering::{Less, Greater, Equal};
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);
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_ordering_reverse() {
34 assert_eq!(Less.reverse(), Greater);
35 assert_eq!(Equal.reverse(), Equal);
36 assert_eq!(Greater.reverse(), Less);
40 fn test_ordering_order() {
41 assert!(Less < Equal);
42 assert_eq!(Greater.cmp(&Less), Greater);
46 fn test_partial_min() {
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)),
69 for &(a, b, result) in &data_integer {
70 assert!(partial_min(a, b) == result);
73 for &(a, b, result) in &data_float {
74 assert!(partial_min(a, b) == result);
79 fn test_partial_max() {
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)),
102 for &(a, b, result) in &data_integer {
103 assert!(partial_max(a, b) == result);
106 for &(a, b, result) in &data_float {
107 assert!(partial_max(a, b) == result);
112 fn test_user_defined_eq() {
113 use core::num::SignedInt;
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
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});