use sparsemap::SparseMap;
use std::collections::BTreeSet;
fn check(bytes: &[u8], expected: &BTreeSet<u64>) {
let m = SparseMap::from_bytes(bytes).expect("Rust must deserialize C output");
let got: BTreeSet<u64> = m.iter().collect();
assert_eq!(&got, expected, "Rust read of C bytes diverged");
let re = m.to_bytes().unwrap();
let back = SparseMap::from_bytes(&re).unwrap();
assert_eq!(back, m, "Rust re-encode round-trip diverged");
}
fn set(iter: impl IntoIterator<Item = u64>) -> BTreeSet<u64> {
iter.into_iter().collect()
}
const EMPTY: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
const SINGLE: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 4, 0, 0,
];
const SCATTERED: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,
0, 128, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 8, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 128, 1,
0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 1, 0, 0, 0,
];
const RUN_5000: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 136, 19, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 136, 19, 0, 0,
0, 12, 0, 64,
];
const RUN_4WINDOWS: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0,
16, 0, 64,
];
const TWO_CLUSTERS: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 150, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0,
0, 0, 0, 255, 255, 255, 255, 15, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 255, 255,
255, 255, 255, 255, 3, 0, 0, 0, 0, 0, 0, 0,
];
const OFFSET_RUN: &[u8] = &[
115, 109, 49, 48, 1, 1, 0, 0, 112, 23, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128,
255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 255, 255, 0, 8, 0, 0, 88, 19, 0, 0, 0, 12, 0, 64,
];
#[test]
fn c_empty() {
let m = SparseMap::from_bytes(EMPTY).expect("empty deserializes");
assert!(m.is_empty());
}
#[test]
fn c_single() {
check(SINGLE, &set([42]));
}
#[test]
fn c_scattered() {
check(SCATTERED, &set([1, 2, 3, 2047, 2048, 4096, 100_000]));
}
#[test]
fn c_run_5000() {
check(RUN_5000, &set(0..5000));
}
#[test]
fn c_run_4windows() {
check(RUN_4WINDOWS, &set(0..8192));
}
#[test]
fn c_two_clusters() {
check(TWO_CLUSTERS, &set((0..100).chain(10_000..10_050)));
}
#[test]
fn c_offset_run() {
check(OFFSET_RUN, &set(1000..7000));
}