Skip to content

Commit

Permalink
Fix tests without num-bigint feature (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjwatson authored Nov 25, 2024
1 parent dbf0c52 commit 9ad1d5a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
43 changes: 31 additions & 12 deletions crates/jiter/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::iter;
use std::str::FromStr;
use std::sync::Arc;

#[cfg(feature = "num-bigint")]
use num_bigint::BigInt;
use smallvec::smallvec;

Expand Down Expand Up @@ -79,6 +80,7 @@ fn display_number(peek: Peek, jiter: &mut Jiter) -> JiterResult<String> {
NumberAny::Int(NumberInt::Int(int)) => {
format!("Int({int}) @ {position}")
}
#[cfg(feature = "num-bigint")]
NumberAny::Int(NumberInt::BigInt(big_int)) => {
format!("BigInt({big_int}) @ {position}")
}
Expand Down Expand Up @@ -164,9 +166,6 @@ single_tests! {
int_zero: ok => "0", "Int(0) @ 1:1";
int_zero_space: ok => "0 ", "Int(0) @ 1:1";
int_neg: ok => "-1234", "Int(-1234) @ 1:1";
big_int: ok => "92233720368547758070", "BigInt(92233720368547758070) @ 1:1";
big_int_neg: ok => "-92233720368547758070", "BigInt(-92233720368547758070) @ 1:1";
big_int2: ok => "99999999999999999999999999999999999999999999999999", "BigInt(99999999999999999999999999999999999999999999999999) @ 1:1";
float_pos: ok => "12.34", "Float(12.34) @ 1:1";
float_neg: ok => "-12.34", "Float(-12.34) @ 1:1";
float_exp: ok => "2.2e10", "Float(22000000000) @ 1:1";
Expand All @@ -178,7 +177,6 @@ single_tests! {
float_exp_massive2: ok => "2e2147483648", "Float(inf) @ 1:1";
float_exp_massive3: ok => "2e2147483646", "Float(inf) @ 1:1";
float_exp_massive4: ok => "2e2147483646", "Float(inf) @ 1:1";
float_exp_massive5: ok => "18446744073709551615000.0", "Float(18446744073709552000000) @ 1:1";
float_exp_massive6: ok => "0.0E667", "Float(0) @ 1:1";
float_exp_tiny0: ok => "2e-2147483647", "Float(0) @ 1:1";
float_exp_tiny1: ok => "2e-2147483648", "Float(0) @ 1:1";
Expand Down Expand Up @@ -235,6 +233,14 @@ single_tests! {
unexpect_value_xx: err => "xx", "ExpectedSomeValue @ 1:1";
}

#[cfg(feature = "num-bigint")]
single_tests! {
big_int: ok => "92233720368547758070", "BigInt(92233720368547758070) @ 1:1";
big_int_neg: ok => "-92233720368547758070", "BigInt(-92233720368547758070) @ 1:1";
big_int2: ok => "99999999999999999999999999999999999999999999999999", "BigInt(99999999999999999999999999999999999999999999999999) @ 1:1";
float_exp_massive5: ok => "18446744073709551615000.0", "Float(18446744073709552000000) @ 1:1";
}

#[test]
fn invalid_string_controls() {
let json = "\"123\x08\x0c\n\r\t\"";
Expand Down Expand Up @@ -1034,6 +1040,7 @@ number_bytes! {
number_bytes_exp_decimal: b" 123.456e4 " => b"123.456e4";
}

#[cfg(feature = "num-bigint")]
#[test]
fn test_4300_int() {
let json = (0..4300).map(|_| "9".to_string()).collect::<Vec<_>>().join("");
Expand All @@ -1048,6 +1055,7 @@ fn test_4300_int() {
}
}

#[cfg(feature = "num-bigint")]
#[test]
fn test_big_int_errs() {
for json in [
Expand Down Expand Up @@ -1316,6 +1324,7 @@ fn value_into_static() -> JsonValue<'static> {
jiter.into_static()
}

#[cfg(feature = "num-bigint")]
#[test]
fn test_into_static() {
let value = crate::value_into_static();
Expand Down Expand Up @@ -1366,6 +1375,7 @@ fn jiter_next_value_owned() {
assert!(matches!(s, Cow::Owned(_)));
}

#[cfg(feature = "num-bigint")]
#[test]
fn i64_max() {
let json = "9223372036854775807";
Expand All @@ -1378,6 +1388,7 @@ fn i64_max() {
}
}

#[cfg(feature = "num-bigint")]
#[test]
fn test_all_int_lengths() {
for int_size in 1..100 {
Expand All @@ -1399,12 +1410,16 @@ fn test_number_int_try_from_bytes() {
let n: NumberInt = b"0".as_ref().try_into().unwrap();
assert_eq!(n, NumberInt::Int(0));

let twenty_nines = "9".repeat(29);
let n: NumberInt = twenty_nines.as_bytes().try_into().unwrap();
match n {
NumberInt::BigInt(v) => assert_eq!(v.to_string(), twenty_nines),
_ => panic!("expected big int"),
#[cfg(feature = "num-bigint")]
{
let twenty_nines = "9".repeat(29);
let n: NumberInt = twenty_nines.as_bytes().try_into().unwrap();
match n {
NumberInt::BigInt(v) => assert_eq!(v.to_string(), twenty_nines),
_ => panic!("expected big int"),
}
}

let e = NumberInt::try_from(b"x23".as_ref()).unwrap_err();
assert_eq!(e.to_string(), "invalid number at index 0");

Expand All @@ -1423,9 +1438,12 @@ fn test_number_int_try_from_bytes() {
let e = NumberInt::try_from(b"0123".as_ref()).unwrap_err();
assert_eq!(e.to_string(), "invalid number at index 1");

let too_long = "9".repeat(4309);
let e = NumberInt::try_from(too_long.as_bytes()).unwrap_err();
assert_eq!(e.to_string(), "number out of range at index 4301");
#[cfg(feature = "num-bigint")]
{
let too_long = "9".repeat(4309);
let e = NumberInt::try_from(too_long.as_bytes()).unwrap_err();
assert_eq!(e.to_string(), "number out of range at index 4301");
}
}

#[test]
Expand Down Expand Up @@ -1623,6 +1641,7 @@ fn jiter_skip_invalid_long_float() {
);
}

#[cfg(feature = "num-bigint")]
#[test]
fn jiter_value_invalid_long_float() {
let e = JsonValue::parse(br#"2121515572557277572557277e"#, false).unwrap_err();
Expand Down
1 change: 1 addition & 0 deletions crates/jiter/tests/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use pyo3::types::PyString;

use jiter::{pystring_fast_new, JsonValue, PythonParse, StringCacheMode};

#[cfg(feature = "num-bigint")]
#[test]
fn test_to_py_object_numeric() {
let value = JsonValue::parse(
Expand Down

0 comments on commit 9ad1d5a

Please sign in to comment.