Skip to content

Commit 195103b

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 195103b

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
@@ -4,257 +4,217 @@ use crate::test::test_utils::{assert_eq, assert_ne};
44
fn test_append_byte() {
55
let mut ba = Default::default();
66
let mut c = 1_u8;
7-
loop {
8-
if c == 34 {
9-
break;
10-
}
7+
while c < 34 {
118
ba.append_byte(c);
129
c += 1;
1310
}
1411

15-
let expected_data = [0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f];
16-
compare_byte_array(@ba, expected_data.span(), 2, 0x2021);
12+
let expected: ByteArray = array![
13+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
15+
0x1f, 0x20, 0x21,
16+
]
17+
.into_iter()
18+
.collect();
19+
assert_eq!(ba, expected, "append_byte error");
1720
}
1821

1922
#[test]
2023
fn test_append_word() {
2124
let mut ba = Default::default();
2225

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

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

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

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

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);
42+
ba.append_word('ABCDEFGHIJKLMNOPQRSTUVWXYZa', 27);
43+
expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiABCDEFGHIJKLMNOPQRSTUVWXYZa";
44+
assert_eq!(ba, expected, "append word filling pending to capacity");
4545

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

5051
#[test]
5152
fn test_append() {
52-
let mut ba1 = test_byte_array_32();
53-
let ba2 = test_byte_array_32();
53+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
5454

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

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

6461
// Same as test_append, but with `+=` instead of `append`.
6562
#[test]
6663
fn test_add_eq() {
67-
let mut ba1 = test_byte_array_32();
68-
let ba2 = test_byte_array_32();
64+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
6965

70-
ba1 += ba2;
66+
ba_32 += ba_32.clone();
7167

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

72+
// Same as test_append and test add_eq, but with `concat`.
7973
#[test]
8074
fn test_concat() {
81-
let ba1 = test_byte_array_32();
82-
let ba2 = test_byte_array_32();
75+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
8376

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

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

9383
// Same as test_concat, but with `+` instead of `concat`.
9484
#[test]
9585
fn test_add() {
96-
let ba1 = test_byte_array_32();
97-
let ba2 = test_byte_array_32();
86+
let mut ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
9887

99-
let ba3 = ba1 + ba2;
88+
let ba_32 = ba_32.clone() + ba_32;
10089

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

10894
// Test concat/append, first byte array empty.
10995
#[test]
11096
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);
97+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
98+
let ba_concat = ByteArrayTrait::concat(@Default::default(), @ba_32);
99+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
118100
}
119101

120102
// Test concat/append, second byte array empty.
121103
#[test]
122104
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);
105+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
106+
let ba_concat = ByteArrayTrait::concat(@ba_32, @Default::default());
107+
assert_eq!(ba_concat, ba_32, "Error concat empty ba");
130108
}
131109

132110
// Test concat/append, first byte array pending word is empty.
133111
#[test]
134112
fn test_concat_first_pending_0() {
135-
let ba1 = test_byte_array_31();
136-
let ba2 = test_byte_array_32();
113+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
114+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
137115

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

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

147122
// Test concat/append, second byte array pending word is empty.
148123
#[test]
149124
fn test_concat_second_pending_0() {
150-
let ba1 = test_byte_array_32();
151-
let ba2 = test_byte_array_31();
125+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
126+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
152127

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

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

162134
// Test concat/append, split index of the words of the second byte array is 16.
163135
#[test]
164136
fn test_concat_split_index_16() {
165-
let ba1 = test_byte_array_16();
166-
let ba2 = test_byte_array_32();
137+
let ba_16: ByteArray = "ABCDEFGHIJKLMNO$";
138+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
167139

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

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

174146
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
175147
#[test]
176148
fn test_concat_split_index_lt_16() {
177-
let ba1 = test_byte_array_1();
178-
let ba2 = test_byte_array_32();
149+
let ba_1: ByteArray = "$";
150+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
179151

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

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

186158
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
187159
#[test]
188160
fn test_concat_split_index_gt_16() {
189-
let ba1 = test_byte_array_30();
190-
let ba2 = test_byte_array_33();
161+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
162+
let ba_33: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
191163

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

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

201170
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
202171
#[test]
203172
fn test_concat_pending_sum_up_to_full() {
204-
let ba1 = test_byte_array_32();
205-
let ba2 = test_byte_array_30();
173+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$";
174+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
206175

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

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

216182
// Sum of the lengths of the pending words of both byte arrays is 31+16.
217183
// That is, the pending words aggregate to a full word, and the last split index is 16.
218184
#[test]
219185
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();
186+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOP$";
187+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
222188

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

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

229195
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
230196
// That is, the pending words aggregate to a full word, and the last split index is <16.
231197
#[test]
232198
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();
199+
let ba_2: ByteArray = "A$";
200+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcd";
235201

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

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

242208
// Sum of the lengths of the pending words of both byte arrays is >31+15
243209
// That is, the pending words aggregate to a full word, and the last split index is >16.
244210
#[test]
245211
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();
212+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
248213

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

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

260220
#[test]
@@ -505,30 +465,6 @@ fn test_from_collect() {
505465

506466
// ========= Test helper functions =========
507467

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-
532468
fn compare_spans<T, +crate::fmt::Debug<T>, +PartialEq<T>, +Copy<T>, +Drop<T>>(
533469
mut a: Span<T>, mut b: Span<T>,
534470
) {
@@ -558,12 +494,6 @@ fn test_byte_array_2() -> ByteArray {
558494
ba1
559495
}
560496

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

0 commit comments

Comments
 (0)