]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/num-range-rev.rs
5eecbe7e03afb207cec9f44a5a9a8cae76b33284
[rust.git] / src / test / run-pass / num-range-rev.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 use std::int;
12 use std::uint;
13
14 fn uint_range(lo: uint, hi: uint, it: &fn(uint) -> bool) -> bool {
15     range(lo, hi).advance(it)
16 }
17
18 fn int_range(lo: int,  hi: int, it: &fn(int) -> bool) -> bool {
19     range(lo, hi).advance(it)
20 }
21
22 fn uint_range_rev(hi: uint, lo: uint, it: &fn(uint) -> bool) -> bool {
23     uint::range_rev(hi, lo, it)
24 }
25
26 fn int_range_rev(hi: int,  lo: int, it: &fn(int) -> bool) -> bool {
27     int::range_rev(hi, lo, it)
28 }
29
30 fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool {
31     int::range_step(a, b, step, it)
32 }
33
34 fn uint_range_step(a: uint, b: uint, step: int, it: &fn(uint) -> bool) -> bool {
35     uint::range_step(a, b, step, it)
36 }
37
38
39 pub fn main() {
40     // int and uint have same result for
41     //   Sum{100 > i >= 2} == (Sum{1 <= i <= 99} - 1) == n*(n+1)/2 - 1 for n=99
42     let mut sum = 0u;
43     do uint_range_rev(100, 2) |i| {
44         sum += i;
45         true
46     };
47     assert_eq!(sum, 4949);
48
49     let mut sum = 0i;
50     do int_range_rev(100, 2) |i| {
51         sum += i;
52         true
53     };
54     assert_eq!(sum, 4949);
55
56
57     // elements are visited in correct order
58     let primes = [2,3,5,7,11];
59     let mut prod = 1i;
60     do uint_range_rev(5, 0) |i| {
61         printfln!("uint 4 downto 0: %u", i);
62         prod *= int::pow(primes[i], i);
63         true
64     };
65     assert_eq!(prod, 11*11*11*11*7*7*7*5*5*3*1);
66     let mut prod = 1i;
67     do int_range_rev(5, 0) |i| {
68         printfln!("int 4 downto 0: %d", i);
69         prod *= int::pow(primes[i], i as uint);
70         true
71     };
72     assert_eq!(prod, 11*11*11*11*7*7*7*5*5*3*1);
73
74
75     // range and range_rev are symmetric.
76     let mut sum_up = 0u;
77     do uint_range(10, 30) |i| {
78         sum_up += i;
79         true
80     };
81     let mut sum_down = 0u;
82     do uint_range_rev(30, 10) |i| {
83         sum_down += i;
84         true
85     };
86     assert_eq!(sum_up, sum_down);
87
88     let mut sum_up = 0;
89     do int_range(-20, 10) |i| {
90         sum_up += i;
91         true
92     };
93     let mut sum_down = 0;
94     do int_range_rev(10, -20) |i| {
95         sum_down += i;
96         true
97     };
98     assert_eq!(sum_up, sum_down);
99
100
101     // empty ranges
102     do int_range_rev(10, 10) |_| {
103         fail!("range should be empty when start == stop");
104         true
105     };
106
107     do uint_range_rev(0, 1) |_| {
108         fail!("range should be empty when start-1 underflows");
109         true
110     };
111
112     // range iterations do not wrap/underflow
113     let mut uflo_loop_visited = ~[];
114     do int_range_step(int::min_value+15, int::min_value, -4) |x| {
115         uflo_loop_visited.push(x - int::min_value);
116         true
117     };
118     assert_eq!(uflo_loop_visited, ~[15, 11, 7, 3]);
119
120     let mut uflo_loop_visited = ~[];
121     do uint_range_step(uint::min_value+15, uint::min_value, -4) |x| {
122         uflo_loop_visited.push(x - uint::min_value);
123         true
124     };
125     assert_eq!(uflo_loop_visited, ~[15, 11, 7, 3]);
126 }