-
Notifications
You must be signed in to change notification settings - Fork 490
/
array_test.cairo
218 lines (183 loc) · 6.29 KB
/
array_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use core::test::test_utils::{assert_eq, assert_ne};
#[test]
fn test_array() {
let arr = array![10, 11, 12];
assert_eq(arr[0], @10, 'array[0] != 10');
assert_eq(arr[1], @11, 'array[1] != 11');
assert_eq(arr[2], @12, 'array[2] != 12');
}
#[test]
#[should_panic]
fn test_array_out_of_bound_1() {
let arr = array![10, 11, 12];
arr[3];
}
#[test]
#[should_panic]
fn test_array_out_of_bound_2() {
let arr = array![10, 11, 12];
arr[11];
}
#[test]
fn test_array_clone() {
let felt252_snap_array: @Array<felt252> = @array![10, 11, 12];
let felt252_snap_array_clone = felt252_snap_array.clone();
assert_eq(@felt252_snap_array_clone.len(), @3, 'array len != 3');
assert_eq(felt252_snap_array_clone[0], @10, 'array[0] != 10');
assert_eq(felt252_snap_array_clone[1], @11, 'array[1] != 11');
assert_eq(felt252_snap_array_clone[2], @12, 'array[2] != 12');
}
#[test]
fn test_span() {
let mut span = array![10, 11, 12].span();
assert_eq(@span.len(), @3, 'Unexpected span length.');
assert_eq(span.get(0).unwrap().unbox(), @10, 'Unexpected element');
assert_eq(span.pop_front().unwrap(), @10, 'Unexpected element');
assert_eq(@span.len(), @2, 'Unexpected span length.');
assert_eq(span[1], @12, 'Unexpected element');
assert_eq(span.pop_back().unwrap(), @12, 'Unexpected element');
assert_eq(@span.len(), @1, 'Unexpected span length.');
}
#[test]
fn test_slice() {
let span = [10, 11, 12].span();
assert_eq!(span.slice(0, 3), span);
assert_eq!(span.slice(0, 2), [10, 11].span());
assert_eq!(span.slice(0, 1), [10].span());
assert!(span.slice(0, 0).is_empty());
assert_eq!(span.slice(1, 2), [11, 12].span());
assert_eq!(span.slice(1, 1), [11].span());
assert!(span.slice(1, 0).is_empty());
}
#[test]
#[should_panic]
fn test_slice_out_of_bound_1() {
[10, 11, 12].span().slice(3, 1);
}
#[test]
#[should_panic]
fn test_slice_out_of_bound_2() {
[10, 11, 12].span().slice(0, 4);
}
#[test]
fn test_equality() {
let arr1 = array![];
let arr2 = array![10, 11, 12];
let arr3 = array![10, 11, 13];
let arr4 = array![10, 11];
let arr5 = array![10, 11, 12, 13];
assert(arr1 == arr1, 'arr1 != arr1');
assert(arr2 == arr2, 'arr2 != arr2');
assert(arr3 == arr3, 'arr3 != arr3');
assert(arr4 == arr4, 'arr4 != arr4');
assert(arr5 == arr5, 'arr5 != arr5');
assert(arr1 != arr2, 'arr1 == arr2');
assert(arr1 != arr3, 'arr1 == arr3');
assert(arr1 != arr4, 'arr1 == arr4');
assert(arr1 != arr5, 'arr1 == arr5');
assert(arr2 != arr3, 'arr2 == arr3');
assert(arr2 != arr4, 'arr2 == arr4');
assert(arr2 != arr5, 'arr2 == arr5');
assert(arr3 != arr4, 'arr3 == arr4');
assert(arr3 != arr5, 'arr3 == arr5');
assert(arr4 != arr5, 'arr4 == arr5');
}
#[test]
fn test_append() {
let mut arr = array![10, 11, 12];
arr.append(13);
assert(arr.len() == 4, 'Unexpected length');
assert_eq(arr[3], @13, 'Unexpected element');
}
#[test]
fn test_append_span() {
let mut arr = array![10, 11, 12];
arr.append_span(arr.span());
assert(arr.len() == 6, 'Unexpected length');
assert_eq(arr[3], @10, 'Unexpected element');
assert_eq(arr[4], @11, 'Unexpected element');
assert_eq(arr[5], @12, 'Unexpected element');
}
#[test]
fn test_felt252_span_from_tuple() {
let span: Span<felt252> = [10, 20, 30].span();
assert!(*span[0] == 10);
assert!(*span[1] == 20);
assert!(*span[2] == 30);
}
#[test]
fn test_tuple_span_from_tuple() {
let span: Span<(felt252, felt252, felt252)> = [(10, 20, 30), (40, 50, 60), (70, 80, 90)].span();
assert!(*span[0] == (10, 20, 30));
assert!(*span[1] == (40, 50, 60));
assert!(*span[2] == (70, 80, 90));
}
#[test]
fn test_fixed_size_array() {
let arr = [10, 11, 12];
let [x, y, z] = arr;
assert_eq!(x, 10);
assert_eq!(y, 11);
assert_eq!(z, 12);
}
fn consume<const N: usize>(_arr: [felt252; N]) {}
#[test]
fn test_fixed_size_array_copy() {
let arr = [10, 11, 12];
consume(arr);
consume(arr);
}
/// Helper for tests removing the box wrapping a fixed sized array wrapped by an option.
fn debox<T, const SIZE: usize>(value: Option<@Box<[T; SIZE]>>) -> Option<@[T; SIZE]> {
Option::Some(value?.as_snapshot().unbox())
}
#[test]
fn test_span_into_fixed_size_array() {
assert!(debox::<felt252, 2>([10, 11, 12].span().try_into()).is_none());
assert!(debox::<felt252, 3>([10, 11, 12].span().try_into()) == Option::Some(@[10, 11, 12]));
assert!(debox::<felt252, 4>([10, 11, 12].span().try_into()).is_none());
assert!(debox::<u256, 2>([10, 11, 12].span().try_into()).is_none());
assert!(debox::<u256, 3>([10, 11, 12].span().try_into()) == Option::Some(@[10, 11, 12]));
assert!(debox::<u256, 4>([10, 11, 12].span().try_into()).is_none());
assert!(debox::<felt252, 1>([].span().try_into()).is_none());
assert!(debox::<felt252, 0>([].span().try_into()) == Option::Some(@[]));
}
#[test]
fn test_span_multi_pop() {
let mut span = array![10, 11, 12, 13].span();
assert!(span.multi_pop_front::<5>().is_none());
assert!(debox(span.multi_pop_front::<4>()) == Option::Some(@[10, 11, 12, 13]));
let mut span = array![10, 11, 12, 13].span();
assert!(debox(span.multi_pop_front::<3>()) == Option::Some(@[10, 11, 12]));
let mut span = array![10, 11, 12, 13].span();
assert!(span.multi_pop_back::<5>().is_none());
assert!(debox(span.multi_pop_back::<4>()) == Option::Some(@[10, 11, 12, 13]));
let mut span = array![10, 11, 12, 13].span();
assert!(debox(span.multi_pop_back::<3>()) == Option::Some(@[11, 12, 13]));
}
mod into_trait_impls {
#[test]
fn array_into_span() {
let array = array![1, 2, 3];
let span: Span<_> = array.into();
assert_eq!(span, array![1, 2, 3].span())
}
#[test]
fn span_into_array() {
let span = array![1, 2, 3].span();
let array: Array<_> = span.into();
assert_eq!(array, array![1, 2, 3]);
}
#[test]
fn array_snap_into_span() {
let array_snap = @array![1, 2, 3];
let span: Span<_> = array_snap.into();
assert_eq!(span, array![1, 2, 3].span())
}
#[test]
fn span_into_array_snap() {
let span = array![1, 2, 3].span();
let array_snap: @Array<_> = span.into();
assert_eq!(array_snap, @array![1, 2, 3]);
}
}