]> git.lizzy.rs Git - rust.git/blob - library/core/tests/iter/mod.rs
Auto merge of #104992 - ehuss:docs-triagebot-assign, r=Mark-Simulacrum
[rust.git] / library / core / tests / iter / mod.rs
1 //! Note
2 //! ----
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!).
5 //!
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`.
8 //!
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.
12 //!
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`.
17
18 mod adapters;
19 mod range;
20 mod sources;
21 mod traits;
22
23 use core::cell::Cell;
24 use core::convert::TryFrom;
25 use core::iter::*;
26
27 pub fn is_trusted_len<I: TrustedLen>(_: I) {}
28
29 #[test]
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)));
35 }
36
37 #[test]
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]);
42 }
43
44 #[test]
45 fn test_functor_laws() {
46     // identity:
47     fn identity<T>(x: T) -> T {
48         x
49     }
50     assert_eq!((0..10).map(identity).sum::<usize>(), (0..10).sum());
51
52     // composition:
53     fn f(x: usize) -> usize {
54         x + 3
55     }
56     fn g(x: usize) -> usize {
57         x * 2
58     }
59     fn h(x: usize) -> usize {
60         g(f(x))
61     }
62     assert_eq!((0..10).map(f).map(g).sum::<usize>(), (0..10).map(h).sum());
63 }
64
65 #[test]
66 fn test_monad_laws_left_identity() {
67     fn f(x: usize) -> impl Iterator<Item = usize> {
68         (0..10).map(move |y| x * y)
69     }
70     assert_eq!(once(42).flat_map(f.clone()).sum::<usize>(), f(42).sum());
71 }
72
73 #[test]
74 fn test_monad_laws_right_identity() {
75     assert_eq!((0..10).flat_map(|x| once(x)).sum::<usize>(), (0..10).sum());
76 }
77
78 #[test]
79 fn test_monad_laws_associativity() {
80     fn f(x: usize) -> impl Iterator<Item = usize> {
81         0..x
82     }
83     fn g(x: usize) -> impl Iterator<Item = usize> {
84         (0..x).rev()
85     }
86     assert_eq!(
87         (0..10).flat_map(f).flat_map(g).sum::<usize>(),
88         (0..10).flat_map(|x| f(x).flat_map(g)).sum::<usize>()
89     );
90 }
91
92 #[test]
93 pub fn extend_for_unit() {
94     let mut x = 0;
95     {
96         let iter = (0..5).map(|_| {
97             x += 1;
98         });
99         ().extend(iter);
100     }
101     assert_eq!(x, 5);
102 }