]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/ops.rs
Removed direct field usage of RangeInclusive in rustc itself.
[rust.git] / src / libcore / tests / ops.rs
1 // Copyright 2014 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 core::ops::{Range, RangeFull, RangeFrom, RangeTo, RangeInclusive};
12
13 // Test the Range structs without the syntactic sugar.
14
15 #[test]
16 fn test_range() {
17     let r = Range { start: 2, end: 10 };
18     let mut count = 0;
19     for (i, ri) in r.enumerate() {
20         assert!(ri == i + 2);
21         assert!(ri >= 2 && ri < 10);
22         count += 1;
23     }
24     assert!(count == 8);
25 }
26
27 #[test]
28 fn test_range_from() {
29     let r = RangeFrom { start: 2 };
30     let mut count = 0;
31     for (i, ri) in r.take(10).enumerate() {
32         assert!(ri == i + 2);
33         assert!(ri >= 2 && ri < 12);
34         count += 1;
35     }
36     assert!(count == 10);
37 }
38
39 #[test]
40 fn test_range_to() {
41     // Not much to test.
42     let _ = RangeTo { end: 42 };
43 }
44
45 #[test]
46 fn test_full_range() {
47     // Not much to test.
48     let _ = RangeFull;
49 }
50
51 #[test]
52 fn test_range_inclusive() {
53     let mut r = RangeInclusive::new(1i8, 2);
54     assert_eq!(r.next(), Some(1));
55     assert_eq!(r.next(), Some(2));
56     assert_eq!(r.next(), None);
57
58     r = RangeInclusive::new(127i8, 127);
59     assert_eq!(r.next(), Some(127));
60     assert_eq!(r.next(), None);
61
62     r = RangeInclusive::new(-128i8, -128);
63     assert_eq!(r.next_back(), Some(-128));
64     assert_eq!(r.next_back(), None);
65
66     // degenerate
67     r = RangeInclusive::new(1, -1);
68     assert_eq!(r.size_hint(), (0, Some(0)));
69     assert_eq!(r.next(), None);
70 }
71
72
73 #[test]
74 fn test_range_is_empty() {
75     use core::f32::*;
76
77     assert!(!(0.0 .. 10.0).is_empty());
78     assert!( (-0.0 .. 0.0).is_empty());
79     assert!( (10.0 .. 0.0).is_empty());
80
81     assert!(!(NEG_INFINITY .. INFINITY).is_empty());
82     assert!( (EPSILON .. NAN).is_empty());
83     assert!( (NAN .. EPSILON).is_empty());
84     assert!( (NAN .. NAN).is_empty());
85
86     assert!(!(0.0 ..= 10.0).is_empty());
87     assert!(!(-0.0 ..= 0.0).is_empty());
88     assert!( (10.0 ..= 0.0).is_empty());
89
90     assert!(!(NEG_INFINITY ..= INFINITY).is_empty());
91     assert!( (EPSILON ..= NAN).is_empty());
92     assert!( (NAN ..= EPSILON).is_empty());
93     assert!( (NAN ..= NAN).is_empty());
94 }