]>
git.proxmox.com Git - rustc.git/blob - library/core/tests/iter/sources.rs
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
));
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
)));
27 fn test_repeat_take_collect() {
28 let v
: Vec
<_
> = repeat(42).take(3).collect();
29 assert_eq
!(v
, vec
![42, 42, 42]);
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
));
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
)));
57 fn test_repeat_with_take_collect() {
59 let v
: Vec
<_
> = repeat_with(|| {
66 assert_eq
!(v
, vec
![1, 2, 4, 8, 16]);
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
);
75 let mut empty
= successors(None
::<u32>, |_
| unimplemented
!());
76 assert_eq
!(empty
.next(), None
);
77 assert_eq
!(empty
.next(), None
);
82 let mut it
= once(42);
83 assert_eq
!(it
.next(), Some(42));
84 assert_eq
!(it
.next(), None
);
89 let count
= Cell
::new(0);
90 let mut it
= once_with(|| {
91 count
.set(count
.get() + 1);
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);
106 let mut it
= empty
::<i32>();
107 assert_eq
!(it
.next(), None
);
111 fn test_repeat_n_drop() {
112 #[derive(Clone, Debug)]
113 struct DropCounter
<'a
>(&'a Cell
<usize>);
114 impl Drop
for DropCounter
<'_
> {
116 self.0.set(self.0.get() + 1);
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);
128 assert_eq
!(count
.get(), 1);
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);
136 assert_eq
!(count
.get(), 1);
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);
154 assert_eq
!(count
.get(), 0);
156 assert_eq
!(count
.get(), 3);