]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/deriving-cmp-shortcircuit.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[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 use std::cmp::Ordering;
16
17 pub struct FailCmp;
18 impl PartialEq for FailCmp {
19     fn eq(&self, _: &FailCmp) -> bool { panic!("eq") }
20 }
21
22 impl PartialOrd for FailCmp {
23     fn partial_cmp(&self, _: &FailCmp) -> Option<Ordering> { panic!("partial_cmp") }
24 }
25
26 impl Eq for FailCmp {}
27
28 impl Ord for FailCmp {
29     fn cmp(&self, _: &FailCmp) -> Ordering { panic!("cmp") }
30 }
31
32 #[derive(PartialEq,PartialOrd,Eq,Ord)]
33 struct ShortCircuit {
34     x: int,
35     y: FailCmp
36 }
37
38 pub fn main() {
39     let a = ShortCircuit { x: 1, y: FailCmp };
40     let b = ShortCircuit { x: 2, y: FailCmp };
41
42     assert!(a != b);
43     assert!(a < b);
44     assert_eq!(a.cmp(&b), ::std::cmp::Ordering::Less);
45 }