#![allow(
clippy::expect_used,
reason = "a broken test setup should fail the test loudly"
)]
use std::ffi::OsString;
use std::fmt::Write;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard};
use jsonvet_cli::Terminal;
#[global_allocator]
static ALLOCATOR: dhat::Alloc = dhat::Alloc;
const ROWS: usize = 100_000;
static COUNTING: Mutex<()> = Mutex::new(());
fn counting() -> MutexGuard<'static, ()> {
COUNTING
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn folder(name: &str) -> PathBuf {
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("allocations-{name}"));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(dir.join(".git")).expect("create the directory");
dir
}
fn rows(
count: usize,
open: &str,
between: &str,
close: &str,
row: impl Fn(&mut String, usize),
) -> String {
let mut text = String::from(open);
for i in 0..count {
if i > 0 {
text.push_str(between);
}
row(&mut text, i);
}
text.push_str(close);
text
}
fn heap(args: Vec<OsString>) -> (dhat::HeapStats, Result<u8, String>, String) {
let profiler = dhat::Profiler::builder().testing().build();
let (code, out) = std::thread::Builder::new()
.stack_size(64 << 20)
.spawn(move || {
let mut out = Vec::new();
let code = jsonvet_cli::run_with(args, false, Terminal::default(), &mut out);
(code, out)
})
.expect("a thread")
.join()
.expect("no panic");
let stats = dhat::HeapStats::get();
drop(profiler);
eprintln!(
"allocated {} blocks, {} bytes, peak {}",
stats.total_blocks, stats.total_bytes, stats.max_bytes
);
(stats, code, String::from_utf8_lossy(&out).into_owned())
}
fn assert_under(blocks: u64, max: u64) {
assert!(blocks <= max, "allocated {blocks} blocks, over {max}");
}
#[test]
fn fmt_allocates_a_fixed_number_of_blocks() {
let _counting = counting();
let dir = folder("fmt");
let text = rows(ROWS, "[\n", ",\n", "\n]\n", |text, i| {
write!(
text,
"{{\"x\":{i},\"y\":[{i},\"s\",{{\"a\":[1,2,3]}}],\"t\":\"text\",\"u\":[[{i}],[{i}]]}}"
)
.expect("write to a string");
});
let file = dir.join("rows.jsonc");
fs::write(&file, text).expect("write the file");
let (stats, code, out) = heap(vec![
OsString::from("fmt"),
OsString::from("--no-config"),
OsString::from("--check"),
file.into_os_string(),
]);
assert_eq!(code, Ok(1), "{out}");
assert_under(stats.total_blocks, 1_000);
assert!(
stats.max_bytes <= 67_900_000,
"peaked at {} bytes, over 67,900,000: is the member stack freed after parsing?",
stats.max_bytes
);
}
#[test]
fn schema_on_json_lines_allocates_a_fixed_number_of_blocks() {
let _counting = counting();
let dir = folder("lines");
let text = rows(ROWS, "", "\n", "\n", |text, i| {
write!(text, "{{\"x\": {i}, \"y\": [{i}, \"s\"], \"t\": \"text\"}}")
.expect("write to a string");
});
let file = dir.join("file.jsonl");
fs::write(&file, text).expect("write the file");
let schema = dir.join("schema.json");
fs::write(
&schema,
r#"{"type": "object", "required": ["x"], "properties": {"y": {"type": "array"}}}"#,
)
.expect("write the schema");
let (stats, code, out) = heap(vec![
OsString::from("--no-config"),
OsString::from("--schema"),
schema.into_os_string(),
file.into_os_string(),
]);
assert_eq!(code, Ok(0), "{out}");
assert_under(stats.total_blocks, 20_000);
}
fn fix_stats(name: &str, count: usize) -> dhat::HeapStats {
let dir = folder(name);
let text = rows(count, "[\n", ",\n", "\n]\n", |text, i| {
write!(
text,
"{{\"x\": {i}, \"y\": [{i}, \"s\"], \"t\": \"text\",}}"
)
.expect("write to a string");
});
let file = dir.join("rows.json");
fs::write(&file, text).expect("write the file");
let (stats, code, out) = heap(vec![
OsString::from("--no-config"),
OsString::from("--dialect"),
OsString::from("json"),
OsString::from("--fix"),
file.clone().into_os_string(),
]);
assert_eq!(code, Ok(0), "{out}");
assert!(!fs::read_to_string(&file).expect("read").contains(",}"));
stats
}
#[test]
fn fix_allocates_two_blocks_a_comma() {
let _counting = counting();
let full = fix_stats("fix", ROWS);
let half = fix_stats("fix-half", ROWS / 2);
let extra = (ROWS - ROWS / 2) as u64;
let blocks = full.total_blocks.saturating_sub(half.total_blocks);
assert!(
blocks <= 2 * extra + 100,
"allocated {blocks} blocks for {extra} more commas ({} at {ROWS} rows, {} at {}), over 2 per comma",
full.total_blocks,
half.total_blocks,
ROWS / 2,
);
assert!(
full.total_bytes <= 800 * ROWS as u64,
"allocated {} bytes, over 800 per comma",
full.total_bytes
);
}