]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/deriving-cmp-shortcircuit.rs
rollup merge of #18413 : bkoropoff/issue-18412
[rust.git] / src / test / run-pass / deriving-cmp-shortcircuit.rs
1 // Copyright 2013 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 // check that the derived impls for the comparison traits shortcircuit
12 // where possible, by having a type that panics when compared as the
13 // second element, so this passes iff the instances shortcircuit.
14
15 pub struct FailCmp;
16 impl PartialEq for FailCmp {
17     fn eq(&self, _: &FailCmp) -> bool { panic!("eq") }
18 }
19
20 impl PartialOrd for FailCmp {
21     fn partial_cmp(&self, _: &FailCmp) -> Option<Ordering> { panic!("partial_cmp") }
22 }
23
24 impl Eq for FailCmp {}
25
26 impl Ord for FailCmp {
27     fn cmp(&self, _: &FailCmp) -> Ordering { panic!("cmp") }
28 }
29
30 #[deriving(PartialEq,PartialOrd,Eq,Ord)]
31 struct ShortCircuit {
32     x: int,
33     y: FailCmp
34 }
35
36 pub fn main() {
37     let a = ShortCircuit { x: 1, y: FailCmp };
38     let b = ShortCircuit { x: 2, y: FailCmp };
39
40     assert!(a != b);
41     assert!(a < b);
42     assert_eq!(a.cmp(&b), ::std::cmp::Less);
43 }