// The containers switch between list and bitmap at 4,096 values, and the // set operations agree with naive ones on random sets. import bitmap import gen import gen/check import std/list import std/random.{type random} import std/test.{assert, assert_equal} @test fn an_empty_bitmap_has_nothing :: { let b = bitmap.empty () assert_equal (bitmap.size b) 0 assert (bitmap.is_empty b) "empty" assert_equal (bitmap.to_list b) [] assert (!bitmap.contains b 0) "contains nothing" } @test fn added_ids_are_contained_in_order :: { let b = bitmap.from_list [5, 1, 5, 70000, 3] assert_equal (bitmap.to_list b) [1, 3, 5, 70000] assert_equal (bitmap.size b) 4 assert (bitmap.contains b 70000) "70000" assert (!bitmap.contains b 69999) "not 69999" // Two high bits: two sparse containers. assert_equal (bitmap.stats b) (2, 0) } @test fn a_container_becomes_a_bitmap_past_4096 :: { let b = bitmap.from_list (list.range 0 4095) assert_equal (bitmap.stats b) (1, 0) let b = bitmap.add b 4096 assert_equal (bitmap.stats b) (0, 1) assert_equal (bitmap.size b) 4097 assert (bitmap.contains b 4096) "4096" let b = bitmap.add b 5000 assert_equal (bitmap.to_list b) (list.range 0 4096 <> [5000]) } @test fn a_bitmap_drops_back_to_a_list_at_4096 :: { let b = bitmap.from_list (list.range 0 4100) assert_equal (bitmap.stats b) (0, 1) let b = list.fold [4100, 4099, 4098, 4097, 4096] b fn b id { bitmap.remove b id } assert_equal (bitmap.stats b) (1, 0) assert_equal (bitmap.to_list b) (list.range 0 4095) } @test fn removing_the_last_value_drops_the_container :: { let b = bitmap.remove (bitmap.from_list [5, 70000]) 5 assert_equal (bitmap.to_list b) [70000] assert_equal (bitmap.stats b) (1, 0) assert (bitmap.is_empty (bitmap.remove b 70000)) "empty" } @test fn a_dense_intersection_stays_a_bitmap :: { let a = bitmap.from_list (list.range 0 6000) let b = bitmap.from_list (list.range 1000 9000) let both = bitmap.intersect a b assert_equal (bitmap.to_list both) (list.range 1000 6000) assert_equal (bitmap.stats both) (0, 1) } @test fn a_sparse_intersection_drops_to_a_list :: { let a = bitmap.from_list (list.range 0 6000) let b = bitmap.from_list (list.range 3000 9000) let both = bitmap.intersect a b assert_equal (bitmap.to_list both) (list.range 3000 6000) assert_equal (bitmap.stats both) (1, 0) } @test @should_panic "negative id" fn adding_a_negative_id_panics :: { let _ = bitmap.add (bitmap.empty ()) (0 - 1) () } @test @should_panic "negative id" fn from_list_with_a_negative_id_panics :: { let _ = bitmap.from_list [0 - 1] () } // ---- against naive sets ----------------------------------------------------- /// The distinct values of `xs`, ascending. fn sorted_unique :: xs: List -> List { dedupe (list.sort xs) } fn dedupe :: xs: List -> List { match xs { [x, y, ..rest] -> if x == y { dedupe [y, ..rest] } else { [x, ..dedupe [y, ..rest]] } , _ -> xs } } /// The values in both, ascending. fn naive_intersect :: xs: List ys: List -> List { let ys = sorted_unique ys list.filter (sorted_unique xs) fn x { list.any ys fn y { y == x } } } /// Two random sets in a small range: sparse list containers. fn small_sets :: -> (List, List) / { let a = gen.list ~min: 0 ~max: 120 fn { gen.int ~low: 0 ~high: 300 } let b = gen.list ~min: 0 ~max: 120 fn { gen.int ~low: 0 ~high: 300 } (a, b) } /// Two random sets spread over several containers. fn wide_sets :: -> (List, List) / { let a = gen.list ~min: 0 ~max: 60 fn { gen.int ~low: 0 ~high: 200000 } let b = gen.list ~min: 0 ~max: 60 fn { gen.int ~low: 0 ~high: 200000 } (a, b) } fn sets_agree_with_the_naive_ones :: pair: (List, List) -> Bool { let (a, b) = pair let x = bitmap.from_list a let y = bitmap.from_list b bitmap.to_list x == sorted_unique a && bitmap.to_list y == sorted_unique b && bitmap.size x == list.length (sorted_unique a) && bitmap.to_list (bitmap.intersect x y) == naive_intersect a b && list.all a fn id { bitmap.contains x id } && list.all (naive_intersect a b) fn id { bitmap.contains y id } } @test fn small_sets_agree_with_the_naive_ones :: { check.seeded ~seed: 101 ~runs: 100 small_sets sets_agree_with_the_naive_ones } @test fn wide_sets_agree_with_the_naive_ones :: { check.seeded ~seed: 102 ~runs: 100 wide_sets sets_agree_with_the_naive_ones } /// A range of more than 4,096 values, at a random offset. fn dense_set :: -> (Int, Int) / { let lo = gen.int ~low: 0 ~high: 1000 let hi = lo + 4200 + gen.int ~low: 0 ~high: 800 (lo, hi) } fn dense_ranges_use_bitmaps :: bounds: (Int, Int) -> Bool { let (lo, hi) = bounds let b = bitmap.from_list (list.range lo hi) bitmap.stats b == (0, 1) && bitmap.size b == hi - lo + 1 && bitmap.to_list b == list.range lo hi && bitmap.contains b lo && bitmap.contains b hi && !bitmap.contains b (hi + 1) } @test fn dense_ranges_use_bitmap_containers :: { check.seeded ~seed: 103 ~runs: 20 dense_set dense_ranges_use_bitmaps } /// Two overlapping dense ranges: their intersection is dense too. fn dense_pair :: -> (Int, Int, Int) / { let lo = gen.int ~low: 0 ~high: 500 let mid = lo + 3000 + gen.int ~low: 0 ~high: 1000 let hi = mid + 3000 + gen.int ~low: 0 ~high: 1000 (lo, mid, hi) } fn dense_intersections_agree :: bounds: (Int, Int, Int) -> Bool { let (lo, mid, hi) = bounds let a = bitmap.from_list (list.range lo (mid + 2500)) let b = bitmap.from_list (list.range mid hi) bitmap.to_list (bitmap.intersect a b) == list.range mid (mid + 2500) } @test fn dense_intersections_agree_with_the_ranges :: { check.seeded ~seed: 104 ~runs: 20 dense_pair dense_intersections_agree } @test fn adds_and_removes_keep_a_random_set :: { check.seeded ~seed: 105 ~runs: 50 fn { let ids = gen.list ~min: 1 ~max: 200 fn { gen.int ~low: 0 ~high: 400 } // An id's operation is fixed: a multiple of 3 removes it, anything // else adds it. let b = list.fold ids (bitmap.empty ()) fn b id { if id % 3 == 0 { bitmap.remove b id } else { bitmap.add b id } } let expected = sorted_unique (list.filter ids fn id { id % 3 != 0 }) bitmap.to_list b == expected && list.all ids fn id { bitmap.contains b id == (id % 3 != 0) } } fn ok { ok } }