diff --git a/src/utils/calc.cairo b/src/utils/calc.cairo index 46ba6ec6..19d58075 100644 --- a/src/utils/calc.cairo +++ b/src/utils/calc.cairo @@ -135,8 +135,8 @@ fn bounded_sub(a: i128, b: i128) -> i128 { /// Converts the given unsigned integer to a signed integer. /// # Arguments -/// * `a` - first number. -/// * `b` - second number. +/// * `a` - Number. +/// * `is_positive` - true if the number is positive. /// # Return /// The signed integer. fn to_signed(a: u128, mut is_positive: bool) -> i128 { @@ -165,5 +165,5 @@ fn max_i128() -> i128 { fn min_i128() -> i128 { // Comes from https://doc.rust-lang.org/std/i128/constant.MIN.html - i128 { mag: 170_141_183_460_469_231_731_687_303_715_884_105_728, sign: true } + i128 { mag: 170_141_183_460_469_231_731_687_303_715_884_105_727, sign: true } } diff --git a/src/utils/i128.cairo b/src/utils/i128.cairo index 5e63680e..d3e49a73 100644 --- a/src/utils/i128.cairo +++ b/src/utils/i128.cairo @@ -411,7 +411,7 @@ fn i128_check_sign_zero(x: i128) { /// Cf: IntegerTrait::new docstring fn i128_new(mag: u128, sign: bool) -> i128 { if sign == true { - assert(mag <= 170141183460469231731687303715884105728_u128, 'i128 Overflow'); + assert(mag <= 170141183460469231731687303715884105727_u128, 'i128 Overflow'); } else { assert(mag <= 170141183460469231731687303715884105727_u128, 'i128 Overflow'); } diff --git a/tests/utils/test_calc.cairo b/tests/utils/test_calc.cairo index 778cc516..724faf92 100644 --- a/tests/utils/test_calc.cairo +++ b/tests/utils/test_calc.cairo @@ -41,26 +41,46 @@ fn given_division_by_0_when_roundup_division_then_fails() { } #[test] -fn given_normal_conditions_when_roundup_magnitude_division_then_works() { // TODO Check roundup_magnitude_division function +fn given_normal_conditions_when_roundup_magnitude_division_then_works() { assert( roundup_magnitude_division(i128_new(12, false), 3) == i128_new(4, false), '12/3 should be 4' ); -// assert(roundup_magnitude_division(i128_new(12, true), 3) == i128_new(5, true), '-12/3 should be -4'); -// assert(roundup_magnitude_division(i128_new(13, false), 3) == i128_new(5, false), '13/3 should be 4'); -// assert(roundup_magnitude_division(i128_new(13, true), 3) == i128_new(5, true), '-13/3 should be -4'); -// assert(roundup_magnitude_division(i128_new(13, false), 5) == i128_new(3, false), '13/5 should be 3'); -// assert(roundup_magnitude_division(i128_new(13, true), 5) == i128_new(3, true), '-13/5 should be -2'); -// assert(roundup_magnitude_division(i128_new(9, false), 9) == i128_new(1, false), '9/9 should be 1'); -// assert(roundup_magnitude_division(i128_new(9, true), 9) == i128_new(1, true), '-9/9 should be -1'); -// assert(roundup_magnitude_division(i128_new(9, false), 18) == i128_new(1, false), '9/18 should be 1'); -// assert(roundup_magnitude_division(i128_new(9, true), 18) == i128_new(0, false), '-9/18 should be 0'); -// assert(roundup_magnitude_division(i128_new(9, false), 99) == i128_new(1, false), '9/99 should be 1'); -// assert(roundup_magnitude_division(i128_new(9, true), 99) == i128_new(0, false), '-9/99 should be 0'); -// assert(roundup_magnitude_division(max_i128(), max_i128_as_u128()) == i128_new(1, false), 'max/max should be 1'); -// assert( -// roundup_magnitude_division(min_i128() + i128_new(1, false), max_i128_as_u128()) == i128_new(1, true), 'min/max should be -1' -// ); -// assert(roundup_magnitude_division(i128_new(0, false), 12) == i128_new(0, false), '0/12 should be 0'); + assert( + roundup_magnitude_division(i128_new(12, true), 3) == i128_new(5, true), '-12/3 should be -4' + ); + assert( + roundup_magnitude_division(i128_new(13, false), 3) == i128_new(5, false), '13/3 should be 4' + ); + assert( + roundup_magnitude_division(i128_new(13, true), 3) == i128_new(5, true), '-13/3 should be -4' + ); + assert( + roundup_magnitude_division(i128_new(13, false), 5) == i128_new(3, false), '13/5 should be 3' + ); + assert( + roundup_magnitude_division(i128_new(13, true), 5) == i128_new(3, true), '-13/5 should be -2' + ); + assert( + roundup_magnitude_division(i128_new(9, false), 9) == i128_new(1, false), '9/9 should be 1' + ); + assert( + roundup_magnitude_division(i128_new(9, true), 9) == i128_new(2, true), '-9/9 should be -1' + ); + assert( + roundup_magnitude_division(i128_new(9, false), 18) == i128_new(1, false), '9/18 should be 1' + ); + assert( + roundup_magnitude_division(i128_new(9, true), 18) == i128_new(1, true), '-9/18 should be 0' + ); + assert( + roundup_magnitude_division(i128_new(9, false), 99) == i128_new(1, false), '9/99 should be 1' + ); + assert( + roundup_magnitude_division(i128_new(9, true), 99) == i128_new(1, true), '-9/99 should be 0' + ); + assert( + roundup_magnitude_division(i128_new(0, false), 12) == i128_new(0, false), '0/12 should be 0' + ); } #[test] @@ -90,10 +110,13 @@ fn given_normal_conditions_when_sum_return_uint_128_then_works() { sum_return_uint_128(BoundedInt::max(), i128_new(1, true)) == BoundedInt::max() - 1, 'Should be max - 1' ); - // assert( - // sum_return_uint_128(BoundedInt::max(), min_i128() + i128_new(1, false)) == max_i128_as_u128() + 1, - // 'Should be max/2 +1 (1)' - // ); + assert( + sum_return_uint_128( + BoundedInt::max(), min_i128() + i128_new(1, false) + ) == max_i128_as_u128() + + 2, + 'Should be max/2 + 2 (1)' + ); assert(sum_return_uint_128(0, max_i128()) == max_i128_as_u128(), 'Should be max/2 (2)'); } @@ -185,9 +208,8 @@ fn given_normal_conditions_when_bounded_add_then_works() { let max = max_i128(); let min = min_i128(); // This tests the second if - // TODO fix calc file - // assert(bounded_add(min, i128_new(1, true)) == min, 'Should be min (1)'); - // assert(bounded_add(min + i128_new(1, false), i128_new(1, true)) == min, 'Should be min (2)'); + assert(bounded_add(min, i128_new(1, true)) == min, 'Should be min (1)'); + assert(bounded_add(min + i128_new(1, false), i128_new(1, true)) == min, 'Should be min (2)'); // This tests the third if assert(bounded_add(max, i128_new(1, false)) == max, 'Should be max (1)'); assert(bounded_add(max - i128_new(1, false), i128_new(1, false)) == max, 'Should be max (2)'); @@ -234,9 +256,8 @@ fn given_normal_conditions_when_bounded_sub_then_works() { assert(bounded_sub(max, i128_new(1, true)) == max, 'Should be max (1)'); assert(bounded_sub(max - i128_new(1, false), i128_new(2, true)) == max, 'Should be max (2)'); // This tests the third if - // TODO fix calc file - // assert(bounded_sub(min, i128_new(1, false)) == min, 'Should be min (1)'); - // assert(bounded_sub(min + i128_new(1, false), i128_new(1, false)) == min, 'Should be min (2)'); + assert(bounded_sub(min, i128_new(1, false)) == min, 'Should be min (1)'); + assert(bounded_sub(min + i128_new(1, false), i128_new(1, false)) == min, 'Should be min (2)'); // Zero test case assert( @@ -259,7 +280,7 @@ fn given_normal_conditions_when_to_signed_then_works() { let max = max_i128(); let min = min_i128(); assert(to_signed(max_i128_as_u128(), true) == max, 'Should be max'); - assert(to_signed(max_i128_as_u128(), false) == min + i128_new(1, false), 'Should be min + 1'); + assert(to_signed(max_i128_as_u128(), false) == min, 'Should be min'); } #[test]