Skip to content

Commit b19fb2c

Browse files
author
Gilad Chase
committed
refactor(test): remove fixed-size bytearray testutils
It's legacy.
1 parent 2f2ffe3 commit b19fb2c

File tree

1 file changed

+111
-144
lines changed

1 file changed

+111
-144
lines changed

corelib/src/test/byte_array_test.cairo

Lines changed: 111 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -250,84 +250,94 @@ fn test_concat_pending_sum_up_to_more_than_word_gt16() {
250250
#[test]
251251
fn test_len() {
252252
let ba: ByteArray = Default::default();
253-
assert(ba.len() == 0, 'wrong ByteArray len');
253+
assert_eq!(ba.len(), 0, "wrong ByteArray len");
254254

255-
let mut ba = test_byte_array_33();
256-
assert(ba.len() == 33, 'wrong ByteArray len');
255+
let mut ba_33: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef$";
256+
let mut ba = ba_33;
257+
assert_eq!(ba.len(), 33, "wrong ByteArray len");
257258

258-
ba.append(@test_byte_array_30());
259-
assert(ba.len() == 63, 'wrong ByteArray len');
259+
let ba_30: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc$";
260+
ba.append(@ba_30);
261+
assert_eq!(ba.len(), 63, "wrong ByteArray len");
260262
}
261263

262264
#[test]
263265
fn test_at_empty() {
264266
let ba: ByteArray = Default::default();
265267

266-
assert(ba.at(0) == None, 'index 0 is not out of bounds');
267-
assert(ba.at(1) == None, 'index 1 is not out of bounds');
268-
assert(ba.at(30) == None, 'index 30 is not out of bounds');
269-
assert(ba.at(31) == None, 'index 31 is not out of bounds');
268+
assert_eq!(ba.at(0), None, "index 0 is not out of bounds");
269+
assert_eq!(ba.at(1), None, "index 1 is not out of bounds");
270+
assert_eq!(ba.at(30), None, "index 30 is not out of bounds");
271+
assert_eq!(ba.at(31), None, "index 31 is not out of bounds");
270272
}
271273

272274
#[test]
273275
fn test_at() {
274-
let mut ba = test_byte_array_31();
275-
ba.append(@test_byte_array_31());
276-
ba.append(@test_byte_array_17());
277-
278-
assert(ba.at(0) == Some(0x01), 'wrong byte at index 0');
279-
assert(ba.at(1) == Some(0x02), 'wrong byte at index 1');
280-
assert(ba.at(2) == Some(0x03), 'wrong byte at index 2');
281-
assert(ba.at(14) == Some(0x0f), 'wrong byte at index 14');
282-
assert(ba.at(15) == Some(0x10), 'wrong byte at index 15');
283-
assert(ba.at(16) == Some(0x11), 'wrong byte at index 16');
284-
assert(ba.at(17) == Some(0x12), 'wrong byte at index 17');
285-
assert(ba.at(29) == Some(0x1e), 'wrong byte at index 29');
286-
assert(ba.at(30) == Some(0x1f), 'wrong byte at index 30');
287-
assert(ba.at(31) == Some(0x01), 'wrong byte at index 31');
288-
assert(ba.at(32) == Some(0x02), 'wrong byte at index 32');
289-
assert(ba.at(61) == Some(0x1f), 'wrong byte at index 61');
290-
assert(ba.at(62) == Some(0x01), 'wrong byte at index 62');
291-
assert(ba.at(63) == Some(0x02), 'wrong byte at index 63');
292-
assert(ba.at(76) == Some(0x0f), 'wrong byte at index 76');
293-
assert(ba.at(77) == Some(0x10), 'wrong byte at index 77');
294-
assert(ba.at(78) == Some(0x11), 'wrong byte at index 78');
295-
assert(ba.at(79) == None, 'index 79 is not out of bounds');
276+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
277+
let mut ba = ba_31.clone();
278+
ba.append(@ba_31);
279+
280+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOPQ";
281+
ba.append(@ba_17);
282+
283+
assert_eq!(ba.at(0), Some('A'), "wrong byte at index 0");
284+
assert_eq!(ba.at(1), Some('B'), "wrong byte at index 1");
285+
assert_eq!(ba.at(2), Some('C'), "wrong byte at index 2");
286+
assert_eq!(ba.at(14), Some('O'), "wrong byte at index 14");
287+
assert_eq!(ba.at(15), Some('P'), "wrong byte at index 15");
288+
assert_eq!(ba.at(16), Some('Q'), "wrong byte at index 16");
289+
assert_eq!(ba.at(17), Some('R'), "wrong byte at index 17");
290+
assert_eq!(ba.at(29), Some('d'), "wrong byte at index 29");
291+
assert_eq!(ba.at(30), Some('e'), "wrong byte at index 30");
292+
assert_eq!(ba.at(31), Some('A'), "wrong byte at index 31");
293+
assert_eq!(ba.at(32), Some('B'), "wrong byte at index 32");
294+
assert_eq!(ba.at(61), Some('e'), "wrong byte at index 61");
295+
assert_eq!(ba.at(62), Some('A'), "wrong byte at index 62");
296+
assert_eq!(ba.at(63), Some('B'), "wrong byte at index 63");
297+
assert_eq!(ba.at(76), Some('O'), "wrong byte at index 76");
298+
assert_eq!(ba.at(77), Some('P'), "wrong byte at index 77");
299+
assert_eq!(ba.at(78), Some('Q'), "wrong byte at index 78");
300+
assert_eq!(ba.at(79), None, "index 79 is not out of bounds");
296301
}
297302

298303
// Same as the previous test, but with [] instead of .at() (and without the out-of-bounds case).
299304
#[test]
300305
fn test_index_view() {
301-
let mut ba = test_byte_array_31();
302-
ba.append(@test_byte_array_31());
303-
ba.append(@test_byte_array_17());
304-
305-
assert(ba[0] == 0x01, 'wrong byte at index 0');
306-
assert(ba[1] == 0x02, 'wrong byte at index 1');
307-
assert(ba[2] == 0x03, 'wrong byte at index 2');
308-
assert(ba[14] == 0x0f, 'wrong byte at index 14');
309-
assert(ba[15] == 0x10, 'wrong byte at index 15');
310-
assert(ba[16] == 0x11, 'wrong byte at index 16');
311-
assert(ba[17] == 0x12, 'wrong byte at index 17');
312-
assert(ba[29] == 0x1e, 'wrong byte at index 29');
313-
assert(ba[30] == 0x1f, 'wrong byte at index 30');
314-
assert(ba[31] == 0x01, 'wrong byte at index 31');
315-
assert(ba[32] == 0x02, 'wrong byte at index 32');
316-
assert(ba[61] == 0x1f, 'wrong byte at index 61');
317-
assert(ba[62] == 0x01, 'wrong byte at index 62');
318-
assert(ba[63] == 0x02, 'wrong byte at index 63');
319-
assert(ba[76] == 0x0f, 'wrong byte at index 76');
320-
assert(ba[77] == 0x10, 'wrong byte at index 77');
321-
assert(ba[78] == 0x11, 'wrong byte at index 78');
306+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
307+
let mut ba = ba_31.clone();
308+
ba.append(@ba_31);
309+
310+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOPQ";
311+
ba.append(@ba_17);
312+
313+
assert_eq!(ba[0], 'A', "wrong byte at index 0");
314+
assert_eq!(ba[1], 'B', "wrong byte at index 1");
315+
assert_eq!(ba[2], 'C', "wrong byte at index 2");
316+
assert_eq!(ba[14], 'O', "wrong byte at index 14");
317+
assert_eq!(ba[15], 'P', "wrong byte at index 15");
318+
assert_eq!(ba[16], 'Q', "wrong byte at index 16");
319+
assert_eq!(ba[17], 'R', "wrong byte at index 17");
320+
assert_eq!(ba[29], 'd', "wrong byte at index 29");
321+
assert_eq!(ba[30], 'e', "wrong byte at index 30");
322+
assert_eq!(ba[31], 'A', "wrong byte at index 31");
323+
assert_eq!(ba[32], 'B', "wrong byte at index 32");
324+
assert_eq!(ba[61], 'e', "wrong byte at index 61");
325+
assert_eq!(ba[62], 'A', "wrong byte at index 62");
326+
assert_eq!(ba[63], 'B', "wrong byte at index 63");
327+
assert_eq!(ba[76], 'O', "wrong byte at index 76");
328+
assert_eq!(ba[77], 'P', "wrong byte at index 77");
329+
assert_eq!(ba[78], 'Q', "wrong byte at index 78");
322330
}
323331

324332
// Test panic with [] in case of out-of-bounds
325333
#[test]
326334
#[should_panic(expected: ('Index out of bounds',))]
327335
fn test_index_view_out_of_bounds() {
328-
let mut ba = test_byte_array_31();
329-
ba.append(@test_byte_array_31());
330-
ba.append(@test_byte_array_17());
336+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
337+
let mut ba = ba_31.clone();
338+
ba.append(@ba_31);
339+
let ba_17: ByteArray = "ABCDEFGHIJKLMNOPQ";
340+
ba.append(@ba_17);
331341

332342
let _x = ba[79];
333343
}
@@ -346,29 +356,32 @@ fn test_string_literals() {
346356
#[test]
347357
fn test_equality() {
348358
let byte_array: ByteArray = "a";
349-
assert(@byte_array == @"a", 'Same strings are not equal');
350-
assert(@byte_array != @"b", 'Different strings are equal');
359+
assert_eq!(byte_array, "a", "Same strings are not equal");
360+
assert_ne!(byte_array, "b", "Different strings are equal");
351361

352-
let mut ba1 = test_byte_array_2();
353-
ba1.append(@test_byte_array_31());
354-
let ba2 = test_byte_array_33();
355-
let ba3 = test_byte_array_32();
356-
let mut ba4 = test_byte_array_32();
357-
ba4.append(@test_byte_array_1());
362+
let mut ba1: ByteArray = "01";
363+
let ba_31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
364+
ba1.append(@ba_31);
365+
let ba_33: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg";
366+
let ba2 = ba_33;
367+
let ba_32: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
368+
let ba3 = ba_32.clone();
369+
let mut ba4 = ba_32;
370+
ba4.append(@"a");
358371

359-
assert(@ba1 == @ba1, 'Same ByteArrays are not equal');
360-
assert(@ba2 == @ba2, 'Same ByteArrays are not equal');
361-
assert(@ba3 == @ba3, 'Same ByteArrays are not equal');
362-
assert(@ba4 == @ba4, 'Same ByteArrays are not equal');
372+
assert_eq!(ba1, ba1, "Same ByteArrays are not equal");
373+
assert_eq!(ba2, ba2, "Same ByteArrays are not equal");
374+
assert_eq!(ba3, ba3, "Same ByteArrays are not equal");
375+
assert_eq!(ba4, ba4, "Same ByteArrays are not equal");
363376

364377
// Different data
365-
assert(@ba1 != @ba2, 'Different ByteArrays are equal');
378+
assert_ne!(ba1, ba2, "Different ByteArrays are equal");
366379

367380
// Different pending word length
368-
assert(@ba2 != @ba3, 'Different ByteArrays are equal');
381+
assert_ne!(ba2, ba3, "Different ByteArrays are equal");
369382

370383
// Different pending word
371-
assert(@ba2 != @ba4, 'Different ByteArrays are equal');
384+
assert_ne!(ba2, ba4, "Different ByteArrays are equal");
372385
}
373386

374387
#[test]
@@ -377,64 +390,64 @@ fn test_reverse() {
377390
let ba: ByteArray = "abc";
378391
let ba_rev: ByteArray = "cba";
379392
let palindrome: ByteArray = "rotator";
380-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
381-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
382-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
383-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
393+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
394+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
395+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
396+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
384397

385398
// Arrays of length 16
386399
let ba: ByteArray = "my length is 16.";
387400
let ba_rev: ByteArray = ".61 si htgnel ym";
388401
let palindrome: ByteArray = "nolemon nomelon";
389-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
390-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
391-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
392-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
402+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
403+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
404+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
405+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
393406

394407
// Arrays of 16 < length < 31
395408
let ba: ByteArray = "I am a medium byte array";
396409
let ba_rev: ByteArray = "yarra etyb muidem a ma I";
397410
let palindrome: ByteArray = "nolemon nomelon";
398-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
399-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
400-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
401-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
411+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
412+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
413+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
414+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
402415

403416
// Arrays of length 31
404417
let ba: ByteArray = "I didn't find a good palindrome";
405418
let ba_rev: ByteArray = "emordnilap doog a dnif t'ndid I";
406419
let palindrome: ByteArray = "kayak level rotator level kayak";
407-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
408-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
409-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
410-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
420+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
421+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
422+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
423+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
411424

412425
// Arrays of 31 < length < 47 (31+16)
413426
let ba: ByteArray = "This time I did find a good palindrome!";
414427
let ba_rev: ByteArray = "!emordnilap doog a dnif did I emit sihT";
415428
let palindrome: ByteArray = "noitneverpropagatesifisetagaporprevention";
416-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
417-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
418-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
419-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
429+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
430+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
431+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
432+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
420433

421434
// Arrays of length 47 (31+16)
422435
let ba: ByteArray = "I have found a palindrome, exactly 47 in length";
423436
let ba_rev: ByteArray = "htgnel ni 74 yltcaxe ,emordnilap a dnuof evah I";
424437
let palindrome: ByteArray = "onacloverifaliveeruptsavastpureevilafirevolcano";
425-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
426-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
427-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
428-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
438+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
439+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
440+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
441+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
429442

430443
// Arrays of length > 47 (31+16)
431444
let ba: ByteArray = "This palindrome is not as good, but at least it's long enough";
432445
let ba_rev: ByteArray = "hguone gnol s'ti tsael ta tub ,doog sa ton si emordnilap sihT";
433446
let palindrome: ByteArray = "docnoteidissentafastneverpreventsafatnessidietoncod";
434-
assert_ne(@ba, @ba.rev(), 'ba == ba.rev()');
435-
assert_ne(@ba_rev, @ba_rev.rev(), 'ba_rev == ba_rev.rev()');
436-
assert_eq(@ba, @ba_rev.rev(), 'ba != ba_rev.rev()');
437-
assert_eq(@palindrome, @palindrome.rev(), 'palindrome is not a palindrome');
447+
assert_ne!(ba, ba.rev(), "ba == ba.rev()");
448+
assert_ne!(ba_rev, ba_rev.rev(), "ba_rev == ba_rev.rev()");
449+
assert_eq!(ba, ba_rev.rev(), "ba != ba_rev.rev()");
450+
assert_eq!(palindrome, palindrome.rev(), "palindrome is not a palindrome");
438451
}
439452

440453
#[test]
@@ -492,49 +505,3 @@ fn test_from_collect() {
492505
let ba: ByteArray = array!['h', 'e', 'l', 'l', 'o'].into_iter().collect();
493506
assert_eq!(ba, "hello");
494507
}
495-
496-
// ========= Test helper functions =========
497-
498-
fn test_byte_array_1() -> ByteArray {
499-
let mut ba1 = Default::default();
500-
ba1.append_word(0x01, 1);
501-
ba1
502-
}
503-
504-
fn test_byte_array_2() -> ByteArray {
505-
let mut ba1 = Default::default();
506-
ba1.append_word(0x0102, 2);
507-
ba1
508-
}
509-
510-
fn test_byte_array_17() -> ByteArray {
511-
let mut ba1 = Default::default();
512-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f1011, 17);
513-
ba1
514-
}
515-
516-
fn test_byte_array_30() -> ByteArray {
517-
let mut ba1 = Default::default();
518-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e, 30);
519-
ba1
520-
}
521-
522-
fn test_byte_array_31() -> ByteArray {
523-
let mut ba1 = Default::default();
524-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f, 31);
525-
ba1
526-
}
527-
528-
fn test_byte_array_32() -> ByteArray {
529-
let mut ba1 = Default::default();
530-
ba1.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f, 31);
531-
ba1.append_word(0x20, 1);
532-
ba1
533-
}
534-
535-
fn test_byte_array_33() -> ByteArray {
536-
let mut ba2 = Default::default();
537-
ba2.append_word(0x0102030405060708091a0b0c0d0e0f101112131415161718191a1b1c1d1e1f, 31);
538-
ba2.append_word(0x2021, 2);
539-
ba2
540-
}

0 commit comments

Comments
 (0)