Skip to content

Commit c8e567b

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 dca12ec commit c8e567b

File tree

1 file changed

+86
-166
lines changed

1 file changed

+86
-166
lines changed

corelib/src/test/byte_array_test.cairo

Lines changed: 86 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -3,258 +3,208 @@ use crate::test::test_utils::{assert_eq, assert_ne};
33
#[test]
44
fn test_append_byte() {
55
let mut ba = Default::default();
6-
let mut c = 1_u8;
7-
loop {
8-
if c == 34 {
9-
break;
10-
}
6+
for c in '0'_u8..='Z'_u8 {
117
ba.append_byte(c);
12-
c += 1;
138
}
14-
15-
let expected_data = [0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
16-
compare_byte_array(@ba, expected_data.span(), 2, 0x2021);
9+
assert_eq!(ba, "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ");
1710
}
1811

1912
#[test]
2013
fn test_append_word() {
2114
let mut ba = Default::default();
2215

23-
ba.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e, 30);
24-
compare_byte_array(
25-
@ba, [].span(), 30, 0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
26-
);
16+
ba.append_word('ABCDEFGHIJKLMNOPQRSTUVWXYZabcd', 30);
17+
let mut expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
18+
assert_eq!(ba, expected, "appending word in single bytes31");
2719

28-
ba.append_word(0x1f2021, 3);
29-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
30-
compare_byte_array(@ba, expected_data.span(), 2, 0x2021);
20+
ba.append_word('efg', 3);
21+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
22+
assert_eq!(ba, expected, "append word overflowing pending word");
3123

32-
ba.append_word(0x2223, 2);
33-
compare_byte_array(@ba, expected_data.span(), 4, 0x20212223);
24+
ba.append_word('hi', 2);
25+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi";
26+
assert_eq!(ba, expected, "append word extending new pending word");
3427

3528
// Length is 0, so nothing is actually appended.
36-
ba.append_word(0xffee, 0);
37-
compare_byte_array(@ba, expected_data.span(), 4, 0x20212223);
29+
ba.append_word('jk', 0);
30+
assert_eq!(ba, expected, "append 0 length error");
3831

39-
ba.append_word(0x2425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e, 27);
40-
let expected_data = [
41-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
42-
0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e,
43-
];
44-
compare_byte_array(@ba, expected_data.span(), 0, 0);
32+
ba.append_word('ABCDEFGHIJKLMNOPQRSTUVWXYZa', 27);
33+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiABCDEFGHIJKLMNOPQRSTUVWXYZa";
34+
assert_eq!(ba, expected, "append word filling pending to capacity");
4535

46-
ba.append_word(0x3f, 1);
47-
compare_byte_array(@ba, expected_data.span(), 1, 0x3f);
36+
ba.append_word('b', 1);
37+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiABCDEFGHIJKLMNOPQRSTUVWXYZab";
38+
assert_eq!(ba, expected, "append word starting new pending word");
4839
}
4940

5041
#[test]
5142
fn test_append() {
52-
let mut ba1 = test_byte_array_32();
53-
let ba2 = test_byte_array_32();
43+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
5444

55-
ba1.append(@ba2);
45+
ba_32.append(@ba_32);
5646

57-
let expected_data = [
58-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
59-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
60-
];
61-
compare_byte_array(@ba1, expected_data.span(), 2, 0x1f20);
47+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
48+
assert_eq!(ba_32, expected, "append bytearray across new pending word");
6249
}
6350

6451
// Same as test_append, but with `+=` instead of `append`.
6552
#[test]
6653
fn test_add_eq() {
67-
let mut ba1 = test_byte_array_32();
68-
let ba2 = test_byte_array_32();
54+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
6955

70-
ba1 += ba2;
56+
ba_32 += ba_32.clone();
7157

72-
let expected_data = [
73-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
74-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
75-
];
76-
compare_byte_array(@ba1, expected_data.span(), 2, 0x1f20);
58+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
59+
assert_eq!(ba_32, expected, "add-eq bytearray across new pending word");
7760
}
7861

62+
// Same as test_append and test add_eq, but with `concat`.
7963
#[test]
8064
fn test_concat() {
81-
let ba1 = test_byte_array_32();
82-
let ba2 = test_byte_array_32();
65+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
8366

84-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
67+
let ba = ByteArrayTrait::concat(@ba_32, @ba_32);
8568

86-
let expected_data = [
87-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
88-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
89-
];
90-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
69+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
70+
assert_eq!(ba, expected, "add-eq bytearray across new pending word");
9171
}
9272

9373
// Same as test_concat, but with `+` instead of `concat`.
9474
#[test]
9575
fn test_add() {
96-
let ba1 = test_byte_array_32();
97-
let ba2 = test_byte_array_32();
76+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
9877

99-
let ba3 = ba1 + ba2;
78+
let ba_32 = ba_32.clone() + ba_32;
10079

101-
let expected_data = [
102-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
103-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
104-
];
105-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
80+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
81+
assert_eq!(ba_32, expected, "add-eq bytearray across new pending word");
10682
}
10783

10884
// Test concat/append, first byte array empty.
10985
#[test]
11086
fn test_concat_first_empty() {
111-
let ba1 = Default::default();
112-
let ba2 = test_byte_array_32();
113-
114-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
115-
116-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
117-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
87+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
88+
let ba_concat = ByteArrayTrait::concat(@Default::default(), @ba_32);
89+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
11890
}
11991

12092
// Test concat/append, second byte array empty.
12193
#[test]
12294
fn test_concat_second_empty() {
123-
let ba1 = test_byte_array_32();
124-
let ba2 = Default::default();
125-
126-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
127-
128-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
129-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
95+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
96+
let ba_concat = ByteArrayTrait::concat(@ba_32, @Default::default());
97+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
13098
}
13199

132100
// Test concat/append, first byte array pending word is empty.
133101
#[test]
134102
fn test_concat_first_pending_0() {
135-
let ba1 = test_byte_array_31();
136-
let ba2 = test_byte_array_32();
103+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
104+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
137105

138-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
106+
let ba_concat = ByteArrayTrait::concat(@ba_31, @ba_32);
139107

140-
let expected_data = [
141-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
142-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
143-
];
144-
compare_byte_array(@ba3, expected_data.span(), 1, 0x20);
108+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
109+
assert_eq!(ba_concat, expected, "Error concat with overflow into pending word");
145110
}
146111

147112
// Test concat/append, second byte array pending word is empty.
148113
#[test]
149114
fn test_concat_second_pending_0() {
150-
let ba1 = test_byte_array_32();
151-
let ba2 = test_byte_array_31();
115+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
116+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
152117

153-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
118+
let ba_concat = ByteArrayTrait::concat(@ba_32, @ba_31);
154119

155-
let expected_data = [
156-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
157-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
158-
];
159-
compare_byte_array(@ba3, expected_data.span(), 1, 0x1f);
120+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
121+
assert_eq!(ba_concat, expected, "Error concat with overflow into pending word");
160122
}
161123

162124
// Test concat/append, split index of the words of the second byte array is 16.
163125
#[test]
164126
fn test_concat_split_index_16() {
165-
let ba1 = test_byte_array_16();
166-
let ba2 = test_byte_array_32();
127+
let ba_16: ByteArray = "ABCDEFGHIJKLMNO$";
128+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
167129

168-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
130+
let ba_concat = ByteArrayTrait::concat(@ba_16, @ba_32);
169131

170-
let expected_data = [0x0102030405060708091a0b0c0d0e0f100102030405060708091a0b0c0d0e0f];
171-
compare_byte_array(@ba3, expected_data.span(), 17, 0x101112131415161718191a1b1c1d1e1f20);
132+
let expected: ByteArray = "ABCDEFGHIJKLMNO$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
133+
assert_eq!(ba_concat, expected, "Error concat with split index 16");
172134
}
173135

174136
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
175137
#[test]
176138
fn test_concat_split_index_lt_16() {
177-
let ba1 = test_byte_array_1();
178-
let ba2 = test_byte_array_32();
139+
let ba_1: ByteArray = "$";
140+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
179141

180-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
142+
let ba_concat = ByteArrayTrait::concat(@ba_1, @ba_32);
181143

182-
let expected_data = [0x010102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e];
183-
compare_byte_array(@ba3, expected_data.span(), 2, 0x1f20);
144+
let expected: ByteArray = "$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
145+
assert_eq!(ba_concat, expected, "Error concat with split index < 16");
184146
}
185147

186148
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
187149
#[test]
188150
fn test_concat_split_index_gt_16() {
189-
let ba1 = test_byte_array_30();
190-
let ba2 = test_byte_array_33();
151+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
152+
let ba_33: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
191153

192-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
154+
let ba_concat = ByteArrayTrait::concat(@ba_30, @ba_33);
193155

194-
let expected_data = [
195-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e01,
196-
0x02030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20,
197-
];
198-
compare_byte_array(@ba3, expected_data.span(), 1, 0x21);
156+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
157+
assert_eq!(ba_concat, expected, "Error concat with split index > 16");
199158
}
200159

201160
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
202161
#[test]
203162
fn test_concat_pending_sum_up_to_full() {
204-
let ba1 = test_byte_array_32();
205-
let ba2 = test_byte_array_30();
163+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
164+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
206165

207-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
166+
let ba_concat = ByteArrayTrait::concat(@ba_32, @ba_30);
208167

209-
let expected_data = [
210-
0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f,
211-
0x200102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
212-
];
213-
compare_byte_array(@ba3, expected_data.span(), 0, 0);
168+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
169+
assert_eq!(ba_concat, expected, "Error concat with pending word sum up to full");
214170
}
215171

216172
// Sum of the lengths of the pending words of both byte arrays is 31+16.
217173
// That is, the pending words aggregate to a full word, and the last split index is 16.
218174
#[test]
219175
fn test_concat_pending_sum_up_to_more_than_word_16() {
220-
let ba1 = test_byte_array_17();
221-
let ba2 = test_byte_array_30();
176+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOP$";
177+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
222178

223-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
179+
let ba_concat = ByteArrayTrait::concat(@ba_17, @ba_30);
224180

225-
let expected_data = [0x0102030405060708091a0b0c0d0e0f10110102030405060708091a0b0c0d0e];
226-
compare_byte_array(@ba3, expected_data.span(), 16, 0x0f101112131415161718191a1b1c1d1e);
181+
let expected: ByteArray = "ABCDEFGHIJKLMNOP$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
182+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index 16");
227183
}
228184

229185
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
230186
// That is, the pending words aggregate to a full word, and the last split index is <16.
231187
#[test]
232188
fn test_concat_pending_sum_up_to_more_than_word_lt16() {
233-
let ba1 = test_byte_array_2();
234-
let ba2 = test_byte_array_30();
189+
let ba_2: ByteArray = "A$";
190+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
235191

236-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
192+
let ba_concat = ByteArrayTrait::concat(@ba_2, @ba_30);
237193

238-
let expected_data = [0x01020102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d];
239-
compare_byte_array(@ba3, expected_data.span(), 1, 0x1e);
194+
let expected: ByteArray = "A$ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
195+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index < 16");
240196
}
241197

242198
// Sum of the lengths of the pending words of both byte arrays is >31+15
243199
// That is, the pending words aggregate to a full word, and the last split index is >16.
244200
#[test]
245201
fn test_concat_pending_sum_up_to_more_than_word_gt16() {
246-
let ba1 = test_byte_array_30();
247-
let ba2 = test_byte_array_30();
202+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
248203

249-
let ba3 = ByteArrayTrait::concat(@ba1, @ba2);
204+
let ba_concat = ByteArrayTrait::concat(@ba_30, @ba_30);
250205

251-
let expected_data = [0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e01];
252-
compare_byte_array(
253-
@ba3,
254-
expected_data.span(),
255-
29,
256-
0x02030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e,
257-
);
206+
let expected: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
207+
assert_eq!(ba_concat, expected, "Error pending word overflowed concat with split index > 16");
258208
}
259209

260210
#[test]
@@ -505,30 +455,6 @@ fn test_from_collect() {
505455

506456
// ========= Test helper functions =========
507457

508-
fn compare_byte_array(
509-
ba: @ByteArray, mut data: Span<felt252>, pending_word_len: usize, pending_word: felt252,
510-
) {
511-
assert(ba.data.len() == data.len(), 'wrong data len');
512-
let mut ba_data = ba.data.span();
513-
514-
let mut data_index = 0;
515-
loop {
516-
match ba_data.pop_front() {
517-
Some(x) => {
518-
let actual_word = (*x).into();
519-
let expected_word = *data.pop_front().unwrap();
520-
assert_eq!(actual_word, expected_word, "wrong data for index: {data_index}");
521-
},
522-
None(_) => { break; },
523-
}
524-
data_index += 1;
525-
}
526-
527-
assert_eq!(*ba.pending_word_len, pending_word_len);
528-
let ba_pending_word_felt: felt252 = (*ba.pending_word).into();
529-
assert_eq!(ba_pending_word_felt, pending_word);
530-
}
531-
532458
fn compare_spans<T, +crate::fmt::Debug<T>, +PartialEq<T>, +Copy<T>, +Drop<T>>(
533459
mut a: Span<T>, mut b: Span<T>,
534460
) {
@@ -558,12 +484,6 @@ fn test_byte_array_2() -> ByteArray {
558484
ba1
559485
}
560486

561-
fn test_byte_array_16() -> ByteArray {
562-
let mut ba1 = Default::default();
563-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f10, 16);
564-
ba1
565-
}
566-
567487
fn test_byte_array_17() -> ByteArray {
568488
let mut ba1 = Default::default();
569489
ba1.append_word(0x0102030405060708091a0b0c0d0e0f1011, 17);

0 commit comments

Comments
 (0)