Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add testcases for external iterators using foreach.
- Loading branch information
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let x = [1,..100]; | ||
let mut y = 0; | ||
foreach i in x.iter() { | ||
if y > 10 { | ||
break; | ||
} | ||
y += *i; | ||
} | ||
assert!(y == 11); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/run-pass/foreach-external-iterators-hashmap-break-restart.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::hashmap::HashMap; | ||
|
||
// This is a fancy one: it uses an external iterator established | ||
// outside the loop, breaks, then _picks back up_ and continues | ||
// iterating with it. | ||
|
||
fn main() { | ||
let mut h = HashMap::new(); | ||
let kvs = [(1, 10), (2, 20), (3, 30)]; | ||
foreach &(k,v) in kvs.iter() { | ||
h.insert(k,v); | ||
} | ||
let mut x = 0; | ||
let mut y = 0; | ||
|
||
let mut i = h.iter(); | ||
|
||
foreach (&k,&v) in i { | ||
x += k; | ||
y += v; | ||
break; | ||
} | ||
|
||
foreach (&k,&v) in i { | ||
x += k; | ||
y += v; | ||
} | ||
|
||
assert_eq!(x, 6); | ||
assert_eq!(y, 60); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::hashmap::HashMap; | ||
|
||
fn main() { | ||
let mut h = HashMap::new(); | ||
let kvs = [(1, 10), (2, 20), (3, 30)]; | ||
foreach &(k,v) in kvs.iter() { | ||
h.insert(k,v); | ||
} | ||
let mut x = 0; | ||
let mut y = 0; | ||
foreach (&k,&v) in h.iter() { | ||
x += k; | ||
y += v; | ||
} | ||
assert_eq!(x, 6); | ||
assert_eq!(y, 60); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let x = [1,..100]; | ||
let mut y = 0; | ||
foreach (n,i) in x.iter().enumerate() { | ||
if n < 10 { | ||
loop; | ||
} | ||
y += *i; | ||
} | ||
assert_eq!(y, 90); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let x = [1,..100]; | ||
let y = [2,..100]; | ||
let mut p = 0; | ||
let mut q = 0; | ||
foreach i in x.iter() { | ||
foreach j in y.iter() { | ||
p += *j; | ||
} | ||
q += *i + p; | ||
} | ||
assert!(q == 1010100); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn main() { | ||
let x = [1,..100]; | ||
let mut y = 0; | ||
foreach i in x.iter() { | ||
y += *i | ||
} | ||
assert!(y == 100); | ||
} |
a696f0f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from brson
at graydon@a696f0f
a696f0f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging graydon/rust/foreach-in-sketch = a696f0f into auto
a696f0f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
graydon/rust/foreach-in-sketch = a696f0f merged ok, testing candidate = 6296dc0
a696f0f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/645
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/648
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt/builds/650
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt-vg/builds/642
success: http://buildbot.rust-lang.org/builders/auto-mac-all-opt/builds/645
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/663
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt/builds/666
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/663
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt/builds/667
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt-vg/builds/672
success: http://buildbot.rust-lang.org/builders/auto-linux-all-opt/builds/665
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/647
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt/builds/662
success: http://buildbot.rust-lang.org/builders/auto-bsd-64-opt/builds/436
a696f0f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = 6296dc0