Skip to content

Commit 9da84a2

Browse files
committed
more tests
1 parent e3a95a8 commit 9da84a2

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

crates/batson/tests/main.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fs::File;
22
use std::io::Read;
3+
use std::path::PathBuf;
34
use std::sync::Arc;
45

56
use jiter::{JsonValue, LazyIndexMap};
@@ -213,11 +214,11 @@ macro_rules! json_round_trip_tests {
213214
}
214215

215216
json_round_trip_tests!(
216-
array_empty => r"[]";
217-
array_bool => r"[true,false]";
218-
array_bool_int => r"[true,123]";
219-
array_u8 => r"[1,2,44,255]";
220-
array_i64 => r"[-1,2,44,255,1234]";
217+
array_empty => "[]";
218+
array_bool => "[true,false]";
219+
array_bool_int => "[true,123]";
220+
array_u8 => "[1,2,44,255]";
221+
array_i64 => "[-1,2,44,255,1234]";
221222
array_header => r#"[6,true,false,null,0,[],{},""]"#;
222223
array_het => r#"[true,123,"foo",null]"#;
223224
string_empty => r#""""#;
@@ -234,14 +235,19 @@ json_round_trip_tests!(
234235
object_nested => r#"{"foo":{"bar":true}}"#;
235236
object_nested_array => r#"{"foo":{"bar":[1,2]}}"#;
236237
object_nested_array_nested => r#"{"foo":{"bar":[{"baz":true}]}}"#;
237-
float_zero => r#"0.0"#;
238-
float_neg => r#"-123.45"#;
239-
float_pos => r#"123.456789"#;
238+
float_zero => "0.0";
239+
float_neg => "-123.45";
240+
float_pos => "123.456789";
241+
float_zero_to_10 => "[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0]"; // header only
242+
float_zero_to_12 => "[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]"; // het array
243+
int_zero_to_10 => "[0,1,2,3,4,5,6,7,8,9,10]";
244+
int_zero_to_10_neg => "[0,1,2,3,4,5,6,7,8,9,10,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12]";
245+
array_len_0_to_10 => "[[],[0],[0,1],[0,1,2],[0,1,2,3],[0,1,2,3,4],[0,1,2,3,4,5],[0,1,2,3,4,5,6],[0,1,2,3,4,5,6,7],[0,1,2,3,4,5,6,7,8],[0,1,2,3,4,5,6,7,8,9],[0,1,2,3,4,5,6,7,8,9,10]]";
240246
// bigger than i64::MAX (9223372036854775807)
241-
big_int_pos => r#"92233720368547758070"#;
247+
big_int_pos => "92233720368547758070";
242248
// less than i64::MIN (-9223372036854775808)
243-
big_int_neg => r#"-92233720368547758080"#;
244-
big_int_array => r#"[92233720368547758070,-92233720368547758080,1,2,3,-42]"#;
249+
big_int_neg => "-92233720368547758080";
250+
big_int_array => "[92233720368547758070,-92233720368547758080,1,2,3,-42]";
245251
big_int_object => r#"{"foo":92233720368547758070,"bar":-92233720368547758080}"#;
246252
);
247253

@@ -278,3 +284,33 @@ fn batson_file() {
278284
// dbg!(contents.len());
279285
// dbg!(json.replace(" ", "").replace("\n", "").len());
280286
}
287+
288+
fn read_file(path: &PathBuf) -> Vec<u8> {
289+
let mut file = File::open(path).unwrap();
290+
let mut contents = Vec::new();
291+
file.read_to_end(&mut contents).unwrap();
292+
contents
293+
}
294+
295+
/// Round trip test all the JSON files in the jiter benches directory
296+
#[test]
297+
fn round_trip_json_files() {
298+
let dir = std::fs::read_dir("../jiter/benches").unwrap();
299+
for file in dir.map(|r| r.unwrap()) {
300+
let path = file.path();
301+
if !path.extension().map(|e| e == "json").unwrap_or(false) {
302+
continue;
303+
}
304+
println!("Testing: {path:?}");
305+
306+
let json = read_file(&path);
307+
let value_from_json = JsonValue::parse(&json, false).unwrap();
308+
309+
let bytes = json_to_batson(&json);
310+
let value_from_batson = decode_to_json_value(&bytes).unwrap();
311+
assert!(
312+
compare_json_values(&value_from_json, &value_from_batson),
313+
"Failed for {path:?}"
314+
);
315+
}
316+
}

0 commit comments

Comments
 (0)