-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use data_transformer::cairo_types::{CairoBytes31, ParseBytes31Error}; | ||
use starknet_types_core::felt::Felt; | ||
use std::str::FromStr; | ||
|
||
#[cfg(test)] | ||
mod tests_bytes31 { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_happy_case() { | ||
let bytes31 = CairoBytes31::from_str("0x123456789abcdef").unwrap(); | ||
assert_eq!( | ||
Felt::from(bytes31), | ||
Felt::from_hex_unchecked("0x123456789abcdef") | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_max_value() { | ||
let max_bytes31 = CairoBytes31::MAX; | ||
let from_str = CairoBytes31::from_str( | ||
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
) | ||
.unwrap(); | ||
assert_eq!(max_bytes31, from_str); | ||
} | ||
|
||
#[test] | ||
fn test_overflow() { | ||
let result = CairoBytes31::from_str( | ||
"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
); | ||
assert!(matches!(result, Err(ParseBytes31Error::Overflow))); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_hex() { | ||
let result = CairoBytes31::from_str("invalid_hex"); | ||
assert!(matches!(result, Err(ParseBytes31Error::CairoFromStrError))); | ||
} | ||
|
||
#[test] | ||
fn test_empty_string() { | ||
let result = CairoBytes31::from_str(""); | ||
assert!(matches!(result, Err(ParseBytes31Error::CairoFromStrError))); | ||
} | ||
|
||
#[test] | ||
fn test_felt_conversion() { | ||
let bytes31 = CairoBytes31::from_str("0x123").unwrap(); | ||
let felt: Felt = bytes31.into(); | ||
assert_eq!(felt, Felt::from_hex_unchecked("0x123")); | ||
} | ||
|
||
#[test] | ||
fn test_zero_value() { | ||
let bytes31 = CairoBytes31::from_str("0x0").unwrap(); | ||
assert_eq!(Felt::from(bytes31), Felt::from(0_u8)); | ||
} | ||
|
||
#[test] | ||
fn test_leading_zeros() { | ||
let bytes31 = CairoBytes31::from_str("0x000123").unwrap(); | ||
assert_eq!(Felt::from(bytes31), Felt::from_hex_unchecked("0x123")); | ||
} | ||
|
||
#[test] | ||
fn test_error_display() { | ||
assert_eq!( | ||
ParseBytes31Error::CairoFromStrError.to_string(), | ||
"Failed to parse as Cairo type" | ||
); | ||
assert_eq!( | ||
ParseBytes31Error::Overflow.to_string(), | ||
"Number is too large to fit in 31 bytes" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#[cfg(test)] | ||
mod tests_cairo_u96 { | ||
use data_transformer::cairo_types::{CairoU96, ParseCairoU96Error}; | ||
use starknet_types_core::felt::Felt; | ||
use std::str::FromStr; | ||
|
||
const U96_MAX: u128 = (2u128 << 96) - 1; | ||
|
||
#[test] | ||
fn test_from_str_decimal() { | ||
let zero = CairoU96::from_str("0").unwrap(); | ||
let small = CairoU96::from_str("123").unwrap(); | ||
let large = CairoU96::from_str("1000000").unwrap(); | ||
|
||
assert_eq!(Felt::from(zero), Felt::from(0_u128)); | ||
assert_eq!(Felt::from(small), Felt::from(123_u128)); | ||
assert_eq!(Felt::from(large), Felt::from(1000000_u128)); | ||
} | ||
|
||
#[test] | ||
fn test_from_str_hex() { | ||
let zero = CairoU96::from_str("0").unwrap(); | ||
let small_hex = CairoU96::from_str("ff").unwrap(); | ||
let large_hex = CairoU96::from_str("1234abcd").unwrap(); | ||
|
||
assert_eq!(Felt::from(zero), Felt::from(0_u128)); | ||
assert_eq!(Felt::from(small_hex), Felt::from(255_u128)); | ||
assert_eq!(Felt::from(large_hex), Felt::from(0x1234abcd_u128)); | ||
} | ||
|
||
#[test] | ||
fn test_from_str_max_value() { | ||
let max_value_str = U96_MAX.to_string(); | ||
let result = CairoU96::from_str(&max_value_str).unwrap(); | ||
assert_eq!(Felt::from(result), Felt::from(U96_MAX)); | ||
let max_value_hex = format!("{:x}", U96_MAX); | ||
let result_hex = CairoU96::from_str(&max_value_hex).unwrap(); | ||
assert_eq!(Felt::from(result_hex), Felt::from(U96_MAX)); | ||
} | ||
|
||
#[test] | ||
fn test_from_str_invalid_input() { | ||
assert!(matches!( | ||
CairoU96::from_str("not_a_number"), | ||
Err(ParseCairoU96Error::InvalidString(_)) | ||
)); | ||
|
||
assert!(matches!( | ||
CairoU96::from_str("not_hex"), | ||
Err(ParseCairoU96Error::InvalidString(_)) | ||
)); | ||
|
||
assert!(matches!( | ||
CairoU96::from_str(""), | ||
Err(ParseCairoU96Error::InvalidString(_)) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_conversion_to_felt() { | ||
let values = [ | ||
("0", 0_u128), | ||
("123", 123_u128), | ||
("1000000", 1000000_u128), | ||
("ff", 255_u128), | ||
(&U96_MAX.to_string(), U96_MAX), | ||
]; | ||
|
||
for (input, expected) in values { | ||
let cairo_u96 = CairoU96::from_str(input).unwrap(); | ||
assert_eq!(Felt::from(cairo_u96), Felt::from(expected)); | ||
} | ||
} | ||
} |