From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/serde_json/tests/compiletest.rs | 7 + third_party/rust/serde_json/tests/debug.rs | 81 + third_party/rust/serde_json/tests/lexical.rs | 52 + .../rust/serde_json/tests/lexical/algorithm.rs | 110 + .../rust/serde_json/tests/lexical/exponent.rs | 54 + third_party/rust/serde_json/tests/lexical/float.rs | 581 +++++ third_party/rust/serde_json/tests/lexical/math.rs | 211 ++ third_party/rust/serde_json/tests/lexical/num.rs | 76 + third_party/rust/serde_json/tests/lexical/parse.rs | 204 ++ .../rust/serde_json/tests/lexical/rounding.rs | 316 +++ third_party/rust/serde_json/tests/macros/mod.rs | 61 + third_party/rust/serde_json/tests/map.rs | 47 + third_party/rust/serde_json/tests/regression.rs | 3 + .../rust/serde_json/tests/regression/issue520.rs | 20 + .../rust/serde_json/tests/regression/issue795.rs | 59 + .../rust/serde_json/tests/regression/issue845.rs | 74 + .../rust/serde_json/tests/regression/issue953.rs | 9 + third_party/rust/serde_json/tests/stream.rs | 183 ++ third_party/rust/serde_json/tests/test.rs | 2408 ++++++++++++++++++++ .../rust/serde_json/tests/ui/missing_colon.rs | 5 + .../rust/serde_json/tests/ui/missing_colon.stderr | 12 + .../rust/serde_json/tests/ui/missing_comma.rs | 5 + .../rust/serde_json/tests/ui/missing_comma.stderr | 13 + .../rust/serde_json/tests/ui/missing_value.rs | 5 + .../rust/serde_json/tests/ui/missing_value.stderr | 12 + third_party/rust/serde_json/tests/ui/not_found.rs | 5 + .../rust/serde_json/tests/ui/not_found.stderr | 5 + third_party/rust/serde_json/tests/ui/parse_expr.rs | 5 + .../rust/serde_json/tests/ui/parse_expr.stderr | 11 + third_party/rust/serde_json/tests/ui/parse_key.rs | 5 + .../rust/serde_json/tests/ui/parse_key.stderr | 5 + .../tests/ui/unexpected_after_array_element.rs | 5 + .../tests/ui/unexpected_after_array_element.stderr | 7 + .../tests/ui/unexpected_after_map_entry.rs | 5 + .../tests/ui/unexpected_after_map_entry.stderr | 7 + .../rust/serde_json/tests/ui/unexpected_colon.rs | 5 + .../serde_json/tests/ui/unexpected_colon.stderr | 7 + .../rust/serde_json/tests/ui/unexpected_comma.rs | 5 + .../serde_json/tests/ui/unexpected_comma.stderr | 7 + 39 files changed, 4692 insertions(+) create mode 100644 third_party/rust/serde_json/tests/compiletest.rs create mode 100644 third_party/rust/serde_json/tests/debug.rs create mode 100644 third_party/rust/serde_json/tests/lexical.rs create mode 100644 third_party/rust/serde_json/tests/lexical/algorithm.rs create mode 100644 third_party/rust/serde_json/tests/lexical/exponent.rs create mode 100644 third_party/rust/serde_json/tests/lexical/float.rs create mode 100644 third_party/rust/serde_json/tests/lexical/math.rs create mode 100644 third_party/rust/serde_json/tests/lexical/num.rs create mode 100644 third_party/rust/serde_json/tests/lexical/parse.rs create mode 100644 third_party/rust/serde_json/tests/lexical/rounding.rs create mode 100644 third_party/rust/serde_json/tests/macros/mod.rs create mode 100644 third_party/rust/serde_json/tests/map.rs create mode 100644 third_party/rust/serde_json/tests/regression.rs create mode 100644 third_party/rust/serde_json/tests/regression/issue520.rs create mode 100644 third_party/rust/serde_json/tests/regression/issue795.rs create mode 100644 third_party/rust/serde_json/tests/regression/issue845.rs create mode 100644 third_party/rust/serde_json/tests/regression/issue953.rs create mode 100644 third_party/rust/serde_json/tests/stream.rs create mode 100644 third_party/rust/serde_json/tests/test.rs create mode 100644 third_party/rust/serde_json/tests/ui/missing_colon.rs create mode 100644 third_party/rust/serde_json/tests/ui/missing_colon.stderr create mode 100644 third_party/rust/serde_json/tests/ui/missing_comma.rs create mode 100644 third_party/rust/serde_json/tests/ui/missing_comma.stderr create mode 100644 third_party/rust/serde_json/tests/ui/missing_value.rs create mode 100644 third_party/rust/serde_json/tests/ui/missing_value.stderr create mode 100644 third_party/rust/serde_json/tests/ui/not_found.rs create mode 100644 third_party/rust/serde_json/tests/ui/not_found.stderr create mode 100644 third_party/rust/serde_json/tests/ui/parse_expr.rs create mode 100644 third_party/rust/serde_json/tests/ui/parse_expr.stderr create mode 100644 third_party/rust/serde_json/tests/ui/parse_key.rs create mode 100644 third_party/rust/serde_json/tests/ui/parse_key.stderr create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_colon.rs create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_colon.stderr create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_comma.rs create mode 100644 third_party/rust/serde_json/tests/ui/unexpected_comma.stderr (limited to 'third_party/rust/serde_json/tests') diff --git a/third_party/rust/serde_json/tests/compiletest.rs b/third_party/rust/serde_json/tests/compiletest.rs new file mode 100644 index 0000000000..7974a6249e --- /dev/null +++ b/third_party/rust/serde_json/tests/compiletest.rs @@ -0,0 +1,7 @@ +#[rustversion::attr(not(nightly), ignore)] +#[cfg_attr(miri, ignore)] +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/third_party/rust/serde_json/tests/debug.rs b/third_party/rust/serde_json/tests/debug.rs new file mode 100644 index 0000000000..8ddcf5a381 --- /dev/null +++ b/third_party/rust/serde_json/tests/debug.rs @@ -0,0 +1,81 @@ +use indoc::indoc; +use serde_json::{json, Number, Value}; + +#[test] +fn number() { + assert_eq!(format!("{:?}", Number::from(1)), "Number(1)"); + assert_eq!(format!("{:?}", Number::from(-1)), "Number(-1)"); + assert_eq!( + format!("{:?}", Number::from_f64(1.0).unwrap()), + "Number(1.0)" + ); +} + +#[test] +fn value_null() { + assert_eq!(format!("{:?}", json!(null)), "Null"); +} + +#[test] +fn value_bool() { + assert_eq!(format!("{:?}", json!(true)), "Bool(true)"); + assert_eq!(format!("{:?}", json!(false)), "Bool(false)"); +} + +#[test] +fn value_number() { + assert_eq!(format!("{:?}", json!(1)), "Number(1)"); + assert_eq!(format!("{:?}", json!(-1)), "Number(-1)"); + assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)"); + assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1" + assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41"); +} + +#[test] +fn value_string() { + assert_eq!(format!("{:?}", json!("s")), "String(\"s\")"); +} + +#[test] +fn value_array() { + assert_eq!(format!("{:?}", json!([])), "Array []"); +} + +#[test] +fn value_object() { + assert_eq!(format!("{:?}", json!({})), "Object {}"); +} + +#[test] +fn error() { + let err = serde_json::from_str::("{0}").unwrap_err(); + let expected = "Error(\"key must be a string\", line: 1, column: 2)"; + assert_eq!(format!("{:?}", err), expected); +} + +#[test] +fn indented() { + let j = json!({ + "Array": [true], + "Bool": true, + "EmptyArray": [], + "EmptyObject": {}, + "Null": null, + "Number": 1, + "String": "...", + }); + let expected = indoc! {r#" + Object { + "Array": Array [ + Bool(true), + ], + "Bool": Bool(true), + "EmptyArray": Array [], + "EmptyObject": Object {}, + "Null": Null, + "Number": Number(1), + "String": String("..."), + }"# + }; + assert_eq!(format!("{:#?}", j), expected); +} diff --git a/third_party/rust/serde_json/tests/lexical.rs b/third_party/rust/serde_json/tests/lexical.rs new file mode 100644 index 0000000000..6e0f07b8c3 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical.rs @@ -0,0 +1,52 @@ +#![allow( + clippy::cast_lossless, + clippy::cast_possible_truncation, + clippy::cast_possible_wrap, + clippy::cast_precision_loss, + clippy::cast_sign_loss, + clippy::comparison_chain, + clippy::doc_markdown, + clippy::excessive_precision, + clippy::float_cmp, + clippy::if_not_else, + clippy::module_name_repetitions, + clippy::needless_late_init, + clippy::shadow_unrelated, + clippy::similar_names, + clippy::single_match_else, + clippy::too_many_lines, + clippy::unreadable_literal, + clippy::unseparated_literal_suffix, + clippy::wildcard_imports +)] + +extern crate alloc; + +#[path = "../src/lexical/mod.rs"] +mod lexical; + +mod lib { + pub use std::vec::Vec; + pub use std::{cmp, iter, mem, ops}; +} + +#[path = "lexical/algorithm.rs"] +mod algorithm; + +#[path = "lexical/exponent.rs"] +mod exponent; + +#[path = "lexical/float.rs"] +mod float; + +#[path = "lexical/math.rs"] +mod math; + +#[path = "lexical/num.rs"] +mod num; + +#[path = "lexical/parse.rs"] +mod parse; + +#[path = "lexical/rounding.rs"] +mod rounding; diff --git a/third_party/rust/serde_json/tests/lexical/algorithm.rs b/third_party/rust/serde_json/tests/lexical/algorithm.rs new file mode 100644 index 0000000000..7f3a2c6241 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/algorithm.rs @@ -0,0 +1,110 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::algorithm::*; +use crate::lexical::num::Float; + +#[test] +fn float_fast_path_test() { + // valid + let mantissa = (1 << f32::MANTISSA_SIZE) - 1; + let (min_exp, max_exp) = f32::exponent_limit(); + for exp in min_exp..=max_exp { + let f = fast_path::(mantissa, exp); + assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp)); + } + + // Check slightly above valid exponents + let f = fast_path::(123, 15); + assert_eq!(f, Some(1.23e+17)); + + // Exponent is 1 too high, pushes over the mantissa. + let f = fast_path::(123, 16); + assert!(f.is_none()); + + // Mantissa is too large, checked_mul should overflow. + let f = fast_path::(mantissa, 11); + assert!(f.is_none()); + + // invalid exponents + let (min_exp, max_exp) = f32::exponent_limit(); + let f = fast_path::(mantissa, min_exp - 1); + assert!(f.is_none(), "exponent under min_exp"); + + let f = fast_path::(mantissa, max_exp + 1); + assert!(f.is_none(), "exponent above max_exp"); +} + +#[test] +fn double_fast_path_test() { + // valid + let mantissa = (1 << f64::MANTISSA_SIZE) - 1; + let (min_exp, max_exp) = f64::exponent_limit(); + for exp in min_exp..=max_exp { + let f = fast_path::(mantissa, exp); + assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp)); + } + + // invalid exponents + let (min_exp, max_exp) = f64::exponent_limit(); + let f = fast_path::(mantissa, min_exp - 1); + assert!(f.is_none(), "exponent under min_exp"); + + let f = fast_path::(mantissa, max_exp + 1); + assert!(f.is_none(), "exponent above max_exp"); + + assert_eq!( + Some(0.04628372940652459), + fast_path::(4628372940652459, -17) + ); + assert_eq!(None, fast_path::(26383446160308229, -272)); +} + +#[test] +fn moderate_path_test() { + let (f, valid) = moderate_path::(1234567890, -1, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.0); + + let (f, valid) = moderate_path::(1234567891, -1, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.1); + + let (f, valid) = moderate_path::(12345678912, -2, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.12); + + let (f, valid) = moderate_path::(123456789123, -3, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.123); + + let (f, valid) = moderate_path::(1234567891234, -4, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.1234); + + let (f, valid) = moderate_path::(12345678912345, -5, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.12345); + + let (f, valid) = moderate_path::(123456789123456, -6, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.123456); + + let (f, valid) = moderate_path::(1234567891234567, -7, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.1234567); + + let (f, valid) = moderate_path::(12345678912345679, -8, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 123456789.12345679); + + let (f, valid) = moderate_path::(4628372940652459, -17, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 0.04628372940652459); + + let (f, valid) = moderate_path::(26383446160308229, -272, false); + assert!(valid, "should be valid"); + assert_eq!(f.into_float::(), 2.6383446160308229e-256); + + let (_, valid) = moderate_path::(26383446160308230, -272, false); + assert!(!valid, "should be invalid"); +} diff --git a/third_party/rust/serde_json/tests/lexical/exponent.rs b/third_party/rust/serde_json/tests/lexical/exponent.rs new file mode 100644 index 0000000000..f7a847be38 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/exponent.rs @@ -0,0 +1,54 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::exponent::*; + +#[test] +fn scientific_exponent_test() { + // 0 digits in the integer + assert_eq!(scientific_exponent(0, 0, 5), -6); + assert_eq!(scientific_exponent(10, 0, 5), 4); + assert_eq!(scientific_exponent(-10, 0, 5), -16); + + // >0 digits in the integer + assert_eq!(scientific_exponent(0, 1, 5), 0); + assert_eq!(scientific_exponent(0, 2, 5), 1); + assert_eq!(scientific_exponent(0, 2, 20), 1); + assert_eq!(scientific_exponent(10, 2, 20), 11); + assert_eq!(scientific_exponent(-10, 2, 20), -9); + + // Underflow + assert_eq!( + scientific_exponent(i32::min_value(), 0, 0), + i32::min_value() + ); + assert_eq!( + scientific_exponent(i32::min_value(), 0, 5), + i32::min_value() + ); + + // Overflow + assert_eq!( + scientific_exponent(i32::max_value(), 0, 0), + i32::max_value() - 1 + ); + assert_eq!( + scientific_exponent(i32::max_value(), 5, 0), + i32::max_value() + ); +} + +#[test] +fn mantissa_exponent_test() { + assert_eq!(mantissa_exponent(10, 5, 0), 5); + assert_eq!(mantissa_exponent(0, 5, 0), -5); + assert_eq!( + mantissa_exponent(i32::max_value(), 5, 0), + i32::max_value() - 5 + ); + assert_eq!(mantissa_exponent(i32::max_value(), 0, 5), i32::max_value()); + assert_eq!(mantissa_exponent(i32::min_value(), 5, 0), i32::min_value()); + assert_eq!( + mantissa_exponent(i32::min_value(), 0, 5), + i32::min_value() + 5 + ); +} diff --git a/third_party/rust/serde_json/tests/lexical/float.rs b/third_party/rust/serde_json/tests/lexical/float.rs new file mode 100644 index 0000000000..c87e7e1ed9 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/float.rs @@ -0,0 +1,581 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::float::ExtendedFloat; +use crate::lexical::rounding::round_nearest_tie_even; +use std::{f32, f64}; + +// NORMALIZE + +fn check_normalize(mant: u64, exp: i32, shift: u32, r_mant: u64, r_exp: i32) { + let mut x = ExtendedFloat { mant, exp }; + assert_eq!(x.normalize(), shift); + assert_eq!( + x, + ExtendedFloat { + mant: r_mant, + exp: r_exp + } + ); +} + +#[test] +fn normalize_test() { + // F32 + // 0 + check_normalize(0, 0, 0, 0, 0); + + // min value + check_normalize(1, -149, 63, 9223372036854775808, -212); + + // 1.0e-40 + check_normalize(71362, -149, 47, 10043308644012916736, -196); + + // 1.0e-20 + check_normalize(12379400, -90, 40, 13611294244890214400, -130); + + // 1.0 + check_normalize(8388608, -23, 40, 9223372036854775808, -63); + + // 1e20 + check_normalize(11368684, 43, 40, 12500000250510966784, 3); + + // max value + check_normalize(16777213, 104, 40, 18446740775174668288, 64); + + // F64 + + // min value + check_normalize(1, -1074, 63, 9223372036854775808, -1137); + + // 1.0e-250 + check_normalize(6448907850777164, -883, 11, 13207363278391631872, -894); + + // 1.0e-150 + check_normalize(7371020360979573, -551, 11, 15095849699286165504, -562); + + // 1.0e-45 + check_normalize(6427752177035961, -202, 11, 13164036458569648128, -213); + + // 1.0e-40 + check_normalize(4903985730770844, -185, 11, 10043362776618688512, -196); + + // 1.0e-20 + check_normalize(6646139978924579, -119, 11, 13611294676837537792, -130); + + // 1.0 + check_normalize(4503599627370496, -52, 11, 9223372036854775808, -63); + + // 1e20 + check_normalize(6103515625000000, 14, 11, 12500000000000000000, 3); + + // 1e40 + check_normalize(8271806125530277, 80, 11, 16940658945086007296, 69); + + // 1e150 + check_normalize(5503284107318959, 446, 11, 11270725851789228032, 435); + + // 1e250 + check_normalize(6290184345309700, 778, 11, 12882297539194265600, 767); + + // max value + check_normalize(9007199254740991, 971, 11, 18446744073709549568, 960); +} + +// ROUND + +fn check_round_to_f32(mant: u64, exp: i32, r_mant: u64, r_exp: i32) { + let mut x = ExtendedFloat { mant, exp }; + x.round_to_native::(round_nearest_tie_even); + assert_eq!( + x, + ExtendedFloat { + mant: r_mant, + exp: r_exp + } + ); +} + +#[test] +fn round_to_f32_test() { + // This is lossy, so some of these values are **slightly** rounded. + + // underflow + check_round_to_f32(9223372036854775808, -213, 0, -149); + + // min value + check_round_to_f32(9223372036854775808, -212, 1, -149); + + // 1.0e-40 + check_round_to_f32(10043308644012916736, -196, 71362, -149); + + // 1.0e-20 + check_round_to_f32(13611294244890214400, -130, 12379400, -90); + + // 1.0 + check_round_to_f32(9223372036854775808, -63, 8388608, -23); + + // 1e20 + check_round_to_f32(12500000250510966784, 3, 11368684, 43); + + // max value + check_round_to_f32(18446740775174668288, 64, 16777213, 104); + + // overflow + check_round_to_f32(18446740775174668288, 65, 16777213, 105); +} + +fn check_round_to_f64(mant: u64, exp: i32, r_mant: u64, r_exp: i32) { + let mut x = ExtendedFloat { mant, exp }; + x.round_to_native::(round_nearest_tie_even); + assert_eq!( + x, + ExtendedFloat { + mant: r_mant, + exp: r_exp + } + ); +} + +#[test] +fn round_to_f64_test() { + // This is lossy, so some of these values are **slightly** rounded. + + // underflow + check_round_to_f64(9223372036854775808, -1138, 0, -1074); + + // min value + check_round_to_f64(9223372036854775808, -1137, 1, -1074); + + // 1.0e-250 + check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551); + + // 1.0e-150 + check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551); + + // 1.0e-45 + check_round_to_f64(13164036458569648128, -213, 6427752177035961, -202); + + // 1.0e-40 + check_round_to_f64(10043362776618688512, -196, 4903985730770844, -185); + + // 1.0e-20 + check_round_to_f64(13611294676837537792, -130, 6646139978924579, -119); + + // 1.0 + check_round_to_f64(9223372036854775808, -63, 4503599627370496, -52); + + // 1e20 + check_round_to_f64(12500000000000000000, 3, 6103515625000000, 14); + + // 1e40 + check_round_to_f64(16940658945086007296, 69, 8271806125530277, 80); + + // 1e150 + check_round_to_f64(11270725851789228032, 435, 5503284107318959, 446); + + // 1e250 + check_round_to_f64(12882297539194265600, 767, 6290184345309700, 778); + + // max value + check_round_to_f64(18446744073709549568, 960, 9007199254740991, 971); + + // Bug fixes + // 1.2345e-308 + check_round_to_f64(10234494226754558294, -1086, 2498655817078750, -1074); +} + +fn assert_normalized_eq(mut x: ExtendedFloat, mut y: ExtendedFloat) { + x.normalize(); + y.normalize(); + assert_eq!(x, y); +} + +#[test] +fn from_float() { + let values: [f32; 26] = [ + 1e-40, 2e-40, 1e-35, 2e-35, 1e-30, 2e-30, 1e-25, 2e-25, 1e-20, 2e-20, 1e-15, 2e-15, 1e-10, + 2e-10, 1e-5, 2e-5, 1.0, 2.0, 1e5, 2e5, 1e10, 2e10, 1e15, 2e15, 1e20, 2e20, + ]; + for value in &values { + assert_normalized_eq( + ExtendedFloat::from_float(*value), + ExtendedFloat::from_float(*value as f64), + ); + } +} + +// TO + +// Sample of interesting numbers to check during standard test builds. +const INTEGERS: [u64; 32] = [ + 0, // 0x0 + 1, // 0x1 + 7, // 0x7 + 15, // 0xF + 112, // 0x70 + 119, // 0x77 + 127, // 0x7F + 240, // 0xF0 + 247, // 0xF7 + 255, // 0xFF + 2032, // 0x7F0 + 2039, // 0x7F7 + 2047, // 0x7FF + 4080, // 0xFF0 + 4087, // 0xFF7 + 4095, // 0xFFF + 65520, // 0xFFF0 + 65527, // 0xFFF7 + 65535, // 0xFFFF + 1048560, // 0xFFFF0 + 1048567, // 0xFFFF7 + 1048575, // 0xFFFFF + 16777200, // 0xFFFFF0 + 16777207, // 0xFFFFF7 + 16777215, // 0xFFFFFF + 268435440, // 0xFFFFFF0 + 268435447, // 0xFFFFFF7 + 268435455, // 0xFFFFFFF + 4294967280, // 0xFFFFFFF0 + 4294967287, // 0xFFFFFFF7 + 4294967295, // 0xFFFFFFFF + 18446744073709551615, // 0xFFFFFFFFFFFFFFFF +]; + +#[test] +fn to_f32_test() { + // underflow + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -213, + }; + assert_eq!(x.into_float::(), 0.0); + + // min value + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -212, + }; + assert_eq!(x.into_float::(), 1e-45); + + // 1.0e-40 + let x = ExtendedFloat { + mant: 10043308644012916736, + exp: -196, + }; + assert_eq!(x.into_float::(), 1e-40); + + // 1.0e-20 + let x = ExtendedFloat { + mant: 13611294244890214400, + exp: -130, + }; + assert_eq!(x.into_float::(), 1e-20); + + // 1.0 + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -63, + }; + assert_eq!(x.into_float::(), 1.0); + + // 1e20 + let x = ExtendedFloat { + mant: 12500000250510966784, + exp: 3, + }; + assert_eq!(x.into_float::(), 1e20); + + // max value + let x = ExtendedFloat { + mant: 18446740775174668288, + exp: 64, + }; + assert_eq!(x.into_float::(), 3.402823e38); + + // almost max, high exp + let x = ExtendedFloat { + mant: 1048575, + exp: 108, + }; + assert_eq!(x.into_float::(), 3.4028204e38); + + // max value + 1 + let x = ExtendedFloat { + mant: 16777216, + exp: 104, + }; + assert_eq!(x.into_float::(), f32::INFINITY); + + // max value + 1 + let x = ExtendedFloat { + mant: 1048576, + exp: 108, + }; + assert_eq!(x.into_float::(), f32::INFINITY); + + // 1e40 + let x = ExtendedFloat { + mant: 16940658945086007296, + exp: 69, + }; + assert_eq!(x.into_float::(), f32::INFINITY); + + // Integers. + for int in &INTEGERS { + let fp = ExtendedFloat { mant: *int, exp: 0 }; + assert_eq!(fp.into_float::(), *int as f32, "{:?} as f32", *int); + } +} + +#[test] +fn to_f64_test() { + // underflow + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -1138, + }; + assert_eq!(x.into_float::(), 0.0); + + // min value + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -1137, + }; + assert_eq!(x.into_float::(), 5e-324); + + // 1.0e-250 + let x = ExtendedFloat { + mant: 13207363278391631872, + exp: -894, + }; + assert_eq!(x.into_float::(), 1e-250); + + // 1.0e-150 + let x = ExtendedFloat { + mant: 15095849699286165504, + exp: -562, + }; + assert_eq!(x.into_float::(), 1e-150); + + // 1.0e-45 + let x = ExtendedFloat { + mant: 13164036458569648128, + exp: -213, + }; + assert_eq!(x.into_float::(), 1e-45); + + // 1.0e-40 + let x = ExtendedFloat { + mant: 10043362776618688512, + exp: -196, + }; + assert_eq!(x.into_float::(), 1e-40); + + // 1.0e-20 + let x = ExtendedFloat { + mant: 13611294676837537792, + exp: -130, + }; + assert_eq!(x.into_float::(), 1e-20); + + // 1.0 + let x = ExtendedFloat { + mant: 9223372036854775808, + exp: -63, + }; + assert_eq!(x.into_float::(), 1.0); + + // 1e20 + let x = ExtendedFloat { + mant: 12500000000000000000, + exp: 3, + }; + assert_eq!(x.into_float::(), 1e20); + + // 1e40 + let x = ExtendedFloat { + mant: 16940658945086007296, + exp: 69, + }; + assert_eq!(x.into_float::(), 1e40); + + // 1e150 + let x = ExtendedFloat { + mant: 11270725851789228032, + exp: 435, + }; + assert_eq!(x.into_float::(), 1e150); + + // 1e250 + let x = ExtendedFloat { + mant: 12882297539194265600, + exp: 767, + }; + assert_eq!(x.into_float::(), 1e250); + + // max value + let x = ExtendedFloat { + mant: 9007199254740991, + exp: 971, + }; + assert_eq!(x.into_float::(), 1.7976931348623157e308); + + // max value + let x = ExtendedFloat { + mant: 18446744073709549568, + exp: 960, + }; + assert_eq!(x.into_float::(), 1.7976931348623157e308); + + // overflow + let x = ExtendedFloat { + mant: 9007199254740992, + exp: 971, + }; + assert_eq!(x.into_float::(), f64::INFINITY); + + // overflow + let x = ExtendedFloat { + mant: 18446744073709549568, + exp: 961, + }; + assert_eq!(x.into_float::(), f64::INFINITY); + + // Underflow + // Adapted from failures in strtod. + let x = ExtendedFloat { + exp: -1139, + mant: 18446744073709550712, + }; + assert_eq!(x.into_float::(), 0.0); + + let x = ExtendedFloat { + exp: -1139, + mant: 18446744073709551460, + }; + assert_eq!(x.into_float::(), 0.0); + + let x = ExtendedFloat { + exp: -1138, + mant: 9223372036854776103, + }; + assert_eq!(x.into_float::(), 5e-324); + + // Integers. + for int in &INTEGERS { + let fp = ExtendedFloat { mant: *int, exp: 0 }; + assert_eq!(fp.into_float::(), *int as f64, "{:?} as f64", *int); + } +} + +// OPERATIONS + +fn check_mul(a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) { + let r = a.mul(&b); + assert_eq!(r, c); +} + +#[test] +fn mul_test() { + // Normalized (64-bit mantissa) + let a = ExtendedFloat { + mant: 13164036458569648128, + exp: -213, + }; + let b = ExtendedFloat { + mant: 9223372036854775808, + exp: -62, + }; + let c = ExtendedFloat { + mant: 6582018229284824064, + exp: -211, + }; + check_mul(a, b, c); + + // Check with integers + // 64-bit mantissa + let mut a = ExtendedFloat { mant: 10, exp: 0 }; + let mut b = ExtendedFloat { mant: 10, exp: 0 }; + a.normalize(); + b.normalize(); + assert_eq!(a.mul(&b).into_float::(), 100.0); + + // Check both values need high bits set. + let a = ExtendedFloat { + mant: 1 << 32, + exp: -31, + }; + let b = ExtendedFloat { + mant: 1 << 32, + exp: -31, + }; + assert_eq!(a.mul(&b).into_float::(), 4.0); + + // Check both values need high bits set. + let a = ExtendedFloat { + mant: 10 << 31, + exp: -31, + }; + let b = ExtendedFloat { + mant: 10 << 31, + exp: -31, + }; + assert_eq!(a.mul(&b).into_float::(), 100.0); +} + +fn check_imul(mut a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) { + a.imul(&b); + assert_eq!(a, c); +} + +#[test] +fn imul_test() { + // Normalized (64-bit mantissa) + let a = ExtendedFloat { + mant: 13164036458569648128, + exp: -213, + }; + let b = ExtendedFloat { + mant: 9223372036854775808, + exp: -62, + }; + let c = ExtendedFloat { + mant: 6582018229284824064, + exp: -211, + }; + check_imul(a, b, c); + + // Check with integers + // 64-bit mantissa + let mut a = ExtendedFloat { mant: 10, exp: 0 }; + let mut b = ExtendedFloat { mant: 10, exp: 0 }; + a.normalize(); + b.normalize(); + a.imul(&b); + assert_eq!(a.into_float::(), 100.0); + + // Check both values need high bits set. + let mut a = ExtendedFloat { + mant: 1 << 32, + exp: -31, + }; + let b = ExtendedFloat { + mant: 1 << 32, + exp: -31, + }; + a.imul(&b); + assert_eq!(a.into_float::(), 4.0); + + // Check both values need high bits set. + let mut a = ExtendedFloat { + mant: 10 << 31, + exp: -31, + }; + let b = ExtendedFloat { + mant: 10 << 31, + exp: -31, + }; + a.imul(&b); + assert_eq!(a.into_float::(), 100.0); +} diff --git a/third_party/rust/serde_json/tests/lexical/math.rs b/third_party/rust/serde_json/tests/lexical/math.rs new file mode 100644 index 0000000000..79d3ef3ee5 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/math.rs @@ -0,0 +1,211 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::math::{Limb, Math}; +use std::cmp; + +#[derive(Clone, Default)] +struct Bigint { + data: Vec, +} + +impl Math for Bigint { + fn data(&self) -> &Vec { + &self.data + } + + fn data_mut(&mut self) -> &mut Vec { + &mut self.data + } +} + +#[cfg(limb_width_32)] +pub(crate) fn from_u32(x: &[u32]) -> Vec { + x.iter().cloned().collect() +} + +#[cfg(limb_width_64)] +pub(crate) fn from_u32(x: &[u32]) -> Vec { + let mut v = Vec::::default(); + for xi in x.chunks(2) { + match xi.len() { + 1 => v.push(xi[0] as u64), + 2 => v.push(((xi[1] as u64) << 32) | (xi[0] as u64)), + _ => unreachable!(), + } + } + + v +} + +#[test] +fn compare_test() { + // Simple + let x = Bigint { + data: from_u32(&[1]), + }; + let y = Bigint { + data: from_u32(&[2]), + }; + assert_eq!(x.compare(&y), cmp::Ordering::Less); + assert_eq!(x.compare(&x), cmp::Ordering::Equal); + assert_eq!(y.compare(&x), cmp::Ordering::Greater); + + // Check asymmetric + let x = Bigint { + data: from_u32(&[5, 1]), + }; + let y = Bigint { + data: from_u32(&[2]), + }; + assert_eq!(x.compare(&y), cmp::Ordering::Greater); + assert_eq!(x.compare(&x), cmp::Ordering::Equal); + assert_eq!(y.compare(&x), cmp::Ordering::Less); + + // Check when we use reverse ordering properly. + let x = Bigint { + data: from_u32(&[5, 1, 9]), + }; + let y = Bigint { + data: from_u32(&[6, 2, 8]), + }; + assert_eq!(x.compare(&y), cmp::Ordering::Greater); + assert_eq!(x.compare(&x), cmp::Ordering::Equal); + assert_eq!(y.compare(&x), cmp::Ordering::Less); + + // Complex scenario, check it properly uses reverse ordering. + let x = Bigint { + data: from_u32(&[0, 1, 9]), + }; + let y = Bigint { + data: from_u32(&[4294967295, 0, 9]), + }; + assert_eq!(x.compare(&y), cmp::Ordering::Greater); + assert_eq!(x.compare(&x), cmp::Ordering::Equal); + assert_eq!(y.compare(&x), cmp::Ordering::Less); +} + +#[test] +fn hi64_test() { + assert_eq!(Bigint::from_u64(0xA).hi64(), (0xA000000000000000, false)); + assert_eq!(Bigint::from_u64(0xAB).hi64(), (0xAB00000000000000, false)); + assert_eq!( + Bigint::from_u64(0xAB00000000).hi64(), + (0xAB00000000000000, false) + ); + assert_eq!( + Bigint::from_u64(0xA23456789A).hi64(), + (0xA23456789A000000, false) + ); +} + +#[test] +fn bit_length_test() { + let x = Bigint { + data: from_u32(&[0, 0, 0, 1]), + }; + assert_eq!(x.bit_length(), 97); + + let x = Bigint { + data: from_u32(&[0, 0, 0, 3]), + }; + assert_eq!(x.bit_length(), 98); + + let x = Bigint { + data: from_u32(&[1 << 31]), + }; + assert_eq!(x.bit_length(), 32); +} + +#[test] +fn iadd_small_test() { + // Overflow check (single) + // This should set all the internal data values to 0, the top + // value to (1<<31), and the bottom value to (4>>1). + // This is because the max_value + 1 leads to all 0s, we set the + // topmost bit to 1. + let mut x = Bigint { + data: from_u32(&[4294967295]), + }; + x.iadd_small(5); + assert_eq!(x.data, from_u32(&[4, 1])); + + // No overflow, single value + let mut x = Bigint { + data: from_u32(&[5]), + }; + x.iadd_small(7); + assert_eq!(x.data, from_u32(&[12])); + + // Single carry, internal overflow + let mut x = Bigint::from_u64(0x80000000FFFFFFFF); + x.iadd_small(7); + assert_eq!(x.data, from_u32(&[6, 0x80000001])); + + // Double carry, overflow + let mut x = Bigint::from_u64(0xFFFFFFFFFFFFFFFF); + x.iadd_small(7); + assert_eq!(x.data, from_u32(&[6, 0, 1])); +} + +#[test] +fn imul_small_test() { + // No overflow check, 1-int. + let mut x = Bigint { + data: from_u32(&[5]), + }; + x.imul_small(7); + assert_eq!(x.data, from_u32(&[35])); + + // No overflow check, 2-ints. + let mut x = Bigint::from_u64(0x4000000040000); + x.imul_small(5); + assert_eq!(x.data, from_u32(&[0x00140000, 0x140000])); + + // Overflow, 1 carry. + let mut x = Bigint { + data: from_u32(&[0x33333334]), + }; + x.imul_small(5); + assert_eq!(x.data, from_u32(&[4, 1])); + + // Overflow, 1 carry, internal. + let mut x = Bigint::from_u64(0x133333334); + x.imul_small(5); + assert_eq!(x.data, from_u32(&[4, 6])); + + // Overflow, 2 carries. + let mut x = Bigint::from_u64(0x3333333333333334); + x.imul_small(5); + assert_eq!(x.data, from_u32(&[4, 0, 1])); +} + +#[test] +fn shl_test() { + // Pattern generated via `''.join(["1" +"0"*i for i in range(20)])` + let mut big = Bigint { + data: from_u32(&[0xD2210408]), + }; + big.ishl(5); + assert_eq!(big.data, from_u32(&[0x44208100, 0x1A])); + big.ishl(32); + assert_eq!(big.data, from_u32(&[0, 0x44208100, 0x1A])); + big.ishl(27); + assert_eq!(big.data, from_u32(&[0, 0, 0xD2210408])); + + // 96-bits of previous pattern + let mut big = Bigint { + data: from_u32(&[0x20020010, 0x8040100, 0xD2210408]), + }; + big.ishl(5); + assert_eq!(big.data, from_u32(&[0x400200, 0x802004, 0x44208101, 0x1A])); + big.ishl(32); + assert_eq!( + big.data, + from_u32(&[0, 0x400200, 0x802004, 0x44208101, 0x1A]) + ); + big.ishl(27); + assert_eq!( + big.data, + from_u32(&[0, 0, 0x20020010, 0x8040100, 0xD2210408]) + ); +} diff --git a/third_party/rust/serde_json/tests/lexical/num.rs b/third_party/rust/serde_json/tests/lexical/num.rs new file mode 100644 index 0000000000..1a94be0130 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/num.rs @@ -0,0 +1,76 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::num::{AsPrimitive, Float, Integer, Number}; + +fn check_as_primitive(t: T) { + let _: u32 = t.as_u32(); + let _: u64 = t.as_u64(); + let _: u128 = t.as_u128(); + let _: usize = t.as_usize(); + let _: f32 = t.as_f32(); + let _: f64 = t.as_f64(); +} + +#[test] +fn as_primitive_test() { + check_as_primitive(1u32); + check_as_primitive(1u64); + check_as_primitive(1u128); + check_as_primitive(1usize); + check_as_primitive(1f32); + check_as_primitive(1f64); +} + +fn check_number(x: T, y: T) { + // Copy, partialeq, partialord + let _ = x; + assert!(x < y); + assert!(x != y); + + // Operations + let _ = y + x; + + // Conversions already tested. +} + +#[test] +fn number_test() { + check_number(1u32, 5); + check_number(1u64, 5); + check_number(1u128, 5); + check_number(1usize, 5); + check_number(1f32, 5.0); + check_number(1f64, 5.0); +} + +fn check_integer(x: T) { + // Bitwise operations + let _ = x & T::ZERO; +} + +#[test] +fn integer_test() { + check_integer(65u32); + check_integer(65u64); + check_integer(65u128); + check_integer(65usize); +} + +fn check_float(x: T) { + // Check functions + let _ = x.pow10(5); + let _ = x.to_bits(); + assert!(T::from_bits(x.to_bits()) == x); + + // Check properties + let _ = x.to_bits() & T::SIGN_MASK; + let _ = x.to_bits() & T::EXPONENT_MASK; + let _ = x.to_bits() & T::HIDDEN_BIT_MASK; + let _ = x.to_bits() & T::MANTISSA_MASK; +} + +#[test] +fn float_test() { + check_float(123f32); + check_float(123f64); +} diff --git a/third_party/rust/serde_json/tests/lexical/parse.rs b/third_party/rust/serde_json/tests/lexical/parse.rs new file mode 100644 index 0000000000..80ca25e772 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/parse.rs @@ -0,0 +1,204 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::num::Float; +use crate::lexical::parse::{parse_concise_float, parse_truncated_float}; +use core::f64; +use core::fmt::Debug; + +fn check_concise_float(mantissa: u64, exponent: i32, expected: F) +where + F: Float + Debug, +{ + assert_eq!(parse_concise_float::(mantissa, exponent), expected); +} + +fn check_truncated_float(integer: &str, fraction: &str, exponent: i32, expected: F) +where + F: Float + Debug, +{ + let integer = integer.as_bytes(); + let fraction = fraction.as_bytes(); + assert_eq!( + parse_truncated_float::(integer, fraction, exponent), + expected, + ); +} + +#[test] +fn parse_f32_test() { + check_concise_float(0, 0, 0.0_f32); + check_concise_float(12345, -4, 1.2345_f32); + check_concise_float(12345, -3, 12.345_f32); + check_concise_float(123456789, -4, 12345.6789_f32); + check_concise_float(12345, 6, 1.2345e10_f32); + check_concise_float(12345, -42, 1.2345e-38_f32); + + // Check expected rounding, using borderline cases. + // Round-down, halfway + check_concise_float(16777216, 0, 16777216.0_f32); + check_concise_float(16777217, 0, 16777216.0_f32); + check_concise_float(16777218, 0, 16777218.0_f32); + check_concise_float(33554432, 0, 33554432.0_f32); + check_concise_float(33554434, 0, 33554432.0_f32); + check_concise_float(33554436, 0, 33554436.0_f32); + check_concise_float(17179869184, 0, 17179869184.0_f32); + check_concise_float(17179870208, 0, 17179869184.0_f32); + check_concise_float(17179871232, 0, 17179871232.0_f32); + + // Round-up, halfway + check_concise_float(16777218, 0, 16777218.0_f32); + check_concise_float(16777219, 0, 16777220.0_f32); + check_concise_float(16777220, 0, 16777220.0_f32); + + check_concise_float(33554436, 0, 33554436.0_f32); + check_concise_float(33554438, 0, 33554440.0_f32); + check_concise_float(33554440, 0, 33554440.0_f32); + + check_concise_float(17179871232, 0, 17179871232.0_f32); + check_concise_float(17179872256, 0, 17179873280.0_f32); + check_concise_float(17179873280, 0, 17179873280.0_f32); + + // Round-up, above halfway + check_concise_float(33554435, 0, 33554436.0_f32); + check_concise_float(17179870209, 0, 17179871232.0_f32); + + // Check exactly halfway, round-up at halfway + check_truncated_float("1", "00000017881393432617187499", 0, 1.0000001_f32); + check_truncated_float("1", "000000178813934326171875", 0, 1.0000002_f32); + check_truncated_float("1", "00000017881393432617187501", 0, 1.0000002_f32); +} + +#[test] +fn parse_f64_test() { + check_concise_float(0, 0, 0.0_f64); + check_concise_float(12345, -4, 1.2345_f64); + check_concise_float(12345, -3, 12.345_f64); + check_concise_float(123456789, -4, 12345.6789_f64); + check_concise_float(12345, 6, 1.2345e10_f64); + check_concise_float(12345, -312, 1.2345e-308_f64); + + // Check expected rounding, using borderline cases. + // Round-down, halfway + check_concise_float(9007199254740992, 0, 9007199254740992.0_f64); + check_concise_float(9007199254740993, 0, 9007199254740992.0_f64); + check_concise_float(9007199254740994, 0, 9007199254740994.0_f64); + + check_concise_float(18014398509481984, 0, 18014398509481984.0_f64); + check_concise_float(18014398509481986, 0, 18014398509481984.0_f64); + check_concise_float(18014398509481988, 0, 18014398509481988.0_f64); + + check_concise_float(9223372036854775808, 0, 9223372036854775808.0_f64); + check_concise_float(9223372036854776832, 0, 9223372036854775808.0_f64); + check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64); + + check_truncated_float( + "11417981541647679048466287755595961091061972992", + "", + 0, + 11417981541647679048466287755595961091061972992.0_f64, + ); + check_truncated_float( + "11417981541647680316116887983825362587765178368", + "", + 0, + 11417981541647679048466287755595961091061972992.0_f64, + ); + check_truncated_float( + "11417981541647681583767488212054764084468383744", + "", + 0, + 11417981541647681583767488212054764084468383744.0_f64, + ); + + // Round-up, halfway + check_concise_float(9007199254740994, 0, 9007199254740994.0_f64); + check_concise_float(9007199254740995, 0, 9007199254740996.0_f64); + check_concise_float(9007199254740996, 0, 9007199254740996.0_f64); + + check_concise_float(18014398509481988, 0, 18014398509481988.0_f64); + check_concise_float(18014398509481990, 0, 18014398509481992.0_f64); + check_concise_float(18014398509481992, 0, 18014398509481992.0_f64); + + check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64); + check_concise_float(9223372036854778880, 0, 9223372036854779904.0_f64); + check_concise_float(9223372036854779904, 0, 9223372036854779904.0_f64); + + check_truncated_float( + "11417981541647681583767488212054764084468383744", + "", + 0, + 11417981541647681583767488212054764084468383744.0_f64, + ); + check_truncated_float( + "11417981541647682851418088440284165581171589120", + "", + 0, + 11417981541647684119068688668513567077874794496.0_f64, + ); + check_truncated_float( + "11417981541647684119068688668513567077874794496", + "", + 0, + 11417981541647684119068688668513567077874794496.0_f64, + ); + + // Round-up, above halfway + check_concise_float(9223372036854776833, 0, 9223372036854777856.0_f64); + check_truncated_float( + "11417981541647680316116887983825362587765178369", + "", + 0, + 11417981541647681583767488212054764084468383744.0_f64, + ); + + // Rounding error + // Adapted from failures in strtod. + check_concise_float(22250738585072014, -324, 2.2250738585072014e-308_f64); + check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187499", -308, 2.225073858507201e-308_f64); + check_truncated_float("2", "22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508791414915891303962110687008643869459464552765720740782062174337998814106326732925355228688137214901298112245145188984905722230728525513315575501591439747639798341180199932396254828901710708185069063066665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505108060994073026293712895895000358379996720725430436028407889577179615094551674824347103070260914462157228988025818254518032570701886087211312807951223342628836862232150377566662250398253433597456888442390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718644338377048603786162277173854562306587467901408672332763671875", -308, 2.2250738585072014e-308_f64); + check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187501", -308, 2.2250738585072014e-308_f64); + check_truncated_float("179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791", "9999999999999999999999999999999999999999999999999999999999999999999999", 0, 1.7976931348623157e+308_f64); + check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984374999", -324, 5.0e-324_f64); + check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375", -324, 1.0e-323_f64); + check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375001", -324, 1.0e-323_f64); + check_truncated_float("", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125", 0, 0.0_f64); + + // Rounding error + // Adapted from: + // https://www.exploringbinary.com/how-glibc-strtod-works/ + check_truncated_float("", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0, 2.2250738585072011e-308_f64); + + // Rounding error + // Adapted from test-parse-random failures. + check_concise_float(1009, -31, 1.009e-28_f64); + check_concise_float(18294, 304, f64::INFINITY); + + // Rounding error + // Adapted from a @dangrabcad's issue #20. + check_concise_float(7689539722041643, 149, 7.689539722041643e164_f64); + check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", 0, 7.689539722041643e164_f64); + check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 7.689539722041643e164_f64); + + // Check other cases similar to @dangrabcad's issue #20. + check_truncated_float("9223372036854776833", "0", 0, 9223372036854777856.0_f64); + check_truncated_float( + "11417981541647680316116887983825362587765178369", + "0", + 0, + 11417981541647681583767488212054764084468383744.0_f64, + ); + check_concise_float(90071992547409950, -1, 9007199254740996.0_f64); + check_concise_float(180143985094819900, -1, 18014398509481992.0_f64); + check_truncated_float("9223372036854778880", "0", 0, 9223372036854779904.0_f64); + check_truncated_float( + "11417981541647682851418088440284165581171589120", + "0", + 0, + 11417981541647684119068688668513567077874794496.0_f64, + ); + + // Check other cases ostensibly identified via proptest. + check_truncated_float("71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); + check_truncated_float("126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); + check_truncated_float("38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); +} diff --git a/third_party/rust/serde_json/tests/lexical/rounding.rs b/third_party/rust/serde_json/tests/lexical/rounding.rs new file mode 100644 index 0000000000..7ea17716c3 --- /dev/null +++ b/third_party/rust/serde_json/tests/lexical/rounding.rs @@ -0,0 +1,316 @@ +// Adapted from https://github.com/Alexhuszagh/rust-lexical. + +use crate::lexical::float::ExtendedFloat; +use crate::lexical::num::Float; +use crate::lexical::rounding::*; + +// MASKS + +#[test] +fn lower_n_mask_test() { + assert_eq!(lower_n_mask(0u64), 0b0); + assert_eq!(lower_n_mask(1u64), 0b1); + assert_eq!(lower_n_mask(2u64), 0b11); + assert_eq!(lower_n_mask(10u64), 0b1111111111); + assert_eq!(lower_n_mask(32u64), 0b11111111111111111111111111111111); +} + +#[test] +fn lower_n_halfway_test() { + assert_eq!(lower_n_halfway(0u64), 0b0); + assert_eq!(lower_n_halfway(1u64), 0b1); + assert_eq!(lower_n_halfway(2u64), 0b10); + assert_eq!(lower_n_halfway(10u64), 0b1000000000); + assert_eq!(lower_n_halfway(32u64), 0b10000000000000000000000000000000); +} + +#[test] +fn nth_bit_test() { + assert_eq!(nth_bit(0u64), 0b1); + assert_eq!(nth_bit(1u64), 0b10); + assert_eq!(nth_bit(2u64), 0b100); + assert_eq!(nth_bit(10u64), 0b10000000000); + assert_eq!(nth_bit(31u64), 0b10000000000000000000000000000000); +} + +#[test] +fn internal_n_mask_test() { + assert_eq!(internal_n_mask(1u64, 0u64), 0b0); + assert_eq!(internal_n_mask(1u64, 1u64), 0b1); + assert_eq!(internal_n_mask(2u64, 1u64), 0b10); + assert_eq!(internal_n_mask(4u64, 2u64), 0b1100); + assert_eq!(internal_n_mask(10u64, 2u64), 0b1100000000); + assert_eq!(internal_n_mask(10u64, 4u64), 0b1111000000); + assert_eq!( + internal_n_mask(32u64, 4u64), + 0b11110000000000000000000000000000 + ); +} + +// NEAREST ROUNDING + +#[test] +fn round_nearest_test() { + // Check exactly halfway (b'1100000') + let mut fp = ExtendedFloat { mant: 0x60, exp: 0 }; + let (above, halfway) = round_nearest(&mut fp, 6); + assert!(!above); + assert!(halfway); + assert_eq!(fp.mant, 1); + + // Check above halfway (b'1100001') + let mut fp = ExtendedFloat { mant: 0x61, exp: 0 }; + let (above, halfway) = round_nearest(&mut fp, 6); + assert!(above); + assert!(!halfway); + assert_eq!(fp.mant, 1); + + // Check below halfway (b'1011111') + let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 }; + let (above, halfway) = round_nearest(&mut fp, 6); + assert!(!above); + assert!(!halfway); + assert_eq!(fp.mant, 1); +} + +// DIRECTED ROUNDING + +#[test] +fn round_downward_test() { + // b0000000 + let mut fp = ExtendedFloat { mant: 0x00, exp: 0 }; + round_downward(&mut fp, 6); + assert_eq!(fp.mant, 0); + + // b1000000 + let mut fp = ExtendedFloat { mant: 0x40, exp: 0 }; + round_downward(&mut fp, 6); + assert_eq!(fp.mant, 1); + + // b1100000 + let mut fp = ExtendedFloat { mant: 0x60, exp: 0 }; + round_downward(&mut fp, 6); + assert_eq!(fp.mant, 1); + + // b1110000 + let mut fp = ExtendedFloat { mant: 0x70, exp: 0 }; + round_downward(&mut fp, 6); + assert_eq!(fp.mant, 1); +} + +#[test] +fn round_nearest_tie_even_test() { + // Check round-up, halfway + let mut fp = ExtendedFloat { mant: 0x60, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 2); + + // Check round-down, halfway + let mut fp = ExtendedFloat { mant: 0x20, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 0); + + // Check round-up, above halfway + let mut fp = ExtendedFloat { mant: 0x61, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 2); + + let mut fp = ExtendedFloat { mant: 0x21, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 1); + + // Check round-down, below halfway + let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 1); + + let mut fp = ExtendedFloat { mant: 0x1F, exp: 0 }; + round_nearest_tie_even(&mut fp, 6); + assert_eq!(fp.mant, 0); +} + +// HIGH-LEVEL + +#[test] +fn round_to_float_test() { + // Denormal + let mut fp = ExtendedFloat { + mant: 1 << 63, + exp: f64::DENORMAL_EXPONENT - 15, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 48); + assert_eq!(fp.exp, f64::DENORMAL_EXPONENT); + + // Halfway, round-down (b'1000000000000000000000000000000000000000000000000000010000000000') + let mut fp = ExtendedFloat { + mant: 0x8000000000000400, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 52); + assert_eq!(fp.exp, -52); + + // Halfway, round-up (b'1000000000000000000000000000000000000000000000000000110000000000') + let mut fp = ExtendedFloat { + mant: 0x8000000000000C00, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 2); + assert_eq!(fp.exp, -52); + + // Above halfway + let mut fp = ExtendedFloat { + mant: 0x8000000000000401, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 1); + assert_eq!(fp.exp, -52); + + let mut fp = ExtendedFloat { + mant: 0x8000000000000C01, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 2); + assert_eq!(fp.exp, -52); + + // Below halfway + let mut fp = ExtendedFloat { + mant: 0x80000000000003FF, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 52); + assert_eq!(fp.exp, -52); + + let mut fp = ExtendedFloat { + mant: 0x8000000000000BFF, + exp: -63, + }; + round_to_float::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 1); + assert_eq!(fp.exp, -52); +} + +#[test] +fn avoid_overflow_test() { + // Avoid overflow, fails by 1 + let mut fp = ExtendedFloat { + mant: 0xFFFFFFFFFFFF, + exp: f64::MAX_EXPONENT + 5, + }; + avoid_overflow::(&mut fp); + assert_eq!(fp.mant, 0xFFFFFFFFFFFF); + assert_eq!(fp.exp, f64::MAX_EXPONENT + 5); + + // Avoid overflow, succeeds + let mut fp = ExtendedFloat { + mant: 0xFFFFFFFFFFFF, + exp: f64::MAX_EXPONENT + 4, + }; + avoid_overflow::(&mut fp); + assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0); + assert_eq!(fp.exp, f64::MAX_EXPONENT - 1); +} + +#[test] +fn round_to_native_test() { + // Overflow + let mut fp = ExtendedFloat { + mant: 0xFFFFFFFFFFFF, + exp: f64::MAX_EXPONENT + 4, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0); + assert_eq!(fp.exp, f64::MAX_EXPONENT - 1); + + // Need denormal + let mut fp = ExtendedFloat { + mant: 1, + exp: f64::DENORMAL_EXPONENT + 48, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 48); + assert_eq!(fp.exp, f64::DENORMAL_EXPONENT); + + // Halfway, round-down (b'10000000000000000000000000000000000000000000000000000100000') + let mut fp = ExtendedFloat { + mant: 0x400000000000020, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 52); + assert_eq!(fp.exp, -52); + + // Halfway, round-up (b'10000000000000000000000000000000000000000000000000001100000') + let mut fp = ExtendedFloat { + mant: 0x400000000000060, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 2); + assert_eq!(fp.exp, -52); + + // Above halfway + let mut fp = ExtendedFloat { + mant: 0x400000000000021, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 1); + assert_eq!(fp.exp, -52); + + let mut fp = ExtendedFloat { + mant: 0x400000000000061, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 2); + assert_eq!(fp.exp, -52); + + // Below halfway + let mut fp = ExtendedFloat { + mant: 0x40000000000001F, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1 << 52); + assert_eq!(fp.exp, -52); + + let mut fp = ExtendedFloat { + mant: 0x40000000000005F, + exp: -58, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, (1 << 52) + 1); + assert_eq!(fp.exp, -52); + + // Underflow + // Adapted from failures in strtod. + let mut fp = ExtendedFloat { + exp: -1139, + mant: 18446744073709550712, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 0); + assert_eq!(fp.exp, 0); + + let mut fp = ExtendedFloat { + exp: -1139, + mant: 18446744073709551460, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 0); + assert_eq!(fp.exp, 0); + + let mut fp = ExtendedFloat { + exp: -1138, + mant: 9223372036854776103, + }; + round_to_native::(&mut fp, round_nearest_tie_even); + assert_eq!(fp.mant, 1); + assert_eq!(fp.exp, -1074); +} diff --git a/third_party/rust/serde_json/tests/macros/mod.rs b/third_party/rust/serde_json/tests/macros/mod.rs new file mode 100644 index 0000000000..aaf820f074 --- /dev/null +++ b/third_party/rust/serde_json/tests/macros/mod.rs @@ -0,0 +1,61 @@ +#![allow(unused_macro_rules)] + +macro_rules! json_str { + ([]) => { + "[]" + }; + ([ $e0:tt $(, $e:tt)* $(,)? ]) => { + concat!("[", + json_str!($e0), + $(",", json_str!($e),)* + "]") + }; + ({}) => { + "{}" + }; + ({ $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => { + concat!("{", + stringify!($k0), ":", json_str!($v0), + $(",", stringify!($k), ":", json_str!($v),)* + "}") + }; + (($other:tt)) => { + $other + }; + ($other:tt) => { + stringify!($other) + }; +} + +macro_rules! pretty_str { + ($json:tt) => { + pretty_str_impl!("", $json) + }; +} + +macro_rules! pretty_str_impl { + ($indent:expr, []) => { + "[]" + }; + ($indent:expr, [ $e0:tt $(, $e:tt)* $(,)? ]) => { + concat!("[\n ", + $indent, pretty_str_impl!(concat!(" ", $indent), $e0), + $(",\n ", $indent, pretty_str_impl!(concat!(" ", $indent), $e),)* + "\n", $indent, "]") + }; + ($indent:expr, {}) => { + "{}" + }; + ($indent:expr, { $k0:tt : $v0:tt $(, $k:tt : $v:tt)* $(,)? }) => { + concat!("{\n ", + $indent, stringify!($k0), ": ", pretty_str_impl!(concat!(" ", $indent), $v0), + $(",\n ", $indent, stringify!($k), ": ", pretty_str_impl!(concat!(" ", $indent), $v),)* + "\n", $indent, "}") + }; + ($indent:expr, ($other:tt)) => { + $other + }; + ($indent:expr, $other:tt) => { + stringify!($other) + }; +} diff --git a/third_party/rust/serde_json/tests/map.rs b/third_party/rust/serde_json/tests/map.rs new file mode 100644 index 0000000000..ae01969561 --- /dev/null +++ b/third_party/rust/serde_json/tests/map.rs @@ -0,0 +1,47 @@ +use serde_json::{from_str, Map, Value}; + +#[test] +fn test_preserve_order() { + // Sorted order + #[cfg(not(feature = "preserve_order"))] + const EXPECTED: &[&str] = &["a", "b", "c"]; + + // Insertion order + #[cfg(feature = "preserve_order")] + const EXPECTED: &[&str] = &["b", "a", "c"]; + + let v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap(); + let keys: Vec<_> = v.as_object().unwrap().keys().collect(); + assert_eq!(keys, EXPECTED); +} + +#[test] +fn test_append() { + // Sorted order + #[cfg(not(feature = "preserve_order"))] + const EXPECTED: &[&str] = &["a", "b", "c"]; + + // Insertion order + #[cfg(feature = "preserve_order")] + const EXPECTED: &[&str] = &["b", "a", "c"]; + + let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap(); + let val = v.as_object_mut().unwrap(); + let mut m = Map::new(); + m.append(val); + let keys: Vec<_> = m.keys().collect(); + + assert_eq!(keys, EXPECTED); + assert!(val.is_empty()); +} + +#[cfg(not(no_btreemap_retain))] +#[test] +fn test_retain() { + let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap(); + let val = v.as_object_mut().unwrap(); + val.retain(|k, _| k.as_str() != "b"); + + let keys: Vec<_> = val.keys().collect(); + assert_eq!(keys, &["a", "c"]); +} diff --git a/third_party/rust/serde_json/tests/regression.rs b/third_party/rust/serde_json/tests/regression.rs new file mode 100644 index 0000000000..fb2b25c89b --- /dev/null +++ b/third_party/rust/serde_json/tests/regression.rs @@ -0,0 +1,3 @@ +mod regression { + automod::dir!("tests/regression"); +} diff --git a/third_party/rust/serde_json/tests/regression/issue520.rs b/third_party/rust/serde_json/tests/regression/issue520.rs new file mode 100644 index 0000000000..9ed367731b --- /dev/null +++ b/third_party/rust/serde_json/tests/regression/issue520.rs @@ -0,0 +1,20 @@ +#![allow(clippy::float_cmp)] + +use serde_derive::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug)] +#[serde(tag = "type", content = "data")] +enum E { + Float(f32), +} + +#[test] +fn test() { + let e = E::Float(159.1); + let v = serde_json::to_value(e).unwrap(); + let e = serde_json::from_value::(v).unwrap(); + + match e { + E::Float(f) => assert_eq!(f, 159.1), + } +} diff --git a/third_party/rust/serde_json/tests/regression/issue795.rs b/third_party/rust/serde_json/tests/regression/issue795.rs new file mode 100644 index 0000000000..bb82852c54 --- /dev/null +++ b/third_party/rust/serde_json/tests/regression/issue795.rs @@ -0,0 +1,59 @@ +#![allow(clippy::assertions_on_result_states)] + +use serde::de::{ + Deserialize, Deserializer, EnumAccess, IgnoredAny, MapAccess, VariantAccess, Visitor, +}; +use serde_json::json; +use std::fmt; + +#[derive(Debug)] +pub enum Enum { + Variant { x: u8 }, +} + +impl<'de> Deserialize<'de> for Enum { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct EnumVisitor; + + impl<'de> Visitor<'de> for EnumVisitor { + type Value = Enum; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("enum Enum") + } + + fn visit_enum(self, data: A) -> Result + where + A: EnumAccess<'de>, + { + let (IgnoredAny, variant) = data.variant()?; + variant.struct_variant(&["x"], self) + } + + fn visit_map(self, mut data: A) -> Result + where + A: MapAccess<'de>, + { + let mut x = 0; + if let Some((IgnoredAny, value)) = data.next_entry()? { + x = value; + } + Ok(Enum::Variant { x }) + } + } + + deserializer.deserialize_enum("Enum", &["Variant"], EnumVisitor) + } +} + +#[test] +fn test() { + let s = r#" {"Variant":{"x":0,"y":0}} "#; + assert!(serde_json::from_str::(s).is_err()); + + let j = json!({"Variant":{"x":0,"y":0}}); + assert!(serde_json::from_value::(j).is_err()); +} diff --git a/third_party/rust/serde_json/tests/regression/issue845.rs b/third_party/rust/serde_json/tests/regression/issue845.rs new file mode 100644 index 0000000000..56037ae669 --- /dev/null +++ b/third_party/rust/serde_json/tests/regression/issue845.rs @@ -0,0 +1,74 @@ +#![allow(clippy::trait_duplication_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/8757 + +use serde::{Deserialize, Deserializer}; +use std::convert::TryFrom; +use std::fmt::{self, Display}; +use std::marker::PhantomData; +use std::str::FromStr; + +pub struct NumberVisitor { + marker: PhantomData, +} + +impl<'de, T> serde::de::Visitor<'de> for NumberVisitor +where + T: TryFrom + TryFrom + FromStr, + >::Error: Display, + >::Error: Display, + ::Err: Display, +{ + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("an integer or string") + } + + fn visit_u64(self, v: u64) -> Result + where + E: serde::de::Error, + { + T::try_from(v).map_err(serde::de::Error::custom) + } + + fn visit_i64(self, v: i64) -> Result + where + E: serde::de::Error, + { + T::try_from(v).map_err(serde::de::Error::custom) + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + v.parse().map_err(serde::de::Error::custom) + } +} + +fn deserialize_integer_or_string<'de, D, T>(deserializer: D) -> Result +where + D: Deserializer<'de>, + T: TryFrom + TryFrom + FromStr, + >::Error: Display, + >::Error: Display, + ::Err: Display, +{ + deserializer.deserialize_any(NumberVisitor { + marker: PhantomData, + }) +} + +#[derive(Deserialize, Debug)] +pub struct Struct { + #[serde(deserialize_with = "deserialize_integer_or_string")] + pub i: i64, +} + +#[test] +fn test() { + let j = r#" {"i":100} "#; + println!("{:?}", serde_json::from_str::(j).unwrap()); + + let j = r#" {"i":"100"} "#; + println!("{:?}", serde_json::from_str::(j).unwrap()); +} diff --git a/third_party/rust/serde_json/tests/regression/issue953.rs b/third_party/rust/serde_json/tests/regression/issue953.rs new file mode 100644 index 0000000000..771aa5287c --- /dev/null +++ b/third_party/rust/serde_json/tests/regression/issue953.rs @@ -0,0 +1,9 @@ +use serde_json::Value; + +#[test] +fn test() { + let x1 = serde_json::from_str::("18446744073709551615."); + assert!(x1.is_err()); + let x2 = serde_json::from_str::("18446744073709551616."); + assert!(x2.is_err()); +} diff --git a/third_party/rust/serde_json/tests/stream.rs b/third_party/rust/serde_json/tests/stream.rs new file mode 100644 index 0000000000..ec6b9e3d02 --- /dev/null +++ b/third_party/rust/serde_json/tests/stream.rs @@ -0,0 +1,183 @@ +#![cfg(not(feature = "preserve_order"))] +#![allow(clippy::assertions_on_result_states)] + +use serde_json::{json, Deserializer, Value}; + +// Rustfmt issue https://github.com/rust-lang-nursery/rustfmt/issues/2740 +#[rustfmt::skip] +macro_rules! test_stream { + ($data:expr, $ty:ty, |$stream:ident| $test:block) => { + { + let de = Deserializer::from_str($data); + let mut $stream = de.into_iter::<$ty>(); + assert_eq!($stream.byte_offset(), 0); + $test + } + { + let de = Deserializer::from_slice($data.as_bytes()); + let mut $stream = de.into_iter::<$ty>(); + assert_eq!($stream.byte_offset(), 0); + $test + } + { + let mut bytes = $data.as_bytes(); + let de = Deserializer::from_reader(&mut bytes); + let mut $stream = de.into_iter::<$ty>(); + assert_eq!($stream.byte_offset(), 0); + $test + } + }; +} + +#[test] +fn test_json_stream_newlines() { + let data = "{\"x\":39} {\"x\":40}{\"x\":41}\n{\"x\":42}"; + + test_stream!(data, Value, |stream| { + assert_eq!(stream.next().unwrap().unwrap()["x"], 39); + assert_eq!(stream.byte_offset(), 8); + + assert_eq!(stream.next().unwrap().unwrap()["x"], 40); + assert_eq!(stream.byte_offset(), 17); + + assert_eq!(stream.next().unwrap().unwrap()["x"], 41); + assert_eq!(stream.byte_offset(), 25); + + assert_eq!(stream.next().unwrap().unwrap()["x"], 42); + assert_eq!(stream.byte_offset(), 34); + + assert!(stream.next().is_none()); + assert_eq!(stream.byte_offset(), 34); + }); +} + +#[test] +fn test_json_stream_trailing_whitespaces() { + let data = "{\"x\":42} \t\n"; + + test_stream!(data, Value, |stream| { + assert_eq!(stream.next().unwrap().unwrap()["x"], 42); + assert_eq!(stream.byte_offset(), 8); + + assert!(stream.next().is_none()); + assert_eq!(stream.byte_offset(), 11); + }); +} + +#[test] +fn test_json_stream_truncated() { + let data = "{\"x\":40}\n{\"x\":"; + + test_stream!(data, Value, |stream| { + assert_eq!(stream.next().unwrap().unwrap()["x"], 40); + assert_eq!(stream.byte_offset(), 8); + + assert!(stream.next().unwrap().unwrap_err().is_eof()); + assert_eq!(stream.byte_offset(), 9); + }); +} + +#[test] +fn test_json_stream_truncated_decimal() { + let data = "{\"x\":4."; + + test_stream!(data, Value, |stream| { + assert!(stream.next().unwrap().unwrap_err().is_eof()); + assert_eq!(stream.byte_offset(), 0); + }); +} + +#[test] +fn test_json_stream_truncated_negative() { + let data = "{\"x\":-"; + + test_stream!(data, Value, |stream| { + assert!(stream.next().unwrap().unwrap_err().is_eof()); + assert_eq!(stream.byte_offset(), 0); + }); +} + +#[test] +fn test_json_stream_truncated_exponent() { + let data = "{\"x\":4e"; + + test_stream!(data, Value, |stream| { + assert!(stream.next().unwrap().unwrap_err().is_eof()); + assert_eq!(stream.byte_offset(), 0); + }); +} + +#[test] +fn test_json_stream_empty() { + let data = ""; + + test_stream!(data, Value, |stream| { + assert!(stream.next().is_none()); + assert_eq!(stream.byte_offset(), 0); + }); +} + +#[test] +fn test_json_stream_primitive() { + let data = "{} true{}1[]\nfalse\"hey\"2 "; + + test_stream!(data, Value, |stream| { + assert_eq!(stream.next().unwrap().unwrap(), json!({})); + assert_eq!(stream.byte_offset(), 2); + + assert_eq!(stream.next().unwrap().unwrap(), true); + assert_eq!(stream.byte_offset(), 7); + + assert_eq!(stream.next().unwrap().unwrap(), json!({})); + assert_eq!(stream.byte_offset(), 9); + + assert_eq!(stream.next().unwrap().unwrap(), 1); + assert_eq!(stream.byte_offset(), 10); + + assert_eq!(stream.next().unwrap().unwrap(), json!([])); + assert_eq!(stream.byte_offset(), 12); + + assert_eq!(stream.next().unwrap().unwrap(), false); + assert_eq!(stream.byte_offset(), 18); + + assert_eq!(stream.next().unwrap().unwrap(), "hey"); + assert_eq!(stream.byte_offset(), 23); + + assert_eq!(stream.next().unwrap().unwrap(), 2); + assert_eq!(stream.byte_offset(), 24); + + assert!(stream.next().is_none()); + assert_eq!(stream.byte_offset(), 25); + }); +} + +#[test] +fn test_json_stream_invalid_literal() { + let data = "truefalse"; + + test_stream!(data, Value, |stream| { + let second = stream.next().unwrap().unwrap_err(); + assert_eq!(second.to_string(), "trailing characters at line 1 column 5"); + }); +} + +#[test] +fn test_json_stream_invalid_number() { + let data = "1true"; + + test_stream!(data, Value, |stream| { + let second = stream.next().unwrap().unwrap_err(); + assert_eq!(second.to_string(), "trailing characters at line 1 column 2"); + }); +} + +#[test] +fn test_error() { + let data = "true wrong false"; + + test_stream!(data, Value, |stream| { + assert_eq!(stream.next().unwrap().unwrap(), true); + assert!(stream.next().unwrap().is_err()); + assert!(stream.next().is_none()); + }); +} diff --git a/third_party/rust/serde_json/tests/test.rs b/third_party/rust/serde_json/tests/test.rs new file mode 100644 index 0000000000..f62a545e7f --- /dev/null +++ b/third_party/rust/serde_json/tests/test.rs @@ -0,0 +1,2408 @@ +#![cfg(not(feature = "preserve_order"))] +#![allow( + clippy::assertions_on_result_states, + clippy::cast_precision_loss, + clippy::derive_partial_eq_without_eq, + clippy::excessive_precision, + clippy::float_cmp, + clippy::items_after_statements, + clippy::shadow_unrelated, + clippy::too_many_lines, + clippy::unreadable_literal, + clippy::unseparated_literal_suffix, + clippy::vec_init_then_push, + clippy::zero_sized_map_values +)] +#![cfg_attr(feature = "trace-macros", feature(trace_macros))] +#[cfg(feature = "trace-macros")] +trace_macros!(true); + +#[macro_use] +mod macros; + +#[cfg(feature = "raw_value")] +use ref_cast::RefCast; +use serde::de::{self, IgnoredAny, IntoDeserializer}; +use serde::ser::{self, SerializeMap, SerializeSeq, Serializer}; +use serde::{Deserialize, Serialize}; +use serde_bytes::{ByteBuf, Bytes}; +#[cfg(feature = "raw_value")] +use serde_json::value::RawValue; +use serde_json::{ + from_reader, from_slice, from_str, from_value, json, to_string, to_string_pretty, to_value, + to_vec, Deserializer, Number, Value, +}; +use std::collections::hash_map::DefaultHasher; +use std::collections::BTreeMap; +#[cfg(feature = "raw_value")] +use std::collections::HashMap; +use std::fmt::{self, Debug}; +use std::hash::{Hash, Hasher}; +use std::io; +use std::iter; +use std::marker::PhantomData; +use std::mem; +use std::str::FromStr; +use std::string::ToString; +use std::{f32, f64}; +use std::{i16, i32, i64, i8}; +use std::{u16, u32, u64, u8}; + +macro_rules! treemap { + () => { + BTreeMap::new() + }; + ($($k:expr => $v:expr),+) => { + { + let mut m = BTreeMap::new(); + $( + m.insert($k, $v); + )+ + m + } + }; +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +enum Animal { + Dog, + Frog(String, Vec), + Cat { age: usize, name: String }, + AntHive(Vec), +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +struct Inner { + a: (), + b: usize, + c: Vec, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +struct Outer { + inner: Vec, +} + +fn test_encode_ok(errors: &[(T, &str)]) +where + T: PartialEq + Debug + ser::Serialize, +{ + for &(ref value, out) in errors { + let out = out.to_string(); + + let s = to_string(value).unwrap(); + assert_eq!(s, out); + + let v = to_value(value).unwrap(); + let s = to_string(&v).unwrap(); + assert_eq!(s, out); + } +} + +fn test_pretty_encode_ok(errors: &[(T, &str)]) +where + T: PartialEq + Debug + ser::Serialize, +{ + for &(ref value, out) in errors { + let out = out.to_string(); + + let s = to_string_pretty(value).unwrap(); + assert_eq!(s, out); + + let v = to_value(value).unwrap(); + let s = to_string_pretty(&v).unwrap(); + assert_eq!(s, out); + } +} + +#[test] +fn test_write_null() { + let tests = &[((), "null")]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_u64() { + let tests = &[(3u64, "3"), (u64::MAX, &u64::MAX.to_string())]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_i64() { + let tests = &[ + (3i64, "3"), + (-2i64, "-2"), + (-1234i64, "-1234"), + (i64::MIN, &i64::MIN.to_string()), + ]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_f64() { + let tests = &[ + (3.0, "3.0"), + (3.1, "3.1"), + (-1.5, "-1.5"), + (0.5, "0.5"), + (f64::MIN, "-1.7976931348623157e308"), + (f64::MAX, "1.7976931348623157e308"), + (f64::EPSILON, "2.220446049250313e-16"), + ]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_encode_nonfinite_float_yields_null() { + let v = to_value(::std::f64::NAN).unwrap(); + assert!(v.is_null()); + + let v = to_value(::std::f64::INFINITY).unwrap(); + assert!(v.is_null()); + + let v = to_value(::std::f32::NAN).unwrap(); + assert!(v.is_null()); + + let v = to_value(::std::f32::INFINITY).unwrap(); + assert!(v.is_null()); +} + +#[test] +fn test_write_str() { + let tests = &[("", "\"\""), ("foo", "\"foo\"")]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_bool() { + let tests = &[(true, "true"), (false, "false")]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_char() { + let tests = &[ + ('n', "\"n\""), + ('"', "\"\\\"\""), + ('\\', "\"\\\\\""), + ('/', "\"/\""), + ('\x08', "\"\\b\""), + ('\x0C', "\"\\f\""), + ('\n', "\"\\n\""), + ('\r', "\"\\r\""), + ('\t', "\"\\t\""), + ('\x0B', "\"\\u000b\""), + ('\u{3A3}', "\"\u{3A3}\""), + ]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + +#[test] +fn test_write_list() { + test_encode_ok(&[ + (vec![], "[]"), + (vec![true], "[true]"), + (vec![true, false], "[true,false]"), + ]); + + test_encode_ok(&[ + (vec![vec![], vec![], vec![]], "[[],[],[]]"), + (vec![vec![1, 2, 3], vec![], vec![]], "[[1,2,3],[],[]]"), + (vec![vec![], vec![1, 2, 3], vec![]], "[[],[1,2,3],[]]"), + (vec![vec![], vec![], vec![1, 2, 3]], "[[],[],[1,2,3]]"), + ]); + + test_pretty_encode_ok(&[ + (vec![vec![], vec![], vec![]], pretty_str!([[], [], []])), + ( + vec![vec![1, 2, 3], vec![], vec![]], + pretty_str!([[1, 2, 3], [], []]), + ), + ( + vec![vec![], vec![1, 2, 3], vec![]], + pretty_str!([[], [1, 2, 3], []]), + ), + ( + vec![vec![], vec![], vec![1, 2, 3]], + pretty_str!([[], [], [1, 2, 3]]), + ), + ]); + + test_pretty_encode_ok(&[ + (vec![], "[]"), + (vec![true], pretty_str!([true])), + (vec![true, false], pretty_str!([true, false])), + ]); + + let long_test_list = json!([false, null, ["foo\nbar", 3.5]]); + + test_encode_ok(&[( + long_test_list.clone(), + json_str!([false, null, ["foo\nbar", 3.5]]), + )]); + + test_pretty_encode_ok(&[( + long_test_list, + pretty_str!([false, null, ["foo\nbar", 3.5]]), + )]); +} + +#[test] +fn test_write_object() { + test_encode_ok(&[ + (treemap!(), "{}"), + (treemap!("a".to_string() => true), "{\"a\":true}"), + ( + treemap!( + "a".to_string() => true, + "b".to_string() => false + ), + "{\"a\":true,\"b\":false}", + ), + ]); + + test_encode_ok(&[ + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "{\"a\":{},\"b\":{},\"c\":{}}", + ), + ( + treemap![ + "a".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "{\"a\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"b\":{},\"c\":{}}", + ), + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "c".to_string() => treemap![] + ], + "{\"a\":{},\"b\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"c\":{}}", + ), + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![], + "c".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ] + ], + "{\"a\":{},\"b\":{},\"c\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}}}", + ), + ]); + + test_encode_ok(&[(treemap!['c' => ()], "{\"c\":null}")]); + + test_pretty_encode_ok(&[ + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + pretty_str!({ + "a": {}, + "b": {}, + "c": {} + }), + ), + ( + treemap![ + "a".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + pretty_str!({ + "a": { + "a": { + "a": [ + 1, + 2, + 3 + ] + }, + "b": {}, + "c": {} + }, + "b": {}, + "c": {} + }), + ), + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ], + "c".to_string() => treemap![] + ], + pretty_str!({ + "a": {}, + "b": { + "a": { + "a": [ + 1, + 2, + 3 + ] + }, + "b": {}, + "c": {} + }, + "c": {} + }), + ), + ( + treemap![ + "a".to_string() => treemap![], + "b".to_string() => treemap![], + "c".to_string() => treemap![ + "a".to_string() => treemap!["a" => vec![1,2,3]], + "b".to_string() => treemap![], + "c".to_string() => treemap![] + ] + ], + pretty_str!({ + "a": {}, + "b": {}, + "c": { + "a": { + "a": [ + 1, + 2, + 3 + ] + }, + "b": {}, + "c": {} + } + }), + ), + ]); + + test_pretty_encode_ok(&[ + (treemap!(), "{}"), + ( + treemap!("a".to_string() => true), + pretty_str!({ + "a": true + }), + ), + ( + treemap!( + "a".to_string() => true, + "b".to_string() => false + ), + pretty_str!( { + "a": true, + "b": false + }), + ), + ]); + + let complex_obj = json!({ + "b": [ + {"c": "\x0c\x1f\r"}, + {"d": ""} + ] + }); + + test_encode_ok(&[( + complex_obj.clone(), + json_str!({ + "b": [ + { + "c": (r#""\f\u001f\r""#) + }, + { + "d": "" + } + ] + }), + )]); + + test_pretty_encode_ok(&[( + complex_obj, + pretty_str!({ + "b": [ + { + "c": (r#""\f\u001f\r""#) + }, + { + "d": "" + } + ] + }), + )]); +} + +#[test] +fn test_write_tuple() { + test_encode_ok(&[((5,), "[5]")]); + + test_pretty_encode_ok(&[((5,), pretty_str!([5]))]); + + test_encode_ok(&[((5, (6, "abc")), "[5,[6,\"abc\"]]")]); + + test_pretty_encode_ok(&[((5, (6, "abc")), pretty_str!([5, [6, "abc"]]))]); +} + +#[test] +fn test_write_enum() { + test_encode_ok(&[ + (Animal::Dog, "\"Dog\""), + ( + Animal::Frog("Henry".to_string(), vec![]), + "{\"Frog\":[\"Henry\",[]]}", + ), + ( + Animal::Frog("Henry".to_string(), vec![349]), + "{\"Frog\":[\"Henry\",[349]]}", + ), + ( + Animal::Frog("Henry".to_string(), vec![349, 102]), + "{\"Frog\":[\"Henry\",[349,102]]}", + ), + ( + Animal::Cat { + age: 5, + name: "Kate".to_string(), + }, + "{\"Cat\":{\"age\":5,\"name\":\"Kate\"}}", + ), + ( + Animal::AntHive(vec!["Bob".to_string(), "Stuart".to_string()]), + "{\"AntHive\":[\"Bob\",\"Stuart\"]}", + ), + ]); + + test_pretty_encode_ok(&[ + (Animal::Dog, "\"Dog\""), + ( + Animal::Frog("Henry".to_string(), vec![]), + pretty_str!({ + "Frog": [ + "Henry", + [] + ] + }), + ), + ( + Animal::Frog("Henry".to_string(), vec![349]), + pretty_str!({ + "Frog": [ + "Henry", + [ + 349 + ] + ] + }), + ), + ( + Animal::Frog("Henry".to_string(), vec![349, 102]), + pretty_str!({ + "Frog": [ + "Henry", + [ + 349, + 102 + ] + ] + }), + ), + ]); +} + +#[test] +fn test_write_option() { + test_encode_ok(&[(None, "null"), (Some("jodhpurs"), "\"jodhpurs\"")]); + + test_encode_ok(&[ + (None, "null"), + (Some(vec!["foo", "bar"]), "[\"foo\",\"bar\"]"), + ]); + + test_pretty_encode_ok(&[(None, "null"), (Some("jodhpurs"), "\"jodhpurs\"")]); + + test_pretty_encode_ok(&[ + (None, "null"), + (Some(vec!["foo", "bar"]), pretty_str!(["foo", "bar"])), + ]); +} + +#[test] +fn test_write_newtype_struct() { + #[derive(Serialize, PartialEq, Debug)] + struct Newtype(BTreeMap); + + let inner = Newtype(treemap!(String::from("inner") => 123)); + let outer = treemap!(String::from("outer") => to_value(&inner).unwrap()); + + test_encode_ok(&[(inner, r#"{"inner":123}"#)]); + + test_encode_ok(&[(outer, r#"{"outer":{"inner":123}}"#)]); +} + +#[test] +fn test_deserialize_number_to_untagged_enum() { + #[derive(Eq, PartialEq, Deserialize, Debug)] + #[serde(untagged)] + enum E { + N(i64), + } + + assert_eq!(E::N(0), E::deserialize(Number::from(0)).unwrap()); +} + +fn test_parse_ok(tests: Vec<(&str, T)>) +where + T: Clone + Debug + PartialEq + ser::Serialize + de::DeserializeOwned, +{ + for (s, value) in tests { + let v: T = from_str(s).unwrap(); + assert_eq!(v, value.clone()); + + let v: T = from_slice(s.as_bytes()).unwrap(); + assert_eq!(v, value.clone()); + + // Make sure we can deserialize into a `Value`. + let json_value: Value = from_str(s).unwrap(); + assert_eq!(json_value, to_value(&value).unwrap()); + + // Make sure we can deserialize from a `&Value`. + let v = T::deserialize(&json_value).unwrap(); + assert_eq!(v, value); + + // Make sure we can deserialize from a `Value`. + let v: T = from_value(json_value.clone()).unwrap(); + assert_eq!(v, value); + + // Make sure we can round trip back to `Value`. + let json_value2: Value = from_value(json_value.clone()).unwrap(); + assert_eq!(json_value2, json_value); + + // Make sure we can fully ignore. + let twoline = s.to_owned() + "\n3735928559"; + let mut de = Deserializer::from_str(&twoline); + IgnoredAny::deserialize(&mut de).unwrap(); + assert_eq!(0xDEAD_BEEF, u64::deserialize(&mut de).unwrap()); + + // Make sure every prefix is an EOF error, except that a prefix of a + // number may be a valid number. + if !json_value.is_number() { + for (i, _) in s.trim_end().char_indices() { + assert!(from_str::(&s[..i]).unwrap_err().is_eof()); + assert!(from_str::(&s[..i]).unwrap_err().is_eof()); + } + } + } +} + +// For testing representations that the deserializer accepts but the serializer +// never generates. These do not survive a round-trip through Value. +fn test_parse_unusual_ok(tests: Vec<(&str, T)>) +where + T: Clone + Debug + PartialEq + ser::Serialize + de::DeserializeOwned, +{ + for (s, value) in tests { + let v: T = from_str(s).unwrap(); + assert_eq!(v, value.clone()); + + let v: T = from_slice(s.as_bytes()).unwrap(); + assert_eq!(v, value.clone()); + } +} + +macro_rules! test_parse_err { + ($name:ident::<$($ty:ty),*>($arg:expr) => $expected:expr) => { + let actual = $name::<$($ty),*>($arg).unwrap_err().to_string(); + assert_eq!(actual, $expected, "unexpected {} error", stringify!($name)); + }; +} + +fn test_parse_err(errors: &[(&str, &'static str)]) +where + T: Debug + PartialEq + de::DeserializeOwned, +{ + for &(s, err) in errors { + test_parse_err!(from_str::(s) => err); + test_parse_err!(from_slice::(s.as_bytes()) => err); + } +} + +fn test_parse_slice_err(errors: &[(&[u8], &'static str)]) +where + T: Debug + PartialEq + de::DeserializeOwned, +{ + for &(s, err) in errors { + test_parse_err!(from_slice::(s) => err); + } +} + +fn test_fromstr_parse_err(errors: &[(&str, &'static str)]) +where + T: Debug + PartialEq + FromStr, + ::Err: ToString, +{ + for &(s, err) in errors { + let actual = s.parse::().unwrap_err().to_string(); + assert_eq!(actual, err, "unexpected parsing error"); + } +} + +#[test] +fn test_parse_null() { + test_parse_err::<()>(&[ + ("n", "EOF while parsing a value at line 1 column 1"), + ("nul", "EOF while parsing a value at line 1 column 3"), + ("nulla", "trailing characters at line 1 column 5"), + ]); + + test_parse_ok(vec![("null", ())]); +} + +#[test] +fn test_parse_bool() { + test_parse_err::(&[ + ("t", "EOF while parsing a value at line 1 column 1"), + ("truz", "expected ident at line 1 column 4"), + ("f", "EOF while parsing a value at line 1 column 1"), + ("faz", "expected ident at line 1 column 3"), + ("truea", "trailing characters at line 1 column 5"), + ("falsea", "trailing characters at line 1 column 6"), + ]); + + test_parse_ok(vec![ + ("true", true), + (" true ", true), + ("false", false), + (" false ", false), + ]); +} + +#[test] +fn test_parse_char() { + test_parse_err::(&[ + ( + "\"ab\"", + "invalid value: string \"ab\", expected a character at line 1 column 4", + ), + ( + "10", + "invalid type: integer `10`, expected a character at line 1 column 2", + ), + ]); + + test_parse_ok(vec![ + ("\"n\"", 'n'), + ("\"\\\"\"", '"'), + ("\"\\\\\"", '\\'), + ("\"/\"", '/'), + ("\"\\b\"", '\x08'), + ("\"\\f\"", '\x0C'), + ("\"\\n\"", '\n'), + ("\"\\r\"", '\r'), + ("\"\\t\"", '\t'), + ("\"\\u000b\"", '\x0B'), + ("\"\\u000B\"", '\x0B'), + ("\"\u{3A3}\"", '\u{3A3}'), + ]); +} + +#[test] +fn test_parse_number_errors() { + test_parse_err::(&[ + ("+", "expected value at line 1 column 1"), + (".", "expected value at line 1 column 1"), + ("-", "EOF while parsing a value at line 1 column 1"), + ("00", "invalid number at line 1 column 2"), + ("0x80", "trailing characters at line 1 column 2"), + ("\\0", "expected value at line 1 column 1"), + (".0", "expected value at line 1 column 1"), + ("0.", "EOF while parsing a value at line 1 column 2"), + ("1.", "EOF while parsing a value at line 1 column 2"), + ("1.a", "invalid number at line 1 column 3"), + ("1.e1", "invalid number at line 1 column 3"), + ("1e", "EOF while parsing a value at line 1 column 2"), + ("1e+", "EOF while parsing a value at line 1 column 3"), + ("1a", "trailing characters at line 1 column 2"), + ( + "100e777777777777777777777777777", + "number out of range at line 1 column 14", + ), + ( + "-100e777777777777777777777777777", + "number out of range at line 1 column 15", + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000", // 1e309 + "number out of range at line 1 column 310", + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + .0e9", // 1e309 + "number out of range at line 1 column 305", + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + e9", // 1e309 + "number out of range at line 1 column 303", + ), + ]); +} + +#[test] +fn test_parse_i64() { + test_parse_ok(vec![ + ("-2", -2), + ("-1234", -1234), + (" -1234 ", -1234), + (&i64::MIN.to_string(), i64::MIN), + (&i64::MAX.to_string(), i64::MAX), + ]); +} + +#[test] +fn test_parse_u64() { + test_parse_ok(vec![ + ("0", 0u64), + ("3", 3u64), + ("1234", 1234), + (&u64::MAX.to_string(), u64::MAX), + ]); +} + +#[test] +fn test_parse_negative_zero() { + for negative_zero in &[ + "-0", + "-0.0", + "-0e2", + "-0.0e2", + "-1e-400", + "-1e-4000000000000000000000000000000000000000000000000", + ] { + assert!( + from_str::(negative_zero).unwrap().is_sign_negative(), + "should have been negative: {:?}", + negative_zero, + ); + assert!( + from_str::(negative_zero).unwrap().is_sign_negative(), + "should have been negative: {:?}", + negative_zero, + ); + } +} + +#[test] +fn test_parse_f64() { + test_parse_ok(vec![ + ("0.0", 0.0f64), + ("3.0", 3.0f64), + ("3.1", 3.1), + ("-1.2", -1.2), + ("0.4", 0.4), + // Edge case from: + // https://github.com/serde-rs/json/issues/536#issuecomment-583714900 + ("2.638344616030823e-256", 2.638344616030823e-256), + ]); + + #[cfg(not(feature = "arbitrary_precision"))] + test_parse_ok(vec![ + // With arbitrary-precision enabled, this parses as Number{"3.00"} + // but the float is Number{"3.0"} + ("3.00", 3.0f64), + ("0.4e5", 0.4e5), + ("0.4e+5", 0.4e5), + ("0.4e15", 0.4e15), + ("0.4e+15", 0.4e15), + ("0.4e-01", 0.4e-1), + (" 0.4e-01 ", 0.4e-1), + ("0.4e-001", 0.4e-1), + ("0.4e-0", 0.4e0), + ("0.00e00", 0.0), + ("0.00e+00", 0.0), + ("0.00e-00", 0.0), + ("3.5E-2147483647", 0.0), + ("0.0100000000000000000001", 0.01), + ( + &format!("{}", (i64::MIN as f64) - 1.0), + (i64::MIN as f64) - 1.0, + ), + ( + &format!("{}", (u64::MAX as f64) + 1.0), + (u64::MAX as f64) + 1.0, + ), + (&format!("{}", f64::EPSILON), f64::EPSILON), + ( + "0.0000000000000000000000000000000000000000000000000123e50", + 1.23, + ), + ("100e-777777777777777777777777777", 0.0), + ( + "1010101010101010101010101010101010101010", + 10101010101010101010e20, + ), + ( + "0.1010101010101010101010101010101010101010", + 0.1010101010101010101, + ), + ("0e1000000000000000000000000000000000000000000000", 0.0), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 00000000", + 1e308, + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + .0e8", + 1e308, + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + e8", + 1e308, + ), + ( + "1000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000000000000000000000000000000000000000000000\ + 000000000000000000e-10", + 1e308, + ), + ]); +} + +#[test] +fn test_value_as_f64() { + let v = serde_json::from_str::("1e1000"); + + #[cfg(not(feature = "arbitrary_precision"))] + assert!(v.is_err()); + + #[cfg(feature = "arbitrary_precision")] + assert_eq!(v.unwrap().as_f64(), None); +} + +// Test roundtrip with some values that were not perfectly roundtripped by the +// old f64 deserializer. +#[cfg(feature = "float_roundtrip")] +#[test] +fn test_roundtrip_f64() { + for &float in &[ + // Samples from quickcheck-ing roundtrip with `input: f64`. Comments + // indicate the value returned by the old deserializer. + 51.24817837550540_4, // 51.2481783755054_1 + -93.3113703768803_3, // -93.3113703768803_2 + -36.5739948427534_36, // -36.5739948427534_4 + 52.31400820410624_4, // 52.31400820410624_ + 97.4536532003468_5, // 97.4536532003468_4 + // Samples from `rng.next_u64` + `f64::from_bits` + `is_finite` filter. + 2.0030397744267762e-253, + 7.101215824554616e260, + 1.769268377902049e74, + -1.6727517818542075e58, + 3.9287532173373315e299, + ] { + let json = serde_json::to_string(&float).unwrap(); + let output: f64 = serde_json::from_str(&json).unwrap(); + assert_eq!(float, output); + } +} + +#[test] +fn test_roundtrip_f32() { + // This number has 1 ULP error if parsed via f64 and converted to f32. + // https://github.com/serde-rs/json/pull/671#issuecomment-628534468 + let float = 7.038531e-26; + let json = serde_json::to_string(&float).unwrap(); + let output: f32 = serde_json::from_str(&json).unwrap(); + assert_eq!(float, output); +} + +#[test] +fn test_serialize_char() { + let value = json!( + ({ + let mut map = BTreeMap::new(); + map.insert('c', ()); + map + }) + ); + assert_eq!(&Value::Null, value.get("c").unwrap()); +} + +#[cfg(feature = "arbitrary_precision")] +#[test] +fn test_malicious_number() { + #[derive(Serialize)] + #[serde(rename = "$serde_json::private::Number")] + struct S { + #[serde(rename = "$serde_json::private::Number")] + f: &'static str, + } + + let actual = serde_json::to_value(&S { f: "not a number" }) + .unwrap_err() + .to_string(); + assert_eq!(actual, "invalid number at line 1 column 1"); +} + +#[test] +fn test_parse_number() { + test_parse_ok(vec![ + ("0.0", Number::from_f64(0.0f64).unwrap()), + ("3.0", Number::from_f64(3.0f64).unwrap()), + ("3.1", Number::from_f64(3.1).unwrap()), + ("-1.2", Number::from_f64(-1.2).unwrap()), + ("0.4", Number::from_f64(0.4).unwrap()), + ]); + + test_fromstr_parse_err::(&[ + (" 1.0", "invalid number at line 1 column 1"), + ("1.0 ", "invalid number at line 1 column 4"), + ("\t1.0", "invalid number at line 1 column 1"), + ("1.0\t", "invalid number at line 1 column 4"), + ]); + + #[cfg(feature = "arbitrary_precision")] + test_parse_ok(vec![ + ("1e999", Number::from_string_unchecked("1e999".to_owned())), + ("1e+999", Number::from_string_unchecked("1e+999".to_owned())), + ("-1e999", Number::from_string_unchecked("-1e999".to_owned())), + ("1e-999", Number::from_string_unchecked("1e-999".to_owned())), + ("1E999", Number::from_string_unchecked("1E999".to_owned())), + ("1E+999", Number::from_string_unchecked("1E+999".to_owned())), + ("-1E999", Number::from_string_unchecked("-1E999".to_owned())), + ("1E-999", Number::from_string_unchecked("1E-999".to_owned())), + ("1E+000", Number::from_string_unchecked("1E+000".to_owned())), + ( + "2.3e999", + Number::from_string_unchecked("2.3e999".to_owned()), + ), + ( + "-2.3e999", + Number::from_string_unchecked("-2.3e999".to_owned()), + ), + ]); +} + +#[test] +fn test_parse_string() { + test_parse_err::(&[ + ("\"", "EOF while parsing a string at line 1 column 1"), + ("\"lol", "EOF while parsing a string at line 1 column 4"), + ("\"lol\"a", "trailing characters at line 1 column 6"), + ( + "\"\\uD83C\\uFFFF\"", + "lone leading surrogate in hex escape at line 1 column 13", + ), + ( + "\"\n\"", + "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0", + ), + ( + "\"\x1F\"", + "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2", + ), + ]); + + test_parse_slice_err::(&[ + ( + &[b'"', 159, 146, 150, b'"'], + "invalid unicode code point at line 1 column 5", + ), + ( + &[b'"', b'\\', b'n', 159, 146, 150, b'"'], + "invalid unicode code point at line 1 column 7", + ), + ( + &[b'"', b'\\', b'u', 48, 48, 51], + "EOF while parsing a string at line 1 column 6", + ), + ( + &[b'"', b'\\', b'u', 250, 48, 51, 48, b'"'], + "invalid escape at line 1 column 4", + ), + ( + &[b'"', b'\\', b'u', 48, 250, 51, 48, b'"'], + "invalid escape at line 1 column 5", + ), + ( + &[b'"', b'\\', b'u', 48, 48, 250, 48, b'"'], + "invalid escape at line 1 column 6", + ), + ( + &[b'"', b'\\', b'u', 48, 48, 51, 250, b'"'], + "invalid escape at line 1 column 7", + ), + ( + &[b'"', b'\n', b'"'], + "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0", + ), + ( + &[b'"', b'\x1F', b'"'], + "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2", + ), + ]); + + test_parse_ok(vec![ + ("\"\"", String::new()), + ("\"foo\"", "foo".to_string()), + (" \"foo\" ", "foo".to_string()), + ("\"\\\"\"", "\"".to_string()), + ("\"\\b\"", "\x08".to_string()), + ("\"\\n\"", "\n".to_string()), + ("\"\\r\"", "\r".to_string()), + ("\"\\t\"", "\t".to_string()), + ("\"\\u12ab\"", "\u{12ab}".to_string()), + ("\"\\uAB12\"", "\u{AB12}".to_string()), + ("\"\\uD83C\\uDF95\"", "\u{1F395}".to_string()), + ]); +} + +#[test] +fn test_parse_list() { + test_parse_err::>(&[ + ("[", "EOF while parsing a list at line 1 column 1"), + ("[ ", "EOF while parsing a list at line 1 column 2"), + ("[1", "EOF while parsing a list at line 1 column 2"), + ("[1,", "EOF while parsing a value at line 1 column 3"), + ("[1,]", "trailing comma at line 1 column 4"), + ("[1 2]", "expected `,` or `]` at line 1 column 4"), + ("[]a", "trailing characters at line 1 column 3"), + ]); + + test_parse_ok(vec![ + ("[]", vec![]), + ("[ ]", vec![]), + ("[null]", vec![()]), + (" [ null ] ", vec![()]), + ]); + + test_parse_ok(vec![("[true]", vec![true])]); + + test_parse_ok(vec![("[3,1]", vec![3u64, 1]), (" [ 3 , 1 ] ", vec![3, 1])]); + + test_parse_ok(vec![("[[3], [1, 2]]", vec![vec![3u64], vec![1, 2]])]); + + test_parse_ok(vec![("[1]", (1u64,))]); + + test_parse_ok(vec![("[1, 2]", (1u64, 2u64))]); + + test_parse_ok(vec![("[1, 2, 3]", (1u64, 2u64, 3u64))]); + + test_parse_ok(vec![("[1, [2, 3]]", (1u64, (2u64, 3u64)))]); +} + +#[test] +fn test_parse_object() { + test_parse_err::>(&[ + ("{", "EOF while parsing an object at line 1 column 1"), + ("{ ", "EOF while parsing an object at line 1 column 2"), + ("{1", "key must be a string at line 1 column 2"), + ("{ \"a\"", "EOF while parsing an object at line 1 column 5"), + ("{\"a\"", "EOF while parsing an object at line 1 column 4"), + ("{\"a\" ", "EOF while parsing an object at line 1 column 5"), + ("{\"a\" 1", "expected `:` at line 1 column 6"), + ("{\"a\":", "EOF while parsing a value at line 1 column 5"), + ("{\"a\":1", "EOF while parsing an object at line 1 column 6"), + ("{\"a\":1 1", "expected `,` or `}` at line 1 column 8"), + ("{\"a\":1,", "EOF while parsing a value at line 1 column 7"), + ("{}a", "trailing characters at line 1 column 3"), + ]); + + test_parse_ok(vec![ + ("{}", treemap!()), + ("{ }", treemap!()), + ("{\"a\":3}", treemap!("a".to_string() => 3u64)), + ("{ \"a\" : 3 }", treemap!("a".to_string() => 3)), + ( + "{\"a\":3,\"b\":4}", + treemap!("a".to_string() => 3, "b".to_string() => 4), + ), + ( + " { \"a\" : 3 , \"b\" : 4 } ", + treemap!("a".to_string() => 3, "b".to_string() => 4), + ), + ]); + + test_parse_ok(vec![( + "{\"a\": {\"b\": 3, \"c\": 4}}", + treemap!( + "a".to_string() => treemap!( + "b".to_string() => 3u64, + "c".to_string() => 4 + ) + ), + )]); + + test_parse_ok(vec![("{\"c\":null}", treemap!('c' => ()))]); +} + +#[test] +fn test_parse_struct() { + test_parse_err::(&[ + ( + "5", + "invalid type: integer `5`, expected struct Outer at line 1 column 1", + ), + ( + "\"hello\"", + "invalid type: string \"hello\", expected struct Outer at line 1 column 7", + ), + ( + "{\"inner\": true}", + "invalid type: boolean `true`, expected a sequence at line 1 column 14", + ), + ("{}", "missing field `inner` at line 1 column 2"), + ( + r#"{"inner": [{"b": 42, "c": []}]}"#, + "missing field `a` at line 1 column 29", + ), + ]); + + test_parse_ok(vec![ + ( + "{ + \"inner\": [] + }", + Outer { inner: vec![] }, + ), + ( + "{ + \"inner\": [ + { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] } + ] + }", + Outer { + inner: vec![Inner { + a: (), + b: 2, + c: vec!["abc".to_string(), "xyz".to_string()], + }], + }, + ), + ]); + + let v: Outer = from_str( + "[ + [ + [ null, 2, [\"abc\", \"xyz\"] ] + ] + ]", + ) + .unwrap(); + + assert_eq!( + v, + Outer { + inner: vec![Inner { + a: (), + b: 2, + c: vec!["abc".to_string(), "xyz".to_string()], + }], + } + ); + + let j = json!([null, 2, []]); + Inner::deserialize(&j).unwrap(); + Inner::deserialize(j).unwrap(); +} + +#[test] +fn test_parse_option() { + test_parse_ok(vec![ + ("null", None::), + ("\"jodhpurs\"", Some("jodhpurs".to_string())), + ]); + + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] + struct Foo { + x: Option, + } + + let value: Foo = from_str("{}").unwrap(); + assert_eq!(value, Foo { x: None }); + + test_parse_ok(vec![ + ("{\"x\": null}", Foo { x: None }), + ("{\"x\": 5}", Foo { x: Some(5) }), + ]); +} + +#[test] +fn test_parse_enum_errors() { + test_parse_err::( + &[ + ("{}", "expected value at line 1 column 2"), + ("[]", "expected value at line 1 column 1"), + ("\"unknown\"", + "unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 9"), + ("{\"unknown\":null}", + "unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 10"), + ("{\"Dog\":", "EOF while parsing a value at line 1 column 7"), + ("{\"Dog\":}", "expected value at line 1 column 8"), + ("{\"Dog\":{}}", "invalid type: map, expected unit at line 1 column 7"), + ("\"Frog\"", "invalid type: unit variant, expected tuple variant"), + ("\"Frog\" 0 ", "invalid type: unit variant, expected tuple variant"), + ("{\"Frog\":{}}", + "invalid type: map, expected tuple variant Animal::Frog at line 1 column 8"), + ("{\"Cat\":[]}", "invalid length 0, expected struct variant Animal::Cat with 2 elements at line 1 column 9"), + ("{\"Cat\":[0]}", "invalid length 1, expected struct variant Animal::Cat with 2 elements at line 1 column 10"), + ("{\"Cat\":[0, \"\", 2]}", "trailing characters at line 1 column 16"), + ("{\"Cat\":{\"age\": 5, \"name\": \"Kate\", \"foo\":\"bar\"}", + "unknown field `foo`, expected `age` or `name` at line 1 column 39"), + + // JSON does not allow trailing commas in data structures + ("{\"Cat\":[0, \"Kate\",]}", "trailing comma at line 1 column 19"), + ("{\"Cat\":{\"age\": 2, \"name\": \"Kate\",}}", + "trailing comma at line 1 column 34"), + ], + ); +} + +#[test] +fn test_parse_enum() { + test_parse_ok(vec![ + ("\"Dog\"", Animal::Dog), + (" \"Dog\" ", Animal::Dog), + ( + "{\"Frog\":[\"Henry\",[]]}", + Animal::Frog("Henry".to_string(), vec![]), + ), + ( + " { \"Frog\": [ \"Henry\" , [ 349, 102 ] ] } ", + Animal::Frog("Henry".to_string(), vec![349, 102]), + ), + ( + "{\"Cat\": {\"age\": 5, \"name\": \"Kate\"}}", + Animal::Cat { + age: 5, + name: "Kate".to_string(), + }, + ), + ( + " { \"Cat\" : { \"age\" : 5 , \"name\" : \"Kate\" } } ", + Animal::Cat { + age: 5, + name: "Kate".to_string(), + }, + ), + ( + " { \"AntHive\" : [\"Bob\", \"Stuart\"] } ", + Animal::AntHive(vec!["Bob".to_string(), "Stuart".to_string()]), + ), + ]); + + test_parse_unusual_ok(vec![ + ("{\"Dog\":null}", Animal::Dog), + (" { \"Dog\" : null } ", Animal::Dog), + ]); + + test_parse_ok(vec![( + concat!( + "{", + " \"a\": \"Dog\",", + " \"b\": {\"Frog\":[\"Henry\", []]}", + "}" + ), + treemap!( + "a".to_string() => Animal::Dog, + "b".to_string() => Animal::Frog("Henry".to_string(), vec![]) + ), + )]); +} + +#[test] +fn test_parse_trailing_whitespace() { + test_parse_ok(vec![ + ("[1, 2] ", vec![1u64, 2]), + ("[1, 2]\n", vec![1, 2]), + ("[1, 2]\t", vec![1, 2]), + ("[1, 2]\t \n", vec![1, 2]), + ]); +} + +#[test] +fn test_multiline_errors() { + test_parse_err::>(&[( + "{\n \"foo\":\n \"bar\"", + "EOF while parsing an object at line 3 column 6", + )]); +} + +#[test] +fn test_missing_option_field() { + #[derive(Debug, PartialEq, Deserialize)] + struct Foo { + x: Option, + } + + let value: Foo = from_str("{}").unwrap(); + assert_eq!(value, Foo { x: None }); + + let value: Foo = from_str("{\"x\": 5}").unwrap(); + assert_eq!(value, Foo { x: Some(5) }); + + let value: Foo = from_value(json!({})).unwrap(); + assert_eq!(value, Foo { x: None }); + + let value: Foo = from_value(json!({"x": 5})).unwrap(); + assert_eq!(value, Foo { x: Some(5) }); +} + +#[test] +fn test_missing_nonoption_field() { + #[derive(Debug, PartialEq, Deserialize)] + struct Foo { + x: u32, + } + + test_parse_err::(&[("{}", "missing field `x` at line 1 column 2")]); +} + +#[test] +fn test_missing_renamed_field() { + #[derive(Debug, PartialEq, Deserialize)] + struct Foo { + #[serde(rename = "y")] + x: Option, + } + + let value: Foo = from_str("{}").unwrap(); + assert_eq!(value, Foo { x: None }); + + let value: Foo = from_str("{\"y\": 5}").unwrap(); + assert_eq!(value, Foo { x: Some(5) }); + + let value: Foo = from_value(json!({})).unwrap(); + assert_eq!(value, Foo { x: None }); + + let value: Foo = from_value(json!({"y": 5})).unwrap(); + assert_eq!(value, Foo { x: Some(5) }); +} + +#[test] +fn test_serialize_seq_with_no_len() { + #[derive(Clone, Debug, PartialEq)] + struct MyVec(Vec); + + impl ser::Serialize for MyVec + where + T: ser::Serialize, + { + #[inline] + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + let mut seq = serializer.serialize_seq(None)?; + for elem in &self.0 { + seq.serialize_element(elem)?; + } + seq.end() + } + } + + struct Visitor { + marker: PhantomData>, + } + + impl<'de, T> de::Visitor<'de> for Visitor + where + T: de::Deserialize<'de>, + { + type Value = MyVec; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("array") + } + + #[inline] + fn visit_unit(self) -> Result, E> + where + E: de::Error, + { + Ok(MyVec(Vec::new())) + } + + #[inline] + fn visit_seq(self, mut visitor: V) -> Result, V::Error> + where + V: de::SeqAccess<'de>, + { + let mut values = Vec::new(); + + while let Some(value) = visitor.next_element()? { + values.push(value); + } + + Ok(MyVec(values)) + } + } + + impl<'de, T> de::Deserialize<'de> for MyVec + where + T: de::Deserialize<'de>, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: de::Deserializer<'de>, + { + deserializer.deserialize_map(Visitor { + marker: PhantomData, + }) + } + } + + let mut vec = Vec::new(); + vec.push(MyVec(Vec::new())); + vec.push(MyVec(Vec::new())); + let vec: MyVec> = MyVec(vec); + + test_encode_ok(&[(vec.clone(), "[[],[]]")]); + + let s = to_string_pretty(&vec).unwrap(); + let expected = pretty_str!([[], []]); + assert_eq!(s, expected); +} + +#[test] +fn test_serialize_map_with_no_len() { + #[derive(Clone, Debug, PartialEq)] + struct MyMap(BTreeMap); + + impl ser::Serialize for MyMap + where + K: ser::Serialize + Ord, + V: ser::Serialize, + { + #[inline] + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + let mut map = serializer.serialize_map(None)?; + for (k, v) in &self.0 { + map.serialize_entry(k, v)?; + } + map.end() + } + } + + struct Visitor { + marker: PhantomData>, + } + + impl<'de, K, V> de::Visitor<'de> for Visitor + where + K: de::Deserialize<'de> + Eq + Ord, + V: de::Deserialize<'de>, + { + type Value = MyMap; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("map") + } + + #[inline] + fn visit_unit(self) -> Result, E> + where + E: de::Error, + { + Ok(MyMap(BTreeMap::new())) + } + + #[inline] + fn visit_map(self, mut visitor: Visitor) -> Result, Visitor::Error> + where + Visitor: de::MapAccess<'de>, + { + let mut values = BTreeMap::new(); + + while let Some((key, value)) = visitor.next_entry()? { + values.insert(key, value); + } + + Ok(MyMap(values)) + } + } + + impl<'de, K, V> de::Deserialize<'de> for MyMap + where + K: de::Deserialize<'de> + Eq + Ord, + V: de::Deserialize<'de>, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: de::Deserializer<'de>, + { + deserializer.deserialize_map(Visitor { + marker: PhantomData, + }) + } + } + + let mut map = BTreeMap::new(); + map.insert("a", MyMap(BTreeMap::new())); + map.insert("b", MyMap(BTreeMap::new())); + let map: MyMap<_, MyMap> = MyMap(map); + + test_encode_ok(&[(map.clone(), "{\"a\":{},\"b\":{}}")]); + + let s = to_string_pretty(&map).unwrap(); + let expected = pretty_str!({ + "a": {}, + "b": {} + }); + assert_eq!(s, expected); +} + +#[cfg(not(miri))] +#[test] +fn test_deserialize_from_stream() { + use serde_json::to_writer; + use std::net::{TcpListener, TcpStream}; + use std::thread; + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct Message { + message: String, + } + + let l = TcpListener::bind("localhost:20000").unwrap(); + + thread::spawn(|| { + let l = l; + for stream in l.incoming() { + let mut stream = stream.unwrap(); + let read_stream = stream.try_clone().unwrap(); + + let mut de = Deserializer::from_reader(read_stream); + let request = Message::deserialize(&mut de).unwrap(); + let response = Message { + message: request.message, + }; + to_writer(&mut stream, &response).unwrap(); + } + }); + + let mut stream = TcpStream::connect("localhost:20000").unwrap(); + let request = Message { + message: "hi there".to_string(), + }; + to_writer(&mut stream, &request).unwrap(); + + let mut de = Deserializer::from_reader(stream); + let response = Message::deserialize(&mut de).unwrap(); + + assert_eq!(request, response); +} + +#[test] +fn test_serialize_rejects_bool_keys() { + let map = treemap!( + true => 2, + false => 4 + ); + + let err = to_vec(&map).unwrap_err(); + assert_eq!(err.to_string(), "key must be a string"); +} + +#[test] +fn test_serialize_rejects_adt_keys() { + let map = treemap!( + Some("a") => 2, + Some("b") => 4, + None => 6 + ); + + let err = to_vec(&map).unwrap_err(); + assert_eq!(err.to_string(), "key must be a string"); +} + +#[test] +fn test_bytes_ser() { + let buf = vec![]; + let bytes = Bytes::new(&buf); + assert_eq!(to_string(&bytes).unwrap(), "[]".to_string()); + + let buf = vec![1, 2, 3]; + let bytes = Bytes::new(&buf); + assert_eq!(to_string(&bytes).unwrap(), "[1,2,3]".to_string()); +} + +#[test] +fn test_byte_buf_ser() { + let bytes = ByteBuf::new(); + assert_eq!(to_string(&bytes).unwrap(), "[]".to_string()); + + let bytes = ByteBuf::from(vec![1, 2, 3]); + assert_eq!(to_string(&bytes).unwrap(), "[1,2,3]".to_string()); +} + +#[test] +fn test_byte_buf_de() { + let bytes = ByteBuf::new(); + let v: ByteBuf = from_str("[]").unwrap(); + assert_eq!(v, bytes); + + let bytes = ByteBuf::from(vec![1, 2, 3]); + let v: ByteBuf = from_str("[1, 2, 3]").unwrap(); + assert_eq!(v, bytes); +} + +#[test] +fn test_byte_buf_de_lone_surrogate() { + let bytes = ByteBuf::from(vec![237, 160, 188]); + let v: ByteBuf = from_str(r#""\ud83c""#).unwrap(); + assert_eq!(v, bytes); + + let bytes = ByteBuf::from(vec![237, 160, 188, 10]); + let v: ByteBuf = from_str(r#""\ud83c\n""#).unwrap(); + assert_eq!(v, bytes); + + let bytes = ByteBuf::from(vec![237, 160, 188, 32]); + let v: ByteBuf = from_str(r#""\ud83c ""#).unwrap(); + assert_eq!(v, bytes); + + let bytes = ByteBuf::from(vec![237, 176, 129]); + let v: ByteBuf = from_str(r#""\udc01""#).unwrap(); + assert_eq!(v, bytes); + + let res = from_str::(r#""\ud83c\!""#); + assert!(res.is_err()); + + let res = from_str::(r#""\ud83c\u""#); + assert!(res.is_err()); + + let res = from_str::(r#""\ud83c\ud83c""#); + assert!(res.is_err()); +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_raw_de_lone_surrogate() { + use serde_json::value::RawValue; + + assert!(from_str::>(r#""\ud83c""#).is_ok()); + assert!(from_str::>(r#""\ud83c\n""#).is_ok()); + assert!(from_str::>(r#""\ud83c ""#).is_ok()); + assert!(from_str::>(r#""\udc01 ""#).is_ok()); + assert!(from_str::>(r#""\udc01\!""#).is_err()); + assert!(from_str::>(r#""\udc01\u""#).is_err()); + assert!(from_str::>(r#""\ud83c\ud83c""#).is_ok()); +} + +#[test] +fn test_byte_buf_de_multiple() { + let s: Vec = from_str(r#"["ab\nc", "cd\ne"]"#).unwrap(); + let a = ByteBuf::from(b"ab\nc".to_vec()); + let b = ByteBuf::from(b"cd\ne".to_vec()); + assert_eq!(vec![a, b], s); +} + +#[test] +fn test_json_pointer() { + // Test case taken from https://tools.ietf.org/html/rfc6901#page-5 + let data: Value = from_str( + r#"{ + "foo": ["bar", "baz"], + "": 0, + "a/b": 1, + "c%d": 2, + "e^f": 3, + "g|h": 4, + "i\\j": 5, + "k\"l": 6, + " ": 7, + "m~n": 8 + }"#, + ) + .unwrap(); + assert_eq!(data.pointer("").unwrap(), &data); + assert_eq!(data.pointer("/foo").unwrap(), &json!(["bar", "baz"])); + assert_eq!(data.pointer("/foo/0").unwrap(), &json!("bar")); + assert_eq!(data.pointer("/").unwrap(), &json!(0)); + assert_eq!(data.pointer("/a~1b").unwrap(), &json!(1)); + assert_eq!(data.pointer("/c%d").unwrap(), &json!(2)); + assert_eq!(data.pointer("/e^f").unwrap(), &json!(3)); + assert_eq!(data.pointer("/g|h").unwrap(), &json!(4)); + assert_eq!(data.pointer("/i\\j").unwrap(), &json!(5)); + assert_eq!(data.pointer("/k\"l").unwrap(), &json!(6)); + assert_eq!(data.pointer("/ ").unwrap(), &json!(7)); + assert_eq!(data.pointer("/m~0n").unwrap(), &json!(8)); + // Invalid pointers + assert!(data.pointer("/unknown").is_none()); + assert!(data.pointer("/e^f/ertz").is_none()); + assert!(data.pointer("/foo/00").is_none()); + assert!(data.pointer("/foo/01").is_none()); +} + +#[test] +fn test_json_pointer_mut() { + // Test case taken from https://tools.ietf.org/html/rfc6901#page-5 + let mut data: Value = from_str( + r#"{ + "foo": ["bar", "baz"], + "": 0, + "a/b": 1, + "c%d": 2, + "e^f": 3, + "g|h": 4, + "i\\j": 5, + "k\"l": 6, + " ": 7, + "m~n": 8 + }"#, + ) + .unwrap(); + + // Basic pointer checks + assert_eq!(data.pointer_mut("/foo").unwrap(), &json!(["bar", "baz"])); + assert_eq!(data.pointer_mut("/foo/0").unwrap(), &json!("bar")); + assert_eq!(data.pointer_mut("/").unwrap(), 0); + assert_eq!(data.pointer_mut("/a~1b").unwrap(), 1); + assert_eq!(data.pointer_mut("/c%d").unwrap(), 2); + assert_eq!(data.pointer_mut("/e^f").unwrap(), 3); + assert_eq!(data.pointer_mut("/g|h").unwrap(), 4); + assert_eq!(data.pointer_mut("/i\\j").unwrap(), 5); + assert_eq!(data.pointer_mut("/k\"l").unwrap(), 6); + assert_eq!(data.pointer_mut("/ ").unwrap(), 7); + assert_eq!(data.pointer_mut("/m~0n").unwrap(), 8); + + // Invalid pointers + assert!(data.pointer_mut("/unknown").is_none()); + assert!(data.pointer_mut("/e^f/ertz").is_none()); + assert!(data.pointer_mut("/foo/00").is_none()); + assert!(data.pointer_mut("/foo/01").is_none()); + + // Mutable pointer checks + *data.pointer_mut("/").unwrap() = 100.into(); + assert_eq!(data.pointer("/").unwrap(), 100); + *data.pointer_mut("/foo/0").unwrap() = json!("buzz"); + assert_eq!(data.pointer("/foo/0").unwrap(), &json!("buzz")); + + // Example of ownership stealing + assert_eq!( + data.pointer_mut("/a~1b") + .map(|m| mem::replace(m, json!(null))) + .unwrap(), + 1 + ); + assert_eq!(data.pointer("/a~1b").unwrap(), &json!(null)); + + // Need to compare against a clone so we don't anger the borrow checker + // by taking out two references to a mutable value + let mut d2 = data.clone(); + assert_eq!(data.pointer_mut("").unwrap(), &mut d2); +} + +#[test] +fn test_stack_overflow() { + let brackets: String = iter::repeat('[') + .take(127) + .chain(iter::repeat(']').take(127)) + .collect(); + let _: Value = from_str(&brackets).unwrap(); + + let brackets = "[".repeat(129); + test_parse_err::(&[(&brackets, "recursion limit exceeded at line 1 column 128")]); +} + +#[test] +#[cfg(feature = "unbounded_depth")] +fn test_disable_recursion_limit() { + let brackets: String = iter::repeat('[') + .take(140) + .chain(iter::repeat(']').take(140)) + .collect(); + + let mut deserializer = Deserializer::from_str(&brackets); + deserializer.disable_recursion_limit(); + Value::deserialize(&mut deserializer).unwrap(); +} + +#[test] +fn test_integer_key() { + // map with integer keys + let map = treemap!( + 1 => 2, + -1 => 6 + ); + let j = r#"{"-1":6,"1":2}"#; + test_encode_ok(&[(&map, j)]); + test_parse_ok(vec![(j, map)]); + + let j = r#"{"x":null}"#; + test_parse_err::>(&[( + j, + "invalid type: string \"x\", expected i32 at line 1 column 4", + )]); +} + +#[test] +fn test_integer128_key() { + let map = treemap! { + 100000000000000000000000000000000000000u128 => () + }; + let j = r#"{"100000000000000000000000000000000000000":null}"#; + assert_eq!(to_string(&map).unwrap(), j); + assert_eq!(from_str::>(j).unwrap(), map); +} + +#[test] +fn test_deny_float_key() { + #[derive(Eq, PartialEq, Ord, PartialOrd)] + struct Float; + impl Serialize for Float { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_f32(1.0) + } + } + + // map with float key + let map = treemap!(Float => "x"); + assert!(serde_json::to_value(map).is_err()); +} + +#[test] +fn test_borrowed_key() { + let map: BTreeMap<&str, ()> = from_str("{\"borrowed\":null}").unwrap(); + let expected = treemap! { "borrowed" => () }; + assert_eq!(map, expected); + + #[derive(Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq)] + struct NewtypeStr<'a>(&'a str); + + let map: BTreeMap = from_str("{\"borrowed\":null}").unwrap(); + let expected = treemap! { NewtypeStr("borrowed") => () }; + assert_eq!(map, expected); +} + +#[test] +fn test_effectively_string_keys() { + #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Serialize, Deserialize)] + enum Enum { + One, + Two, + } + let map = treemap! { + Enum::One => 1, + Enum::Two => 2 + }; + let expected = r#"{"One":1,"Two":2}"#; + test_encode_ok(&[(&map, expected)]); + test_parse_ok(vec![(expected, map)]); + + #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Serialize, Deserialize)] + struct Wrapper(String); + let map = treemap! { + Wrapper("zero".to_owned()) => 0, + Wrapper("one".to_owned()) => 1 + }; + let expected = r#"{"one":1,"zero":0}"#; + test_encode_ok(&[(&map, expected)]); + test_parse_ok(vec![(expected, map)]); +} + +#[test] +fn test_json_macro() { + // This is tricky because the <...> is not a single TT and the comma inside + // looks like an array element separator. + let _ = json!([ + as Clone>::clone(&Ok(())), + as Clone>::clone(&Err(())) + ]); + + // Same thing but in the map values. + let _ = json!({ + "ok": as Clone>::clone(&Ok(())), + "err": as Clone>::clone(&Err(())) + }); + + // It works in map keys but only if they are parenthesized. + let _ = json!({ + ( as Clone>::clone(&Ok("")).unwrap()): "ok", + ( as Clone>::clone(&Err("")).unwrap_err()): "err" + }); + + #[deny(unused_results)] + let _ = json!({ "architecture": [true, null] }); +} + +#[test] +fn issue_220() { + #[derive(Debug, PartialEq, Eq, Deserialize)] + enum E { + V(u8), + } + + assert!(from_str::(r#" "V"0 "#).is_err()); + + assert_eq!(from_str::(r#"{"V": 0}"#).unwrap(), E::V(0)); +} + +macro_rules! number_partialeq_ok { + ($($n:expr)*) => { + $( + let value = to_value($n).unwrap(); + let s = $n.to_string(); + assert_eq!(value, $n); + assert_eq!($n, value); + assert_ne!(value, s); + )* + } +} + +#[test] +fn test_partialeq_number() { + number_partialeq_ok!(0 1 100 + i8::MIN i8::MAX i16::MIN i16::MAX i32::MIN i32::MAX i64::MIN i64::MAX + u8::MIN u8::MAX u16::MIN u16::MAX u32::MIN u32::MAX u64::MIN u64::MAX + f32::MIN f32::MAX f32::MIN_EXP f32::MAX_EXP f32::MIN_POSITIVE + f64::MIN f64::MAX f64::MIN_EXP f64::MAX_EXP f64::MIN_POSITIVE + f32::consts::E f32::consts::PI f32::consts::LN_2 f32::consts::LOG2_E + f64::consts::E f64::consts::PI f64::consts::LN_2 f64::consts::LOG2_E + ); +} + +#[test] +#[cfg(integer128)] +#[cfg(feature = "arbitrary_precision")] +fn test_partialeq_integer128() { + number_partialeq_ok!(i128::MIN i128::MAX u128::MIN u128::MAX) +} + +#[test] +fn test_partialeq_string() { + let v = to_value("42").unwrap(); + assert_eq!(v, "42"); + assert_eq!("42", v); + assert_ne!(v, 42); + assert_eq!(v, String::from("42")); + assert_eq!(String::from("42"), v); +} + +#[test] +fn test_partialeq_bool() { + let v = to_value(true).unwrap(); + assert_eq!(v, true); + assert_eq!(true, v); + assert_ne!(v, false); + assert_ne!(v, "true"); + assert_ne!(v, 1); + assert_ne!(v, 0); +} + +struct FailReader(io::ErrorKind); + +impl io::Read for FailReader { + fn read(&mut self, _: &mut [u8]) -> io::Result { + Err(io::Error::new(self.0, "oh no!")) + } +} + +#[test] +fn test_category() { + assert!(from_str::("123").unwrap_err().is_data()); + + assert!(from_str::("]").unwrap_err().is_syntax()); + + assert!(from_str::("").unwrap_err().is_eof()); + assert!(from_str::("\"").unwrap_err().is_eof()); + assert!(from_str::("\"\\").unwrap_err().is_eof()); + assert!(from_str::("\"\\u").unwrap_err().is_eof()); + assert!(from_str::("\"\\u0").unwrap_err().is_eof()); + assert!(from_str::("\"\\u00").unwrap_err().is_eof()); + assert!(from_str::("\"\\u000").unwrap_err().is_eof()); + + assert!(from_str::>("[").unwrap_err().is_eof()); + assert!(from_str::>("[0").unwrap_err().is_eof()); + assert!(from_str::>("[0,").unwrap_err().is_eof()); + + assert!(from_str::>("{") + .unwrap_err() + .is_eof()); + assert!(from_str::>("{\"k\"") + .unwrap_err() + .is_eof()); + assert!(from_str::>("{\"k\":") + .unwrap_err() + .is_eof()); + assert!(from_str::>("{\"k\":0") + .unwrap_err() + .is_eof()); + assert!(from_str::>("{\"k\":0,") + .unwrap_err() + .is_eof()); + + let fail = FailReader(io::ErrorKind::NotConnected); + assert!(from_reader::<_, String>(fail).unwrap_err().is_io()); +} + +#[test] +// Clippy false positive: https://github.com/Manishearth/rust-clippy/issues/292 +#[allow(clippy::needless_lifetimes)] +fn test_into_io_error() { + fn io_error<'de, T: Deserialize<'de> + Debug>(j: &'static str) -> io::Error { + from_str::(j).unwrap_err().into() + } + + assert_eq!( + io_error::("\"\\u").kind(), + io::ErrorKind::UnexpectedEof + ); + assert_eq!(io_error::("0").kind(), io::ErrorKind::InvalidData); + assert_eq!(io_error::("]").kind(), io::ErrorKind::InvalidData); + + let fail = FailReader(io::ErrorKind::NotConnected); + let io_err: io::Error = from_reader::<_, u8>(fail).unwrap_err().into(); + assert_eq!(io_err.kind(), io::ErrorKind::NotConnected); +} + +#[test] +fn test_borrow() { + let s: &str = from_str("\"borrowed\"").unwrap(); + assert_eq!("borrowed", s); + + let s: &str = from_slice(b"\"borrowed\"").unwrap(); + assert_eq!("borrowed", s); +} + +#[test] +fn null_invalid_type() { + let err = serde_json::from_str::("null").unwrap_err(); + assert_eq!( + format!("{}", err), + String::from("invalid type: null, expected a string at line 1 column 4") + ); +} + +#[test] +fn test_integer128() { + let signed = &[i128::min_value(), -1, 0, 1, i128::max_value()]; + let unsigned = &[0, 1, u128::max_value()]; + + for integer128 in signed { + let expected = integer128.to_string(); + assert_eq!(to_string(integer128).unwrap(), expected); + assert_eq!(from_str::(&expected).unwrap(), *integer128); + } + + for integer128 in unsigned { + let expected = integer128.to_string(); + assert_eq!(to_string(integer128).unwrap(), expected); + assert_eq!(from_str::(&expected).unwrap(), *integer128); + } + + test_parse_err::(&[ + ( + "-170141183460469231731687303715884105729", + "number out of range at line 1 column 40", + ), + ( + "170141183460469231731687303715884105728", + "number out of range at line 1 column 39", + ), + ]); + + test_parse_err::(&[ + ("-1", "number out of range at line 1 column 1"), + ( + "340282366920938463463374607431768211456", + "number out of range at line 1 column 39", + ), + ]); +} + +#[test] +fn test_integer128_to_value() { + let signed = &[i128::from(i64::min_value()), i128::from(u64::max_value())]; + let unsigned = &[0, u128::from(u64::max_value())]; + + for integer128 in signed { + let expected = integer128.to_string(); + assert_eq!(to_value(integer128).unwrap().to_string(), expected); + } + + for integer128 in unsigned { + let expected = integer128.to_string(); + assert_eq!(to_value(integer128).unwrap().to_string(), expected); + } + + if !cfg!(feature = "arbitrary_precision") { + let err = to_value(u128::from(u64::max_value()) + 1).unwrap_err(); + assert_eq!(err.to_string(), "number out of range"); + } +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_borrowed_raw_value() { + #[derive(Serialize, Deserialize)] + struct Wrapper<'a> { + a: i8, + #[serde(borrow)] + b: &'a RawValue, + c: i8, + } + + let wrapper_from_str: Wrapper = + serde_json::from_str(r#"{"a": 1, "b": {"foo": 2}, "c": 3}"#).unwrap(); + assert_eq!(r#"{"foo": 2}"#, wrapper_from_str.b.get()); + + let wrapper_to_string = serde_json::to_string(&wrapper_from_str).unwrap(); + assert_eq!(r#"{"a":1,"b":{"foo": 2},"c":3}"#, wrapper_to_string); + + let wrapper_to_value = serde_json::to_value(&wrapper_from_str).unwrap(); + assert_eq!(json!({"a": 1, "b": {"foo": 2}, "c": 3}), wrapper_to_value); + + let array_from_str: Vec<&RawValue> = + serde_json::from_str(r#"["a", 42, {"foo": "bar"}, null]"#).unwrap(); + assert_eq!(r#""a""#, array_from_str[0].get()); + assert_eq!(r#"42"#, array_from_str[1].get()); + assert_eq!(r#"{"foo": "bar"}"#, array_from_str[2].get()); + assert_eq!(r#"null"#, array_from_str[3].get()); + + let array_to_string = serde_json::to_string(&array_from_str).unwrap(); + assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string); +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_raw_value_in_map_key() { + #[derive(RefCast)] + #[repr(transparent)] + struct RawMapKey(RawValue); + + impl<'de> Deserialize<'de> for &'de RawMapKey { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let raw_value = <&RawValue>::deserialize(deserializer)?; + Ok(RawMapKey::ref_cast(raw_value)) + } + } + + impl PartialEq for RawMapKey { + fn eq(&self, other: &Self) -> bool { + self.0.get() == other.0.get() + } + } + + impl Eq for RawMapKey {} + + impl Hash for RawMapKey { + fn hash(&self, hasher: &mut H) { + self.0.get().hash(hasher); + } + } + + let map_from_str: HashMap<&RawMapKey, &RawValue> = + serde_json::from_str(r#" {"\\k":"\\v"} "#).unwrap(); + let (map_k, map_v) = map_from_str.into_iter().next().unwrap(); + assert_eq!("\"\\\\k\"", map_k.0.get()); + assert_eq!("\"\\\\v\"", map_v.get()); +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_boxed_raw_value() { + #[derive(Serialize, Deserialize)] + struct Wrapper { + a: i8, + b: Box, + c: i8, + } + + let wrapper_from_str: Wrapper = + serde_json::from_str(r#"{"a": 1, "b": {"foo": 2}, "c": 3}"#).unwrap(); + assert_eq!(r#"{"foo": 2}"#, wrapper_from_str.b.get()); + + let wrapper_from_reader: Wrapper = + serde_json::from_reader(br#"{"a": 1, "b": {"foo": 2}, "c": 3}"#.as_ref()).unwrap(); + assert_eq!(r#"{"foo": 2}"#, wrapper_from_reader.b.get()); + + let wrapper_from_value: Wrapper = + serde_json::from_value(json!({"a": 1, "b": {"foo": 2}, "c": 3})).unwrap(); + assert_eq!(r#"{"foo":2}"#, wrapper_from_value.b.get()); + + let wrapper_to_string = serde_json::to_string(&wrapper_from_str).unwrap(); + assert_eq!(r#"{"a":1,"b":{"foo": 2},"c":3}"#, wrapper_to_string); + + let wrapper_to_value = serde_json::to_value(&wrapper_from_str).unwrap(); + assert_eq!(json!({"a": 1, "b": {"foo": 2}, "c": 3}), wrapper_to_value); + + let array_from_str: Vec> = + serde_json::from_str(r#"["a", 42, {"foo": "bar"}, null]"#).unwrap(); + assert_eq!(r#""a""#, array_from_str[0].get()); + assert_eq!(r#"42"#, array_from_str[1].get()); + assert_eq!(r#"{"foo": "bar"}"#, array_from_str[2].get()); + assert_eq!(r#"null"#, array_from_str[3].get()); + + let array_from_reader: Vec> = + serde_json::from_reader(br#"["a", 42, {"foo": "bar"}, null]"#.as_ref()).unwrap(); + assert_eq!(r#""a""#, array_from_reader[0].get()); + assert_eq!(r#"42"#, array_from_reader[1].get()); + assert_eq!(r#"{"foo": "bar"}"#, array_from_reader[2].get()); + assert_eq!(r#"null"#, array_from_reader[3].get()); + + let array_to_string = serde_json::to_string(&array_from_str).unwrap(); + assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string); +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_raw_invalid_utf8() { + let j = &[b'"', b'\xCE', b'\xF8', b'"']; + let value_err = serde_json::from_slice::(j).unwrap_err(); + let raw_value_err = serde_json::from_slice::>(j).unwrap_err(); + + assert_eq!( + value_err.to_string(), + "invalid unicode code point at line 1 column 4", + ); + assert_eq!( + raw_value_err.to_string(), + "invalid unicode code point at line 1 column 4", + ); +} + +#[cfg(feature = "raw_value")] +#[test] +fn test_serialize_unsized_value_to_raw_value() { + assert_eq!( + serde_json::value::to_raw_value("foobar").unwrap().get(), + r#""foobar""#, + ); +} + +#[test] +fn test_borrow_in_map_key() { + #[derive(Deserialize, Debug)] + struct Outer { + #[allow(dead_code)] + map: BTreeMap, + } + + #[derive(Ord, PartialOrd, Eq, PartialEq, Debug)] + struct MyMapKey(usize); + + impl<'de> Deserialize<'de> for MyMapKey { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + let s = <&str>::deserialize(deserializer)?; + let n = s.parse().map_err(de::Error::custom)?; + Ok(MyMapKey(n)) + } + } + + let value = json!({ "map": { "1": null } }); + Outer::deserialize(&value).unwrap(); +} + +#[test] +fn test_value_into_deserializer() { + #[derive(Deserialize)] + struct Outer { + inner: Inner, + } + + #[derive(Deserialize)] + struct Inner { + string: String, + } + + let mut map = BTreeMap::new(); + map.insert("inner", json!({ "string": "Hello World" })); + + let outer = Outer::deserialize(map.into_deserializer()).unwrap(); + assert_eq!(outer.inner.string, "Hello World"); +} + +#[test] +fn hash_positive_and_negative_zero() { + fn hash(obj: impl Hash) -> u64 { + let mut hasher = DefaultHasher::new(); + obj.hash(&mut hasher); + hasher.finish() + } + + let k1 = serde_json::from_str::("0.0").unwrap(); + let k2 = serde_json::from_str::("-0.0").unwrap(); + if cfg!(feature = "arbitrary_precision") { + assert_ne!(k1, k2); + assert_ne!(hash(k1), hash(k2)); + } else { + assert_eq!(k1, k2); + assert_eq!(hash(k1), hash(k2)); + } +} diff --git a/third_party/rust/serde_json/tests/ui/missing_colon.rs b/third_party/rust/serde_json/tests/ui/missing_colon.rs new file mode 100644 index 0000000000..d93b7b907d --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_colon.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "a" }); +} diff --git a/third_party/rust/serde_json/tests/ui/missing_colon.stderr b/third_party/rust/serde_json/tests/ui/missing_colon.stderr new file mode 100644 index 0000000000..1515211adf --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_colon.stderr @@ -0,0 +1,12 @@ +error: unexpected end of macro invocation + --> tests/ui/missing_colon.rs:4:5 + | +4 | json!({ "a" }); + | ^^^^^^^^^^^^^^ missing tokens in macro arguments + | +note: while trying to match `@` + --> src/macros.rs + | + | (@array [$($elems:expr,)*]) => { + | ^ + = note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/third_party/rust/serde_json/tests/ui/missing_comma.rs b/third_party/rust/serde_json/tests/ui/missing_comma.rs new file mode 100644 index 0000000000..8818c3e60c --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_comma.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "1": "" "2": "" }); +} diff --git a/third_party/rust/serde_json/tests/ui/missing_comma.stderr b/third_party/rust/serde_json/tests/ui/missing_comma.stderr new file mode 100644 index 0000000000..bafa0f8915 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_comma.stderr @@ -0,0 +1,13 @@ +error: no rules expected the token `"2"` + --> tests/ui/missing_comma.rs:4:21 + | +4 | json!({ "1": "" "2": "" }); + | -^^^ no rules expected this token in macro call + | | + | help: missing comma here + | +note: while trying to match `,` + --> src/macros.rs + | + | ($e:expr , $($tt:tt)*) => {}; + | ^ diff --git a/third_party/rust/serde_json/tests/ui/missing_value.rs b/third_party/rust/serde_json/tests/ui/missing_value.rs new file mode 100644 index 0000000000..0ba14e22c2 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_value.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "a" : }); +} diff --git a/third_party/rust/serde_json/tests/ui/missing_value.stderr b/third_party/rust/serde_json/tests/ui/missing_value.stderr new file mode 100644 index 0000000000..9c9de99ca9 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/missing_value.stderr @@ -0,0 +1,12 @@ +error: unexpected end of macro invocation + --> tests/ui/missing_value.rs:4:5 + | +4 | json!({ "a" : }); + | ^^^^^^^^^^^^^^^^ missing tokens in macro arguments + | +note: while trying to match `@` + --> src/macros.rs + | + | (@array [$($elems:expr,)*]) => { + | ^ + = note: this error originates in the macro `json_internal` which comes from the expansion of the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/third_party/rust/serde_json/tests/ui/not_found.rs b/third_party/rust/serde_json/tests/ui/not_found.rs new file mode 100644 index 0000000000..2df6870dc3 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/not_found.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "a" : x }); +} diff --git a/third_party/rust/serde_json/tests/ui/not_found.stderr b/third_party/rust/serde_json/tests/ui/not_found.stderr new file mode 100644 index 0000000000..6fec180409 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/not_found.stderr @@ -0,0 +1,5 @@ +error[E0425]: cannot find value `x` in this scope + --> tests/ui/not_found.rs:4:19 + | +4 | json!({ "a" : x }); + | ^ not found in this scope diff --git a/third_party/rust/serde_json/tests/ui/parse_expr.rs b/third_party/rust/serde_json/tests/ui/parse_expr.rs new file mode 100644 index 0000000000..e7f1805b1e --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/parse_expr.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "a" : ~ }); +} diff --git a/third_party/rust/serde_json/tests/ui/parse_expr.stderr b/third_party/rust/serde_json/tests/ui/parse_expr.stderr new file mode 100644 index 0000000000..cd3e1c94d1 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/parse_expr.stderr @@ -0,0 +1,11 @@ +error: no rules expected the token `~` + --> tests/ui/parse_expr.rs:4:19 + | +4 | json!({ "a" : ~ }); + | ^ no rules expected this token in macro call + | +note: while trying to match meta-variable `$e:expr` + --> src/macros.rs + | + | ($e:expr , $($tt:tt)*) => {}; + | ^^^^^^^ diff --git a/third_party/rust/serde_json/tests/ui/parse_key.rs b/third_party/rust/serde_json/tests/ui/parse_key.rs new file mode 100644 index 0000000000..858bd71692 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/parse_key.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "".s : true }); +} diff --git a/third_party/rust/serde_json/tests/ui/parse_key.stderr b/third_party/rust/serde_json/tests/ui/parse_key.stderr new file mode 100644 index 0000000000..f10c21800e --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/parse_key.stderr @@ -0,0 +1,5 @@ +error[E0609]: no field `s` on type `&'static str` + --> tests/ui/parse_key.rs:4:16 + | +4 | json!({ "".s : true }); + | ^ diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs new file mode 100644 index 0000000000..226c58cf17 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!([ true => ]); +} diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr new file mode 100644 index 0000000000..ef449f764d --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_after_array_element.stderr @@ -0,0 +1,7 @@ +error: no rules expected the token `=>` + --> tests/ui/unexpected_after_array_element.rs:4:18 + | +4 | json!([ true => ]); + | ^^ no rules expected this token in macro call + | + = note: while trying to match end of macro diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs new file mode 100644 index 0000000000..0dfb731559 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "k": true => }); +} diff --git a/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr new file mode 100644 index 0000000000..c62d90ba0d --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_after_map_entry.stderr @@ -0,0 +1,7 @@ +error: no rules expected the token `=>` + --> tests/ui/unexpected_after_map_entry.rs:4:23 + | +4 | json!({ "k": true => }); + | ^^ no rules expected this token in macro call + | + = note: while trying to match end of macro diff --git a/third_party/rust/serde_json/tests/ui/unexpected_colon.rs b/third_party/rust/serde_json/tests/ui/unexpected_colon.rs new file mode 100644 index 0000000000..e767ea6f7d --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_colon.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ : true }); +} diff --git a/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr b/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr new file mode 100644 index 0000000000..7e47726bc6 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_colon.stderr @@ -0,0 +1,7 @@ +error: no rules expected the token `:` + --> tests/ui/unexpected_colon.rs:4:13 + | +4 | json!({ : true }); + | ^ no rules expected this token in macro call + | + = note: while trying to match end of macro diff --git a/third_party/rust/serde_json/tests/ui/unexpected_comma.rs b/third_party/rust/serde_json/tests/ui/unexpected_comma.rs new file mode 100644 index 0000000000..338874ed8f --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_comma.rs @@ -0,0 +1,5 @@ +use serde_json::json; + +fn main() { + json!({ "a" , "b": true }); +} diff --git a/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr b/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr new file mode 100644 index 0000000000..552f399a50 --- /dev/null +++ b/third_party/rust/serde_json/tests/ui/unexpected_comma.stderr @@ -0,0 +1,7 @@ +error: no rules expected the token `,` + --> tests/ui/unexpected_comma.rs:4:17 + | +4 | json!({ "a" , "b": true }); + | ^ no rules expected this token in macro call + | + = note: while trying to match end of macro -- cgit v1.2.3