-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathiter_test.cairo
49 lines (41 loc) · 1.01 KB
/
iter_test.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::iter::IntoIterator;
#[test]
fn test_into_iter() {
let v = array![1, 2, 3];
let mut iter = v.into_iter();
assert_eq!(Option::Some(1), iter.next());
assert_eq!(Option::Some(2), iter.next());
assert_eq!(Option::Some(3), iter.next());
assert_eq!(Option::None, iter.next());
}
#[derive(Drop, Debug)]
struct MyCollection {
arr: Array<u32>,
}
#[generate_trait]
impl MyCollectionImpl of MyCollectionTrait {
fn new() -> MyCollection {
MyCollection { arr: ArrayTrait::new() }
}
fn add(ref self: MyCollection, elem: u32) {
self.arr.append(elem);
}
}
impl MyCollectionIntoIterator of IntoIterator<MyCollection> {
type IntoIter = crate::array::ArrayIter<u32>;
fn into_iter(self: MyCollection) -> Self::IntoIter {
self.arr.into_iter()
}
}
#[test]
fn test_into_iter_impl() {
let mut c = MyCollectionTrait::new();
c.add(0);
c.add(1);
c.add(2);
let mut n = 0;
for i in c {
assert_eq!(i, n);
n += 1;
};
}