@@ -2,30 +2,6 @@ use crate::test::test_utils::{assert_eq, assert_ne};
2
2
3
3
// ========= Test-utils =========
4
4
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
-
29
5
fn compare_spans <T , + crate :: fmt :: Debug <T >, + PartialEq <T >, + Copy <T >, + Drop <T >>(
30
6
mut a : Span <T >, mut b : Span <T >,
31
7
) {
@@ -55,12 +31,6 @@ fn test_byte_array_2() -> ByteArray {
55
31
ba1
56
32
}
57
33
58
- fn test_byte_array_16 () -> ByteArray {
59
- let mut ba1 = Default :: default ();
60
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f10 , 16 );
61
- ba1
62
- }
63
-
64
34
fn test_byte_array_17 () -> ByteArray {
65
35
let mut ba1 = Default :: default ();
66
36
ba1 . append_word (0x0102030405060708091a0b0c0d0e0f1011 , 17 );
@@ -99,257 +69,217 @@ fn test_byte_array_33() -> ByteArray {
99
69
fn test_append_byte () {
100
70
let mut ba = Default :: default ();
101
71
let mut c = 1_u8 ;
102
- loop {
103
- if c == 34 {
104
- break ;
105
- }
72
+ while c < 34 {
106
73
ba . append_byte (c );
107
74
c += 1 ;
108
75
}
109
76
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" );
112
85
}
113
86
114
87
#[test]
115
88
fn test_append_word () {
116
89
let mut ba = Default :: default ();
117
90
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" );
122
94
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 " );
126
98
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" );
129
102
130
103
// 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 " );
133
106
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" );
140
110
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" );
143
114
}
144
115
145
116
#[test]
146
117
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$" ;
149
119
150
- ba1 . append (@ ba2 );
120
+ ba_32 . append (@ ba_32 );
151
121
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" );
157
124
}
158
125
159
126
// Same as test_append, but with `+=` instead of `append`.
160
127
#[test]
161
128
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$" ;
164
130
165
- ba1 += ba2 ;
131
+ ba_32 += ba_32 . clone () ;
166
132
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" );
172
135
}
173
136
137
+ // Same as test_append and test add_eq, but with `concat`.
174
138
#[test]
175
139
fn test_concat () {
176
- let ba1 = test_byte_array_32 ();
177
- let ba2 = test_byte_array_32 ();
140
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
178
141
179
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
142
+ let ba = ByteArrayTrait :: concat (@ ba_32 , @ ba_32 );
180
143
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" );
186
146
}
187
147
188
148
// Same as test_concat, but with `+` instead of `concat`.
189
149
#[test]
190
150
fn test_add () {
191
- let ba1 = test_byte_array_32 ();
192
- let ba2 = test_byte_array_32 ();
151
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
193
152
194
- let ba3 = ba1 + ba2 ;
153
+ let ba_32 = ba_32 . clone () + ba_32 ;
195
154
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" );
201
157
}
202
158
203
159
// Test concat/append, first byte array empty.
204
160
#[test]
205
161
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" );
213
165
}
214
166
215
167
// Test concat/append, second byte array empty.
216
168
#[test]
217
169
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" );
225
173
}
226
174
227
175
// Test concat/append, first byte array pending word is empty.
228
176
#[test]
229
177
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$ " ;
232
180
233
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
181
+ let ba_concat = ByteArrayTrait :: concat (@ ba_31 , @ ba_32 );
234
182
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" );
240
185
}
241
186
242
187
// Test concat/append, second byte array pending word is empty.
243
188
#[test]
244
189
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 " ;
247
192
248
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
193
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_31 );
249
194
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" );
255
197
}
256
198
257
199
// Test concat/append, split index of the words of the second byte array is 16.
258
200
#[test]
259
201
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 " ;
262
204
263
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
205
+ let ba_concat = ByteArrayTrait :: concat (@ ba_16 , @ ba_32 );
264
206
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 " );
267
209
}
268
210
269
211
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
270
212
#[test]
271
213
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 " ;
274
216
275
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
217
+ let ba_concat = ByteArrayTrait :: concat (@ ba_1 , @ ba_32 );
276
218
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 " );
279
221
}
280
222
281
223
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
282
224
#[test]
283
225
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 " ;
286
228
287
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
229
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_33 );
288
230
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" );
294
233
}
295
234
296
235
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
297
236
#[test]
298
237
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 " ;
301
240
302
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
241
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_30 );
303
242
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" );
309
245
}
310
246
311
247
// Sum of the lengths of the pending words of both byte arrays is 31+16.
312
248
// That is, the pending words aggregate to a full word, and the last split index is 16.
313
249
#[test]
314
250
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 " ;
317
253
318
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
254
+ let ba_concat = ByteArrayTrait :: concat (@ ba_17 , @ ba_30 );
319
255
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 " );
322
258
}
323
259
324
260
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
325
261
// That is, the pending words aggregate to a full word, and the last split index is <16.
326
262
#[test]
327
263
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 " ;
330
266
331
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
267
+ let ba_concat = ByteArrayTrait :: concat (@ ba_2 , @ ba_30 );
332
268
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 " );
335
271
}
336
272
337
273
// Sum of the lengths of the pending words of both byte arrays is >31+15
338
274
// That is, the pending words aggregate to a full word, and the last split index is >16.
339
275
#[test]
340
276
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$" ;
343
278
344
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
279
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_30 );
345
280
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" );
353
283
}
354
284
355
285
#[test]
0 commit comments