#[macro_use] extern crate compacts; extern crate snap; extern crate zstd; use compacts::bits::{PopCount, Set}; use std::{fs, io}; // https://github.com/RoaringBitmap/RoaringFormatSpec #[test] fn set_and() { let xs = &bitset!(16, 20); let ys = &bitset!(10, 11, 20); let zs = &bitset!(30, 40); assert_eq!(xs.and(ys).and(zs).bits().count(), 0); assert_eq!(xs.and(ys.and(zs)).bits().count(), 0); assert_eq!(zs.and(ys).and(xs).bits().count(), 0); assert_eq!(zs.and(ys.and(xs)).bits().count(), 0); } #[test] fn set_or() { let xs = &bitset!(16, 20); let ys = &bitset!(10, 11, 20); let zs = &bitset!(10, 12, 20); assert_eq!(xs.or(ys).or(zs).bits().count(), 5); assert_eq!(xs.or(ys.or(zs)).bits().count(), 5); assert_eq!(zs.or(ys).or(xs).bits().count(), 5); assert_eq!(zs.or(ys.or(xs)).bits().count(), 5); } #[test] fn set_and_not() { let xs = &bitset!(10, 11, 12, 16, 20); let ys = &bitset!(10, 11, 20); assert_eq!(xs.and_not(ys).bits().count(), 2); } #[test] fn set_xor() { let xs = &bitset!(10, 11, 12, 16, 20); let ys = &bitset!(10, 11, 20, 26, 30); assert_eq!(xs.xor(ys).bits().count(), 4); } #[test] fn set_file_io() { let m1 = bitsetwithruns(); let m2 = bitsetwithoutruns(); for i in 0..100_000 { if i % 1000 == 0 { assert!(m1[i] && m2[i]); } else { assert!(!m1[i] && !m2[i]); } } for i in 100_000..200_000 { assert!(m1[i * 3] && m2[i * 3]); } for i in 700_000..800_000 { assert!(m1[i] && m2[i]); } assert_eq!(m1.count1(), m2.count1()); assert_eq!(m1.count0(), m2.count0()); for (b1, b2) in m1.bits().zip(m2.bits()) { assert_eq!(b1, b2); } let body = { let mut file = fs::File::open("./tests/bitmapwithruns.bin").unwrap(); let mut body = Vec::with_capacity(0x2000); io::copy(&mut file, &mut body).unwrap(); body }; let buff = { let mut buff = Vec::with_capacity(0); { m1.write_to(&mut buff).unwrap(); } println!("no-compress {}", buff.len()); buff }; assert_eq!(body, buff); } #[test] fn set_snappy() { use std::io::Write; let m1 = bitsetwithruns(); let mut w = Vec::with_capacity(0x2000); { let mut buf = snap::Writer::new(&mut w); m1.write_to(&mut buf).unwrap(); buf.flush().unwrap(); } println!("snappy {}", w.len()); let mut r = snap::Reader::new(io::Cursor::new(w)); let m2 = Set::read_from(&mut r).unwrap(); assert_eq!(m1.count1(), m2.count1()); assert_eq!(m1.count0(), m2.count0()); for (b1, b2) in m1.bits().zip(m2.bits()) { assert_eq!(b1, b2); } } #[test] fn set_zstd() { let m1 = bitsetwithruns(); let mut w = Vec::with_capacity(0x2000); { let mut enc = zstd::Encoder::new(&mut w, 0).unwrap(); m1.write_to(&mut enc).unwrap(); enc.finish().unwrap(); } println!("zstd {}", w.len()); let mut r = zstd::Decoder::new(io::Cursor::new(w)).unwrap(); let m2 = Set::read_from(&mut r).unwrap(); assert_eq!(m1.count1(), m2.count1()); assert_eq!(m1.count0(), m2.count0()); for (b1, b2) in m1.bits().zip(m2.bits()) { assert_eq!(b1, b2); } } fn must_open_bitset(p: &str) -> Set { let file = fs::File::open(p).unwrap(); let mut bits = Set::read_from(&mut io::BufReader::new(file)).unwrap(); bits.shrink(); bits } fn bitsetwithruns() -> Set { must_open_bitset("./tests/bitmapwithruns.bin") } fn bitsetwithoutruns() -> Set { must_open_bitset("./tests/bitmapwithoutruns.bin") }