@@ -4,257 +4,217 @@ use crate::test::test_utils::{assert_eq, assert_ne};
4
4
fn test_append_byte () {
5
5
let mut ba = Default :: default ();
6
6
let mut c = 1_u8 ;
7
- loop {
8
- if c == 34 {
9
- break ;
10
- }
7
+ while c < 34 {
11
8
ba . append_byte (c );
12
9
c += 1 ;
13
10
}
14
11
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" );
17
20
}
18
21
19
22
#[test]
20
23
fn test_append_word () {
21
24
let mut ba = Default :: default ();
22
25
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" );
27
29
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 " );
31
33
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" );
34
37
35
38
// 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 " );
38
41
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" );
45
45
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" );
48
49
}
49
50
50
51
#[test]
51
52
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$" ;
54
54
55
- ba1 . append (@ ba2 );
55
+ ba_32 . append (@ ba_32 );
56
56
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" );
62
59
}
63
60
64
61
// Same as test_append, but with `+=` instead of `append`.
65
62
#[test]
66
63
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$" ;
69
65
70
- ba1 += ba2 ;
66
+ ba_32 += ba_32 . clone () ;
71
67
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" );
77
70
}
78
71
72
+ // Same as test_append and test add_eq, but with `concat`.
79
73
#[test]
80
74
fn test_concat () {
81
- let ba1 = test_byte_array_32 ();
82
- let ba2 = test_byte_array_32 ();
75
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
83
76
84
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
77
+ let ba = ByteArrayTrait :: concat (@ ba_32 , @ ba_32 );
85
78
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" );
91
81
}
92
82
93
83
// Same as test_concat, but with `+` instead of `concat`.
94
84
#[test]
95
85
fn test_add () {
96
- let ba1 = test_byte_array_32 ();
97
- let ba2 = test_byte_array_32 ();
86
+ let mut ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde$" ;
98
87
99
- let ba3 = ba1 + ba2 ;
88
+ let ba_32 = ba_32 . clone () + ba_32 ;
100
89
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" );
106
92
}
107
93
108
94
// Test concat/append, first byte array empty.
109
95
#[test]
110
96
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" );
118
100
}
119
101
120
102
// Test concat/append, second byte array empty.
121
103
#[test]
122
104
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" );
130
108
}
131
109
132
110
// Test concat/append, first byte array pending word is empty.
133
111
#[test]
134
112
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$ " ;
137
115
138
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
116
+ let ba_concat = ByteArrayTrait :: concat (@ ba_31 , @ ba_32 );
139
117
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" );
145
120
}
146
121
147
122
// Test concat/append, second byte array pending word is empty.
148
123
#[test]
149
124
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 " ;
152
127
153
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
128
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_31 );
154
129
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" );
160
132
}
161
133
162
134
// Test concat/append, split index of the words of the second byte array is 16.
163
135
#[test]
164
136
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 " ;
167
139
168
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
140
+ let ba_concat = ByteArrayTrait :: concat (@ ba_16 , @ ba_32 );
169
141
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 " );
172
144
}
173
145
174
146
// Test concat/append, split index of the words of the second byte array is < 16, specifically 1.
175
147
#[test]
176
148
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 " ;
179
151
180
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
152
+ let ba_concat = ByteArrayTrait :: concat (@ ba_1 , @ ba_32 );
181
153
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 " );
184
156
}
185
157
186
158
// Test concat/append, split index of the words of the second byte array is > 16, specifically 30.
187
159
#[test]
188
160
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 " ;
191
163
192
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
164
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_33 );
193
165
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" );
199
168
}
200
169
201
170
// Sum of the lengths of the pending words of both byte arrays is 31 (a full word).
202
171
#[test]
203
172
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 " ;
206
175
207
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
176
+ let ba_concat = ByteArrayTrait :: concat (@ ba_32 , @ ba_30 );
208
177
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" );
214
180
}
215
181
216
182
// Sum of the lengths of the pending words of both byte arrays is 31+16.
217
183
// That is, the pending words aggregate to a full word, and the last split index is 16.
218
184
#[test]
219
185
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 " ;
222
188
223
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
189
+ let ba_concat = ByteArrayTrait :: concat (@ ba_17 , @ ba_30 );
224
190
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 " );
227
193
}
228
194
229
195
// Sum of the lengths of the pending words of both byte arrays is in [32, 31+15].
230
196
// That is, the pending words aggregate to a full word, and the last split index is <16.
231
197
#[test]
232
198
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 " ;
235
201
236
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
202
+ let ba_concat = ByteArrayTrait :: concat (@ ba_2 , @ ba_30 );
237
203
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 " );
240
206
}
241
207
242
208
// Sum of the lengths of the pending words of both byte arrays is >31+15
243
209
// That is, the pending words aggregate to a full word, and the last split index is >16.
244
210
#[test]
245
211
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$" ;
248
213
249
- let ba3 = ByteArrayTrait :: concat (@ ba1 , @ ba2 );
214
+ let ba_concat = ByteArrayTrait :: concat (@ ba_30 , @ ba_30 );
250
215
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" );
258
218
}
259
219
260
220
#[test]
@@ -505,30 +465,6 @@ fn test_from_collect() {
505
465
506
466
// ========= Test helper functions =========
507
467
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
468
fn compare_spans <T , + crate :: fmt :: Debug <T >, + PartialEq <T >, + Copy <T >, + Drop <T >>(
533
469
mut a : Span <T >, mut b : Span <T >,
534
470
) {
@@ -558,12 +494,6 @@ fn test_byte_array_2() -> ByteArray {
558
494
ba1
559
495
}
560
496
561
- fn test_byte_array_16 () -> ByteArray {
562
- let mut ba1 = Default :: default ();
563
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f10 , 16 );
564
- ba1
565
- }
566
-
567
497
fn test_byte_array_17 () -> ByteArray {
568
498
let mut ba1 = Default :: default ();
569
499
ba1 . append_word (0x0102030405060708091a0b0c0d0e0f1011 , 17 );
0 commit comments