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::Ordering::{Less, Greater, Equal};
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);
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);
32 fn test_ordering_reverse() {
33 assert_eq!(Less.reverse(), Greater);
34 assert_eq!(Equal.reverse(), Equal);
35 assert_eq!(Greater.reverse(), Less);
39 fn test_ordering_order() {
40 assert!(Less < Equal);
41 assert_eq!(Greater.cmp(&Less), Greater);
45 fn test_user_defined_eq() {
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
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});