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
+ #[ deriving( Eq , TotalEq , Ord , TotalOrd ) ]
12
+ struct S < T > {
13
+ x : T ,
14
+ y : T
15
+ }
16
+
17
+ #[ deriving( Eq , TotalEq , Ord , TotalOrd ) ]
18
+ struct TS < T > ( T , T ) ;
19
+
20
+ #[ deriving( Eq , TotalEq , Ord , TotalOrd ) ]
21
+ enum E < T > {
22
+ E0 ,
23
+ E1 ( T ) ,
24
+ E2 ( T , T )
25
+ }
26
+
27
+ #[ deriving( Eq , TotalEq , Ord , TotalOrd ) ]
28
+ enum ES < T > {
29
+ ES1 { x : T } ,
30
+ ES2 { x : T , y : T }
31
+ }
32
+
33
+
34
+ pub fn main ( ) {
35
+ let s1 = S { x : 1 , y : 1 } , s2 = S { x : 1 , y : 2 } ;
36
+ let ts1 = TS ( 1 , 1 ) , ts2 = TS ( 1 , 2 ) ;
37
+ let e0 = E0 , e11 = E1 ( 1 ) , e12 = E1 ( 2 ) , e21 = E2 ( 1 , 1 ) , e22 = E2 ( 1 , 2 ) ;
38
+ let es11 = ES1 { x : 1 } , es12 = ES1 { x : 2 } , es21 = ES2 { x : 1 , y : 1 } , es22 = ES2 { x : 1 , y : 2 } ;
39
+
40
+ test ( [ s1, s2] ) ;
41
+ test ( [ ts1, ts2] ) ;
42
+ test ( [ e0, e11, e12, e21, e22] ) ;
43
+ test ( [ es11, es12, es21, es22] ) ;
44
+ }
45
+
46
+ fn test < T : Eq +TotalEq +Ord +TotalOrd > ( ts : & [ T ] ) {
47
+ // compare each element against all other elements. The list
48
+ // should be in sorted order, so that if i < j, then ts[i] <
49
+ // ts[j], etc.
50
+ for ts. eachi |i, t1| {
51
+ for ts. eachi |j, t2| {
52
+ let ord = i. cmp ( & j) ;
53
+
54
+ let eq = i == j;
55
+ let lt = i < j, le = i <= j;
56
+ let gt = i > j, ge = i >= j;
57
+
58
+ // Eq
59
+ assert_eq ! ( * t1 == * t2, eq) ;
60
+
61
+ // TotalEq
62
+ assert_eq ! ( t1. equals( t2) , eq) ;
63
+
64
+ // Ord
65
+ assert_eq ! ( * t1 < * t2, lt) ;
66
+ assert_eq ! ( * t1 > * t2, gt) ;
67
+
68
+ assert_eq ! ( * t1 <= * t2, le) ;
69
+ assert_eq ! ( * t1 >= * t2, ge) ;
70
+
71
+ // TotalOrd
72
+ assert_eq ! ( t1. cmp( t2) , ord) ;
73
+ }
74
+ }
75
+ }
0 commit comments