#![cfg(not(miri))]
mod common;
use anyhow::Result;
use webgraph_cli::cli_main;
#[test]
fn test_build_ef_missing_nodes_property() -> Result<()> {
let tmp = tempfile::tempdir()?;
let basename = tmp.path().join("bad");
std::fs::write(basename.with_extension("properties"), "endianness = big\n")?;
std::fs::write(basename.with_extension("graph"), b"")?;
std::fs::write(basename.with_extension("offsets"), b"")?;
let base = basename.display().to_string();
let res = cli_main(vec!["webgraph", "build", "ef", &base]);
assert!(res.is_err(), "expected error, got {res:?}");
Ok(())
}
#[test]
fn test_unknown_endianness_is_an_error() -> Result<()> {
let tmp = tempfile::tempdir()?;
let basename = tmp.path().join("weird");
std::fs::write(
basename.with_extension("properties"),
"endianness = middle\nnodes = 1\narcs = 0\n",
)?;
std::fs::write(basename.with_extension("graph"), b"")?;
let base = basename.display().to_string();
let res = cli_main(vec!["webgraph", "build", "offsets", &base]);
assert!(res.is_err(), "expected error, got {res:?}");
Ok(())
}
#[test]
fn test_from_arcs_all_malformed_is_an_error() -> Result<()> {
use clap::Parser;
let tmp = tempfile::tempdir()?;
let dst = tmp.path().join("out").display().to_string();
let args =
webgraph_cli::from::arcs::CliArgs::try_parse_from(["arcs", "--num-nodes", "3", &dst])?;
let res = webgraph_cli::from::arcs::from_csv(args, std::io::Cursor::new("0 1\n1 2\n"));
assert!(res.is_err(), "expected error, got {res:?}");
Ok(())
}
#[test]
fn test_from_arcs_lines_to_skip_ignores_comments() -> Result<()> {
use clap::Parser;
let tmp = tempfile::tempdir()?;
let dst = tmp.path().join("skip").display().to_string();
let args = webgraph_cli::from::arcs::CliArgs::try_parse_from([
"arcs",
"--num-nodes",
"3",
"--lines-to-skip",
"1",
&dst,
])?;
webgraph_cli::from::arcs::from_csv(args, std::io::Cursor::new("# header\n0\t1\n1\t2\n"))?;
let props = std::fs::read_to_string(format!("{dst}.properties"))?;
assert!(props.contains("arcs=1"), "properties: {props}");
Ok(())
}
#[test]
fn test_perm_comp_rejects_non_permutations() -> Result<()> {
let tmp = tempfile::tempdir()?;
let good = tmp.path().join("good.txt");
let out_of_range = tmp.path().join("oor.txt");
let duplicated = tmp.path().join("dup.txt");
let dst = tmp.path().join("out.txt");
std::fs::write(&good, "1\n0\n")?;
std::fs::write(&out_of_range, "0\n2\n")?;
std::fs::write(&duplicated, "0\n0\n")?;
let (good, out_of_range, duplicated, dst) = (
good.display().to_string(),
out_of_range.display().to_string(),
duplicated.display().to_string(),
dst.display().to_string(),
);
let res = cli_main(vec!["webgraph", "perm", "comp", &dst, &good, &out_of_range]);
assert!(res.is_err(), "out-of-range accepted: {res:?}");
let res = cli_main(vec!["webgraph", "perm", "comp", &dst, &good, &duplicated]);
assert!(res.is_err(), "duplicate accepted: {res:?}");
cli_main(vec![
"webgraph",
"perm",
"comp",
"--no-check",
&dst,
&good,
&duplicated,
])?;
cli_main(vec!["webgraph", "perm", "comp", &dst, &good, &good])?;
let composed = std::fs::read_to_string(&dst)?;
assert_eq!(composed.trim(), "0\n1");
Ok(())
}
#[test]
fn test_max_ref_count_validation() {
use clap::Parser;
assert!(
webgraph_cli::from::arcs::CliArgs::try_parse_from([
"arcs",
"--num-nodes",
"3",
"--max-ref-count",
"-2",
"out",
])
.is_err()
);
let args = webgraph_cli::from::arcs::CliArgs::try_parse_from([
"arcs",
"--num-nodes",
"3",
"--max-ref-count",
"-1",
"out",
])
.unwrap();
assert_eq!(args.ca.max_ref_count, -1);
let flags: webgraph::prelude::CompFlags = args.ca.into();
assert_eq!(flags.max_ref_count, usize::MAX);
}
#[test]
fn test_run_llp_empty_graph() -> Result<()> {
use dsi_bitstream::prelude::BE;
use webgraph::graphs::vec_graph::VecGraph;
let tmp = tempfile::tempdir()?;
let base = tmp.path().join("empty").display().to_string();
common::compress_with_ef::<BE>(&VecGraph::empty(0), &base)?;
cli_main(vec!["webgraph", "build", "dcf", &base])?;
let perm = tmp.path().join("empty.perm").display().to_string();
cli_main(vec!["webgraph", "run", "llp", &base, &perm])?;
assert_eq!(std::fs::read_to_string(&perm)?.trim(), "");
Ok(())
}