Skip to content

Commit

Permalink
test: Modified tests in the files on the math folder
Browse files Browse the repository at this point in the history
Signed-off-by: Abinand P <[email protected]>
  • Loading branch information
Abiji-2020 committed Oct 30, 2024
1 parent 86f99ab commit fe62288
Showing 1 changed file with 55 additions and 71 deletions.
126 changes: 55 additions & 71 deletions crates/proof-of-sql/src/base/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub(crate) fn try_convert_intermediate_decimal_to_scalar<S: Scalar>(
mod scale_adjust_test {

use super::*;
use crate::base::scalar::Curve25519Scalar;
use crate::base::scalar::test_scalar::TestScalar;
use num_bigint::BigInt;

#[test]
Expand All @@ -181,14 +181,12 @@ mod scale_adjust_test {

let target_scale = 5;

assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX)).unwrap(),
target_scale
)
.is_err()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX)).unwrap(),
target_scale
)
.is_err());
}

#[test]
Expand All @@ -207,13 +205,13 @@ mod scale_adjust_test {
let decimal = "120.00".parse().unwrap();
let target_scale = -1;
let expected = [12, 0, 0, 0];
let result = try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
let result = try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale,
)
.unwrap();
assert_eq!(result, Curve25519Scalar::from(expected));
assert_eq!(result, TestScalar::from(expected));
}

#[test]
Expand All @@ -222,28 +220,28 @@ mod scale_adjust_test {
let target_scale = -2;
let expected_limbs = [123, 0, 0, 0];

let limbs = try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
let limbs = try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX)).unwrap(),
target_scale,
)
.unwrap();

assert_eq!(limbs, Curve25519Scalar::from(expected_limbs));
assert_eq!(limbs, TestScalar::from(expected_limbs));
}

#[test]
fn we_can_match_negative_decimals() {
let decimal = "-123.45".parse().unwrap();
let target_scale = 2;
let expected_limbs = [12345, 0, 0, 0];
let limbs = try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
let limbs = try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX)).unwrap(),
target_scale,
)
.unwrap();
assert_eq!(limbs, -Curve25519Scalar::from(expected_limbs));
assert_eq!(limbs, -TestScalar::from(expected_limbs));
}

#[allow(clippy::cast_possible_wrap)]
Expand All @@ -254,94 +252,80 @@ mod scale_adjust_test {
.parse()
.unwrap();
let target_scale = 6; // now precision exceeds maximum
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_err()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_err());

// maximum decimal value we can support
let decimal =
"99999999999999999999999999999999999999999999999999999999999999999999999999.0"
.parse()
.unwrap();
let target_scale = 1;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok());

// scaling larger than max will fail
let decimal =
"999999999999999999999999999999999999999999999999999999999999999999999999999.0"
.parse()
.unwrap();
let target_scale = 1;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_err()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_err());

// smallest possible decimal value we can support (either signed/unsigned)
let decimal =
"0.000000000000000000000000000000000000000000000000000000000000000000000000001"
.parse()
.unwrap();
let target_scale = MAX_SUPPORTED_PRECISION as i8;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_ok()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_ok());

// this is ok because it can be scaled to 75 precision
let decimal = "0.1".parse().unwrap();
let target_scale = MAX_SUPPORTED_PRECISION as i8;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok());

// this exceeds max precision
let decimal = "1.0".parse().unwrap();
let target_scale = 75;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_err()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(u8::try_from(decimal.precision()).unwrap_or(u8::MAX),).unwrap(),
target_scale
)
.is_err());

// but this is ok
let decimal = "1.0".parse().unwrap();
let target_scale = 74;
assert!(
try_convert_intermediate_decimal_to_scalar::<Curve25519Scalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok()
);
assert!(try_convert_intermediate_decimal_to_scalar::<TestScalar>(
&decimal,
Precision::new(MAX_SUPPORTED_PRECISION).unwrap(),
target_scale
)
.is_ok());
}
}

0 comments on commit fe62288

Please sign in to comment.