3 //! You're probably viewing this file because you're adding a test (or you might
4 //! just be browsing, in that case, hey there!).
6 //! The iter test suite is split into two big modules, and some miscellaneous
7 //! smaller modules. The two big modules are `adapters` and `traits`.
9 //! `adapters` are for methods on `Iterator` that adapt the data inside the
10 //! iterator, whether it be by emitting another iterator or returning an item
11 //! from inside the iterator after executing a closure on each item.
13 //! `traits` are for trait's that extend an `Iterator` (and the `Iterator`
14 //! trait itself, mostly containing miscellaneous methods). For the most part,
15 //! if a test in `traits` uses a specific adapter, then it should be moved to
16 //! that adapter's test file in `adapters`.
24 use core::convert::TryFrom;
27 pub fn is_trusted_len<I: TrustedLen>(_: I) {}
30 fn test_multi_iter() {
31 let xs = [1, 2, 3, 4];
32 let ys = [4, 3, 2, 1];
33 assert!(xs.iter().eq(ys.iter().rev()));
34 assert!(xs.iter().lt(xs.iter().skip(2)));
38 fn test_counter_from_iter() {
39 let it = (0..).step_by(5).take(10);
40 let xs: Vec<isize> = FromIterator::from_iter(it);
41 assert_eq!(xs, [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
45 fn test_functor_laws() {
47 fn identity<T>(x: T) -> T {
50 assert_eq!((0..10).map(identity).sum::<usize>(), (0..10).sum());
53 fn f(x: usize) -> usize {
56 fn g(x: usize) -> usize {
59 fn h(x: usize) -> usize {
62 assert_eq!((0..10).map(f).map(g).sum::<usize>(), (0..10).map(h).sum());
66 fn test_monad_laws_left_identity() {
67 fn f(x: usize) -> impl Iterator<Item = usize> {
68 (0..10).map(move |y| x * y)
70 assert_eq!(once(42).flat_map(f.clone()).sum::<usize>(), f(42).sum());
74 fn test_monad_laws_right_identity() {
75 assert_eq!((0..10).flat_map(|x| once(x)).sum::<usize>(), (0..10).sum());
79 fn test_monad_laws_associativity() {
80 fn f(x: usize) -> impl Iterator<Item = usize> {
83 fn g(x: usize) -> impl Iterator<Item = usize> {
87 (0..10).flat_map(f).flat_map(g).sum::<usize>(),
88 (0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>()
93 pub fn extend_for_unit() {
96 let iter = (0..5).map(|_| {