diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-05-03 16:33:33 -0400 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-05-10 19:20:20 -0400 |
| commit | b05aae2d4151a5985d58758fcd46037fb39a5fb9 (patch) | |
| tree | c12b1b3738ade87f372a3388b13c698b1929d639 | |
| parent | cdc266e47d8ee63a1eaf29c775f2cbc5f3a61bb4 (diff) | |
| download | rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.tar.gz rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.zip | |
test: Use the new `for` protocol
| -rwxr-xr-x | src/libcore/core | bin | 0 -> 9455580 bytes | |||
| -rw-r--r-- | src/libcore/iter.rs | 31 | ||||
| -rw-r--r-- | src/libcore/str.rs | 4 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 28 | ||||
| -rw-r--r-- | src/libstd/dlist.rs | 12 | ||||
| -rw-r--r-- | src/libstd/fileinput.rs | 60 | ||||
| -rw-r--r-- | src/libstd/treemap.rs | 4 | ||||
| -rw-r--r-- | src/test/bench/graph500-bfs.rs | 7 | ||||
| -rw-r--r-- | src/test/compile-fail/bad-for-loop.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/borrowck-lend-flow-loop.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-2817-2.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-2817.rs | 4 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-3651-2.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-3651.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/private-method.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/assignability-trait.rs | 14 | ||||
| -rw-r--r-- | src/test/run-pass/borrowck-mut-uniq.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/class-impl-very-parameterized-trait.rs | 15 | ||||
| -rw-r--r-- | src/test/run-pass/do-for-empty-args.rs | 11 | ||||
| -rw-r--r-- | src/test/run-pass/do-for-no-args.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/ret-break-cont-in-block.rs | 5 |
21 files changed, 164 insertions, 45 deletions
diff --git a/src/libcore/core b/src/libcore/core new file mode 100755 index 00000000000..790c07db685 --- /dev/null +++ b/src/libcore/core Binary files differdiff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 0f064af657a..d5649d3dfd2 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -86,9 +86,34 @@ pub fn to_vec<T>(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] { #[cfg(not(stage0))] pub fn any<T>(predicate: &fn(T) -> bool, iter: &fn(f: &fn(T) -> bool) -> bool) -> bool { - // If the predicate returns true, we break. If we ever broke, then we found - // something - !iter(|x| !predicate(x)) + for iter |x| { + if predicate(x) { + return true; + } + } + return false; +} + +/** + * Return true if `predicate` is true for all values yielded by an internal iterator. + * + * # Example: + * + * ~~~~ + * assert!(all(|&x: &uint| x < 6, |f| uint::range(1, 6, f))); + * assert!(!all(|&x: &uint| x < 5, |f| uint::range(1, 6, f))); + * ~~~~ + */ +#[inline(always)] +#[cfg(stage0)] +pub fn all<T>(predicate: &fn(T) -> bool, + iter: &fn(f: &fn(T) -> bool)) -> bool { + for iter |x| { + if !predicate(x) { + return false; + } + } + return true; } /** diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 4ab6f659e9c..d7b27f53e7a 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -3404,7 +3404,7 @@ mod tests { let lf = ~"\nMary had a little lamb\nLittle lamb\n"; let crlf = ~"\r\nMary had a little lamb\r\nLittle lamb\r\n"; - fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) { + fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } assert!(vec::all2(v, u, |a,b| a == b)); @@ -3424,7 +3424,7 @@ mod tests { #[test] fn test_words () { - fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) { + fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } assert!(vec::all2(v, u, |a,b| a == b)); diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index e1947b77473..7eba2cbf0cc 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1744,6 +1744,7 @@ pub fn each_permutation<T:Copy>(v: &[T], put: &fn(ts: &[T]) -> bool) -> bool { * ~~~ * */ +#[cfg(stage0)] pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) { assert!(1u <= n); if n > v.len() { return; } @@ -1751,6 +1752,29 @@ pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) { if !it(v.slice(i, i + n)) { return } } } +/** + * Iterate over all contiguous windows of length `n` of the vector `v`. + * + * # Example + * + * Print the adjacent pairs of a vector (i.e. `[1,2]`, `[2,3]`, `[3,4]`) + * + * ~~~ + * for windowed(2, &[1,2,3,4]) |v| { + * io::println(fmt!("%?", v)); + * } + * ~~~ + * + */ +#[cfg(not(stage0))] +pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) -> bool { + assert!(1u <= n); + if n > v.len() { return true; } + for uint::range(0, v.len() - n + 1) |i| { + if !it(v.slice(i, i + n)) { return false; } + } + return true; +} /** * Work with the buffer of a vector. @@ -4566,7 +4590,7 @@ mod tests { } i += 0; false - } + }; } #[test] @@ -4581,7 +4605,7 @@ mod tests { } i += 0; false - } + }; } #[test] diff --git a/src/libstd/dlist.rs b/src/libstd/dlist.rs index 93740f31b9b..741bd629680 100644 --- a/src/libstd/dlist.rs +++ b/src/libstd/dlist.rs @@ -393,6 +393,7 @@ pub impl<T> DList<T> { } /// Iterate over nodes. + #[cfg(stage0)] fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) { let mut link = self.peek_n(); while link.is_some() { @@ -401,6 +402,17 @@ pub impl<T> DList<T> { link = nobe.next_link(); } } + /// Iterate over nodes. + #[cfg(not(stage0))] + fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) -> bool { + let mut link = self.peek_n(); + while link.is_some() { + let nobe = link.get(); + if !f(nobe) { return false; } + link = nobe.next_link(); + } + return true; + } /// Check data structure integrity. O(n). fn assert_consistent(@mut self) { diff --git a/src/libstd/fileinput.rs b/src/libstd/fileinput.rs index 90774d19595..2c5cbc1cbf9 100644 --- a/src/libstd/fileinput.rs +++ b/src/libstd/fileinput.rs @@ -256,10 +256,21 @@ impl FileInput { (line numbers and file names, see documentation for `FileInputState`). Otherwise identical to `lines_each`. */ + #[cfg(stage0)] pub fn each_line_state(&self, f: &fn(&str, FileInputState) -> bool) { self.each_line(|line| f(line, copy self.fi.state)); } + /** + Apply `f` to each line successively, along with some state + (line numbers and file names, see documentation for + `FileInputState`). Otherwise identical to `lines_each`. + */ + #[cfg(not(stage0))] + pub fn each_line_state(&self, + f: &fn(&str, FileInputState) -> bool) -> bool { + self.each_line(|line| f(line, copy self.fi.state)) + } /** @@ -367,10 +378,22 @@ reading from `stdin`). Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input(f: &fn(&str) -> bool) { let mut i = FileInput::from_args(); i.each_line(f); } +/** +Iterate directly over the command line arguments (no arguments implies +reading from `stdin`). + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input(f: &fn(&str) -> bool) -> bool { + let mut i = FileInput::from_args(); + i.each_line(f) +} /** Iterate directly over the command line arguments (no arguments @@ -379,20 +402,44 @@ provided at each call. Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_state(f: &fn(&str, FileInputState) -> bool) { let mut i = FileInput::from_args(); i.each_line_state(f); } +/** +Iterate directly over the command line arguments (no arguments +implies reading from `stdin`) with the current state of the iteration +provided at each call. + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_state(f: &fn(&str, FileInputState) -> bool) -> bool { + let mut i = FileInput::from_args(); + i.each_line_state(f) +} /** Iterate over a vector of files (an empty vector implies just `stdin`). Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) { let mut i = FileInput::from_vec(files); i.each_line(f); } +/** +Iterate over a vector of files (an empty vector implies just `stdin`). + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) -> bool { + let mut i = FileInput::from_vec(files); + i.each_line(f) +} /** Iterate over a vector of files (an empty vector implies just `stdin`) @@ -400,11 +447,24 @@ with the current state of the iteration provided at each call. Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_vec_state(files: ~[Option<Path>], f: &fn(&str, FileInputState) -> bool) { let mut i = FileInput::from_vec(files); i.each_line_state(f); } +/** +Iterate over a vector of files (an empty vector implies just `stdin`) +with the current state of the iteration provided at each call. + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_vec_state(files: ~[Option<Path>], + f: &fn(&str, FileInputState) -> bool) -> bool { + let mut i = FileInput::from_vec(files); + i.each_line_state(f) +} #[cfg(test)] mod test { diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index d68b08dc475..252bb1a6af8 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -674,7 +674,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> { a = x.next(); } } - return a.each(|&x| f(x)) && y.advance(f); + return b.each(|&x| f(x)) && y.advance(f); } } @@ -1326,7 +1326,7 @@ mod test_set { } fn check(a: &[int], b: &[int], expected: &[int], - f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool)) { + f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool) -> bool) { let mut set_a = TreeSet::new(); let mut set_b = TreeSet::new(); diff --git a/src/test/bench/graph500-bfs.rs b/src/test/bench/graph500-bfs.rs index 1842a8ff42e..fb276723543 100644 --- a/src/test/bench/graph500-bfs.rs +++ b/src/test/bench/graph500-bfs.rs @@ -10,8 +10,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[allow(deprecated_mode)]; - /*! An implementation of the Graph500 Breadth First Search problem in Rust. @@ -23,7 +21,7 @@ use std::arc; use std::time; use std::deque::Deque; use std::par; -use core::hashmap::{HashMap, HashSet}; +use core::hashmap::HashSet; use core::int::abs; use core::rand::RngUtil; @@ -83,14 +81,13 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph { HashSet::new() }; - do vec::each(edges) |e| { + for vec::each(edges) |e| { match *e { (i, j) => { graph[i].insert(j); graph[j].insert(i); } } - true } do vec::map_consume(graph) |mut v| { diff --git a/src/test/compile-fail/bad-for-loop.rs b/src/test/compile-fail/bad-for-loop.rs index 8835c577fa8..7ff51eff8ee 100644 --- a/src/test/compile-fail/bad-for-loop.rs +++ b/src/test/compile-fail/bad-for-loop.rs @@ -11,4 +11,5 @@ fn main() { fn baz(_x: &fn(y: int) -> int) {} for baz |_e| { } //~ ERROR A `for` loop iterator should expect a closure that returns `bool` + //~^ ERROR expected `for` closure to return `bool` } diff --git a/src/test/compile-fail/borrowck-lend-flow-loop.rs b/src/test/compile-fail/borrowck-lend-flow-loop.rs index b6384ad9590..f7a72d6e610 100644 --- a/src/test/compile-fail/borrowck-lend-flow-loop.rs +++ b/src/test/compile-fail/borrowck-lend-flow-loop.rs @@ -17,7 +17,7 @@ fn borrow(_v: &int) {} fn borrow_mut(_v: &mut int) {} fn cond() -> bool { fail!() } -fn for_func(_f: &fn() -> bool) { fail!() } +fn for_func(_f: &fn() -> bool) -> bool { fail!() } fn produce<T>() -> T { fail!(); } fn inc(v: &mut ~int) { diff --git a/src/test/compile-fail/issue-2817-2.rs b/src/test/compile-fail/issue-2817-2.rs index 6084552f0ed..17b0d88a6a8 100644 --- a/src/test/compile-fail/issue-2817-2.rs +++ b/src/test/compile-fail/issue-2817-2.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -fn not_bool(f: &fn(int) -> ~str) {} +fn not_bool(f: &fn(int) -> ~str) -> bool {} fn main() { for uint::range(0, 100000) |_i| { //~ ERROR A for-loop body must return (), but diff --git a/src/test/compile-fail/issue-2817.rs b/src/test/compile-fail/issue-2817.rs index 8c28fcdc7fc..77585d15b6b 100644 --- a/src/test/compile-fail/issue-2817.rs +++ b/src/test/compile-fail/issue-2817.rs @@ -16,10 +16,10 @@ fn uuid_random() -> uint { fail!(); } fn main() { do uint::range(0, 100000) |_i| { //~ ERROR Do-block body must return bool, but - } + }; // should get a more general message if the callback // doesn't return nil do uint::range(0, 100000) |_i| { //~ ERROR mismatched types ~"str" - } + }; } diff --git a/src/test/compile-fail/issue-3651-2.rs b/src/test/compile-fail/issue-3651-2.rs index 2431313df63..98a02b6b746 100644 --- a/src/test/compile-fail/issue-3651-2.rs +++ b/src/test/compile-fail/issue-3651-2.rs @@ -9,5 +9,5 @@ // except according to those terms. fn main() { - do 5.times {} //~ ERROR Do-block body must return bool, but returns () here. Perhaps + do 5.times {}; //~ ERROR Do-block body must return bool, but returns () here. Perhaps } diff --git a/src/test/compile-fail/issue-3651.rs b/src/test/compile-fail/issue-3651.rs index 38e9348155a..8d704859fe5 100644 --- a/src/test/compile-fail/issue-3651.rs +++ b/src/test/compile-fail/issue-3651.rs @@ -10,4 +10,5 @@ fn main() { for task::spawn { return true; } //~ ERROR A `for` loop iterator should expect a closure that + //~^ ERROR expected `for` closure to return `bool` } diff --git a/src/test/compile-fail/private-method.rs b/src/test/compile-fail/private-method.rs index c918758ad7c..0d84bc2fc60 100644 --- a/src/test/compile-fail/private-method.rs +++ b/src/test/compile-fail/private-method.rs @@ -18,7 +18,7 @@ mod kitties { } pub impl cat { - priv fn nap(&self) { uint::range(1u, 10000u, |_i| false)} + priv fn nap(&self) { uint::range(1u, 10000u, |_i| false); } } pub fn cat(in_x : uint, in_y : int) -> cat { diff --git a/src/test/run-pass/assignability-trait.rs b/src/test/run-pass/assignability-trait.rs index c557aa7e223..6946ed3fbcf 100644 --- a/src/test/run-pass/assignability-trait.rs +++ b/src/test/run-pass/assignability-trait.rs @@ -13,22 +13,18 @@ // it. trait iterable<A> { - fn iterate(&self, blk: &fn(x: &A) -> bool); + fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool; } impl<'self,A> iterable<A> for &'self [A] { - fn iterate(&self, f: &fn(x: &A) -> bool) { - for vec::each(*self) |e| { - if !f(e) { break; } - } + fn iterate(&self, f: &fn(x: &A) -> bool) -> bool { + vec::each(*self, f) } } impl<A> iterable<A> for ~[A] { - fn iterate(&self, f: &fn(x: &A) -> bool) { - for vec::each(*self) |e| { - if !f(e) { break; } - } + fn iterate(&self, f: &fn(x: &A) -> bool) -> bool { + vec::each(*self, f) } } diff --git a/src/test/run-pass/borrowck-mut-uniq.rs b/src/test/run-pass/borrowck-mut-uniq.rs index 5f5b9c59d76..778637701c5 100644 --- a/src/test/run-pass/borrowck-mut-uniq.rs +++ b/src/test/run-pass/borrowck-mut-uniq.rs @@ -18,7 +18,7 @@ fn add_int(x: &mut Ints, v: int) { x.values <-> values; } -fn iter_ints(x: &Ints, f: &fn(x: &int) -> bool) { +fn iter_ints(x: &Ints, f: &fn(x: &int) -> bool) -> bool { let l = x.values.len(); uint::range(0, l, |i| f(&x.values[i])) } diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index b89bf064092..39d4b25f262 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -59,25 +59,26 @@ impl<T> Mutable for cat<T> { } impl<T> Map<int, T> for cat<T> { - fn each<'a>(&'a self, f: &fn(&int, &'a T) -> bool) { + fn each<'a>(&'a self, f: &fn(&int, &'a T) -> bool) -> bool { let mut n = int::abs(self.meows); while n > 0 { - if !f(&n, &self.name) { break; } + if !f(&n, &self.name) { return false; } n -= 1; } + return true; } fn contains_key(&self, k: &int) -> bool { *k <= self.meows } - fn each_key(&self, f: &fn(v: &int) -> bool) { - for self.each |k, _| { if !f(k) { break; } loop;}; + fn each_key(&self, f: &fn(v: &int) -> bool) -> bool { + self.each(|k, _| f(k)) } - fn each_value<'a>(&'a self, f: &fn(v: &'a T) -> bool) { - for self.each |_, v| { if !f(v) { break; } loop;}; + fn each_value<'a>(&'a self, f: &fn(v: &'a T) -> bool) -> bool { + self.each(|_, v| f(v)) } - fn mutate_values(&mut self, _f: &fn(&int, &mut T) -> bool) { + fn mutate_values(&mut self, _f: &fn(&int, &mut T) -> bool) -> bool { fail!(~"nope") } diff --git a/src/test/run-pass/do-for-empty-args.rs b/src/test/run-pass/do-for-empty-args.rs index c86c1768111..fb1bc37fd5e 100644 --- a/src/test/run-pass/do-for-empty-args.rs +++ b/src/test/run-pass/do-for-empty-args.rs @@ -11,14 +11,15 @@ // no-reformat // Testing various forms of `do` and `for` with empty arg lists -fn f(f: &fn() -> bool) { +fn f(f: &fn() -> bool) -> bool { + true } pub fn main() { - do f() || { true } - do f() { true } - do f || { true } - do f { true } + do f() || { true }; + do f() { true }; + do f || { true }; + do f { true }; for f() || { } for f() { } for f || { } diff --git a/src/test/run-pass/do-for-no-args.rs b/src/test/run-pass/do-for-no-args.rs index c89d693c816..e9d7c946a9a 100644 --- a/src/test/run-pass/do-for-no-args.rs +++ b/src/test/run-pass/do-for-no-args.rs @@ -10,7 +10,7 @@ // Testing that we can drop the || in for/do exprs -fn f(f: @fn() -> bool) { } +fn f(f: @fn() -> bool) -> bool { true } fn d(f: @fn()) { } diff --git a/src/test/run-pass/ret-break-cont-in-block.rs b/src/test/run-pass/ret-break-cont-in-block.rs index ab61cee4223..1792a89d64f 100644 --- a/src/test/run-pass/ret-break-cont-in-block.rs +++ b/src/test/run-pass/ret-break-cont-in-block.rs @@ -12,12 +12,13 @@ use core::cmp::Eq; -fn iter<T>(v: ~[T], it: &fn(&T) -> bool) { +fn iter<T>(v: ~[T], it: &fn(&T) -> bool) -> bool { let mut i = 0u, l = v.len(); while i < l { - if !it(&v[i]) { break; } + if !it(&v[i]) { return false; } i += 1u; } + return true; } fn find_pos<T:Eq + Copy + Clone>(n: T, h: ~[T]) -> Option<uint> { |
