Skip to content

Commit

Permalink
Add find_first and find_last tests on a giant range
Browse files Browse the repository at this point in the history
It's possible to generate giant iterators in Rayon, and these tests show
that they can still be short-circuited properly in find operations.
  • Loading branch information
cuviper committed Jun 13, 2017
1 parent d74c49e commit 6b8b71a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/iter/find_first_last/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,50 @@ fn find_last_folder_yields_last_match() {
let f = f.consume(0_i32).consume(1_i32).consume(2_i32);
assert_eq!(f.complete(), Some(2_i32));
}


/// Produce a parallel iterator for 0u128..10²⁷
fn octillion() -> impl ParallelIterator<Item = u128> {
(0u32..1_000_000_000)
.into_par_iter()
.with_max_len(1_000)
.map(|i| i as u64 * 1_000_000_000)
.flat_map(
|i| {
(0u32..1_000_000_000)
.into_par_iter()
.with_max_len(1_000)
.map(move |j| i + j as u64)
}
)
.map(|i| i as u128 * 1_000_000_000)
.flat_map(
|i| {
(0u32..1_000_000_000)
.into_par_iter()
.with_max_len(1_000)
.map(move |j| i + j as u128)
}
)
}

#[test]
fn find_first_octillion() {
let x = octillion().find_first(|_| true);
assert_eq!(x, Some(0));
}

#[test]
fn find_last_octillion() {
// FIXME: If we don't use at least two threads, then we end up walking
// through the entire iterator sequentially, without the benefit of any
// short-circuiting. We probably don't want testing to wait that long. ;)
// It would be nice if `find_last` could prioritize the later splits,
// basically flipping the `join` args, without needing indexed `rev`.
// (or could we have an unindexed `rev`?)
let config = ::Configuration::new().num_threads(2);
let pool = ::ThreadPool::new(config).unwrap();

let x = pool.install(|| octillion().find_last(|_| true));
assert_eq!(x, Some(999999999999999999999999999));
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(non_camel_case_types)] // I prefer to use ALL_CAPS for type parameters
#![cfg_attr(test, feature(conservative_impl_trait))]
#![cfg_attr(test, feature(i128_type))]

// If you're not compiling the unstable code, it often happens that
// there is stuff that is considered "dead code" and so forth. So
Expand Down

0 comments on commit 6b8b71a

Please sign in to comment.