Skip to content

Commit a2f209e

Browse files
author
Gilad Chase
committed
refactor(test): remove compare_byte_array util and rework tests
This is legacy from when we didn't have PartialEq and had limited constructors. Note 1: `PartialEq` also compares the pending word and it's index, it's just no longer necessary to explicitly mention it, and probably better not to given that it's an implementation detail. Note 2: `format!` can also be used to create the expected here, but omitted to reduce the different APIs each unit test touches.
1 parent fa5fc99 commit a2f209e

File tree

1 file changed

+93
-163
lines changed

1 file changed

+93
-163
lines changed

corelib/src/test/byte_array_test.cairo

Lines changed: 93 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@ use crate::test::test_utils::{assert_eq, assert_ne};
22

33
// ========= Test-utils =========
44

5-
fn compare_byte_array(
6-
ba: @ByteArray, mut data: Span<felt252>, pending_word_len: usize, pending_word: felt252,
7-
) {
8-
assert(ba.data.len() == data.len(), 'wrong data len');
9-
let mut ba_data = ba.data.span();
10-
11-
let mut data_index = 0;
12-
loop {
13-
match ba_data.pop_front() {
14-
Some(x) => {
15-
let actual_word = (*x).into();
16-
let expected_word = *data.pop_front().unwrap();
17-
assert_eq!(actual_word, expected_word, "wrong data for index: {data_index}");
18-
},
19-
None(_) => { break; },
20-
}
21-
data_index += 1;
22-
}
23-
24-
assert_eq!(*ba.pending_word_len, pending_word_len);
25-
let ba_pending_word_felt: felt252 = (*ba.pending_word).into();
26-
assert_eq!(ba_pending_word_felt, pending_word);
27-
}
28-
295
fn compare_spans<T, +crate::fmt::Debug<T>, +PartialEq<T>, +Copy<T>, +Drop<T>>(
306
mut a: Span<T>, mut b: Span<T>,
317
) {
@@ -55,12 +31,6 @@ fn test_byte_array_2() -> ByteArray {
5531
ba1
5632
}
5733

58-
fn test_byte_array_16() -> ByteArray {
59-
let mut ba1 = Default::default();
60-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f10, 16);
61-
ba1
62-
}
63-
6434
fn test_byte_array_17() -> ByteArray {
6535
let mut ba1 = Default::default();
6636
ba1.append_word(0x0102030405060708091a0b0c0d0e0f1011, 17);
@@ -99,257 +69,217 @@ fn test_byte_array_33() -> ByteArray {
9969
fn test_append_byte() {
10070
let mut ba = Default::default();
10171
let mut c = 1_u8;
102-
loop {
103-
if c == 34 {
104-
break;
105-
}
72+
while c < 34 {
10673
ba.append_byte(c);
10774
c += 1;
10875
}
10976

110-
let expected_data = [0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
111-
compare_byte_array(@ba, expected_data.span(), 2, 0x2021);
77+
let expected: ByteArray = array![
78+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
79+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
80+
0x1f, 0x20, 0x21,
81+
]
82+
.into_iter()
83+
.collect();
84+
assert_eq!(ba, expected, "append_byte error");
11285
}
11386

11487
#[test]
11588
fn test_append_word() {
11689
let mut ba = Default::default();
11790

118-
ba.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e, 30);
119-
compare_byte_array(
120-
@ba, [].span(), 30, 0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
121-
);
91+
ba.append_word('ABCDEFGHIJKLMNOPQRSTUVWXYZabcd', 30);
92+
let mut expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
93+
assert_eq!(ba, expected, "appending word in single bytes31");
12294

123-
ba.append_word(0x1f2021, 3);
124-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
125-
compare_byte_array(@ba, expected_data.span(), 2, 0x2021);
95+
ba.append_word('efg', 3);
96+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
97+
assert_eq!(ba, expected, "append word overflowing pending word");
12698

127-
ba.append_word(0x2223, 2);
128-
compare_byte_array(@ba, expected_data.span(), 4, 0x20212223);
99+
ba.append_word('hi', 2);
100+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi";
101+
assert_eq!(ba, expected, "append word extending new pending word");
129102

130103
// Length is 0, so nothing is actually appended.
131-
ba.append_word(0xffee, 0);
132-
compare_byte_array(@ba, expected_data.span(), 4, 0x20212223);
104+
ba.append_word('jk', 0);
105+
assert_eq!(ba, expected, "append 0 length error");
133106

134-
ba.append_word(0x2425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e, 27);
135-
let expected_data = [
136-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
137-
0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e,
138-
];
139-
compare_byte_array(@ba, expected_data.span(), 0, 0);
107+
ba.append_word('ABCDEFGHIJKLMNOPQRSTUVWXYZa', 27);
108+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiABCDEFGHIJKLMNOPQRSTUVWXYZa";
109+
assert_eq!(ba, expected, "append word filling pending to capacity");
140110

141-
ba.append_word(0x3f, 1);
142-
compare_byte_array(@ba, expected_data.span(), 1, 0x3f);
111+
ba.append_word('b', 1);
112+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiABCDEFGHIJKLMNOPQRSTUVWXYZab";
113+
assert_eq!(ba, expected, "append word starting new pending word");
143114
}
144115

145116
#[test]
146117
fn test_append() {
147-
let mut ba1 = test_byte_array_32();
148-
let ba2 = test_byte_array_32();
118+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
149119

150-
ba1.append(@ba2);
120+
ba_32.append(@ba_32);
151121

152-
let expected_data = [
153-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
154-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
155-
];
156-
compare_byte_array(@ba1, expected_data.span(), 2, 0x1f20);
122+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
123+
assert_eq!(ba_32, expected, "append bytearray across new pending word");
157124
}
158125

159126
// Same as test_append, but with `+=` instead of `append`.
160127
#[test]
161128
fn test_add_eq() {
162-
let mut ba1 = test_byte_array_32();
163-
let ba2 = test_byte_array_32();
129+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
164130

165-
ba1 += ba2;
131+
ba_32 += ba_32.clone();
166132

167-
let expected_data = [
168-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
169-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
170-
];
171-
compare_byte_array(@ba1, expected_data.span(), 2, 0x1f20);
133+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
134+
assert_eq!(ba_32, expected, "add-eq bytearray across new pending word");
172135
}
173136

137+
// Same as test_append and test add_eq, but with `concat`.
174138
#[test]
175139
fn test_concat() {
176-
let ba1 = test_byte_array_32();
177-
let ba2 = test_byte_array_32();
140+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
178141

179-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
142+
let ba = ByteArrayTrait::concat(@ba_32, @ba_32);
180143

181-
let expected_data = [
182-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
183-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
184-
];
185-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
144+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
145+
assert_eq!(ba, expected, "add-eq bytearray across new pending word");
186146
}
187147

188148
// Same as test_concat, but with `+` instead of `concat`.
189149
#[test]
190150
fn test_add() {
191-
let ba1 = test_byte_array_32();
192-
let ba2 = test_byte_array_32();
151+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
193152

194-
let ba3 = ba1 + ba2;
153+
let ba_32 = ba_32.clone() + ba_32;
195154

196-
let expected_data = [
197-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
198-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
199-
];
200-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
155+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
156+
assert_eq!(ba_32, expected, "add-eq bytearray across new pending word");
201157
}
202158

203159
// Test concat/append, first byte array empty.
204160
#[test]
205161
fn test_concat_first_empty() {
206-
let ba1 = Default::default();
207-
let ba2 = test_byte_array_32();
208-
209-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
210-
211-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
212-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
162+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
163+
let ba_concat = ByteArrayTrait::concat(@Default::default(), @ba_32);
164+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
213165
}
214166

215167
// Test concat/append, second byte array empty.
216168
#[test]
217169
fn test_concat_second_empty() {
218-
let ba1 = test_byte_array_32();
219-
let ba2 = Default::default();
220-
221-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
222-
223-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
224-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
170+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
171+
let ba_concat = ByteArrayTrait::concat(@ba_32, @Default::default());
172+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
225173
}
226174

227175
// Test concat/append, first byte array pending word is empty.
228176
#[test]
229177
fn test_concat_first_pending_0() {
230-
let ba1 = test_byte_array_31();
231-
let ba2 = test_byte_array_32();
178+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
179+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
232180

233-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
181+
let ba_concat = ByteArrayTrait::concat(@ba_31, @ba_32);
234182

235-
let expected_data = [
236-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
237-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
238-
];
239-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
183+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
184+
assert_eq!(ba_concat, expected, "Error concat with overflow into pending word");
240185
}
241186

242187
// Test concat/append, second byte array pending word is empty.
243188
#[test]
244189
fn test_concat_second_pending_0() {
245-
let ba1 = test_byte_array_32();
246-
let ba2 = test_byte_array_31();
190+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
191+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
247192

248-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
193+
let ba_concat = ByteArrayTrait::concat(@ba_32, @ba_31);
249194

250-
let expected_data = [
251-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
252-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
253-
];
254-
compare_byte_array(@ba3, expected_data.span(), 1, 0x1f);
195+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
196+
assert_eq!(ba_concat, expected, "Error concat with overflow into pending word");
255197
}
256198

257199
// Test concat/append, split index of the words of the second byte array is 16.
258200
#[test]
259201
fn test_concat_split_index_16() {
260-
let ba1 = test_byte_array_16();
261-
let ba2 = test_byte_array_32();
202+
let ba_16: ByteArray = "ABCDEFGHIJKLMNO$";
203+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
262204

263-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
205+
let ba_concat = ByteArrayTrait::concat(@ba_16, @ba_32);
264206

265-
let expected_data = [0x0102030405060708091a0b0c0d0e0f100102030405060708091a0b0c0d0e0f];
266-
compare_byte_array(@ba3, expected_data.span(), 17, 0x101112131415161718191a1b1c1d1e1f20);
207+
let expected: ByteArray = "ABCDEFGHIJKLMNO$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
208+
assert_eq!(ba_concat, expected, "Error concat with split index 16");
267209
}
268210

269211
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
270212
#[test]
271213
fn test_concat_split_index_lt_16() {
272-
let ba1 = test_byte_array_1();
273-
let ba2 = test_byte_array_32();
214+
let ba_1: ByteArray = "$";
215+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
274216

275-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
217+
let ba_concat = ByteArrayTrait::concat(@ba_1, @ba_32);
276218

277-
let expected_data = [0x010102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e];
278-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
219+
let expected: ByteArray = "$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
220+
assert_eq!(ba_concat, expected, "Error concat with split index < 16");
279221
}
280222

281223
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
282224
#[test]
283225
fn test_concat_split_index_gt_16() {
284-
let ba1 = test_byte_array_30();
285-
let ba2 = test_byte_array_33();
226+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
227+
let ba_33: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
286228

287-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
229+
let ba_concat = ByteArrayTrait::concat(@ba_30, @ba_33);
288230

289-
let expected_data = [
290-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e01,
291-
0x02030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20,
292-
];
293-
compare_byte_array(@ba3, expected_data.span(), 1, 0x21);
231+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
232+
assert_eq!(ba_concat, expected, "Error concat with split index > 16");
294233
}
295234

296235
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
297236
#[test]
298237
fn test_concat_pending_sum_up_to_full() {
299-
let ba1 = test_byte_array_32();
300-
let ba2 = test_byte_array_30();
238+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
239+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
301240

302-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
241+
let ba_concat = ByteArrayTrait::concat(@ba_32, @ba_30);
303242

304-
let expected_data = [
305-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
306-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
307-
];
308-
compare_byte_array(@ba3, expected_data.span(), 0, 0);
243+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
244+
assert_eq!(ba_concat, expected, "Error concat with pending word sum up to full");
309245
}
310246

311247
// Sum of the lengths of the pending words of both byte arrays is 31+16.
312248
// That is, the pending words aggregate to a full word, and the last split index is 16.
313249
#[test]
314250
fn test_concat_pending_sum_up_to_more_than_word_16() {
315-
let ba1 = test_byte_array_17();
316-
let ba2 = test_byte_array_30();
251+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOP$";
252+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
317253

318-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
254+
let ba_concat = ByteArrayTrait::concat(@ba_17, @ba_30);
319255

320-
let expected_data = [0x0102030405060708091a0b0c0d0e0f10110102030405060708091a0b0c0d0e];
321-
compare_byte_array(@ba3, expected_data.span(), 16, 0x0f101112131415161718191a1b1c1d1e);
256+
let expected: ByteArray = "ABCDEFGHIJKLMNOP$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
257+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index 16");
322258
}
323259

324260
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
325261
// That is, the pending words aggregate to a full word, and the last split index is <16.
326262
#[test]
327263
fn test_concat_pending_sum_up_to_more_than_word_lt16() {
328-
let ba1 = test_byte_array_2();
329-
let ba2 = test_byte_array_30();
264+
let ba_2: ByteArray = "A$";
265+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
330266

331-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
267+
let ba_concat = ByteArrayTrait::concat(@ba_2, @ba_30);
332268

333-
let expected_data = [0x01020102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d];
334-
compare_byte_array(@ba3, expected_data.span(), 1, 0x1e);
269+
let expected: ByteArray = "A$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
270+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index < 16");
335271
}
336272

337273
// Sum of the lengths of the pending words of both byte arrays is >31+15
338274
// That is, the pending words aggregate to a full word, and the last split index is >16.
339275
#[test]
340276
fn test_concat_pending_sum_up_to_more_than_word_gt16() {
341-
let ba1 = test_byte_array_30();
342-
let ba2 = test_byte_array_30();
277+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
343278

344-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
279+
let ba_concat = ByteArrayTrait::concat(@ba_30, @ba_30);
345280

346-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e01];
347-
compare_byte_array(
348-
@ba3,
349-
expected_data.span(),
350-
29,
351-
0x02030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
352-
);
281+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
282+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index > 16");
353283
}
354284

355285
#[test]

0 commit comments

Comments
 (0)