summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
commit018c4950b9406055dec02ef0fb52f132e2bb1e2c (patch)
treea835ebdf2088ef88fa681f8fad45f09922c1ae9a /vendor/itertools/tests
parentAdding debian version 1.75.0+dfsg1-5. (diff)
downloadrustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.tar.xz
rustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/itertools/tests')
-rw-r--r--vendor/itertools/tests/adaptors_no_collect.rs11
-rw-r--r--vendor/itertools/tests/merge_join.rs69
-rw-r--r--vendor/itertools/tests/quick.rs314
-rw-r--r--vendor/itertools/tests/specializations.rs310
-rw-r--r--vendor/itertools/tests/test_core.rs77
-rw-r--r--vendor/itertools/tests/test_std.rs557
-rw-r--r--vendor/itertools/tests/zip.rs18
7 files changed, 1037 insertions, 319 deletions
diff --git a/vendor/itertools/tests/adaptors_no_collect.rs b/vendor/itertools/tests/adaptors_no_collect.rs
index 103db23f1..977224af2 100644
--- a/vendor/itertools/tests/adaptors_no_collect.rs
+++ b/vendor/itertools/tests/adaptors_no_collect.rs
@@ -22,9 +22,14 @@ impl Iterator for PanickingCounter {
}
fn no_collect_test<A, T>(to_adaptor: T)
- where A: Iterator, T: Fn(PanickingCounter) -> A
+where
+ A: Iterator,
+ T: Fn(PanickingCounter) -> A,
{
- let counter = PanickingCounter { curr: 0, max: 10_000 };
+ let counter = PanickingCounter {
+ curr: 0,
+ max: 10_000,
+ };
let adaptor = to_adaptor(counter);
for _ in adaptor.take(5) {}
@@ -43,4 +48,4 @@ fn combinations_no_collect() {
#[test]
fn combinations_with_replacement_no_collect() {
no_collect_test(|iter| iter.combinations_with_replacement(5))
-} \ No newline at end of file
+}
diff --git a/vendor/itertools/tests/merge_join.rs b/vendor/itertools/tests/merge_join.rs
index 3280b7d4e..776252fc5 100644
--- a/vendor/itertools/tests/merge_join.rs
+++ b/vendor/itertools/tests/merge_join.rs
@@ -1,108 +1,101 @@
-use itertools::EitherOrBoth;
use itertools::free::merge_join_by;
+use itertools::EitherOrBoth;
#[test]
fn empty() {
let left: Vec<u32> = vec![];
let right: Vec<u32> = vec![];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![];
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn left_only() {
- let left: Vec<u32> = vec![1,2,3];
+ let left: Vec<u32> = vec![1, 2, 3];
let right: Vec<u32> = vec![];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Left(1),
EitherOrBoth::Left(2),
- EitherOrBoth::Left(3)
+ EitherOrBoth::Left(3),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn right_only() {
let left: Vec<u32> = vec![];
- let right: Vec<u32> = vec![1,2,3];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let right: Vec<u32> = vec![1, 2, 3];
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Right(1),
EitherOrBoth::Right(2),
- EitherOrBoth::Right(3)
+ EitherOrBoth::Right(3),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn first_left_then_right() {
- let left: Vec<u32> = vec![1,2,3];
- let right: Vec<u32> = vec![4,5,6];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let left: Vec<u32> = vec![1, 2, 3];
+ let right: Vec<u32> = vec![4, 5, 6];
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Left(1),
EitherOrBoth::Left(2),
EitherOrBoth::Left(3),
EitherOrBoth::Right(4),
EitherOrBoth::Right(5),
- EitherOrBoth::Right(6)
+ EitherOrBoth::Right(6),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn first_right_then_left() {
- let left: Vec<u32> = vec![4,5,6];
- let right: Vec<u32> = vec![1,2,3];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let left: Vec<u32> = vec![4, 5, 6];
+ let right: Vec<u32> = vec![1, 2, 3];
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Right(1),
EitherOrBoth::Right(2),
EitherOrBoth::Right(3),
EitherOrBoth::Left(4),
EitherOrBoth::Left(5),
- EitherOrBoth::Left(6)
+ EitherOrBoth::Left(6),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn interspersed_left_and_right() {
- let left: Vec<u32> = vec![1,3,5];
- let right: Vec<u32> = vec![2,4,6];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let left: Vec<u32> = vec![1, 3, 5];
+ let right: Vec<u32> = vec![2, 4, 6];
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Left(1),
EitherOrBoth::Right(2),
EitherOrBoth::Left(3),
EitherOrBoth::Right(4),
EitherOrBoth::Left(5),
- EitherOrBoth::Right(6)
+ EitherOrBoth::Right(6),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
#[test]
fn overlapping_left_and_right() {
- let left: Vec<u32> = vec![1,3,4,6];
- let right: Vec<u32> = vec![2,3,4,5];
- let expected_result: Vec<EitherOrBoth<u32, u32>> = vec![
+ let left: Vec<u32> = vec![1, 3, 4, 6];
+ let right: Vec<u32> = vec![2, 3, 4, 5];
+ let expected_result: Vec<EitherOrBoth<u32>> = vec![
EitherOrBoth::Left(1),
EitherOrBoth::Right(2),
EitherOrBoth::Both(3, 3),
EitherOrBoth::Both(4, 4),
EitherOrBoth::Right(5),
- EitherOrBoth::Left(6)
+ EitherOrBoth::Left(6),
];
- let actual_result = merge_join_by(left, right, |l, r| l.cmp(r))
- .collect::<Vec<_>>();
+ let actual_result = merge_join_by(left, right, |l, r| l.cmp(r)).collect::<Vec<_>>();
assert_eq!(expected_result, actual_result);
}
diff --git a/vendor/itertools/tests/quick.rs b/vendor/itertools/tests/quick.rs
index c19af6c1e..6f45a63d0 100644
--- a/vendor/itertools/tests/quick.rs
+++ b/vendor/itertools/tests/quick.rs
@@ -3,34 +3,23 @@
//!
//! In particular we test the tedious size_hint and exact size correctness.
+#![allow(deprecated, unstable_name_collisions)]
+
+use itertools::free::{
+ cloned, enumerate, multipeek, peek_nth, put_back, put_back_n, rciter, zip, zip_eq,
+};
+use itertools::Itertools;
+use itertools::{iproduct, izip, multizip, EitherOrBoth};
use quickcheck as qc;
+use std::cmp::{max, min, Ordering};
+use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::num::Wrapping;
use std::ops::Range;
-use std::cmp::{max, min, Ordering};
-use std::collections::{HashMap, HashSet};
-use itertools::Itertools;
-use itertools::{
- multizip,
- EitherOrBoth,
- iproduct,
- izip,
-};
-use itertools::free::{
- cloned,
- enumerate,
- multipeek,
- peek_nth,
- put_back,
- put_back_n,
- rciter,
- zip,
- zip_eq,
-};
-use rand::Rng;
-use rand::seq::SliceRandom;
use quickcheck::TestResult;
+use rand::seq::SliceRandom;
+use rand::Rng;
/// Trait for size hint modifier types
trait HintKind: Copy + Send + qc::Arbitrary {
@@ -66,8 +55,10 @@ struct Inexact {
impl HintKind for Inexact {
fn loosen_bounds(&self, org_hint: (usize, Option<usize>)) -> (usize, Option<usize>) {
let (org_lower, org_upper) = org_hint;
- (org_lower.saturating_sub(self.underestimate),
- org_upper.and_then(move |x| x.checked_add(self.overestimate)))
+ (
+ org_lower.saturating_sub(self.underestimate),
+ org_upper.and_then(move |x| x.checked_add(self.overestimate)),
+ )
}
}
@@ -84,19 +75,15 @@ impl qc::Arbitrary for Inexact {
}
}
- fn shrink(&self) -> Box<dyn Iterator<Item=Self>> {
+ fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let underestimate_value = self.underestimate;
let overestimate_value = self.overestimate;
- Box::new(
- underestimate_value.shrink().flat_map(move |ue_value|
- overestimate_value.shrink().map(move |oe_value|
- Inexact {
- underestimate: ue_value,
- overestimate: oe_value,
- }
- )
- )
- )
+ Box::new(underestimate_value.shrink().flat_map(move |ue_value| {
+ overestimate_value.shrink().map(move |oe_value| Inexact {
+ underestimate: ue_value,
+ overestimate: oe_value,
+ })
+ }))
}
}
@@ -116,7 +103,9 @@ struct Iter<T, SK: HintKind = Inexact> {
hint_kind: SK,
}
-impl<T, HK> Iter<T, HK> where HK: HintKind
+impl<T, HK> Iter<T, HK>
+where
+ HK: HintKind,
{
fn new(it: Range<T>, hint_kind: HK) -> Self {
Iter {
@@ -128,64 +117,66 @@ impl<T, HK> Iter<T, HK> where HK: HintKind
}
impl<T, HK> Iterator for Iter<T, HK>
- where Range<T>: Iterator,
- <Range<T> as Iterator>::Item: Default,
- HK: HintKind,
+where
+ Range<T>: Iterator,
+ <Range<T> as Iterator>::Item: Default,
+ HK: HintKind,
{
type Item = <Range<T> as Iterator>::Item;
- fn next(&mut self) -> Option<Self::Item>
- {
+ fn next(&mut self) -> Option<Self::Item> {
let elt = self.iterator.next();
if elt.is_none() {
self.fuse_flag += 1;
// check fuse flag
if self.fuse_flag == 2 {
- return Some(Default::default())
+ return Some(Default::default());
}
}
elt
}
- fn size_hint(&self) -> (usize, Option<usize>)
- {
+ fn size_hint(&self) -> (usize, Option<usize>) {
let org_hint = self.iterator.size_hint();
self.hint_kind.loosen_bounds(org_hint)
}
}
impl<T, HK> DoubleEndedIterator for Iter<T, HK>
- where Range<T>: DoubleEndedIterator,
- <Range<T> as Iterator>::Item: Default,
- HK: HintKind
+where
+ Range<T>: DoubleEndedIterator,
+ <Range<T> as Iterator>::Item: Default,
+ HK: HintKind,
{
- fn next_back(&mut self) -> Option<Self::Item> { self.iterator.next_back() }
+ fn next_back(&mut self) -> Option<Self::Item> {
+ self.iterator.next_back()
+ }
}
-impl<T> ExactSizeIterator for Iter<T, Exact> where Range<T>: ExactSizeIterator,
+impl<T> ExactSizeIterator for Iter<T, Exact>
+where
+ Range<T>: ExactSizeIterator,
<Range<T> as Iterator>::Item: Default,
-{ }
+{
+}
impl<T, HK> qc::Arbitrary for Iter<T, HK>
- where T: qc::Arbitrary,
- HK: HintKind,
+where
+ T: qc::Arbitrary,
+ HK: HintKind,
{
- fn arbitrary<G: qc::Gen>(g: &mut G) -> Self
- {
+ fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
Iter::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g))
}
- fn shrink(&self) -> Box<dyn Iterator<Item=Iter<T, HK>>>
- {
+ fn shrink(&self) -> Box<dyn Iterator<Item = Iter<T, HK>>> {
let r = self.iterator.clone();
let hint_kind = self.hint_kind;
- Box::new(
- r.start.shrink().flat_map(move |a|
- r.end.shrink().map(move |b|
- Iter::new(a.clone()..b, hint_kind)
- )
- )
- )
+ Box::new(r.start.shrink().flat_map(move |a| {
+ r.end
+ .shrink()
+ .map(move |b| Iter::new(a.clone()..b, hint_kind))
+ }))
}
}
@@ -201,7 +192,10 @@ struct ShiftRange<HK = Inexact> {
hint_kind: HK,
}
-impl<HK> Iterator for ShiftRange<HK> where HK: HintKind {
+impl<HK> Iterator for ShiftRange<HK>
+where
+ HK: HintKind,
+{
type Item = Iter<i32, HK>;
fn next(&mut self) -> Option<Self::Item> {
@@ -219,10 +213,11 @@ impl<HK> Iterator for ShiftRange<HK> where HK: HintKind {
}
}
-impl ExactSizeIterator for ShiftRange<Exact> { }
+impl ExactSizeIterator for ShiftRange<Exact> {}
impl<HK> qc::Arbitrary for ShiftRange<HK>
- where HK: HintKind
+where
+ HK: HintKind,
{
fn arbitrary<G: qc::Gen>(g: &mut G) -> Self {
const MAX_STARTING_RANGE_DIFF: i32 = 32;
@@ -250,7 +245,7 @@ impl<HK> qc::Arbitrary for ShiftRange<HK>
fn correct_count<I, F>(get_it: F) -> bool
where
I: Iterator,
- F: Fn() -> I
+ F: Fn() -> I,
{
let mut counts = vec![get_it().count()];
@@ -276,7 +271,10 @@ where
for (i, returned_count) in counts.into_iter().enumerate() {
let actual_count = total_actual_count - i;
if actual_count != returned_count {
- println!("Total iterations: {} True count: {} returned count: {}", i, actual_count, returned_count);
+ println!(
+ "Total iterations: {} True count: {} returned count: {}",
+ i, actual_count, returned_count
+ );
return false;
}
@@ -299,12 +297,10 @@ fn correct_size_hint<I: Iterator>(mut it: I) -> bool {
// check all the size hints
for &(low, hi) in &hints {
true_count -= 1;
- if low > true_count ||
- (hi.is_some() && hi.unwrap() < true_count)
- {
+ if low > true_count || (hi.is_some() && hi.unwrap() < true_count) {
println!("True size: {:?}, size hint: {:?}", true_count, (low, hi));
//println!("All hints: {:?}", hints);
- return false
+ return false;
}
}
true
@@ -313,13 +309,19 @@ fn correct_size_hint<I: Iterator>(mut it: I) -> bool {
fn exact_size<I: ExactSizeIterator>(mut it: I) -> bool {
// check every iteration
let (mut low, mut hi) = it.size_hint();
- if Some(low) != hi { return false; }
+ if Some(low) != hi {
+ return false;
+ }
while let Some(_) = it.next() {
let (xlow, xhi) = it.size_hint();
- if low != xlow + 1 { return false; }
+ if low != xlow + 1 {
+ return false;
+ }
low = xlow;
hi = xhi;
- if Some(low) != hi { return false; }
+ if Some(low) != hi {
+ return false;
+ }
}
let (low, hi) = it.size_hint();
low == 0 && hi == Some(0)
@@ -329,13 +331,19 @@ fn exact_size<I: ExactSizeIterator>(mut it: I) -> bool {
fn exact_size_for_this<I: Iterator>(mut it: I) -> bool {
// check every iteration
let (mut low, mut hi) = it.size_hint();
- if Some(low) != hi { return false; }
+ if Some(low) != hi {
+ return false;
+ }
while let Some(_) = it.next() {
let (xlow, xhi) = it.size_hint();
- if low != xlow + 1 { return false; }
+ if low != xlow + 1 {
+ return false;
+ }
low = xlow;
hi = xhi;
- if Some(low) != hi { return false; }
+ if Some(low) != hi {
+ return false;
+ }
}
let (low, hi) = it.size_hint();
low == 0 && hi == Some(0)
@@ -751,6 +759,56 @@ quickcheck! {
}
quickcheck! {
+ fn correct_peek_nth(mut a: Vec<u16>) -> () {
+ let mut it = peek_nth(a.clone());
+ for start_pos in 0..a.len() + 2 {
+ for real_idx in start_pos..a.len() + 2 {
+ let peek_idx = real_idx - start_pos;
+ assert_eq!(it.peek_nth(peek_idx), a.get(real_idx));
+ assert_eq!(it.peek_nth_mut(peek_idx), a.get_mut(real_idx));
+ }
+ assert_eq!(it.next(), a.get(start_pos).copied());
+ }
+ }
+
+ fn peek_nth_mut_replace(a: Vec<u16>, b: Vec<u16>) -> () {
+ let mut it = peek_nth(a.iter());
+ for i in 0..a.len().min(b.len()) {
+ *it.peek_nth_mut(i).unwrap() = &b[i];
+ }
+ for i in 0..a.len() {
+ assert_eq!(it.next().unwrap(), b.get(i).unwrap_or(&a[i]));
+ }
+ assert_eq!(it.next(), None);
+ assert_eq!(it.next(), None);
+ }
+
+ fn peek_nth_next_if(a: Vec<u8>) -> () {
+ let mut it = peek_nth(a.clone());
+ for (idx, mut value) in a.iter().copied().enumerate() {
+ let should_be_none = it.next_if(|x| x != &value);
+ assert_eq!(should_be_none, None);
+ if value % 5 == 0 {
+ // Sometimes, peek up to 3 further.
+ let n = value as usize % 3;
+ let nth = it.peek_nth(n);
+ assert_eq!(nth, a.get(idx + n));
+ } else if value % 5 == 1 {
+ // Sometimes, peek next element mutably.
+ if let Some(v) = it.peek_mut() {
+ *v = v.wrapping_sub(1);
+ let should_be_none = it.next_if_eq(&value);
+ assert_eq!(should_be_none, None);
+ value = value.wrapping_sub(1);
+ }
+ }
+ let eq = it.next_if_eq(&value);
+ assert_eq!(eq, Some(value));
+ }
+ }
+}
+
+quickcheck! {
fn dedup_via_coalesce(a: Vec<i32>) -> bool {
let mut b = a.clone();
b.dedup();
@@ -895,8 +953,31 @@ quickcheck! {
}
quickcheck! {
- fn size_combinations(it: Iter<i16>) -> bool {
- correct_size_hint(it.tuple_combinations::<(_, _)>())
+ fn size_combinations(a: Iter<i16>) -> bool {
+ let it = a.clone().tuple_combinations::<(_, _)>();
+ correct_size_hint(it.clone()) && it.count() == binomial(a.count(), 2)
+ }
+
+ fn exact_size_combinations_1(a: Vec<u8>) -> bool {
+ let it = a.iter().tuple_combinations::<(_,)>();
+ exact_size_for_this(it.clone()) && it.count() == binomial(a.len(), 1)
+ }
+ fn exact_size_combinations_2(a: Vec<u8>) -> bool {
+ let it = a.iter().tuple_combinations::<(_, _)>();
+ exact_size_for_this(it.clone()) && it.count() == binomial(a.len(), 2)
+ }
+ fn exact_size_combinations_3(mut a: Vec<u8>) -> bool {
+ a.truncate(15);
+ let it = a.iter().tuple_combinations::<(_, _, _)>();
+ exact_size_for_this(it.clone()) && it.count() == binomial(a.len(), 3)
+ }
+}
+
+fn binomial(n: usize, k: usize) -> usize {
+ if k > n {
+ 0
+ } else {
+ (n - k + 1..=n).product::<usize>() / (1..=k).product::<usize>()
}
}
@@ -1111,6 +1192,10 @@ quickcheck! {
true
}
+ fn circular_tuple_windows_exact_size(a: Vec<u8>) -> bool {
+ exact_size(a.iter().circular_tuple_windows::<(_, _, _, _)>())
+ }
+
fn equal_tuple_windows_1(a: Vec<u8>) -> bool {
let x = a.windows(1).map(|s| (&s[0], ));
let y = a.iter().tuple_windows::<(_,)>();
@@ -1135,6 +1220,14 @@ quickcheck! {
itertools::equal(x, y)
}
+ fn tuple_windows_exact_size_1(a: Vec<u8>) -> bool {
+ exact_size(a.iter().tuple_windows::<(_,)>())
+ }
+
+ fn tuple_windows_exact_size_4(a: Vec<u8>) -> bool {
+ exact_size(a.iter().tuple_windows::<(_, _, _, _)>())
+ }
+
fn equal_tuples_1(a: Vec<u8>) -> bool {
let x = a.chunks(1).map(|s| (&s[0], ));
let y = a.iter().tuples::<(_,)>();
@@ -1166,6 +1259,18 @@ quickcheck! {
assert_eq!(buffer.len(), a.len() % 4);
exact_size(buffer)
}
+
+ fn tuples_size_hint_inexact(a: Iter<u8>) -> bool {
+ correct_size_hint(a.clone().tuples::<(_,)>())
+ && correct_size_hint(a.clone().tuples::<(_, _)>())
+ && correct_size_hint(a.tuples::<(_, _, _, _)>())
+ }
+
+ fn tuples_size_hint_exact(a: Iter<u8, Exact>) -> bool {
+ exact_size(a.clone().tuples::<(_,)>())
+ && exact_size(a.clone().tuples::<(_, _)>())
+ && exact_size(a.tuples::<(_, _, _, _)>())
+ }
}
// with_position
@@ -1332,7 +1437,7 @@ quickcheck! {
Some(acc.unwrap_or(0) + val)
}
});
-
+
let group_map_lookup = a.iter()
.map(|&b| b as u64)
.map(|i| (i % modulo, i))
@@ -1352,7 +1457,7 @@ quickcheck! {
for m in 0..modulo {
assert_eq!(
- lookup.get(&m).copied(),
+ lookup.get(&m).copied(),
a.iter()
.map(|&b| b as u64)
.filter(|&val| val % modulo == m)
@@ -1367,6 +1472,35 @@ quickcheck! {
}
}
+ fn correct_grouping_map_by_fold_with_modulo_key(a: Vec<u8>, modulo: u8) -> () {
+ #[derive(Debug, Default, PartialEq)]
+ struct Accumulator {
+ acc: u64,
+ }
+
+ let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
+ let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
+ .into_grouping_map_by(|i| i % modulo)
+ .fold_with(|_key, _val| Default::default(), |Accumulator { acc }, &key, val| {
+ assert!(val % modulo == key);
+ let acc = acc + val;
+ Accumulator { acc }
+ });
+
+ let group_map_lookup = a.iter()
+ .map(|&b| b as u64)
+ .map(|i| (i % modulo, i))
+ .into_group_map()
+ .into_iter()
+ .map(|(key, vals)| (key, vals.into_iter().sum())).map(|(key, acc)| (key,Accumulator { acc }))
+ .collect::<HashMap<_,_>>();
+ assert_eq!(lookup, group_map_lookup);
+
+ for (&key, &Accumulator { acc: sum }) in lookup.iter() {
+ assert_eq!(sum, a.iter().map(|&b| b as u64).filter(|&val| val % modulo == key).sum::<u64>());
+ }
+ }
+
fn correct_grouping_map_by_fold_modulo_key(a: Vec<u8>, modulo: u8) -> () {
let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
@@ -1472,7 +1606,7 @@ quickcheck! {
assert_eq!(Some(max), a.iter().copied().filter(|&val| val % modulo == key).max_by_key(|&val| val));
}
}
-
+
fn correct_grouping_map_by_min_modulo_key(a: Vec<u8>, modulo: u8) -> () {
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).min();
@@ -1523,7 +1657,7 @@ quickcheck! {
assert_eq!(Some(min), a.iter().copied().filter(|&val| val % modulo == key).min_by_key(|&val| val));
}
}
-
+
fn correct_grouping_map_by_minmax_modulo_key(a: Vec<u8>, modulo: u8) -> () {
let modulo = if modulo == 0 { 1 } else { modulo }; // Avoid `% 0`
let lookup = a.iter().copied().into_grouping_map_by(|i| i % modulo).minmax();
@@ -1636,7 +1770,7 @@ quickcheck! {
.min_by(|_, _, _| Ordering::Equal);
assert_eq!(lookup[&0], 0);
-
+
let lookup = (0..=10)
.into_grouping_map_by(|_| 0)
.minmax_by(|_, _, _| Ordering::Equal);
@@ -1694,12 +1828,10 @@ quickcheck! {
}
}
-
-fn is_fused<I: Iterator>(mut it: I) -> bool
-{
+fn is_fused<I: Iterator>(mut it: I) -> bool {
for _ in it.by_ref() {}
- for _ in 0..10{
- if it.next().is_some(){
+ for _ in 0..10 {
+ if it.next().is_some() {
return false;
}
}
@@ -1740,7 +1872,7 @@ quickcheck! {
!is_fused(a.clone().interleave_shortest(b.clone())) &&
is_fused(a.fuse().interleave_shortest(b.fuse()))
}
-
+
fn fused_product(a: Iter<i16>, b: Iter<i16>) -> bool
{
is_fused(a.fuse().cartesian_product(b.fuse()))
diff --git a/vendor/itertools/tests/specializations.rs b/vendor/itertools/tests/specializations.rs
index 057e11c9f..fe14234d6 100644
--- a/vendor/itertools/tests/specializations.rs
+++ b/vendor/itertools/tests/specializations.rs
@@ -1,6 +1,8 @@
+#![allow(unstable_name_collisions)]
+
use itertools::Itertools;
+use quickcheck::{quickcheck, TestResult};
use std::fmt::Debug;
-use quickcheck::quickcheck;
struct Unspecialized<I>(I);
impl<I> Iterator for Unspecialized<I>
@@ -15,24 +17,25 @@ where
}
}
-macro_rules! check_specialized {
- ($src:expr, |$it:pat| $closure:expr) => {
- let $it = $src.clone();
- let v1 = $closure;
-
- let $it = Unspecialized($src.clone());
- let v2 = $closure;
-
- assert_eq!(v1, v2);
- }
-}
-
-fn test_specializations<IterItem, Iter>(
- it: &Iter,
-) where
+fn test_specializations<IterItem, Iter>(it: &Iter)
+where
IterItem: Eq + Debug + Clone,
Iter: Iterator<Item = IterItem> + Clone,
{
+ macro_rules! check_specialized {
+ ($src:expr, |$it:pat| $closure:expr) => {
+ // Many iterators special-case the first elements, so we test specializations for iterators that have already been advanced.
+ let mut src = $src.clone();
+ for _ in 0..5 {
+ let $it = src.clone();
+ let v1 = $closure;
+ let $it = Unspecialized(src.clone());
+ let v2 = $closure;
+ assert_eq!(v1, v2);
+ src.next();
+ }
+ }
+ }
check_specialized!(it, |i| i.count());
check_specialized!(it, |i| i.last());
check_specialized!(it, |i| i.collect::<Vec<_>>());
@@ -50,7 +53,7 @@ fn test_specializations<IterItem, Iter>(
let first = i.next();
let all_result = i.all(|x| {
parameters_from_all.push(x.clone());
- Some(x)==first
+ Some(x) == first
});
(parameters_from_all, all_result)
});
@@ -73,9 +76,188 @@ fn test_specializations<IterItem, Iter>(
}
quickcheck! {
+ fn interleave(v: Vec<u8>, w: Vec<u8>) -> () {
+ test_specializations(&v.iter().interleave(w.iter()));
+ }
+
+ fn interleave_shortest(v: Vec<u8>, w: Vec<u8>) -> () {
+ test_specializations(&v.iter().interleave_shortest(w.iter()));
+ }
+
+ fn batching(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().batching(Iterator::next));
+ }
+
+ fn tuple_windows(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().tuple_windows::<(_,)>());
+ test_specializations(&v.iter().tuple_windows::<(_, _)>());
+ test_specializations(&v.iter().tuple_windows::<(_, _, _)>());
+ }
+
+ fn circular_tuple_windows(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().circular_tuple_windows::<(_,)>());
+ test_specializations(&v.iter().circular_tuple_windows::<(_, _)>());
+ test_specializations(&v.iter().circular_tuple_windows::<(_, _, _)>());
+ }
+
+ fn tuples(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().tuples::<(_,)>());
+ test_specializations(&v.iter().tuples::<(_, _)>());
+ test_specializations(&v.iter().tuples::<(_, _, _)>());
+ }
+
+ fn cartesian_product(a: Vec<u8>, b: Vec<u8>) -> TestResult {
+ if a.len() * b.len() > 100 {
+ return TestResult::discard();
+ }
+ test_specializations(&a.iter().cartesian_product(&b));
+ TestResult::passed()
+ }
+
+ #[ignore] // It currently fails because `MultiProduct` is not fused.
+ fn multi_cartesian_product(a: Vec<u8>, b: Vec<u8>, c: Vec<u8>) -> TestResult {
+ if a.len() * b.len() * c.len() > 100 {
+ return TestResult::discard();
+ }
+ test_specializations(&vec![a, b, c].into_iter().multi_cartesian_product());
+ TestResult::passed()
+ }
+
+ fn coalesce(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().coalesce(|x, y| if x == y { Ok(x) } else { Err((x, y)) }))
+ }
+
+ fn dedup(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().dedup())
+ }
+
+ fn dedup_by(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().dedup_by(PartialOrd::ge))
+ }
+
+ fn dedup_with_count(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().dedup_with_count())
+ }
+
+ fn dedup_by_with_count(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().dedup_by_with_count(PartialOrd::ge))
+ }
+
+ fn duplicates(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().duplicates());
+ }
+
+ fn duplicates_by(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().duplicates_by(|x| *x % 10));
+ }
+
+ fn unique(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().unique());
+ }
+
+ fn unique_by(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().unique_by(|x| *x % 50));
+ }
+
+ fn take_while_inclusive(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().copied().take_while_inclusive(|&x| x < 100));
+ }
+
+ fn while_some(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().map(|&x| if x < 100 { Some(2 * x) } else { None }).while_some());
+ }
+
+ fn pad_using(v: Vec<u8>) -> () {
+ use std::convert::TryFrom;
+ test_specializations(&v.iter().copied().pad_using(10, |i| u8::try_from(5 * i).unwrap_or(u8::MAX)));
+ }
+
+ fn with_position(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().with_position());
+ }
+
+ fn positions(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().positions(|x| x % 5 == 0));
+ }
+
+ fn update(v: Vec<u8>) -> () {
+ test_specializations(&v.iter().copied().update(|x| *x = x.wrapping_mul(7)));
+ }
+
+ fn tuple_combinations(v: Vec<u8>) -> TestResult {
+ if v.len() > 10 {
+ return TestResult::discard();
+ }
+ test_specializations(&v.iter().tuple_combinations::<(_,)>());
+ test_specializations(&v.iter().tuple_combinations::<(_, _)>());
+ test_specializations(&v.iter().tuple_combinations::<(_, _, _)>());
+ TestResult::passed()
+ }
+
fn intersperse(v: Vec<u8>) -> () {
test_specializations(&v.into_iter().intersperse(0));
}
+
+ fn intersperse_with(v: Vec<u8>) -> () {
+ test_specializations(&v.into_iter().intersperse_with(|| 0));
+ }
+
+ fn combinations(a: Vec<u8>, n: u8) -> TestResult {
+ if n > 3 || a.len() > 8 {
+ return TestResult::discard();
+ }
+ test_specializations(&a.iter().combinations(n as usize));
+ TestResult::passed()
+ }
+
+ fn combinations_with_replacement(a: Vec<u8>, n: u8) -> TestResult {
+ if n > 3 || a.len() > 7 {
+ return TestResult::discard();
+ }
+ test_specializations(&a.iter().combinations_with_replacement(n as usize));
+ TestResult::passed()
+ }
+
+ fn permutations(a: Vec<u8>, n: u8) -> TestResult {
+ if n > 3 || a.len() > 8 {
+ return TestResult::discard();
+ }
+ test_specializations(&a.iter().permutations(n as usize));
+ TestResult::passed()
+ }
+
+ fn powerset(a: Vec<u8>) -> TestResult {
+ if a.len() > 6 {
+ return TestResult::discard();
+ }
+ test_specializations(&a.iter().powerset());
+ TestResult::passed()
+ }
+
+ fn zip_longest(a: Vec<u8>, b: Vec<u8>) -> () {
+ test_specializations(&a.into_iter().zip_longest(b))
+ }
+
+ fn zip_eq(a: Vec<u8>) -> () {
+ test_specializations(&a.iter().zip_eq(a.iter().rev()))
+ }
+
+ fn multizip(a: Vec<u8>) -> () {
+ let its = (a.iter(), a.iter().rev(), a.iter().take(50));
+ test_specializations(&itertools::multizip(its));
+ }
+
+ fn izip(a: Vec<u8>, b: Vec<u8>) -> () {
+ test_specializations(&itertools::izip!(b.iter(), a, b.iter().rev()));
+ }
+
+ fn iproduct(a: Vec<u8>, b: Vec<u8>, c: Vec<u8>) -> TestResult {
+ if a.len() * b.len() * c.len() > 200 {
+ return TestResult::discard();
+ }
+ test_specializations(&itertools::iproduct!(a, b.iter(), c));
+ TestResult::passed()
+ }
}
quickcheck! {
@@ -85,11 +267,85 @@ quickcheck! {
pb.put_back(1);
test_specializations(&pb);
}
+
+ fn put_back_n(v: Vec<u8>, n: u8) -> () {
+ let mut it = itertools::put_back_n(v);
+ for k in 0..n {
+ it.put_back(k);
+ }
+ test_specializations(&it);
+ }
+
+ fn multipeek(v: Vec<u8>, n: u8) -> () {
+ let mut it = v.into_iter().multipeek();
+ for _ in 0..n {
+ it.peek();
+ }
+ test_specializations(&it);
+ }
+
+ fn peek_nth_with_peek(v: Vec<u8>, n: u8) -> () {
+ let mut it = itertools::peek_nth(v);
+ for _ in 0..n {
+ it.peek();
+ }
+ test_specializations(&it);
+ }
+
+ fn peek_nth_with_peek_nth(v: Vec<u8>, n: u8) -> () {
+ let mut it = itertools::peek_nth(v);
+ it.peek_nth(n as usize);
+ test_specializations(&it);
+ }
+
+ fn peek_nth_with_peek_mut(v: Vec<u8>, n: u8) -> () {
+ let mut it = itertools::peek_nth(v);
+ for _ in 0..n {
+ if let Some(x) = it.peek_mut() {
+ *x = x.wrapping_add(50);
+ }
+ }
+ test_specializations(&it);
+ }
+
+ fn peek_nth_with_peek_nth_mut(v: Vec<u8>, n: u8) -> () {
+ let mut it = itertools::peek_nth(v);
+ if let Some(x) = it.peek_nth_mut(n as usize) {
+ *x = x.wrapping_add(50);
+ }
+ test_specializations(&it);
+ }
}
quickcheck! {
- fn merge_join_by_qc(i1: Vec<usize>, i2: Vec<usize>) -> () {
- test_specializations(&i1.into_iter().merge_join_by(i2.into_iter(), std::cmp::Ord::cmp));
+ fn merge(a: Vec<u8>, b: Vec<u8>) -> () {
+ test_specializations(&a.into_iter().merge(b))
+ }
+
+ fn merge_by(a: Vec<u8>, b: Vec<u8>) -> () {
+ test_specializations(&a.into_iter().merge_by(b, PartialOrd::ge))
+ }
+
+ fn merge_join_by_ordering(i1: Vec<u8>, i2: Vec<u8>) -> () {
+ test_specializations(&i1.into_iter().merge_join_by(i2, Ord::cmp));
+ }
+
+ fn merge_join_by_bool(i1: Vec<u8>, i2: Vec<u8>) -> () {
+ test_specializations(&i1.into_iter().merge_join_by(i2, PartialOrd::ge));
+ }
+
+ fn kmerge(a: Vec<i8>, b: Vec<i8>, c: Vec<i8>) -> () {
+ test_specializations(&vec![a, b, c]
+ .into_iter()
+ .map(|v| v.into_iter().sorted())
+ .kmerge());
+ }
+
+ fn kmerge_by(a: Vec<i8>, b: Vec<i8>, c: Vec<i8>) -> () {
+ test_specializations(&vec![a, b, c]
+ .into_iter()
+ .map(|v| v.into_iter().sorted_by_key(|a| a.abs()))
+ .kmerge_by(|a, b| a.abs() < b.abs()));
}
}
@@ -97,15 +353,27 @@ quickcheck! {
fn map_into(v: Vec<u8>) -> () {
test_specializations(&v.into_iter().map_into::<u32>());
}
-}
-quickcheck! {
fn map_ok(v: Vec<Result<u8, char>>) -> () {
test_specializations(&v.into_iter().map_ok(|u| u.checked_add(1)));
}
+
+ fn filter_ok(v: Vec<Result<u8, char>>) -> () {
+ test_specializations(&v.into_iter().filter_ok(|&i| i < 20));
+ }
+
+ fn filter_map_ok(v: Vec<Result<u8, char>>) -> () {
+ test_specializations(&v.into_iter().filter_map_ok(|i| if i < 20 { Some(i * 2) } else { None }));
+ }
+
+ // `Option<u8>` because `Vec<u8>` would be very slow!! And we can't give `[u8; 3]`.
+ fn flatten_ok(v: Vec<Result<Option<u8>, char>>) -> () {
+ test_specializations(&v.into_iter().flatten_ok());
+ }
}
quickcheck! {
+ // TODO Replace this function by a normal call to test_specializations
fn process_results(v: Vec<Result<u8, u8>>) -> () {
helper(v.iter().copied());
helper(v.iter().copied().filter(Result::is_ok));
diff --git a/vendor/itertools/tests/test_core.rs b/vendor/itertools/tests/test_core.rs
index df94eb665..c624a7e33 100644
--- a/vendor/itertools/tests/test_core.rs
+++ b/vendor/itertools/tests/test_core.rs
@@ -4,18 +4,19 @@
//! option. This file may not be copied, modified, or distributed
//! except according to those terms.
#![no_std]
+#![allow(deprecated)]
-use core::iter;
-use itertools as it;
-use crate::it::Itertools;
+use crate::it::chain;
+use crate::it::free::put_back;
use crate::it::interleave;
use crate::it::intersperse;
use crate::it::intersperse_with;
-use crate::it::multizip;
-use crate::it::free::put_back;
use crate::it::iproduct;
use crate::it::izip;
-use crate::it::chain;
+use crate::it::multizip;
+use crate::it::Itertools;
+use core::iter;
+use itertools as it;
#[test]
fn product2() {
@@ -34,13 +35,12 @@ fn product_temporary() {
for (_x, _y, _z) in iproduct!(
[0, 1, 2].iter().cloned(),
[0, 1, 2].iter().cloned(),
- [0, 1, 2].iter().cloned())
- {
+ [0, 1, 2].iter().cloned()
+ ) {
// ok
}
}
-
#[test]
fn izip_macro() {
let mut zip = izip!(2..3);
@@ -61,7 +61,7 @@ fn izip_macro() {
#[test]
fn izip2() {
let _zip1: iter::Zip<_, _> = izip!(1.., 2..);
- let _zip2: iter::Zip<_, _> = izip!(1.., 2.., );
+ let _zip2: iter::Zip<_, _> = izip!(1.., 2..,);
}
#[test]
@@ -109,7 +109,7 @@ fn chain_macro() {
#[test]
fn chain2() {
let _ = chain!(1.., 2..);
- let _ = chain!(1.., 2.., );
+ let _ = chain!(1.., 2..,);
}
#[test]
@@ -127,7 +127,7 @@ fn write_to() {
#[test]
fn test_interleave() {
- let xs: [u8; 0] = [];
+ let xs: [u8; 0] = [];
let ys = [7u8, 9, 8, 10];
let zs = [2u8, 77];
let it = interleave(xs.iter(), ys.iter());
@@ -211,7 +211,6 @@ fn merge() {
it::assert_equal((0..10).step(2).merge((1..10).step(2)), 0..10);
}
-
#[test]
fn repeatn() {
let s = "α";
@@ -231,29 +230,33 @@ fn count_clones() {
use core::cell::Cell;
#[derive(PartialEq, Debug)]
struct Foo {
- n: Cell<usize>
+ n: Cell<usize>,
}
- impl Clone for Foo
- {
- fn clone(&self) -> Self
- {
+ impl Clone for Foo {
+ fn clone(&self) -> Self {
let n = self.n.get();
self.n.set(n + 1);
- Foo { n: Cell::new(n + 1) }
+ Foo {
+ n: Cell::new(n + 1),
+ }
}
}
-
for n in 0..10 {
- let f = Foo{n: Cell::new(0)};
+ let f = Foo { n: Cell::new(0) };
let it = it::repeat_n(f, n);
// drain it
let last = it.last();
if n == 0 {
assert_eq!(last, None);
} else {
- assert_eq!(last, Some(Foo{n: Cell::new(n - 1)}));
+ assert_eq!(
+ last,
+ Some(Foo {
+ n: Cell::new(n - 1)
+ })
+ );
}
}
}
@@ -285,16 +288,36 @@ fn tree_fold1() {
#[test]
fn exactly_one() {
assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
- assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
- assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
- assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
+ assert!((0..10)
+ .filter(|&x| x > 1 && x < 4)
+ .exactly_one()
+ .unwrap_err()
+ .eq(2..4));
+ assert!((0..10)
+ .filter(|&x| x > 1 && x < 5)
+ .exactly_one()
+ .unwrap_err()
+ .eq(2..5));
+ assert!((0..10)
+ .filter(|&_| false)
+ .exactly_one()
+ .unwrap_err()
+ .eq(0..0));
}
#[test]
fn at_most_one() {
assert_eq!((0..10).filter(|&x| x == 2).at_most_one().unwrap(), Some(2));
- assert!((0..10).filter(|&x| x > 1 && x < 4).at_most_one().unwrap_err().eq(2..4));
- assert!((0..10).filter(|&x| x > 1 && x < 5).at_most_one().unwrap_err().eq(2..5));
+ assert!((0..10)
+ .filter(|&x| x > 1 && x < 4)
+ .at_most_one()
+ .unwrap_err()
+ .eq(2..4));
+ assert!((0..10)
+ .filter(|&x| x > 1 && x < 5)
+ .at_most_one()
+ .unwrap_err()
+ .eq(2..5));
assert_eq!((0..10).filter(|&_| false).at_most_one().unwrap(), None);
}
diff --git a/vendor/itertools/tests/test_std.rs b/vendor/itertools/tests/test_std.rs
index 77207d87e..732be7b7d 100644
--- a/vendor/itertools/tests/test_std.rs
+++ b/vendor/itertools/tests/test_std.rs
@@ -1,19 +1,25 @@
-use quickcheck as qc;
-use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng};
-use rand::{seq::SliceRandom, thread_rng};
-use std::{cmp::min, fmt::Debug, marker::PhantomData};
-use itertools as it;
-use crate::it::Itertools;
-use crate::it::ExactlyOneError;
-use crate::it::multizip;
-use crate::it::multipeek;
-use crate::it::peek_nth;
-use crate::it::free::rciter;
-use crate::it::free::put_back_n;
-use crate::it::FoldWhile;
+#![allow(unstable_name_collisions)]
+
use crate::it::cloned;
+use crate::it::free::put_back_n;
+use crate::it::free::rciter;
use crate::it::iproduct;
use crate::it::izip;
+use crate::it::multipeek;
+use crate::it::multizip;
+use crate::it::peek_nth;
+use crate::it::ExactlyOneError;
+use crate::it::FoldWhile;
+use crate::it::Itertools;
+use itertools as it;
+use quickcheck as qc;
+use rand::{
+ distributions::{Distribution, Standard},
+ rngs::StdRng,
+ Rng, SeedableRng,
+};
+use rand::{seq::SliceRandom, thread_rng};
+use std::{cmp::min, fmt::Debug, marker::PhantomData};
#[test]
fn product3() {
@@ -27,9 +33,7 @@ fn product3() {
}
}
}
- for (_, _, _, _) in iproduct!(0..3, 0..2, 0..2, 0..3) {
- /* test compiles */
- }
+ for (_, _, _, _) in iproduct!(0..3, 0..2, 0..2, 0..3) { /* test compiles */ }
}
#[test]
@@ -62,9 +66,15 @@ fn duplicates_by() {
let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
let ys = ["aa", "bbbb", "cccc"];
it::assert_equal(ys.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()));
- it::assert_equal(ys.iter(), xs.iter().rev().duplicates_by(|x| x[..2].to_string()).rev());
+ it::assert_equal(
+ ys.iter(),
+ xs.iter().rev().duplicates_by(|x| x[..2].to_string()).rev(),
+ );
let ys_rev = ["ccc", "aa", "bbbbb"];
- it::assert_equal(ys_rev.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()).rev());
+ it::assert_equal(
+ ys_rev.iter(),
+ xs.iter().duplicates_by(|x| x[..2].to_string()).rev(),
+ );
}
#[test]
@@ -86,7 +96,10 @@ fn duplicates() {
let xs = vec![0, 1, 2, 1, 2];
let ys = vec![1, 2];
assert_eq!(ys, xs.iter().duplicates().cloned().collect_vec());
- assert_eq!(ys, xs.iter().rev().duplicates().rev().cloned().collect_vec());
+ assert_eq!(
+ ys,
+ xs.iter().rev().duplicates().rev().cloned().collect_vec()
+ );
let ys_rev = vec![2, 1];
assert_eq!(ys_rev, xs.iter().duplicates().rev().cloned().collect_vec());
}
@@ -96,9 +109,15 @@ fn unique_by() {
let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
let ys = ["aaa", "bbbbb", "ccc"];
it::assert_equal(ys.iter(), xs.iter().unique_by(|x| x[..2].to_string()));
- it::assert_equal(ys.iter(), xs.iter().rev().unique_by(|x| x[..2].to_string()).rev());
+ it::assert_equal(
+ ys.iter(),
+ xs.iter().rev().unique_by(|x| x[..2].to_string()).rev(),
+ );
let ys_rev = ["cccc", "aaaaa", "bbbb"];
- it::assert_equal(ys_rev.iter(), xs.iter().unique_by(|x| x[..2].to_string()).rev());
+ it::assert_equal(
+ ys_rev.iter(),
+ xs.iter().unique_by(|x| x[..2].to_string()).rev(),
+ );
}
#[test]
@@ -149,13 +168,13 @@ fn dedup() {
#[test]
fn coalesce() {
let data = vec![-1., -2., -3., 3., 1., 0., -1.];
- let it = data.iter().cloned().coalesce(|x, y|
+ let it = data.iter().cloned().coalesce(|x, y| {
if (x >= 0.) == (y >= 0.) {
Ok(x + y)
} else {
Err((x, y))
}
- );
+ });
itertools::assert_equal(it.clone(), vec![-6., 4., -1.]);
assert_eq!(
it.fold(vec![], |mut v, n| {
@@ -168,17 +187,37 @@ fn coalesce() {
#[test]
fn dedup_by() {
- let xs = [(0, 0), (0, 1), (1, 1), (2, 1), (0, 2), (3, 1), (0, 3), (1, 3)];
+ let xs = [
+ (0, 0),
+ (0, 1),
+ (1, 1),
+ (2, 1),
+ (0, 2),
+ (3, 1),
+ (0, 3),
+ (1, 3),
+ ];
let ys = [(0, 0), (0, 1), (0, 2), (3, 1), (0, 3)];
- it::assert_equal(ys.iter(), xs.iter().dedup_by(|x, y| x.1==y.1));
+ it::assert_equal(ys.iter(), xs.iter().dedup_by(|x, y| x.1 == y.1));
let xs = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)];
let ys = [(0, 1)];
- it::assert_equal(ys.iter(), xs.iter().dedup_by(|x, y| x.0==y.0));
-
- let xs = [(0, 0), (0, 1), (1, 1), (2, 1), (0, 2), (3, 1), (0, 3), (1, 3)];
+ it::assert_equal(ys.iter(), xs.iter().dedup_by(|x, y| x.0 == y.0));
+
+ let xs = [
+ (0, 0),
+ (0, 1),
+ (1, 1),
+ (2, 1),
+ (0, 2),
+ (3, 1),
+ (0, 3),
+ (1, 3),
+ ];
let ys = [(0, 0), (0, 1), (0, 2), (3, 1), (0, 3)];
let mut xs_d = Vec::new();
- xs.iter().dedup_by(|x, y| x.1==y.1).fold((), |(), &elt| xs_d.push(elt));
+ xs.iter()
+ .dedup_by(|x, y| x.1 == y.1)
+ .fold((), |(), &elt| xs_d.push(elt));
assert_eq!(&xs_d, &ys);
}
@@ -195,18 +234,38 @@ fn dedup_with_count() {
it::assert_equal(ys.iter().cloned(), xs.iter().dedup_with_count());
}
-
#[test]
fn dedup_by_with_count() {
- let xs = [(0, 0), (0, 1), (1, 1), (2, 1), (0, 2), (3, 1), (0, 3), (1, 3)];
- let ys = [(1, &(0, 0)), (3, &(0, 1)), (1, &(0, 2)), (1, &(3, 1)), (2, &(0, 3))];
+ let xs = [
+ (0, 0),
+ (0, 1),
+ (1, 1),
+ (2, 1),
+ (0, 2),
+ (3, 1),
+ (0, 3),
+ (1, 3),
+ ];
+ let ys = [
+ (1, &(0, 0)),
+ (3, &(0, 1)),
+ (1, &(0, 2)),
+ (1, &(3, 1)),
+ (2, &(0, 3)),
+ ];
- it::assert_equal(ys.iter().cloned(), xs.iter().dedup_by_with_count(|x, y| x.1==y.1));
+ it::assert_equal(
+ ys.iter().cloned(),
+ xs.iter().dedup_by_with_count(|x, y| x.1 == y.1),
+ );
let xs = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)];
- let ys = [( 5, &(0, 1))];
+ let ys = [(5, &(0, 1))];
- it::assert_equal(ys.iter().cloned(), xs.iter().dedup_by_with_count(|x, y| x.0==y.0));
+ it::assert_equal(
+ ys.iter().cloned(),
+ xs.iter().dedup_by_with_count(|x, y| x.0 == y.0),
+ );
}
#[test]
@@ -227,7 +286,7 @@ fn all_equal_value() {
assert_eq!("AABBCCC".chars().all_equal_value(), Err(Some(('A', 'B'))));
assert_eq!("AAAAAAA".chars().all_equal_value(), Ok('A'));
{
- let mut it = [1,2,3].iter().copied();
+ let mut it = [1, 2, 3].iter().copied();
let result = it.all_equal_value();
assert_eq!(result, Err(Some((1, 2))));
let remaining = it.next();
@@ -256,7 +315,7 @@ fn test_put_back_n() {
#[test]
fn tee() {
- let xs = [0, 1, 2, 3];
+ let xs = [0, 1, 2, 3];
let (mut t1, mut t2) = xs.iter().cloned().tee();
assert_eq!(t1.next(), Some(0));
assert_eq!(t2.next(), Some(0));
@@ -280,7 +339,6 @@ fn tee() {
it::assert_equal(t1.zip(t2), xs.iter().cloned().zip(xs.iter().cloned()));
}
-
#[test]
fn test_rciter() {
let xs = [0, 1, 1, 1, 2, 1, 3, 5, 6];
@@ -304,19 +362,19 @@ fn test_rciter() {
#[allow(deprecated)]
#[test]
fn trait_pointers() {
- struct ByRef<'r, I: ?Sized>(&'r mut I) ;
+ struct ByRef<'r, I: ?Sized>(&'r mut I);
- impl<'r, X, I: ?Sized> Iterator for ByRef<'r, I> where
- I: 'r + Iterator<Item=X>
+ impl<'r, X, I: ?Sized> Iterator for ByRef<'r, I>
+ where
+ I: 'r + Iterator<Item = X>,
{
type Item = X;
- fn next(&mut self) -> Option<Self::Item>
- {
+ fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
- let mut it = Box::new(0..10) as Box<dyn Iterator<Item=i32>>;
+ let mut it = Box::new(0..10) as Box<dyn Iterator<Item = i32>>;
assert_eq!(it.next(), Some(0));
{
@@ -336,9 +394,16 @@ fn trait_pointers() {
#[test]
fn merge_by() {
- let odd : Vec<(u32, &str)> = vec![(1, "hello"), (3, "world"), (5, "!")];
+ let odd: Vec<(u32, &str)> = vec![(1, "hello"), (3, "world"), (5, "!")];
let even = vec![(2, "foo"), (4, "bar"), (6, "baz")];
- let expected = vec![(1, "hello"), (2, "foo"), (3, "world"), (4, "bar"), (5, "!"), (6, "baz")];
+ let expected = vec![
+ (1, "hello"),
+ (2, "foo"),
+ (3, "world"),
+ (4, "bar"),
+ (5, "!"),
+ (6, "baz"),
+ ];
let results = odd.iter().merge_by(even.iter(), |a, b| a.0 <= b.0);
it::assert_equal(results, expected.iter());
}
@@ -352,7 +417,7 @@ fn merge_by_btree() {
let mut bt2 = BTreeMap::new();
bt2.insert("foo", 2);
bt2.insert("bar", 4);
- let results = bt1.into_iter().merge_by(bt2.into_iter(), |a, b| a.0 <= b.0 );
+ let results = bt1.into_iter().merge_by(bt2.into_iter(), |a, b| a.0 <= b.0);
let expected = vec![("bar", 4), ("foo", 2), ("hello", 1), ("world", 3)];
it::assert_equal(results, expected.into_iter());
}
@@ -394,19 +459,17 @@ fn kmerge_empty_size_hint() {
#[test]
fn join() {
let many = [1, 2, 3];
- let one = [1];
+ let one = [1];
let none: Vec<i32> = vec![];
assert_eq!(many.iter().join(", "), "1, 2, 3");
- assert_eq!( one.iter().join(", "), "1");
+ assert_eq!(one.iter().join(", "), "1");
assert_eq!(none.iter().join(", "), "");
}
#[test]
fn sorted_unstable_by() {
- let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| {
- a.cmp(&b)
- });
+ let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| a.cmp(&b));
it::assert_equal(sc, vec![1, 2, 3, 4]);
let v = (0..5).sorted_unstable_by(|&a, &b| a.cmp(&b).reverse());
@@ -424,9 +487,7 @@ fn sorted_unstable_by_key() {
#[test]
fn sorted_by() {
- let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| {
- a.cmp(&b)
- });
+ let sc = [3, 4, 1, 2].iter().cloned().sorted_by(|&a, &b| a.cmp(&b));
it::assert_equal(sc, vec![1, 2, 3, 4]);
let v = (0..5).sorted_by(|&a, &b| a.cmp(&b).reverse());
@@ -459,11 +520,13 @@ struct RandIter<T: 'static + Clone + Send, R: 'static + Clone + Rng + SeedableRn
idx: usize,
len: usize,
rng: R,
- _t: PhantomData<T>
+ _t: PhantomData<T>,
}
impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> Iterator for RandIter<T, R>
-where Standard: Distribution<T> {
+where
+ Standard: Distribution<T>,
+{
type Item = T;
fn next(&mut self) -> Option<T> {
if self.idx == self.len {
@@ -481,7 +544,7 @@ impl<T: Clone + Send, R: Clone + Rng + SeedableRng + Send> qc::Arbitrary for Ran
idx: 0,
len: g.size(),
rng: R::seed_from_u64(g.next_u64()),
- _t : PhantomData{},
+ _t: PhantomData {},
}
}
}
@@ -495,10 +558,7 @@ where
{
let j = i.clone();
let k = k as usize;
- it::assert_equal(
- i.k_smallest(k),
- j.sorted().take(k)
- )
+ it::assert_equal(i.k_smallest(k), j.sorted().take(k))
}
macro_rules! generic_test {
@@ -550,7 +610,7 @@ fn sorted_by_cached_key() {
#[test]
fn test_multipeek() {
- let nums = vec![1u8,2,3,4,5];
+ let nums = vec![1u8, 2, 3, 4, 5];
let mp = multipeek(nums.iter().copied());
assert_eq!(nums, mp.collect::<Vec<_>>());
@@ -591,7 +651,7 @@ fn test_multipeek_reset() {
#[test]
fn test_multipeek_peeking_next() {
use crate::it::PeekingNext;
- let nums = vec![1u8,2,3,4,5,6,7];
+ let nums = vec![1u8, 2, 3, 4, 5, 6, 7];
let mut mp = multipeek(nums.iter().copied());
assert_eq!(mp.peeking_next(|&x| x != 0), Some(1));
@@ -616,7 +676,7 @@ fn test_multipeek_peeking_next() {
#[test]
fn test_peek_nth() {
- let nums = vec![1u8,2,3,4,5];
+ let nums = vec![1u8, 2, 3, 4, 5];
let iter = peek_nth(nums.iter().copied());
assert_eq!(nums, iter.collect::<Vec<_>>());
@@ -651,7 +711,7 @@ fn test_peek_nth() {
#[test]
fn test_peek_nth_peeking_next() {
use it::PeekingNext;
- let nums = vec![1u8,2,3,4,5,6,7];
+ let nums = vec![1u8, 2, 3, 4, 5, 6, 7];
let mut iter = peek_nth(nums.iter().copied());
assert_eq!(iter.peeking_next(|&x| x != 0), Some(1));
@@ -679,6 +739,35 @@ fn test_peek_nth_peeking_next() {
}
#[test]
+fn test_peek_nth_next_if() {
+ let nums = vec![1u8, 2, 3, 4, 5, 6, 7];
+ let mut iter = peek_nth(nums.iter().copied());
+
+ assert_eq!(iter.next_if(|&x| x != 0), Some(1));
+ assert_eq!(iter.next(), Some(2));
+
+ assert_eq!(iter.peek_nth(0), Some(&3));
+ assert_eq!(iter.peek_nth(1), Some(&4));
+ assert_eq!(iter.next_if_eq(&3), Some(3));
+ assert_eq!(iter.peek(), Some(&4));
+
+ assert_eq!(iter.next_if(|&x| x != 4), None);
+ assert_eq!(iter.next_if_eq(&4), Some(4));
+ assert_eq!(iter.peek_nth(0), Some(&5));
+ assert_eq!(iter.peek_nth(1), Some(&6));
+
+ assert_eq!(iter.next_if(|&x| x != 5), None);
+ assert_eq!(iter.peek(), Some(&5));
+
+ assert_eq!(iter.next_if(|&x| x % 2 == 1), Some(5));
+ assert_eq!(iter.next_if_eq(&6), Some(6));
+ assert_eq!(iter.peek_nth(0), Some(&7));
+ assert_eq!(iter.peek_nth(1), None);
+ assert_eq!(iter.next(), Some(7));
+ assert_eq!(iter.peek(), None);
+}
+
+#[test]
fn pad_using() {
it::assert_equal((0..0).pad_using(1, |_| 1), 1..2);
@@ -717,11 +806,11 @@ fn group_by() {
for &idx in &indices[..] {
let (key, text) = match idx {
- 0 => ('A', "Aaa".chars()),
- 1 => ('B', "Bbb".chars()),
- 2 => ('C', "ccCc".chars()),
- 3 => ('D', "DDDD".chars()),
- _ => unreachable!(),
+ 0 => ('A', "Aaa".chars()),
+ 1 => ('B', "Bbb".chars()),
+ 2 => ('C', "ccCc".chars()),
+ 3 => ('D', "DDDD".chars()),
+ _ => unreachable!(),
};
assert_eq!(key, subs[idx].0);
it::assert_equal(&mut subs[idx].1, text);
@@ -746,9 +835,11 @@ fn group_by() {
{
let mut ntimes = 0;
let text = "AABCCC";
- for (_, sub) in &text.chars().group_by(|&x| { ntimes += 1; x}) {
- for _ in sub {
- }
+ for (_, sub) in &text.chars().group_by(|&x| {
+ ntimes += 1;
+ x
+ }) {
+ for _ in sub {}
}
assert_eq!(ntimes, text.len());
}
@@ -756,8 +847,10 @@ fn group_by() {
{
let mut ntimes = 0;
let text = "AABCCC";
- for _ in &text.chars().group_by(|&x| { ntimes += 1; x}) {
- }
+ for _ in &text.chars().group_by(|&x| {
+ ntimes += 1;
+ x
+ }) {}
assert_eq!(ntimes, text.len());
}
@@ -797,8 +890,7 @@ fn group_by_lazy_2() {
if i < 2 {
groups.push(group);
} else if i < 4 {
- for _ in group {
- }
+ for _ in group {}
} else {
groups.push(group);
}
@@ -810,7 +902,11 @@ fn group_by_lazy_2() {
// use groups as chunks
let data = vec![0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3];
let mut i = 0;
- let grouper = data.iter().group_by(move |_| { let k = i / 3; i += 1; k });
+ let grouper = data.iter().group_by(move |_| {
+ let k = i / 3;
+ i += 1;
+ k
+ });
for (i, group) in &grouper {
match i {
0 => it::assert_equal(group, &[0, 0, 0]),
@@ -861,8 +957,8 @@ fn concat_empty() {
#[test]
fn concat_non_empty() {
- let data = vec![vec![1,2,3], vec![4,5,6], vec![7,8,9]];
- assert_eq!(data.into_iter().concat(), vec![1,2,3,4,5,6,7,8,9])
+ let data = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
+ assert_eq!(data.into_iter().concat(), vec![1, 2, 3, 4, 5, 6, 7, 8, 9])
}
#[test]
@@ -870,19 +966,20 @@ fn combinations() {
assert!((1..3).combinations(5).next().is_none());
let it = (1..3).combinations(2);
- it::assert_equal(it, vec![
- vec![1, 2],
- ]);
+ it::assert_equal(it, vec![vec![1, 2]]);
let it = (1..5).combinations(2);
- it::assert_equal(it, vec![
- vec![1, 2],
- vec![1, 3],
- vec![1, 4],
- vec![2, 3],
- vec![2, 4],
- vec![3, 4],
- ]);
+ it::assert_equal(
+ it,
+ vec![
+ vec![1, 2],
+ vec![1, 3],
+ vec![1, 4],
+ vec![2, 3],
+ vec![2, 4],
+ vec![3, 4],
+ ],
+ );
it::assert_equal((0..0).tuple_combinations::<(_, _)>(), <Vec<_>>::new());
it::assert_equal((0..1).tuple_combinations::<(_, _)>(), <Vec<_>>::new());
@@ -902,13 +999,88 @@ fn combinations_of_too_short() {
}
}
-
#[test]
fn combinations_zero() {
it::assert_equal((1..3).combinations(0), vec![vec![]]);
it::assert_equal((0..0).combinations(0), vec![vec![]]);
}
+fn binomial(n: usize, k: usize) -> usize {
+ if k > n {
+ 0
+ } else {
+ (n - k + 1..=n).product::<usize>() / (1..=k).product::<usize>()
+ }
+}
+
+#[test]
+fn combinations_range_count() {
+ for n in 0..=10 {
+ for k in 0..=10 {
+ let len = binomial(n, k);
+ let mut it = (0..n).combinations(k);
+ assert_eq!(len, it.clone().count());
+ assert_eq!(len, it.size_hint().0);
+ assert_eq!(Some(len), it.size_hint().1);
+ for count in (0..len).rev() {
+ let elem = it.next();
+ assert!(elem.is_some());
+ assert_eq!(count, it.clone().count());
+ assert_eq!(count, it.size_hint().0);
+ assert_eq!(Some(count), it.size_hint().1);
+ }
+ let should_be_none = it.next();
+ assert!(should_be_none.is_none());
+ }
+ }
+}
+
+#[test]
+fn combinations_inexact_size_hints() {
+ for k in 0..=10 {
+ let mut numbers = (0..18).filter(|i| i % 2 == 0); // 9 elements
+ let mut it = numbers.clone().combinations(k);
+ let real_n = numbers.clone().count();
+ let len = binomial(real_n, k);
+ assert_eq!(len, it.clone().count());
+
+ let mut nb_loaded = 0;
+ let sh = numbers.size_hint();
+ assert_eq!(binomial(sh.0 + nb_loaded, k), it.size_hint().0);
+ assert_eq!(sh.1.map(|n| binomial(n + nb_loaded, k)), it.size_hint().1);
+
+ for next_count in 1..=len {
+ let elem = it.next();
+ assert!(elem.is_some());
+ assert_eq!(len - next_count, it.clone().count());
+ if next_count == 1 {
+ // The very first time, the lazy buffer is prefilled.
+ nb_loaded = numbers.by_ref().take(k).count();
+ } else {
+ // Then it loads one item each time until exhausted.
+ let nb = numbers.next();
+ if nb.is_some() {
+ nb_loaded += 1;
+ }
+ }
+ let sh = numbers.size_hint();
+ if next_count > real_n - k + 1 {
+ assert_eq!(0, sh.0);
+ assert_eq!(Some(0), sh.1);
+ assert_eq!(real_n, nb_loaded);
+ // Once it's fully loaded, size hints of `it` are exacts.
+ }
+ assert_eq!(binomial(sh.0 + nb_loaded, k) - next_count, it.size_hint().0);
+ assert_eq!(
+ sh.1.map(|n| binomial(n + nb_loaded, k) - next_count),
+ it.size_hint().1
+ );
+ }
+ let should_be_none = it.next();
+ assert!(should_be_none.is_none());
+ }
+}
+
#[test]
fn permutations_zero() {
it::assert_equal((1..3).permutations(0), vec![vec![]]);
@@ -916,6 +1088,40 @@ fn permutations_zero() {
}
#[test]
+fn permutations_range_count() {
+ for n in 0..=7 {
+ for k in 0..=7 {
+ let len = if k <= n { (n - k + 1..=n).product() } else { 0 };
+ let mut it = (0..n).permutations(k);
+ assert_eq!(len, it.clone().count());
+ assert_eq!(len, it.size_hint().0);
+ assert_eq!(Some(len), it.size_hint().1);
+ for count in (0..len).rev() {
+ let elem = it.next();
+ assert!(elem.is_some());
+ assert_eq!(count, it.clone().count());
+ assert_eq!(count, it.size_hint().0);
+ assert_eq!(Some(count), it.size_hint().1);
+ }
+ let should_be_none = it.next();
+ assert!(should_be_none.is_none());
+ }
+ }
+}
+
+#[test]
+fn permutations_overflowed_size_hints() {
+ let mut it = std::iter::repeat(()).permutations(2);
+ assert_eq!(it.size_hint().0, usize::MAX);
+ assert_eq!(it.size_hint().1, None);
+ for nb_generated in 1..=1000 {
+ it.next();
+ assert!(it.size_hint().0 >= usize::MAX - nb_generated);
+ assert_eq!(it.size_hint().1, None);
+ }
+}
+
+#[test]
fn combinations_with_replacement() {
// Pool smaller than n
it::assert_equal((0..1).combinations_with_replacement(2), vec![vec![0, 0]]);
@@ -932,15 +1138,9 @@ fn combinations_with_replacement() {
],
);
// Zero size
- it::assert_equal(
- (0..3).combinations_with_replacement(0),
- vec![vec![]],
- );
+ it::assert_equal((0..3).combinations_with_replacement(0), vec![vec![]]);
// Zero size on empty pool
- it::assert_equal(
- (0..0).combinations_with_replacement(0),
- vec![vec![]],
- );
+ it::assert_equal((0..0).combinations_with_replacement(0), vec![vec![]]);
// Empty pool
it::assert_equal(
(0..0).combinations_with_replacement(2),
@@ -949,20 +1149,69 @@ fn combinations_with_replacement() {
}
#[test]
+fn combinations_with_replacement_range_count() {
+ for n in 0..=7 {
+ for k in 0..=7 {
+ let len = binomial(usize::saturating_sub(n + k, 1), k);
+ let mut it = (0..n).combinations_with_replacement(k);
+ assert_eq!(len, it.clone().count());
+ assert_eq!(len, it.size_hint().0);
+ assert_eq!(Some(len), it.size_hint().1);
+ for count in (0..len).rev() {
+ let elem = it.next();
+ assert!(elem.is_some());
+ assert_eq!(count, it.clone().count());
+ assert_eq!(count, it.size_hint().0);
+ assert_eq!(Some(count), it.size_hint().1);
+ }
+ let should_be_none = it.next();
+ assert!(should_be_none.is_none());
+ }
+ }
+}
+
+#[test]
fn powerset() {
it::assert_equal((0..0).powerset(), vec![vec![]]);
it::assert_equal((0..1).powerset(), vec![vec![], vec![0]]);
- it::assert_equal((0..2).powerset(), vec![vec![], vec![0], vec![1], vec![0, 1]]);
- it::assert_equal((0..3).powerset(), vec![
- vec![],
- vec![0], vec![1], vec![2],
- vec![0, 1], vec![0, 2], vec![1, 2],
- vec![0, 1, 2]
- ]);
+ it::assert_equal(
+ (0..2).powerset(),
+ vec![vec![], vec![0], vec![1], vec![0, 1]],
+ );
+ it::assert_equal(
+ (0..3).powerset(),
+ vec![
+ vec![],
+ vec![0],
+ vec![1],
+ vec![2],
+ vec![0, 1],
+ vec![0, 2],
+ vec![1, 2],
+ vec![0, 1, 2],
+ ],
+ );
assert_eq!((0..4).powerset().count(), 1 << 4);
assert_eq!((0..8).powerset().count(), 1 << 8);
assert_eq!((0..16).powerset().count(), 1 << 16);
+
+ for n in 0..=10 {
+ let mut it = (0..n).powerset();
+ let len = 2_usize.pow(n);
+ assert_eq!(len, it.clone().count());
+ assert_eq!(len, it.size_hint().0);
+ assert_eq!(Some(len), it.size_hint().1);
+ for count in (0..len).rev() {
+ let elem = it.next();
+ assert!(elem.is_some());
+ assert_eq!(count, it.clone().count());
+ assert_eq!(count, it.size_hint().0);
+ assert_eq!(Some(count), it.size_hint().1);
+ }
+ let should_be_none = it.next();
+ assert!(should_be_none.is_none());
+ }
}
#[test]
@@ -987,8 +1236,7 @@ fn diff_longer() {
let diff = it::diff_with(a.iter(), b_map, |a, b| *a == b);
assert!(match diff {
- Some(it::Diff::Longer(_, remaining)) =>
- remaining.collect::<Vec<_>>() == vec![5, 6],
+ Some(it::Diff::Longer(_, remaining)) => remaining.collect::<Vec<_>>() == vec![5, 6],
_ => false,
});
}
@@ -1056,8 +1304,8 @@ fn extrema_set() {
#[test]
fn minmax() {
- use std::cmp::Ordering;
use crate::it::MinMaxResult;
+ use std::cmp::Ordering;
// A peculiar type: Equality compares both tuple items, but ordering only the
// first item. This is so we can check the stability property easily.
@@ -1076,7 +1324,10 @@ fn minmax() {
}
}
- assert_eq!(None::<Option<u32>>.iter().minmax(), MinMaxResult::NoElements);
+ assert_eq!(
+ None::<Option<u32>>.iter().minmax(),
+ MinMaxResult::NoElements
+ );
assert_eq!(Some(1u32).iter().minmax(), MinMaxResult::OneElement(&1));
@@ -1089,7 +1340,11 @@ fn minmax() {
assert_eq!(min, &Val(2, 0));
assert_eq!(max, &Val(0, 2));
- let (min, max) = data.iter().minmax_by(|x, y| x.1.cmp(&y.1)).into_option().unwrap();
+ let (min, max) = data
+ .iter()
+ .minmax_by(|x, y| x.1.cmp(&y.1))
+ .into_option()
+ .unwrap();
assert_eq!(min, &Val(2, 0));
assert_eq!(max, &Val(0, 2));
}
@@ -1112,8 +1367,9 @@ fn format() {
#[test]
fn while_some() {
- let ns = (1..10).map(|x| if x % 5 != 0 { Some(x) } else { None })
- .while_some();
+ let ns = (1..10)
+ .map(|x| if x % 5 != 0 { Some(x) } else { None })
+ .while_some();
it::assert_equal(ns, vec![1, 2, 3, 4]);
}
@@ -1122,15 +1378,18 @@ fn while_some() {
fn fold_while() {
let mut iterations = 0;
let vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- let sum = vec.into_iter().fold_while(0, |acc, item| {
- iterations += 1;
- let new_sum = acc + item;
- if new_sum <= 20 {
- FoldWhile::Continue(new_sum)
- } else {
- FoldWhile::Done(acc)
- }
- }).into_inner();
+ let sum = vec
+ .into_iter()
+ .fold_while(0, |acc, item| {
+ iterations += 1;
+ let new_sum = acc + item;
+ if new_sum <= 20 {
+ FoldWhile::Continue(new_sum)
+ } else {
+ FoldWhile::Done(acc)
+ }
+ })
+ .into_inner();
assert_eq!(iterations, 6);
assert_eq!(sum, 15);
}
@@ -1157,7 +1416,11 @@ fn tree_fold1() {
"0 1 x 2 3 x x 4 5 x 6 7 x x x 8 9 x 10 11 x x 12 13 x 14 15 x x x x",
];
for (i, &s) in x.iter().enumerate() {
- let expected = if s.is_empty() { None } else { Some(s.to_string()) };
+ let expected = if s.is_empty() {
+ None
+ } else {
+ Some(s.to_string())
+ };
let num_strings = (0..i).map(|x| x.to_string());
let actual = num_strings.tree_fold1(|a, b| format!("{} {} x", a, b));
assert_eq!(actual, expected);
@@ -1169,16 +1432,52 @@ fn exactly_one_question_mark_syntax_works() {
exactly_one_question_mark_return().unwrap_err();
}
-fn exactly_one_question_mark_return() -> Result<(), ExactlyOneError<std::slice::Iter<'static, ()>>> {
+fn exactly_one_question_mark_return() -> Result<(), ExactlyOneError<std::slice::Iter<'static, ()>>>
+{
[].iter().exactly_one()?;
Ok(())
}
#[test]
fn multiunzip() {
- let (a, b, c): (Vec<_>, Vec<_>, Vec<_>) = [(0, 1, 2), (3, 4, 5), (6, 7, 8)].iter().cloned().multiunzip();
+ let (a, b, c): (Vec<_>, Vec<_>, Vec<_>) = [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
+ .iter()
+ .cloned()
+ .multiunzip();
assert_eq!((a, b, c), (vec![0, 3, 6], vec![1, 4, 7], vec![2, 5, 8]));
let (): () = [(), (), ()].iter().cloned().multiunzip();
- let t: (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)].iter().cloned().multiunzip();
- assert_eq!(t, (vec![0], vec![1], vec![2], vec![3], vec![4], vec![5], vec![6], vec![7], vec![8], vec![9], vec![10], vec![11]));
+ let t: (
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ Vec<_>,
+ ) = [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)]
+ .iter()
+ .cloned()
+ .multiunzip();
+ assert_eq!(
+ t,
+ (
+ vec![0],
+ vec![1],
+ vec![2],
+ vec![3],
+ vec![4],
+ vec![5],
+ vec![6],
+ vec![7],
+ vec![8],
+ vec![9],
+ vec![10],
+ vec![11]
+ )
+ );
}
diff --git a/vendor/itertools/tests/zip.rs b/vendor/itertools/tests/zip.rs
index 75157d34f..f2554d7a4 100644
--- a/vendor/itertools/tests/zip.rs
+++ b/vendor/itertools/tests/zip.rs
@@ -1,17 +1,18 @@
-use itertools::Itertools;
-use itertools::EitherOrBoth::{Both, Left, Right};
use itertools::free::zip_eq;
use itertools::multizip;
+use itertools::EitherOrBoth::{Both, Left, Right};
+use itertools::Itertools;
#[test]
fn zip_longest_fused() {
let a = [Some(1), None, Some(3), Some(4)];
let b = [1, 2, 3];
- let unfused = a.iter().batching(|it| *it.next().unwrap())
+ let unfused = a
+ .iter()
+ .batching(|it| *it.next().unwrap())
.zip_longest(b.iter().cloned());
- itertools::assert_equal(unfused,
- vec![Both(1, 1), Right(2), Right(3)]);
+ itertools::assert_equal(unfused, vec![Both(1, 1), Right(2), Right(3)]);
}
#[test]
@@ -55,11 +56,9 @@ fn test_double_ended_zip() {
assert_eq!(it.next_back(), None);
}
-
#[should_panic]
#[test]
-fn zip_eq_panic1()
-{
+fn zip_eq_panic1() {
let a = [1, 2];
let b = [1, 2, 3];
@@ -68,8 +67,7 @@ fn zip_eq_panic1()
#[should_panic]
#[test]
-fn zip_eq_panic2()
-{
+fn zip_eq_panic2() {
let a: [i32; 0] = [];
let b = [1, 2, 3];