use std::collections::BTreeMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use coip_builder::{BuildOptions, CoipBuilder};
use coip_format::{Codec, Value};
use coip_reader::Reader;
#[test]
fn test_all_primitives_empty_maps_dynamic_and_lists() {
let mut b = CoipBuilder::new(BuildOptions::default());
b.set_build_epoch(1_700_000_000);
let mut expected: Vec<(Ipv4Addr, BTreeMap<String, Value>)> = Vec::new();
for i in 0u32..40 {
let mut m = BTreeMap::new();
m.insert("u8_v".into(), Value::U8((i % 250) as u8));
m.insert("u16_v".into(), Value::U16((i * 100) as u16));
m.insert("u32_v".into(), Value::U32(i * 100_000));
m.insert("u64_v".into(), Value::U64((i as u64) * 1_000_000_000));
m.insert(
"u128_v".into(),
Value::U128((i as u128) << 80 | (i as u128)),
);
m.insert("i32_v".into(), Value::I32((i as i32) - 20));
m.insert("i64_v".into(), Value::I64((i as i64) * -10_000_000_000));
m.insert(
"f32_v".into(),
Value::F32(if i == 0 { -0.0f32 } else { (i as f32) * 0.25 }),
);
m.insert(
"f64_v".into(),
Value::F64(if i == 0 {
-0.0f64
} else {
37.1234567 + (i as f64)
}),
);
m.insert("bytes_v".into(), Value::Bytes(vec![0xDE, 0xAD, i as u8]));
m.insert("bool_v".into(), Value::Bool(i % 2 == 0));
if i % 3 == 0 {
m.insert("opt_struct".into(), Value::Map(BTreeMap::new()));
} else if i % 3 == 1 {
let mut sub = BTreeMap::new();
sub.insert("k".into(), Value::Utf8(format!("v{i}")));
m.insert("opt_struct".into(), Value::Map(sub));
}
if i % 2 == 0 {
m.insert("mixed".into(), Value::Utf8(format!("str-{i}")));
} else {
m.insert("mixed".into(), Value::U32(i));
}
let mut sub1 = BTreeMap::new();
sub1.insert("code".into(), Value::Utf8(format!("S{}", i % 3)));
sub1.insert("id".into(), Value::U32(i % 3));
m.insert("low_list".into(), Value::Array(vec![Value::Map(sub1)]));
m.insert(
"high_list".into(),
Value::Array(vec![Value::U32(i * 10), Value::U32(i * 10 + 1)]),
);
let mut big_map = BTreeMap::new();
for k in 0..35 {
if k < 30 || i == 0 {
big_map.insert(format!("k{k:02}"), Value::Utf8(format!("val-{k}-{i}")));
}
}
m.insert("big_map".into(), Value::Map(big_map));
let ip = Ipv4Addr::new(192, 0, 2, i as u8);
b.insert_ipv4_cidr(ip, 32, m.clone()).unwrap();
expected.push((ip, m));
}
let (bytes, _) = b.build().unwrap();
let reader = Reader::from_bytes(&bytes).unwrap();
reader.verify_checksums().unwrap();
for (ip, exp) in &expected {
let got = reader.lookup_value(IpAddr::V4(*ip)).unwrap().unwrap();
assert!(
got.bit_eq(&Value::Map(exp.clone())),
"mismatch for {ip}:\n exp={exp:?}\n got={got:?}"
);
}
let tmp = std::env::temp_dir().join("coip_test_edge_cases.coi");
std::fs::write(&tmp, &bytes).unwrap();
let file_reader = Reader::open_file(&tmp).unwrap();
for (ip, exp) in &expected {
let got = file_reader.lookup_value(IpAddr::V4(*ip)).unwrap().unwrap();
assert!(got.bit_eq(&Value::Map(exp.clone())));
}
let _ = std::fs::remove_file(&tmp);
}
#[test]
fn test_large_front_coded_flat_dict_and_uncompressed_profile() {
let mut b = CoipBuilder::new(BuildOptions {
profile_codec: Codec::None,
row_group_size: 1024,
dict_block_size: 2048,
..BuildOptions::default()
});
for idx in 0u32..27_000 {
let tag_id = idx % 9_000;
let mut m = BTreeMap::new();
m.insert(
"org".into(),
Value::Utf8(format!("Autonomous-System-Network-{tag_id:05}")),
);
m.insert("asn".into(), Value::U32(idx));
let ip = Ipv4Addr::from(0x0A00_0000u32 + idx);
b.insert_ipv4_cidr(ip, 32, m).unwrap();
}
let (bytes, _) = b.build().unwrap();
let reader = Reader::from_bytes(&bytes).unwrap();
reader.verify_checksums().unwrap();
for probe in [0u32, 1, 2047, 2048, 8999, 9000, 18_500, 26_999] {
let ip = Ipv4Addr::from(0x0A00_0000u32 + probe);
let got = reader.lookup_value(IpAddr::V4(ip)).unwrap().unwrap();
let map = got.as_map().unwrap();
assert_eq!(
map.get("org"),
Some(&Value::Utf8(format!(
"Autonomous-System-Network-{:05}",
probe % 9_000
)))
);
assert_eq!(map.get("asn"), Some(&Value::U32(probe)));
}
}
#[test]
fn test_ipv6_host_ext_overrides_and_boundaries() {
let mut b = CoipBuilder::new(BuildOptions::default());
let mut r1 = BTreeMap::new();
r1.insert("tag".into(), Value::Utf8("base_slash48".into()));
let mut r2 = BTreeMap::new();
r2.insert("tag".into(), Value::Utf8("host_slash128".into()));
b.insert_ipv6_cidr("2001:db8:1::".parse::<Ipv6Addr>().unwrap(), 48, r1.clone())
.unwrap();
b.insert_ipv6_cidr(
"2001:db8:2::42".parse::<Ipv6Addr>().unwrap(),
128,
r2.clone(),
)
.unwrap();
let (bytes, _) = b.build().unwrap();
let reader = Reader::from_bytes(&bytes).unwrap();
assert_eq!(
reader
.lookup_value(IpAddr::V6("2001:db8:1:ffff::abcd".parse().unwrap()))
.unwrap(),
Some(Value::Map(r1))
);
assert_eq!(
reader
.lookup_value(IpAddr::V6("2001:db8:2::42".parse().unwrap()))
.unwrap(),
Some(Value::Map(r2))
);
assert_eq!(
reader
.lookup_value(IpAddr::V6("2001:db8:2::43".parse().unwrap()))
.unwrap(),
None
);
}
#[test]
fn test_ultrafast_lz4_zstd_dicts_new_rid_modes_direct_serde_and_concurrency() {
use std::sync::Arc;
let build_dataset = |opts: BuildOptions| -> Vec<u8> {
let mut b = CoipBuilder::new(opts);
b.set_build_epoch(1_750_000_000);
for idx in 0u32..12_000 {
let rec_kind = idx % 400;
let mut m = BTreeMap::new();
let mut country = BTreeMap::new();
country.insert(
"iso_code".into(),
Value::Utf8(if rec_kind % 2 == 0 {
"US".into()
} else {
"JP".into()
}),
);
country.insert("geoname_id".into(), Value::U32(1000 + (rec_kind % 20)));
let mut names = BTreeMap::new();
names.insert(
"en".into(),
Value::Utf8(format!("Country-{}", rec_kind % 20)),
);
names.insert(
"zh-CN".into(),
Value::Utf8(format!("国家-{}", rec_kind % 20)),
);
names.insert("de".into(), Value::Utf8(format!("Land-{}", rec_kind % 20)));
names.insert("ja".into(), Value::Utf8(format!("国-{}", rec_kind % 20)));
country.insert("names".into(), Value::Map(names));
m.insert("country".into(), Value::Map(country));
let mut loc = BTreeMap::new();
loc.insert(
"latitude".into(),
Value::F64(35.0 + (rec_kind as f64) * 0.01),
);
loc.insert(
"longitude".into(),
Value::F64(139.0 - (rec_kind as f64) * 0.01),
);
loc.insert(
"accuracy_radius".into(),
Value::U16((rec_kind % 50) as u16 + 1),
);
loc.insert("time_zone".into(), Value::Utf8("Asia/Tokyo".into()));
m.insert("location".into(), Value::Map(loc));
let start = 0x0B00_0000u32 + idx * 4;
b.insert_ipv4_cidr(Ipv4Addr::from(start), 31, m).unwrap();
}
b.build().unwrap().0
};
#[derive(Debug, serde::Deserialize, PartialEq)]
struct DirectGeo {
country: Option<DirectCountry>,
location: Option<DirectLoc>,
}
#[derive(Debug, serde::Deserialize, PartialEq)]
struct DirectCountry {
iso_code: Option<String>,
names: Option<BTreeMap<String, String>>,
}
#[derive(Debug, serde::Deserialize, PartialEq)]
struct DirectLoc {
latitude: Option<f64>,
longitude: Option<f64>,
accuracy_radius: Option<u16>,
time_zone: Option<String>,
}
let compact_bytes = build_dataset(BuildOptions {
index_block_size: 256,
train_zstd_dicts: true,
zstd_level: 3,
..BuildOptions::compact()
});
let ultrafast_bytes = build_dataset(BuildOptions::ultrafast());
for bytes in [compact_bytes, ultrafast_bytes] {
let reader = Arc::new(Reader::from_vec(bytes).unwrap());
reader.verify_checksums().unwrap();
let proj = Arc::new(
reader
.compile_projection([
"country.iso_code",
"country.names.zh-CN",
"location.latitude",
"location.longitude",
"location.accuracy_radius",
"location.time_zone",
])
.unwrap(),
);
let handles: Vec<_> = (0..4)
.map(|tid| {
let r = Arc::clone(&reader);
let p = Arc::clone(&proj);
std::thread::spawn(move || {
for idx in (tid * 500..tid * 500 + 500).map(|x| x as u32) {
let ip = IpAddr::V4(Ipv4Addr::from(0x0B00_0000u32 + idx * 4));
let gap_ip = IpAddr::V4(Ipv4Addr::from(0x0B00_0000u32 + idx * 4 + 2));
assert_eq!(r.lookup_record_id(gap_ip).unwrap(), 0);
let got: DirectGeo = r.lookup_with_projection(ip, &p).unwrap().unwrap();
let rk = idx % 400;
let exp_iso = if rk % 2 == 0 { "US" } else { "JP" };
let country = got.country.unwrap();
assert_eq!(country.iso_code.as_deref(), Some(exp_iso));
assert_eq!(
country.names.unwrap().get("zh-CN").map(String::as_str),
Some(format!("国家-{}", rk % 20).as_str())
);
let loc = got.location.unwrap();
assert_eq!(loc.latitude, Some(35.0 + (rk as f64) * 0.01));
assert_eq!(loc.longitude, Some(139.0 - (rk as f64) * 0.01));
assert_eq!(loc.accuracy_radius, Some((rk % 50) as u16 + 1));
assert_eq!(loc.time_zone.as_deref(), Some("Asia/Tokyo"));
}
})
})
.collect();
for h in handles {
h.join().unwrap();
}
}
}