]> git.proxmox.com Git - rustc.git/blob - library/core/tests/iter/sources.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / library / core / tests / iter / sources.rs
1 use super::*;
2 use core::iter::*;
3
4 #[test]
5 fn test_repeat() {
6 let mut it = repeat(42);
7 assert_eq!(it.next(), Some(42));
8 assert_eq!(it.next(), Some(42));
9 assert_eq!(it.next(), Some(42));
10 assert_eq!(repeat(42).size_hint(), (usize::MAX, None));
11 }
12
13 #[test]
14 fn test_repeat_take() {
15 let mut it = repeat(42).take(3);
16 assert_eq!(it.next(), Some(42));
17 assert_eq!(it.next(), Some(42));
18 assert_eq!(it.next(), Some(42));
19 assert_eq!(it.next(), None);
20 is_trusted_len(repeat(42).take(3));
21 assert_eq!(repeat(42).take(3).size_hint(), (3, Some(3)));
22 assert_eq!(repeat(42).take(0).size_hint(), (0, Some(0)));
23 assert_eq!(repeat(42).take(usize::MAX).size_hint(), (usize::MAX, Some(usize::MAX)));
24 }
25
26 #[test]
27 fn test_repeat_take_collect() {
28 let v: Vec<_> = repeat(42).take(3).collect();
29 assert_eq!(v, vec![42, 42, 42]);
30 }
31
32 #[test]
33 fn test_repeat_with() {
34 #[derive(PartialEq, Debug)]
35 struct NotClone(usize);
36 let mut it = repeat_with(|| NotClone(42));
37 assert_eq!(it.next(), Some(NotClone(42)));
38 assert_eq!(it.next(), Some(NotClone(42)));
39 assert_eq!(it.next(), Some(NotClone(42)));
40 assert_eq!(repeat_with(|| NotClone(42)).size_hint(), (usize::MAX, None));
41 }
42
43 #[test]
44 fn test_repeat_with_take() {
45 let mut it = repeat_with(|| 42).take(3);
46 assert_eq!(it.next(), Some(42));
47 assert_eq!(it.next(), Some(42));
48 assert_eq!(it.next(), Some(42));
49 assert_eq!(it.next(), None);
50 is_trusted_len(repeat_with(|| 42).take(3));
51 assert_eq!(repeat_with(|| 42).take(3).size_hint(), (3, Some(3)));
52 assert_eq!(repeat_with(|| 42).take(0).size_hint(), (0, Some(0)));
53 assert_eq!(repeat_with(|| 42).take(usize::MAX).size_hint(), (usize::MAX, Some(usize::MAX)));
54 }
55
56 #[test]
57 fn test_repeat_with_take_collect() {
58 let mut curr = 1;
59 let v: Vec<_> = repeat_with(|| {
60 let tmp = curr;
61 curr *= 2;
62 tmp
63 })
64 .take(5)
65 .collect();
66 assert_eq!(v, vec![1, 2, 4, 8, 16]);
67 }
68
69 #[test]
70 fn test_successors() {
71 let mut powers_of_10 = successors(Some(1_u16), |n| n.checked_mul(10));
72 assert_eq!(powers_of_10.by_ref().collect::<Vec<_>>(), &[1, 10, 100, 1_000, 10_000]);
73 assert_eq!(powers_of_10.next(), None);
74
75 let mut empty = successors(None::<u32>, |_| unimplemented!());
76 assert_eq!(empty.next(), None);
77 assert_eq!(empty.next(), None);
78 }
79
80 #[test]
81 fn test_once() {
82 let mut it = once(42);
83 assert_eq!(it.next(), Some(42));
84 assert_eq!(it.next(), None);
85 }
86
87 #[test]
88 fn test_once_with() {
89 let count = Cell::new(0);
90 let mut it = once_with(|| {
91 count.set(count.get() + 1);
92 42
93 });
94
95 assert_eq!(count.get(), 0);
96 assert_eq!(it.next(), Some(42));
97 assert_eq!(count.get(), 1);
98 assert_eq!(it.next(), None);
99 assert_eq!(count.get(), 1);
100 assert_eq!(it.next(), None);
101 assert_eq!(count.get(), 1);
102 }
103
104 #[test]
105 fn test_empty() {
106 let mut it = empty::<i32>();
107 assert_eq!(it.next(), None);
108 }
109
110 #[test]
111 fn test_repeat_n_drop() {
112 #[derive(Clone, Debug)]
113 struct DropCounter<'a>(&'a Cell<usize>);
114 impl Drop for DropCounter<'_> {
115 fn drop(&mut self) {
116 self.0.set(self.0.get() + 1);
117 }
118 }
119
120 // `repeat_n(x, 0)` drops `x` immediately
121 let count = Cell::new(0);
122 let item = DropCounter(&count);
123 let mut it = repeat_n(item, 0);
124 assert_eq!(count.get(), 1);
125 assert!(it.next().is_none());
126 assert_eq!(count.get(), 1);
127 drop(it);
128 assert_eq!(count.get(), 1);
129
130 // Dropping the iterator needs to drop the item if it's non-empty
131 let count = Cell::new(0);
132 let item = DropCounter(&count);
133 let it = repeat_n(item, 3);
134 assert_eq!(count.get(), 0);
135 drop(it);
136 assert_eq!(count.get(), 1);
137
138 // Dropping the iterator doesn't drop the item if it was exhausted
139 let count = Cell::new(0);
140 let item = DropCounter(&count);
141 let mut it = repeat_n(item, 3);
142 assert_eq!(count.get(), 0);
143 let x0 = it.next().unwrap();
144 assert_eq!(count.get(), 0);
145 let x1 = it.next().unwrap();
146 assert_eq!(count.get(), 0);
147 let x2 = it.next().unwrap();
148 assert_eq!(count.get(), 0);
149 assert!(it.next().is_none());
150 assert_eq!(count.get(), 0);
151 assert!(it.next().is_none());
152 assert_eq!(count.get(), 0);
153 drop(it);
154 assert_eq!(count.get(), 0);
155 drop((x0, x1, x2));
156 assert_eq!(count.get(), 3);
157 }