]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/cmp-default.rs
fd040d109108fe173ceedc95a6bbe046babfe5b4
[rust.git] / src / test / run-pass / cmp-default.rs
1 // Copyright 2012 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 std::cmp::Ordering;
12
13 // Test default methods in PartialOrd and PartialEq
14 //
15 struct Fool(bool);
16
17 impl PartialEq for Fool {
18     fn eq(&self, other: &Fool) -> bool {
19         let Fool(this) = *self;
20         let Fool(other) = *other;
21         this != other
22     }
23 }
24
25 struct Int(int);
26
27 impl PartialEq for Int {
28     fn eq(&self, other: &Int) -> bool {
29         let Int(this) = *self;
30         let Int(other) = *other;
31         this == other
32     }
33 }
34
35 impl PartialOrd for Int {
36     fn partial_cmp(&self, other: &Int) -> Option<Ordering> {
37         let Int(this) = *self;
38         let Int(other) = *other;
39         this.partial_cmp(&other)
40     }
41 }
42
43 struct RevInt(int);
44
45 impl PartialEq for RevInt {
46     fn eq(&self, other: &RevInt) -> bool {
47         let RevInt(this) = *self;
48         let RevInt(other) = *other;
49         this == other
50     }
51 }
52
53 impl PartialOrd for RevInt {
54     fn partial_cmp(&self, other: &RevInt) -> Option<Ordering> {
55         let RevInt(this) = *self;
56         let RevInt(other) = *other;
57         other.partial_cmp(&this)
58     }
59 }
60
61 pub fn main() {
62     assert!(Int(2) >  Int(1));
63     assert!(Int(2) >= Int(1));
64     assert!(Int(1) >= Int(1));
65     assert!(Int(1) <  Int(2));
66     assert!(Int(1) <= Int(2));
67     assert!(Int(1) <= Int(1));
68
69     assert!(RevInt(2) <  RevInt(1));
70     assert!(RevInt(2) <= RevInt(1));
71     assert!(RevInt(1) <= RevInt(1));
72     assert!(RevInt(1) >  RevInt(2));
73     assert!(RevInt(1) >= RevInt(2));
74     assert!(RevInt(1) >= RevInt(1));
75
76     assert!(Fool(true)  == Fool(false));
77     assert!(Fool(true)  != Fool(true));
78     assert!(Fool(false) != Fool(false));
79     assert!(Fool(false) == Fool(true));
80 }