@@ -3,258 +3,208 @@ use crate::test::test_utils::{assert_eq, assert_ne};
3
3
#[test]
4
4
fn test_append_byte () {
5
5
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 {
11
7
ba . append_byte (c );
12
- c += 1 ;
13
8
}
14
-
15
- let expected_data = [0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f ];
16
- compare_byte_array (@ ba , expected_data . span (), 2 , 0x2021 );
9
+ assert_eq! (ba , " 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
17
10
}
18
11
19
12
#[test]
20
13
fn test_append_word () {
21
14
let mut ba = Default :: default ();
22
15
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" );
27
19
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 " );
31
23
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" );
34
27
35
28
// 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 " );
38
31
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" );
45
35
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" );
48
39
}
49
40
50
41
#[test]
51
42
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$" ;
54
44
55
- ba1 . append (@ ba2 );
45
+ ba_32 . append (@ ba_32 );
56
46
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" );
62
49
}
63
50
64
51
// Same as test_append, but with `+=` instead of `append`.
65
52
#[test]
66
53
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$" ;
69
55
70
- ba1 += ba2 ;
56
+ ba_32 += ba_32 . clone () ;
71
57
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" );
77
60
}
78
61
62
+ // Same as test_append and test add_eq, but with `concat`.
79
63
#[test]
80
64
fn test_concat () {
81
- let ba1 = test_byte_array_32 ();
82
- let ba2 = test_byte_array_32 ();
65
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
83
66
84
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
67
+ let ba = ByteArrayTrait :: concat (@ ba_32 , @ ba_32 );
85
68
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" );
91
71
}
92
72
93
73
// Same as test_concat, but with `+` instead of `concat`.
94
74
#[test]
95
75
fn test_add () {
96
- let ba1 = test_byte_array_32 ();
97
- let ba2 = test_byte_array_32 ();
76
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
98
77
99
- let ba3 = ba1 + ba2 ;
78
+ let ba_32 = ba_32 . clone () + ba_32 ;
100
79
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" );
106
82
}
107
83
108
84
// Test concat/append, first byte array empty.
109
85
#[test]
110
86
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" );
118
90
}
119
91
120
92
// Test concat/append, second byte array empty.
121
93
#[test]
122
94
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" );
130
98
}
131
99
132
100
// Test concat/append, first byte array pending word is empty.
133
101
#[test]
134
102
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$ " ;
137
105
138
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
106
+ let ba_concat = ByteArrayTrait :: concat (@ ba_31 , @ ba_32 );
139
107
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" );
145
110
}
146
111
147
112
// Test concat/append, second byte array pending word is empty.
148
113
#[test]
149
114
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 " ;
152
117
153
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
118
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_31 );
154
119
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" );
160
122
}
161
123
162
124
// Test concat/append, split index of the words of the second byte array is 16.
163
125
#[test]
164
126
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 " ;
167
129
168
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
130
+ let ba_concat = ByteArrayTrait :: concat (@ ba_16 , @ ba_32 );
169
131
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 " );
172
134
}
173
135
174
136
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
175
137
#[test]
176
138
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 " ;
179
141
180
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
142
+ let ba_concat = ByteArrayTrait :: concat (@ ba_1 , @ ba_32 );
181
143
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 " );
184
146
}
185
147
186
148
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
187
149
#[test]
188
150
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 " ;
191
153
192
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
154
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_33 );
193
155
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" );
199
158
}
200
159
201
160
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
202
161
#[test]
203
162
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 " ;
206
165
207
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
166
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_30 );
208
167
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" );
214
170
}
215
171
216
172
// Sum of the lengths of the pending words of both byte arrays is 31+16.
217
173
// That is, the pending words aggregate to a full word, and the last split index is 16.
218
174
#[test]
219
175
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 " ;
222
178
223
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
179
+ let ba_concat = ByteArrayTrait :: concat (@ ba_17 , @ ba_30 );
224
180
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 " );
227
183
}
228
184
229
185
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
230
186
// That is, the pending words aggregate to a full word, and the last split index is <16.
231
187
#[test]
232
188
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 " ;
235
191
236
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
192
+ let ba_concat = ByteArrayTrait :: concat (@ ba_2 , @ ba_30 );
237
193
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 " );
240
196
}
241
197
242
198
// Sum of the lengths of the pending words of both byte arrays is >31+15
243
199
// That is, the pending words aggregate to a full word, and the last split index is >16.
244
200
#[test]
245
201
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$" ;
248
203
249
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
204
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_30 );
250
205
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" );
258
208
}
259
209
260
210
#[test]
@@ -505,30 +455,6 @@ fn test_from_collect() {
505
455
506
456
// ========= Test helper functions =========
507
457
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
-
532
458
fn compare_spans <T , + crate :: fmt :: Debug <T >, + PartialEq <T >, + Copy <T >, + Drop <T >>(
533
459
mut a : Span <T >, mut b : Span <T >,
534
460
) {
@@ -558,12 +484,6 @@ fn test_byte_array_2() -> ByteArray {
558
484
ba1
559
485
}
560
486
561
- fn test_byte_array_16 () -> ByteArray {
562
- let mut ba1 = Default :: default ();
563
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f10 , 16 );
564
- ba1
565
- }
566
-
567
487
fn test_byte_array_17 () -> ByteArray {
568
488
let mut ba1 = Default :: default ();
569
489
ba1 . append_word (0x0102030405060708091a0b0c0d0e0f1011 , 17 );
0 commit comments