Skip to content

Commit

Permalink
add tests for I256 type
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-0x committed Sep 30, 2024
1 parent ef6977c commit 8237aeb
Showing 1 changed file with 174 additions and 8 deletions.
182 changes: 174 additions & 8 deletions crates/torii/core/src/sql/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Sub, SubAssign};

use starknet::core::types::U256;
Expand Down Expand Up @@ -61,10 +62,15 @@ impl Add for I256 {
I256 { value: self.value + other.value, is_negative: self.is_negative }
} else {
// Different signs: subtract the smaller value from the larger one
if self.value >= other.value {
I256 { value: self.value - other.value, is_negative: self.is_negative }
} else {
I256 { value: other.value - self.value, is_negative: other.is_negative }
match self.value.cmp(&other.value) {
Ordering::Greater => {
I256 { value: self.value - other.value, is_negative: self.is_negative }
}
Ordering::Less => {
I256 { value: other.value - self.value, is_negative: other.is_negative }
}
// If both values are equal, the result is zero and not negative
Ordering::Equal => I256 { value: U256::from(0u8), is_negative: false },
}
}
}
Expand All @@ -79,10 +85,15 @@ impl Sub for I256 {
I256 { value: self.value + other.value, is_negative: self.is_negative }
} else {
// Same sign: subtract the values
if self.value >= other.value {
I256 { value: self.value - other.value, is_negative: self.is_negative }
} else {
I256 { value: other.value - self.value, is_negative: !other.is_negative }
match self.value.cmp(&other.value) {
Ordering::Greater => {
I256 { value: self.value - other.value, is_negative: self.is_negative }
}
Ordering::Less => {
I256 { value: other.value - self.value, is_negative: !self.is_negative }
}
// If both values are equal, the result is zero and not negative
Ordering::Equal => I256 { value: U256::from(0u8), is_negative: false },
}
}
}
Expand All @@ -99,3 +110,158 @@ impl SubAssign for I256 {
*self = *self - other;
}

Check warning on line 111 in crates/torii/core/src/sql/utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/core/src/sql/utils.rs#L109-L111

Added lines #L109 - L111 were not covered by tests
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_add_zero_false_and_zero_false() {
// 0,false + 0,false == 0,false
let a = I256::default();
let b = I256::default();
let result = a + b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_add_zero_true_and_zero_false() {
// 0,true + 0,false
let a = I256 { value: U256::from(0u8), is_negative: true };
let b = I256::default();
let result = a + b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_sub_zero_false_and_zero_false() {
// 0,false - 0,false
let a = I256::default();
let b = I256::default();
let result = a - b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_sub_zero_true_and_zero_false() {
// 0,true - 0,false
let a = I256 { value: U256::from(0u8), is_negative: true };
let b = I256::default();
let result = a - b;
assert_eq!(result.value, U256::from(0u8));
assert!(result.is_negative);
}

#[test]
fn test_add_positive_and_negative_equal_values() {
// 5,false + 5,true == 0,false
let a = I256::from(U256::from(5u8));
let b = I256 { value: U256::from(5u8), is_negative: true };
let result = a + b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_sub_positive_and_negative() {
// 10,false - 5,true == 15,false
let a = I256::from(U256::from(10u8));
let b = I256 { value: U256::from(5u8), is_negative: true };
let result = a - b;
assert_eq!(result.value, U256::from(15u8));
assert!(!result.is_negative);
}

#[test]
fn test_sub_larger_from_smaller() {
// 5,false - 10,true == 5,true
let a = I256::from(U256::from(5u8));
let b = I256::from(U256::from(10u8));
let result = a - b;
assert_eq!(result.value, U256::from(5u8));
assert!(result.is_negative);
}

#[test]
fn test_add_mixed_signs() {
// 15,false + 10,true == 5,false
let a = I256::from(U256::from(15u8));
let b = I256 { value: U256::from(10u8), is_negative: true };
let result = a + b;
assert_eq!(result.value, U256::from(5u8));
assert!(!result.is_negative);
}

#[test]
fn test_sub_mixed_signs() {
// 5,false - 10,true == 15,false
let a = I256::from(U256::from(5u8));
let b = I256 { value: U256::from(10u8), is_negative: true };
let result = a - b;
assert_eq!(result.value, U256::from(15u8));
assert!(!result.is_negative);
}

#[test]
fn test_add_negative_and_negative() {
// -5,true + -3,true == -8,true
let a = I256 { value: U256::from(5u8), is_negative: true };
let b = I256 { value: U256::from(3u8), is_negative: true };
let result = a + b;
assert_eq!(result.value, U256::from(8u8));
assert!(result.is_negative);
}

#[test]
fn test_sub_negative_and_negative() {
// -5,true - -3,true == -2,true
let a = I256 { value: U256::from(5u8), is_negative: true };
let b = I256 { value: U256::from(3u8), is_negative: true };
let result = a - b;
assert_eq!(result.value, U256::from(2u8));
assert!(result.is_negative);
}

#[test]
fn test_subtraction_resulting_zero() {
// 5,false - 5,false == 0,false
let a = I256::from(U256::from(5u8));
let b = I256::from(U256::from(5u8));
let result = a - b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_subtraction_resulting_zero_negative() {
// 5,true - 5,true == 0,false
let a = I256 { value: U256::from(5u8), is_negative: true };
let b = I256 { value: U256::from(5u8), is_negative: true };
let result = a - b;
assert_eq!(result.value, U256::from(0u8));
assert!(!result.is_negative);
}

#[test]
fn test_add_negative_and_positive_result_positive() {
// -10,true + 15,false == 5,false
let a = I256 { value: U256::from(10u8), is_negative: true };
let b = I256::from(U256::from(15u8));
let result = a + b;
assert_eq!(result.value, U256::from(5u8));
assert!(!result.is_negative);
}

#[test]
fn test_add_negative_and_positive_result_negative() {
// -15,true + 5,false == -10,true
let a = I256 { value: U256::from(15u8), is_negative: true };
let b = I256::from(U256::from(5u8));
let result = a + b;
assert_eq!(result.value, U256::from(10u8));
assert!(result.is_negative);
}
}

0 comments on commit 8237aeb

Please sign in to comment.