1
- use crate :: test :: test_utils :: {assert_eq, assert_ne};
1
+ use core :: test :: test_utils :: {assert_eq, assert_ne};
2
2
3
3
#[test]
4
4
fn test_append_byte () {
@@ -210,84 +210,94 @@ fn test_concat_pending_sum_up_to_more_than_word_gt16() {
210
210
#[test]
211
211
fn test_len () {
212
212
let ba : ByteArray = Default :: default ();
213
- assert (ba . len () == 0 , ' wrong ByteArray len' );
213
+ assert_eq! (ba . len (), 0 , " wrong ByteArray len" );
214
214
215
- let mut ba = test_byte_array_33 ();
216
- assert (ba . len () == 33 , ' wrong ByteArray len' );
215
+ let mut ba_33 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef$" ;
216
+ let mut ba = ba_33 ;
217
+ assert_eq! (ba . len (), 33 , " wrong ByteArray len" );
217
218
218
- ba . append (@ test_byte_array_30 ());
219
- assert (ba . len () == 63 , ' wrong ByteArray len' );
219
+ let ba_30 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabc$" ;
220
+ ba . append (@ ba_30 );
221
+ assert_eq! (ba . len (), 63 , " wrong ByteArray len" );
220
222
}
221
223
222
224
#[test]
223
225
fn test_at_empty () {
224
226
let ba : ByteArray = Default :: default ();
225
227
226
- assert (ba . at (0 ) == None , ' index 0 is not out of bounds' );
227
- assert (ba . at (1 ) == None , ' index 1 is not out of bounds' );
228
- assert (ba . at (30 ) == None , ' index 30 is not out of bounds' );
229
- assert (ba . at (31 ) == None , ' index 31 is not out of bounds' );
228
+ assert_eq! (ba . at (0 ), None , " index 0 is not out of bounds" );
229
+ assert_eq! (ba . at (1 ), None , " index 1 is not out of bounds" );
230
+ assert_eq! (ba . at (30 ), None , " index 30 is not out of bounds" );
231
+ assert_eq! (ba . at (31 ), None , " index 31 is not out of bounds" );
230
232
}
231
233
232
234
#[test]
233
235
fn test_at () {
234
- let mut ba = test_byte_array_31 ();
235
- ba . append (@ test_byte_array_31 ());
236
- ba . append (@ test_byte_array_17 ());
237
-
238
- assert (ba . at (0 ) == Some (0x01 ), ' wrong byte at index 0' );
239
- assert (ba . at (1 ) == Some (0x02 ), ' wrong byte at index 1' );
240
- assert (ba . at (2 ) == Some (0x03 ), ' wrong byte at index 2' );
241
- assert (ba . at (14 ) == Some (0x0f ), ' wrong byte at index 14' );
242
- assert (ba . at (15 ) == Some (0x10 ), ' wrong byte at index 15' );
243
- assert (ba . at (16 ) == Some (0x11 ), ' wrong byte at index 16' );
244
- assert (ba . at (17 ) == Some (0x12 ), ' wrong byte at index 17' );
245
- assert (ba . at (29 ) == Some (0x1e ), ' wrong byte at index 29' );
246
- assert (ba . at (30 ) == Some (0x1f ), ' wrong byte at index 30' );
247
- assert (ba . at (31 ) == Some (0x01 ), ' wrong byte at index 31' );
248
- assert (ba . at (32 ) == Some (0x02 ), ' wrong byte at index 32' );
249
- assert (ba . at (61 ) == Some (0x1f ), ' wrong byte at index 61' );
250
- assert (ba . at (62 ) == Some (0x01 ), ' wrong byte at index 62' );
251
- assert (ba . at (63 ) == Some (0x02 ), ' wrong byte at index 63' );
252
- assert (ba . at (76 ) == Some (0x0f ), ' wrong byte at index 76' );
253
- assert (ba . at (77 ) == Some (0x10 ), ' wrong byte at index 77' );
254
- assert (ba . at (78 ) == Some (0x11 ), ' wrong byte at index 78' );
255
- assert (ba . at (79 ) == None , ' index 79 is not out of bounds' );
236
+ let ba_31 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde" ;
237
+ let mut ba = ba_31 . clone ();
238
+ ba . append (@ ba_31 );
239
+
240
+ let ba_17 : ByteArray = " ABCDEFGHIJKLMNOPQ" ;
241
+ ba . append (@ ba_17 );
242
+
243
+ assert_eq! (ba . at (0 ), Some (' A' ), " wrong byte at index 0" );
244
+ assert_eq! (ba . at (1 ), Some (' B' ), " wrong byte at index 1" );
245
+ assert_eq! (ba . at (2 ), Some (' C' ), " wrong byte at index 2" );
246
+ assert_eq! (ba . at (14 ), Some (' O' ), " wrong byte at index 14" );
247
+ assert_eq! (ba . at (15 ), Some (' P' ), " wrong byte at index 15" );
248
+ assert_eq! (ba . at (16 ), Some (' Q' ), " wrong byte at index 16" );
249
+ assert_eq! (ba . at (17 ), Some (' R' ), " wrong byte at index 17" );
250
+ assert_eq! (ba . at (29 ), Some (' d' ), " wrong byte at index 29" );
251
+ assert_eq! (ba . at (30 ), Some (' e' ), " wrong byte at index 30" );
252
+ assert_eq! (ba . at (31 ), Some (' A' ), " wrong byte at index 31" );
253
+ assert_eq! (ba . at (32 ), Some (' B' ), " wrong byte at index 32" );
254
+ assert_eq! (ba . at (61 ), Some (' e' ), " wrong byte at index 61" );
255
+ assert_eq! (ba . at (62 ), Some (' A' ), " wrong byte at index 62" );
256
+ assert_eq! (ba . at (63 ), Some (' B' ), " wrong byte at index 63" );
257
+ assert_eq! (ba . at (76 ), Some (' O' ), " wrong byte at index 76" );
258
+ assert_eq! (ba . at (77 ), Some (' P' ), " wrong byte at index 77" );
259
+ assert_eq! (ba . at (78 ), Some (' Q' ), " wrong byte at index 78" );
260
+ assert_eq! (ba . at (79 ), None , " index 79 is not out of bounds" );
256
261
}
257
262
258
263
// Same as the previous test, but with [] instead of .at() (and without the out-of-bounds case).
259
264
#[test]
260
265
fn test_index_view () {
261
- let mut ba = test_byte_array_31 ();
262
- ba . append (@ test_byte_array_31 ());
263
- ba . append (@ test_byte_array_17 ());
264
-
265
- assert (ba [0 ] == 0x01 , ' wrong byte at index 0' );
266
- assert (ba [1 ] == 0x02 , ' wrong byte at index 1' );
267
- assert (ba [2 ] == 0x03 , ' wrong byte at index 2' );
268
- assert (ba [14 ] == 0x0f , ' wrong byte at index 14' );
269
- assert (ba [15 ] == 0x10 , ' wrong byte at index 15' );
270
- assert (ba [16 ] == 0x11 , ' wrong byte at index 16' );
271
- assert (ba [17 ] == 0x12 , ' wrong byte at index 17' );
272
- assert (ba [29 ] == 0x1e , ' wrong byte at index 29' );
273
- assert (ba [30 ] == 0x1f , ' wrong byte at index 30' );
274
- assert (ba [31 ] == 0x01 , ' wrong byte at index 31' );
275
- assert (ba [32 ] == 0x02 , ' wrong byte at index 32' );
276
- assert (ba [61 ] == 0x1f , ' wrong byte at index 61' );
277
- assert (ba [62 ] == 0x01 , ' wrong byte at index 62' );
278
- assert (ba [63 ] == 0x02 , ' wrong byte at index 63' );
279
- assert (ba [76 ] == 0x0f , ' wrong byte at index 76' );
280
- assert (ba [77 ] == 0x10 , ' wrong byte at index 77' );
281
- assert (ba [78 ] == 0x11 , ' wrong byte at index 78' );
266
+ let ba_31 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde" ;
267
+ let mut ba = ba_31 . clone ();
268
+ ba . append (@ ba_31 );
269
+
270
+ let ba_17 : ByteArray = " ABCDEFGHIJKLMNOPQ" ;
271
+ ba . append (@ ba_17 );
272
+
273
+ assert_eq! (ba [0 ], ' A' , " wrong byte at index 0" );
274
+ assert_eq! (ba [1 ], ' B' , " wrong byte at index 1" );
275
+ assert_eq! (ba [2 ], ' C' , " wrong byte at index 2" );
276
+ assert_eq! (ba [14 ], ' O' , " wrong byte at index 14" );
277
+ assert_eq! (ba [15 ], ' P' , " wrong byte at index 15" );
278
+ assert_eq! (ba [16 ], ' Q' , " wrong byte at index 16" );
279
+ assert_eq! (ba [17 ], ' R' , " wrong byte at index 17" );
280
+ assert_eq! (ba [29 ], ' d' , " wrong byte at index 29" );
281
+ assert_eq! (ba [30 ], ' e' , " wrong byte at index 30" );
282
+ assert_eq! (ba [31 ], ' A' , " wrong byte at index 31" );
283
+ assert_eq! (ba [32 ], ' B' , " wrong byte at index 32" );
284
+ assert_eq! (ba [61 ], ' e' , " wrong byte at index 61" );
285
+ assert_eq! (ba [62 ], ' A' , " wrong byte at index 62" );
286
+ assert_eq! (ba [63 ], ' B' , " wrong byte at index 63" );
287
+ assert_eq! (ba [76 ], ' O' , " wrong byte at index 76" );
288
+ assert_eq! (ba [77 ], ' P' , " wrong byte at index 77" );
289
+ assert_eq! (ba [78 ], ' Q' , " wrong byte at index 78" );
282
290
}
283
291
284
292
// Test panic with [] in case of out-of-bounds
285
293
#[test]
286
294
#[should_panic(expected: (' Index out of bounds' ,))]
287
295
fn test_index_view_out_of_bounds () {
288
- let mut ba = test_byte_array_31 ();
289
- ba . append (@ test_byte_array_31 ());
290
- ba . append (@ test_byte_array_17 ());
296
+ let ba_31 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde" ;
297
+ let mut ba = ba_31 . clone ();
298
+ ba . append (@ ba_31 );
299
+ let ba_17 : ByteArray = " ABCDEFGHIJKLMNOPQ" ;
300
+ ba . append (@ ba_17 );
291
301
292
302
let _x = ba [79 ];
293
303
}
@@ -306,29 +316,32 @@ fn test_string_literals() {
306
316
#[test]
307
317
fn test_equality () {
308
318
let byte_array : ByteArray = " a" ;
309
- assert (@ byte_array == @ " a" , ' Same strings are not equal' );
310
- assert (@ byte_array != @ " b" , ' Different strings are equal' );
319
+ assert_eq! (@ byte_array , @ " a" , " Same strings are not equal" );
320
+ assert_ne! (@ byte_array , @ " b" , " Different strings are equal" );
311
321
312
- let mut ba1 = test_byte_array_2 ();
313
- ba1 . append (@ test_byte_array_31 ());
314
- let ba2 = test_byte_array_33 ();
315
- let ba3 = test_byte_array_32 ();
316
- let mut ba4 = test_byte_array_32 ();
317
- ba4 . append (@ test_byte_array_1 ());
322
+ let mut ba1 : ByteArray = " 01" ;
323
+ let ba_31 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcde" ;
324
+ ba1 . append (@ ba_31 );
325
+ let ba_33 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg" ;
326
+ let ba2 = ba_33 ;
327
+ let ba_32 : ByteArray = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef" ;
328
+ let ba3 = ba_32 . clone ();
329
+ let mut ba4 = ba_32 ;
330
+ ba4 . append (@ " a" );
318
331
319
- assert (@ ba1 == @ ba1 , ' Same ByteArrays are not equal' );
320
- assert (@ ba2 == @ ba2 , ' Same ByteArrays are not equal' );
321
- assert (@ ba3 == @ ba3 , ' Same ByteArrays are not equal' );
322
- assert (@ ba4 == @ ba4 , ' Same ByteArrays are not equal' );
332
+ assert_eq! (@ ba1 , @ ba1 , " Same ByteArrays are not equal" );
333
+ assert_eq! (@ ba2 , @ ba2 , " Same ByteArrays are not equal" );
334
+ assert_eq! (@ ba3 , @ ba3 , " Same ByteArrays are not equal" );
335
+ assert_eq! (@ ba4 , @ ba4 , " Same ByteArrays are not equal" );
323
336
324
337
// Different data
325
- assert (@ ba1 != @ ba2 , ' Different ByteArrays are equal' );
338
+ assert_ne! (@ ba1 , @ ba2 , " Different ByteArrays are equal" );
326
339
327
340
// Different pending word length
328
- assert (@ ba2 != @ ba3 , ' Different ByteArrays are equal' );
341
+ assert_ne! (@ ba2 , @ ba3 , " Different ByteArrays are equal" );
329
342
330
343
// Different pending word
331
- assert (@ ba2 != @ ba4 , ' Different ByteArrays are equal' );
344
+ assert_ne! (@ ba2 , @ ba4 , " Different ByteArrays are equal" );
332
345
}
333
346
334
347
#[test]
@@ -452,49 +465,3 @@ fn test_from_collect() {
452
465
let ba : ByteArray = array! [' h' , ' e' , ' l' , ' l' , ' o' ]. into_iter (). collect ();
453
466
assert_eq! (ba , " hello" );
454
467
}
455
-
456
- // ========= Test helper functions =========
457
-
458
- fn test_byte_array_1 () -> ByteArray {
459
- let mut ba1 = Default :: default ();
460
- ba1 . append_word (0x01 , 1 );
461
- ba1
462
- }
463
-
464
- fn test_byte_array_2 () -> ByteArray {
465
- let mut ba1 = Default :: default ();
466
- ba1 . append_word (0x0102 , 2 );
467
- ba1
468
- }
469
-
470
- fn test_byte_array_17 () -> ByteArray {
471
- let mut ba1 = Default :: default ();
472
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f1011 , 17 );
473
- ba1
474
- }
475
-
476
- fn test_byte_array_30 () -> ByteArray {
477
- let mut ba1 = Default :: default ();
478
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e , 30 );
479
- ba1
480
- }
481
-
482
- fn test_byte_array_31 () -> ByteArray {
483
- let mut ba1 = Default :: default ();
484
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f , 31 );
485
- ba1
486
- }
487
-
488
- fn test_byte_array_32 () -> ByteArray {
489
- let mut ba1 = Default :: default ();
490
- ba1 . append_word (0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f , 31 );
491
- ba1 . append_word (0x20 , 1 );
492
- ba1
493
- }
494
-
495
- fn test_byte_array_33 () -> ByteArray {
496
- let mut ba2 = Default :: default ();
497
- ba2 . append_word (0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f , 31 );
498
- ba2 . append_word (0x2021 , 2 );
499
- ba2
500
- }
0 commit comments