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.
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.
14 fn uint_range(lo: uint, hi: uint, it: &fn(uint) -> bool) -> bool {
15 range(lo, hi).advance(it)
18 fn int_range(lo: int, hi: int, it: &fn(int) -> bool) -> bool {
19 range(lo, hi).advance(it)
22 fn uint_range_rev(hi: uint, lo: uint, it: &fn(uint) -> bool) -> bool {
23 uint::range_rev(hi, lo, it)
26 fn int_range_rev(hi: int, lo: int, it: &fn(int) -> bool) -> bool {
27 int::range_rev(hi, lo, it)
30 fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool {
31 int::range_step(a, b, step, it)
34 fn uint_range_step(a: uint, b: uint, step: int, it: &fn(uint) -> bool) -> bool {
35 uint::range_step(a, b, step, it)
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
43 do uint_range_rev(100, 2) |i| {
47 assert_eq!(sum, 4949);
50 do int_range_rev(100, 2) |i| {
54 assert_eq!(sum, 4949);
57 // elements are visited in correct order
58 let primes = [2,3,5,7,11];
60 do uint_range_rev(5, 0) |i| {
61 printfln!("uint 4 downto 0: %u", i);
62 prod *= int::pow(primes[i], i);
65 assert_eq!(prod, 11*11*11*11*7*7*7*5*5*3*1);
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);
72 assert_eq!(prod, 11*11*11*11*7*7*7*5*5*3*1);
75 // range and range_rev are symmetric.
77 do uint_range(10, 30) |i| {
81 let mut sum_down = 0u;
82 do uint_range_rev(30, 10) |i| {
86 assert_eq!(sum_up, sum_down);
89 do int_range(-20, 10) |i| {
94 do int_range_rev(10, -20) |i| {
98 assert_eq!(sum_up, sum_down);
102 do int_range_rev(10, 10) |_| {
103 fail!("range should be empty when start == stop");
107 do uint_range_rev(0, 1) |_| {
108 fail!("range should be empty when start-1 underflows");
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);
118 assert_eq!(uflo_loop_visited, ~[15, 11, 7, 3]);
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);
125 assert_eq!(uflo_loop_visited, ~[15, 11, 7, 3]);