diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-03-31 02:01:44 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-04-12 17:11:58 +1000 |
| commit | 99492796dcaac41966dc54f7ab4b8e33e641bb73 (patch) | |
| tree | 30808c46577acd0cf20791cc27d73d102b666cc0 | |
| parent | 7906c5572a8c4c5c0f6aa6e69bb63d64de50d697 (diff) | |
| download | rust-99492796dcaac41966dc54f7ab4b8e33e641bb73.tar.gz rust-99492796dcaac41966dc54f7ab4b8e33e641bb73.zip | |
testsuite: add tests for derived Eq, TotalEq, Ord, TotalOrd.
| -rw-r--r-- | src/test/run-pass/deriving-cmp.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/test/run-pass/deriving-cmp.rs b/src/test/run-pass/deriving-cmp.rs new file mode 100644 index 00000000000..56968fc1210 --- /dev/null +++ b/src/test/run-pass/deriving-cmp.rs @@ -0,0 +1,75 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[deriving(Eq, TotalEq, Ord, TotalOrd)] +struct S<T> { + x: T, + y: T +} + +#[deriving(Eq, TotalEq, Ord, TotalOrd)] +struct TS<T>(T,T); + +#[deriving(Eq, TotalEq, Ord, TotalOrd)] +enum E<T> { + E0, + E1(T), + E2(T,T) +} + +#[deriving(Eq, TotalEq, Ord, TotalOrd)] +enum ES<T> { + ES1 { x: T }, + ES2 { x: T, y: T } +} + + +pub fn main() { + let s1 = S {x: 1, y: 1}, s2 = S {x: 1, y: 2}; + let ts1 = TS(1, 1), ts2 = TS(1,2); + let e0 = E0, e11 = E1(1), e12 = E1(2), e21 = E2(1,1), e22 = E2(1, 2); + let es11 = ES1 {x: 1}, es12 = ES1 {x: 2}, es21 = ES2 {x: 1, y: 1}, es22 = ES2 {x: 1, y: 2}; + + test([s1, s2]); + test([ts1, ts2]); + test([e0, e11, e12, e21, e22]); + test([es11, es12, es21, es22]); +} + +fn test<T: Eq+TotalEq+Ord+TotalOrd>(ts: &[T]) { + // compare each element against all other elements. The list + // should be in sorted order, so that if i < j, then ts[i] < + // ts[j], etc. + for ts.eachi |i, t1| { + for ts.eachi |j, t2| { + let ord = i.cmp(&j); + + let eq = i == j; + let lt = i < j, le = i <= j; + let gt = i > j, ge = i >= j; + + // Eq + assert_eq!(*t1 == *t2, eq); + + // TotalEq + assert_eq!(t1.equals(t2), eq); + + // Ord + assert_eq!(*t1 < *t2, lt); + assert_eq!(*t1 > *t2, gt); + + assert_eq!(*t1 <= *t2, le); + assert_eq!(*t1 >= *t2, ge); + + // TotalOrd + assert_eq!(t1.cmp(t2), ord); + } + } +} \ No newline at end of file |
