Skip to content

Commit 6a2896f

Browse files
tests: Add test for nan
Signed-off-by: Patrick José Pereira <[email protected]>
1 parent acda615 commit 6a2896f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

tests/debug.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ fn value_bool() {
2424

2525
#[test]
2626
fn value_number() {
27+
#[cfg(all(not(feature = "arbitrary_precision"), feature = "std"))]
28+
assert_eq!(format!("{:?}", json!(::std::f64::NAN)), "Number(NaN)");
29+
2730
assert_eq!(format!("{:?}", json!(1)), "Number(1)");
2831
assert_eq!(format!("{:?}", json!(-1)), "Number(-1)");
2932
assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)");

tests/test.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,21 @@ fn test_write_f64() {
161161
#[test]
162162
fn test_encode_nonfinite_float_yields_null() {
163163
let v = to_value(::std::f64::NAN).unwrap();
164-
assert!(v.is_null());
164+
if !cfg!(feature = "arbitrary_precision") && cfg!(feature = "std") {
165+
assert!(v.is_f64());
166+
} else {
167+
assert!(v.is_null());
168+
}
165169

166170
let v = to_value(::std::f64::INFINITY).unwrap();
167171
assert!(v.is_null());
168172

169173
let v = to_value(::std::f32::NAN).unwrap();
170-
assert!(v.is_null());
174+
if !cfg!(feature = "arbitrary_precision") && cfg!(feature = "std") {
175+
assert!(v.is_f64());
176+
} else {
177+
assert!(v.is_null());
178+
}
171179

172180
let v = to_value(::std::f32::INFINITY).unwrap();
173181
assert!(v.is_null());
@@ -693,6 +701,17 @@ fn test_parse_null() {
693701
test_parse_ok(vec![("null", ())]);
694702
}
695703

704+
#[test]
705+
fn test_parse_nan() {
706+
test_parse_err::<()>(&[
707+
("n", "EOF while parsing a value at line 1 column 1"),
708+
("na", "expected ident at line 1 column 2"),
709+
("nana", "expected ident at line 1 column 2"),
710+
]);
711+
712+
assert!(from_str::<f64>("nan").unwrap().is_nan());
713+
}
714+
696715
#[test]
697716
fn test_parse_bool() {
698717
test_parse_err::<bool>(&[

0 commit comments

Comments
 (0)