Skip to content

Commit

Permalink
assert() => assert!()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Aug 5, 2024
1 parent 4ce919c commit caa630d
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 126 deletions.
28 changes: 7 additions & 21 deletions packages/data_structures/src/tests/bit_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ fn test_append_bit() {
.try_into()
.unwrap();
let expected: Array<bytes31> = array![val, val,];
assert(
ba.current() == 0xa * one_shift_left_bytes_felt252(30) * shift_bit(4).into(),
'expected current 0xa'
);
assert_eq!(ba.current(), 0xa * one_shift_left_bytes_felt252(30) * shift_bit(4).into());
assert!(ba.data() == expected, "illegal array");
}

Expand Down Expand Up @@ -94,19 +91,14 @@ fn test_pop_front_empty() {
#[available_gas(20000000)]
fn test_read_word_be() {
let mut ba = sample_bit_array();
assert(
ba.read_word_be(length: 128).unwrap() == Bounded::<u128>::MAX.into() - 1, 'expected max - 1'
);
assert_eq!(ba.read_word_be(length: 128).unwrap(), Bounded::<u128>::MAX.into() - 1);
}

#[test]
#[available_gas(20000000)]
fn test_read_word_le() {
let mut ba = sample_bit_array();
assert(
ba.read_word_le(length: 128).unwrap() == 0x7fffffffffffffffffffffffffffffff,
'unexpected value'
);
assert_eq!(ba.read_word_le(length: 128).unwrap(), 0x7fffffffffffffffffffffffffffffff);
}

#[test]
Expand Down Expand Up @@ -140,10 +132,7 @@ fn test_read_word_be_u512() {
ba.write_word_be(limb1.into(), 128);
ba.write_word_be(limb0.into(), 128);
let limb3 = 0xfffffffffffffffffffffffffffffffe_u128;
assert(
ba.read_word_be_u512(length: 512).unwrap() == u512 { limb0, limb1, limb2, limb3 },
'unexpected value'
);
assert!(ba.read_word_be_u512(length: 512).unwrap() == u512 { limb0, limb1, limb2, limb3 });
}

#[test]
Expand All @@ -157,10 +146,7 @@ fn test_read_word_le_u512() {
ba.write_word_le(limb2.into(), 128);
ba.write_word_le(limb3.into(), 128);
let limb0 = 0x7fffffffffffffffffffffffffffffff_u128;
assert(
ba.read_word_le_u512(length: 512).unwrap() == u512 { limb0, limb1, limb2, limb3 },
'unexpected value'
);
assert!(ba.read_word_le_u512(length: 512).unwrap() == u512 { limb0, limb1, limb2, limb3 });
}

#[test]
Expand All @@ -184,7 +170,7 @@ fn test_read_word_le_half() {
fn test_write_word_be() {
let mut ba: BitArray = Default::default();
ba.write_word_be(Bounded::<u128>::MAX.into() - 2, 128);
assert(ba.read_word_be(128).unwrap() == Bounded::<u128>::MAX.into() - 2, 'unexpected value');
assert_eq!(ba.read_word_be(128).unwrap(), Bounded::<u128>::MAX.into() - 2);
}

#[test]
Expand All @@ -200,7 +186,7 @@ fn test_write_word_be_half() {
fn test_write_word_le() {
let mut ba: BitArray = Default::default();
ba.write_word_le(Bounded::<u128>::MAX.into() - 4, 128);
assert(ba.read_word_le(128).unwrap() == Bounded::<u128>::MAX.into() - 4, 'unexpected value');
assert_eq!(ba.read_word_le(128).unwrap(), Bounded::<u128>::MAX.into() - 4);
}

#[test]
Expand Down
7 changes: 2 additions & 5 deletions packages/data_structures/src/tests/byte_array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ fn test_span_u8_into_byte_array() {
let array: Array<u8> = array![1, 2, 3, 4, 5, 6, 7, 8,];
let ba: ByteArray = array.span().into();
let mut index = 0_usize;
loop {
match ba.at(index) {
Option::Some(byte) => assert(*(array[index]) == byte, 'should equal'),
Option::None => { break; }
};
while let Option::Some(byte) = ba.at(index) {
assert!(*(array[index]) == byte);
index += 1;
};
}
Expand Down
50 changes: 13 additions & 37 deletions packages/data_structures/src/tests/byte_reader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,10 @@ fn test_read_sequence() {
let ba = test_byte_array_64();
let mut rd = ba.reader();
assert!(rd.read_i8() == Option::Some(1), "expected 1");
assert(
rd.read_i128() == Option::Some(0x02030405060708090a0b0c0d0e0f1011), 'not 0x0203040506...'
);
assert(
rd.read_u128() == Option::Some(0x12131415161718191a1b1c1d1e1f2021), 'not 0x1213141516...'
);
assert!(rd.read_i64() == Option::Some(0x2223242526272829), "not 0x22232425...");
assert(
rd.read_u128() == Option::Some(0x2a2b2c2d2e2f30313233343536373839), 'not 0x2a2b2c2d2e...'
);
assert!(rd.read_i128() == Option::Some(0x02030405060708090a0b0c0d0e0f1011));
assert!(rd.read_u128() == Option::Some(0x12131415161718191a1b1c1d1e1f2021));
assert!(rd.read_i64() == Option::Some(0x2223242526272829));
assert!(rd.read_u128() == Option::Some(0x2a2b2c2d2e2f30313233343536373839));
assert!(rd.read_u32() == Option::Some(0x3a3b3c3d), "not 0x3a3b3c3d");
assert!(rd.read_i16() == Option::Some(0x3e3f), "not 0x3e3f");
assert!(rd.read_u8() == Option::Some(0x40), "not 0x40");
Expand All @@ -401,16 +395,10 @@ fn test_read_sequence_arr() {
let ba = test_array_64();
let mut rd = ba.reader();
assert!(rd.read_i8() == Option::Some(1), "expected 1");
assert(
rd.read_i128() == Option::Some(0x02030405060708090a0b0c0d0e0f1011), 'not 0x0203040506...'
);
assert(
rd.read_u128() == Option::Some(0x12131415161718191a1b1c1d1e1f2021), 'not 0x1213141516...'
);
assert!(rd.read_i128() == Option::Some(0x02030405060708090a0b0c0d0e0f1011));
assert!(rd.read_u128() == Option::Some(0x12131415161718191a1b1c1d1e1f2021));
assert!(rd.read_i64() == Option::Some(0x2223242526272829), "not 0x22232425...");
assert(
rd.read_u128() == Option::Some(0x2a2b2c2d2e2f30313233343536373839), 'not 0x2a2b2c2d2e...'
);
assert!(rd.read_u128() == Option::Some(0x2a2b2c2d2e2f30313233343536373839));
assert!(rd.read_u32() == Option::Some(0x3a3b3c3d), "not 0x3a3b3c3d");
assert!(rd.read_i16() == Option::Some(0x3e3f), "not 0x3e3f");
assert!(rd.read_u8() == Option::Some(0x40), "not 0x40");
Expand All @@ -423,16 +411,10 @@ fn test_read_sequence_le() {
let ba = test_byte_array_64();
let mut rd = ba.reader();
assert!(rd.read_i8() == Option::Some(1), "expected 1");
assert(
rd.read_i128_le() == Option::Some(0x11100f0e0d0c0b0a0908070605040302), 'not 0x11100f0e0...'
);
assert(
rd.read_u128_le() == Option::Some(0x21201f1e1d1c1b1a1918171615141312), 'not 0x21201f1e1d...'
);
assert!(rd.read_i128_le() == Option::Some(0x11100f0e0d0c0b0a0908070605040302));
assert!(rd.read_u128_le() == Option::Some(0x21201f1e1d1c1b1a1918171615141312));
assert!(rd.read_i64_le() == Option::Some(0x2928272625242322), "not 0x29282726...");
assert(
rd.read_u128_le() == Option::Some(0x393837363534333231302f2e2d2c2b2a), 'not 0x3938373635...'
);
assert!(rd.read_u128_le() == Option::Some(0x393837363534333231302f2e2d2c2b2a));
assert!(rd.read_u32_le() == Option::Some(0x3d3c3b3a), "not 0x3d3c3b3a");
assert!(rd.read_i16_le() == Option::Some(0x3f3e), "not 0x3f3e");
assert!(rd.read_u8() == Option::Some(0x40), "not 0x40");
Expand All @@ -445,16 +427,10 @@ fn test_read_sequence_le_arr() {
let ba = test_array_64();
let mut rd = ba.reader();
assert!(rd.read_i8() == Option::Some(1), "expected 1");
assert(
rd.read_i128_le() == Option::Some(0x11100f0e0d0c0b0a0908070605040302), 'not 0x11100f0e0...'
);
assert(
rd.read_u128_le() == Option::Some(0x21201f1e1d1c1b1a1918171615141312), 'not 0x21201f1e1d...'
);
assert!(rd.read_i128_le() == Option::Some(0x11100f0e0d0c0b0a0908070605040302));
assert!(rd.read_u128_le() == Option::Some(0x21201f1e1d1c1b1a1918171615141312));
assert!(rd.read_i64_le() == Option::Some(0x2928272625242322), "not 0x29282726...");
assert(
rd.read_u128_le() == Option::Some(0x393837363534333231302f2e2d2c2b2a), 'not 0x3938373635...'
);
assert!(rd.read_u128_le() == Option::Some(0x393837363534333231302f2e2d2c2b2a));
assert!(rd.read_u32_le() == Option::Some(0x3d3c3b3a), "not 0x3d3c3b3a");
assert!(rd.read_i16_le() == Option::Some(0x3f3e), "not 0x3f3e");
assert!(rd.read_u8() == Option::Some(0x40), "not 0x40");
Expand Down
5 changes: 1 addition & 4 deletions packages/linalg/src/tests/kron_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@ fn kron_product_test() {
fn kron_product_test_check_len() {
let xs = array![1_u64];
let ys = array![];
assert(
kron(xs.span(), ys.span()) == Result::Err(KronError::UnequalLength),
'Arrays must have the same len'
);
assert!(kron(xs.span(), ys.span()) == Result::Err(KronError::UnequalLength));
}
2 changes: 1 addition & 1 deletion packages/math/src/tests/ed25519_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ fn affine_point_op() {
x: 34426924428514608760437100447421064591311588584549077394333265447466212246087,
y: 29872771498517479181395568267318965384440757492476580330810382845026939417492
};
assert(res == p1.add(p2, p_non_zero), 'incorrect point addition');
assert!(res == p1.add(p2, p_non_zero));
}
20 changes: 6 additions & 14 deletions packages/math/src/tests/fast_power_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,16 @@ fn fast_power_mod_test() {
assert_eq!(fast_power_mod(2_u128, 3_u128, 17_u128), 8, "invalid result");
assert_eq!(fast_power_mod(3_u128, 4_u128, 17_u128), 13, "invalid result");
assert_eq!(fast_power_mod(2_u128, 100_u128, 1000000007_u128), 976371285, "invalid result");
assert(
fast_power_mod(
2_u128, 127_u128, 340282366920938463463374607431768211454_u128
) == 170141183460469231731687303715884105728,
'invalid result'
);
assert_eq!(
fast_power_mod(2_u128, 127_u128, 34028236692093846346337460743176821144_u128),
8,
"invalid result"
fast_power_mod(2_u128, 127_u128, 340282366920938463463374607431768211454_u128),
170141183460469231731687303715884105728
);
assert_eq!(fast_power_mod(2_u128, 127_u128, 34028236692093846346337460743176821144_u128), 8);

assert_eq!(fast_power_mod(2_u128, 128_u128, 9299_u128), 1412, "invalid result");

assert(
fast_power_mod(
2_u128, 88329_u128, 34028236692093846346337460743176821144_u128
) == 2199023255552,
'invalid result'
assert_eq!(
fast_power_mod(2_u128, 88329_u128, 34028236692093846346337460743176821144_u128),
2199023255552
);
}
20 changes: 5 additions & 15 deletions packages/math/src/tests/math_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,11 @@ fn test_rotl_max() {
#[available_gas(4000000)]
fn test_rotr_min() {
assert_eq!(BitRotate::rotate_right(pow::<u8>(2, 7) + 1, 1), 0b11 * pow(2, 6), "invalid result");
assert(
BitRotate::rotate_right(pow::<u16>(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result'
);
assert(
BitRotate::rotate_right(pow::<u32>(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result'
);
assert(
BitRotate::rotate_right(pow::<u64>(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result'
);
assert(
BitRotate::rotate_right(pow::<u128>(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result'
);
assert(
BitRotate::rotate_right(pow::<u256>(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result'
);
assert_eq!(BitRotate::rotate_right(pow::<u16>(2, 15) + 1, 1), 0b11 * pow(2, 14));
assert_eq!(BitRotate::rotate_right(pow::<u32>(2, 31) + 1, 1), 0b11 * pow(2, 30));
assert_eq!(BitRotate::rotate_right(pow::<u64>(2, 63) + 1, 1), 0b11 * pow(2, 62));
assert_eq!(BitRotate::rotate_right(pow::<u128>(2, 127) + 1, 1), 0b11 * pow(2, 126));
assert_eq!(BitRotate::rotate_right(pow::<u256>(2, 255) + 1, 1), 0b11 * pow(2, 254));
}

#[test]
Expand Down
8 changes: 3 additions & 5 deletions packages/math/src/tests/mod_arithmetics_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ fn pow_mod_test() {
assert_eq!(pow_mod(2, 4, prime_non_zero), 16, "Incorrect result");
assert_eq!(pow_mod(2, 256, prime_non_zero), 38, "Incorrect result");
assert_eq!(pow_mod(2, 260, prime_non_zero), 608, "Incorrect result");
assert(
pow_mod(
10, 260, prime_non_zero
) == 17820046977743035104984469918379927979184337110507416960697246160624073120874,
'Incorrect result'
assert_eq!(
pow_mod(10, 260, prime_non_zero),
17820046977743035104984469918379927979184337110507416960697246160624073120874
);
assert_eq!(pow_mod(4, 174, prime_non_zero), 188166885971377801784666882048, "Incorrect result");
assert_eq!(pow_mod(100, p, prime_non_zero), 100, "Incorrect result");
Expand Down
6 changes: 3 additions & 3 deletions packages/math/src/tests/test_keccak256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn test_keccak256_empty_bytes() {
let hash = keccak256(input.span());
let expected = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;

assert(hash == expected, 'wrong hash value')
assert_eq!(hash, expected)
}

#[test]
Expand All @@ -19,7 +19,7 @@ fn test_keccak256_partial_bytes() {
let hash = keccak256(input.span());
let expected = 0x51e8babe8b42352100dffa7f7b3843c95245d3d545c6cbf5052e80258ae80627;

assert(hash == expected, 'wrong hash value');
assert_eq!(hash, expected);
}

#[test]
Expand Down Expand Up @@ -64,5 +64,5 @@ fn test_keccak256_full_u256() {
let hash = keccak256(input.span());
let expected = 0x98cfb1eca8a71b4a4b1c115f3d5a462296a66487d1d97fb4c47b979c64bde069;

assert(hash == expected, 'wrong hash value');
assert_eq!(hash, expected);
}
21 changes: 7 additions & 14 deletions packages/math/src/tests/u512_arithmetics_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,32 @@ fn mu512(limb0: u128, limb1: u128, limb2: u128, limb3: u128) -> u512 {

#[test]
fn test_u512_add() {
assert(
u512_add(mu512(1, 2, 3, 4), mu512(5, 6, 7, 8)) == mu512(6, 8, 10, 12), 'incorrect u512 add'
);
assert(
u512_add(mu512(MAX_128, 1, 2, 3), mu512(4, 5, 6, 7)) == mu512(3, 7, 8, 10),
'incorrect u512 add'
);
assert!(u512_add(mu512(1, 2, 3, 4), mu512(5, 6, 7, 8)) == mu512(6, 8, 10, 12));
assert!(u512_add(mu512(MAX_128, 1, 2, 3), mu512(4, 5, 6, 7)) == mu512(3, 7, 8, 10));
}

#[test]
fn test_u512_sub() {
let sub0 = u512_sub(mu512(5, 6, 7, 8), mu512(1, 2, 3, 4));
assert(sub0 == mu512(4, 4, 4, 4), 'incorrect u512 sub');
assert!(sub0 == mu512(4, 4, 4, 4));

let sub1 = u512_sub(mu512(3, 2, 1, MAX_128,), mu512(7, 6, 5, 4));
assert(
assert!(
sub1 == mu512(
0xfffffffffffffffffffffffffffffffc,
0xfffffffffffffffffffffffffffffffb,
0xfffffffffffffffffffffffffffffffb,
0xfffffffffffffffffffffffffffffffa
),
'incorrect u512 sub1'
)
);

let sub2 = u512_sub(mu512(3, 2, 1, 1), mu512(7, 6, 5, 0));
assert(
assert!(
sub2 == mu512(
0xfffffffffffffffffffffffffffffffc,
0xfffffffffffffffffffffffffffffffb,
0xfffffffffffffffffffffffffffffffb,
0
),
'incorrect u512 sub2'
)
);
}
12 changes: 6 additions & 6 deletions packages/searching/src/tests/binary_search_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ fn value_not_found() {

// Test with an even length
let mut span = arr.span();
assert!(binary_search(span, 20).is_none(), "value was found");
assert!(binary_search(span, 42000).is_none(), "value was found");
assert!(binary_search(span, 760).is_none(), "value was found");
assert!(binary_search(span, 20).is_none());
assert!(binary_search(span, 42000).is_none());
assert!(binary_search(span, 760).is_none());

// Odd length
arr.append(700); // 6
span = arr.span();
assert!(binary_search(span, 20).is_none(), "value was found");
assert!(binary_search(span, 42000).is_none(), "value was found");
assert!(binary_search(span, 760).is_none(), "value was found");
assert!(binary_search(span, 20).is_none());
assert!(binary_search(span, 42000).is_none());
assert!(binary_search(span, 760).is_none());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/tests/list_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn test_append_get_many() {
// test appending many
while (index != max) {
let append_indexes = contract.do_append(mock_addr, index.into());
assert(append_indexes == (index, index), index.into());
assert_eq!(append_indexes, (index, index));
index += 1;
};

Expand Down

0 comments on commit caa630d

Please sign in to comment.