]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/pure-sum.rs
d5f1caaf74da1570b70580abb0d1a8d3067ad241
[rust.git] / src / test / run-pass / pure-sum.rs
1 // Copyright 2012 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 // Check that functions can modify local state.
12
13 fn sums_to(v: Vec<int> , sum: int) -> bool {
14     let mut i = 0u;
15     let mut sum0 = 0;
16     while i < v.len() {
17         sum0 += v[i];
18         i += 1u;
19     }
20     return sum0 == sum;
21 }
22
23 fn sums_to_using_uniq(v: Vec<int> , sum: int) -> bool {
24     let mut i = 0u;
25     let mut sum0 = ~0;
26     while i < v.len() {
27         *sum0 += v[i];
28         i += 1u;
29     }
30     return *sum0 == sum;
31 }
32
33 fn sums_to_using_rec(v: Vec<int> , sum: int) -> bool {
34     let mut i = 0u;
35     let mut sum0 = F {f: 0};
36     while i < v.len() {
37         sum0.f += v[i];
38         i += 1u;
39     }
40     return sum0.f == sum;
41 }
42
43 struct F<T> { f: T }
44
45 fn sums_to_using_uniq_rec(v: Vec<int> , sum: int) -> bool {
46     let mut i = 0u;
47     let mut sum0 = F {f: ~0};
48     while i < v.len() {
49         *sum0.f += v[i];
50         i += 1u;
51     }
52     return *sum0.f == sum;
53 }
54
55 pub fn main() {
56 }