Skip to content

Commit a696f0f

Browse files
committed
test: add testcases for external iterators using foreach.
1 parent 62b6fa0 commit a696f0f

6 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
fn main() {
12+
let x = [1,..100];
13+
let mut y = 0;
14+
foreach i in x.iter() {
15+
if y > 10 {
16+
break;
17+
}
18+
y += *i;
19+
}
20+
assert!(y == 11);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use std::hashmap::HashMap;
12+
13+
// This is a fancy one: it uses an external iterator established
14+
// outside the loop, breaks, then _picks back up_ and continues
15+
// iterating with it.
16+
17+
fn main() {
18+
let mut h = HashMap::new();
19+
let kvs = [(1, 10), (2, 20), (3, 30)];
20+
foreach &(k,v) in kvs.iter() {
21+
h.insert(k,v);
22+
}
23+
let mut x = 0;
24+
let mut y = 0;
25+
26+
let mut i = h.iter();
27+
28+
foreach (&k,&v) in i {
29+
x += k;
30+
y += v;
31+
break;
32+
}
33+
34+
foreach (&k,&v) in i {
35+
x += k;
36+
y += v;
37+
}
38+
39+
assert_eq!(x, 6);
40+
assert_eq!(y, 60);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use std::hashmap::HashMap;
12+
13+
fn main() {
14+
let mut h = HashMap::new();
15+
let kvs = [(1, 10), (2, 20), (3, 30)];
16+
foreach &(k,v) in kvs.iter() {
17+
h.insert(k,v);
18+
}
19+
let mut x = 0;
20+
let mut y = 0;
21+
foreach (&k,&v) in h.iter() {
22+
x += k;
23+
y += v;
24+
}
25+
assert_eq!(x, 6);
26+
assert_eq!(y, 60);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
fn main() {
12+
let x = [1,..100];
13+
let mut y = 0;
14+
foreach (n,i) in x.iter().enumerate() {
15+
if n < 10 {
16+
loop;
17+
}
18+
y += *i;
19+
}
20+
assert_eq!(y, 90);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
fn main() {
12+
let x = [1,..100];
13+
let y = [2,..100];
14+
let mut p = 0;
15+
let mut q = 0;
16+
foreach i in x.iter() {
17+
foreach j in y.iter() {
18+
p += *j;
19+
}
20+
q += *i + p;
21+
}
22+
assert!(q == 1010100);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
fn main() {
12+
let x = [1,..100];
13+
let mut y = 0;
14+
foreach i in x.iter() {
15+
y += *i
16+
}
17+
assert!(y == 100);
18+
}

0 commit comments

Comments
 (0)