use rand;
use chrono::prelude::Utc;
use croaring::Bitmap;
use rand::Rng;
#[test]
fn test_andnot_bitmap() {
let bitmap: Bitmap = vec![1, 4, 7, 8].into_iter().collect();
let bitmask: Bitmap = vec![5, 6, 7, 8].into_iter().collect();
let res = bitmap.andnot(&bitmask);
assert_eq!(res.to_vec(), vec![1, 4]);
}
#[test]
fn test_and_bitmap() {
let bitmap: Bitmap = vec![1, 4, 7, 8].into_iter().collect();
let bitmask: Bitmap = vec![1, 2, 3, 4].into_iter().collect();
let res = bitmap.and(&bitmask);
assert_eq!(res.to_vec(), vec![1, 4]);
}
#[test]
fn test_flip_bitmap() {
let bitmap: Bitmap = vec![1, 2, 4].into_iter().collect();
let res = bitmap.flip(2..4);
assert_eq!(res.to_vec(), vec![1, 3, 4]);
}
#[test]
fn test_a_small_bitmap() {
let bitmap: Bitmap = vec![1, 99, 1_000].into_iter().collect();
let serialized_buffer = bitmap.serialize();
assert_eq!(serialized_buffer.len(), 22);
}
#[test]
fn test_1000_inputs() {
let mut rng = rand::thread_rng();
let mut bitmap = Bitmap::create();
for _ in 1..1_000 {
let n = rng.gen_range(0, 1_000_000);
bitmap.add(n);
}
let serialized_buffer = bitmap.serialize();
println!(
"bitmap with 1,000 (out of 1,000,000) values in it: {}",
serialized_buffer.len()
);
bitmap.run_optimize();
let serialized_buffer = bitmap.serialize();
println!(
"bitmap with 1,000 (out of 1,000,000) values in it (optimized): {}",
serialized_buffer.len()
);
}
#[test]
fn test_a_big_bitmap() {
let mut bitmap: Bitmap = (1..1_000_000).collect();
let serialized_buffer = bitmap.serialize();
assert_eq!(serialized_buffer.len(), 131_208);
assert!(bitmap.run_optimize());
let serialized_buffer = bitmap.serialize();
assert_eq!(serialized_buffer.len(), 230);
}
#[ignore]
#[test]
fn bench_fast_or() {
let nano_to_millis = 1.0 / 1_000_000.0;
let bitmaps_number = 256;
let size_of_each_bitmap = 1_000;
let init_bitmaps = || -> Vec<Bitmap> {
let mut rng = rand::thread_rng();
let mut bitmaps = vec![];
for _ in 0..bitmaps_number {
let mut bitmap = Bitmap::create();
for _ in 0..size_of_each_bitmap {
let n = rng.gen_range(0, 1_000_000);
bitmap.add(n);
}
bitmaps.push(bitmap);
}
bitmaps
};
let mut bitmaps = init_bitmaps();
let mut bitmap = Bitmap::create();
let start = Utc::now().timestamp_nanos();
for _ in 0..bitmaps_number {
bitmap.or_inplace(&bitmaps.pop().unwrap());
}
let fin = Utc::now().timestamp_nanos();
let dur_ms = (fin - start) as f64 * nano_to_millis;
println!(
" or_inplace(): {:9.3?}ms. bitmap cardinality: {}",
dur_ms,
bitmap.cardinality()
);
let bitmaps = init_bitmaps();
let start = Utc::now().timestamp_nanos();
let bitmap = Bitmap::fast_or(&bitmaps.iter().map(|x| x).collect::<Vec<&Bitmap>>());
let fin = Utc::now().timestamp_nanos();
let dur_ms = (fin - start) as f64 * nano_to_millis;
println!(
" fast_or(): {:9.3?}ms. bitmap cardinality: {}",
dur_ms,
bitmap.cardinality()
);
let bitmaps = init_bitmaps();
let start = Utc::now().timestamp_nanos();
let bitmap = Bitmap::fast_or_heap(&bitmaps.iter().map(|x| x).collect::<Vec<&Bitmap>>());
let fin = Utc::now().timestamp_nanos();
let dur_ms = (fin - start) as f64 * nano_to_millis;
println!(
"fast_or_heap(): {:9.3?}ms. bitmap cardinality: {}",
dur_ms,
bitmap.cardinality()
);
}