// The FM-index's count/locate/extract agree with naive scans of the text, // on examples and on random texts and patterns. import fm import gen import gen/check import std/list import std/random.{type random} import std/string import std/test.{assert, assert_equal} @test fn banana_counts_and_locates_overlapping_occurrences :: { let idx = fm.build "banana" assert_equal (fm.count idx "ana") 2 assert_equal (fm.locate idx "ana") [1, 3] assert_equal (fm.count idx "a") 3 assert_equal (fm.locate idx "a") [1, 3, 5] assert_equal (fm.count idx "banana") 1 assert_equal (fm.count idx "bananas") 0 assert_equal (fm.count idx "x") 0 assert (fm.contains idx "nan") "nan" assert (fm.contains idx "nana") "nana, at position 2" assert (!fm.contains idx "nab") "nab" assert_equal (fm.extract idx 1 3) "ana" assert_equal (fm.length idx) 6 } @test fn an_empty_pattern_has_no_matches :: { let idx = fm.build "banana" assert_equal (fm.count idx "") 0 assert_equal (fm.locate idx "") [] } @test fn an_empty_text_matches_nothing :: { let idx = fm.build "" assert_equal (fm.length idx) 0 assert_equal (fm.count idx "a") 0 assert_equal (fm.locate idx "a") [] } @test fn a_run_of_one_letter_overlaps :: { let idx = fm.build "aaaa" assert_equal (fm.count idx "aa") 3 assert_equal (fm.locate idx "aa") [0, 1, 2] } @test fn a_corpus_maps_occurrences_to_documents :: { let c = fm.build_corpus ["banana", "bandana", "ananas"] assert_equal (fm.corpus_count c "ana") 5 assert_equal (fm.corpus_locate c "ana") [(0, 1), (0, 3), (1, 4), (2, 0), (2, 2)] assert_equal (fm.corpus_locate c "dan") [(1, 3)] assert_equal (fm.corpus_count c "xyz") 0 } @test fn a_pattern_with_the_separator_matches_nothing :: { let c = fm.build_corpus ["ab", "cd"] assert_equal (fm.corpus_count c "b\u{0}c") 0 assert_equal (fm.corpus_locate c "b\u{0}c") [] } @test @should_panic "document separator" fn a_document_with_the_separator_panics :: { let _ = fm.build_corpus ["ok", "not \u{0} ok"] () } // ---- against naive scans ------------------------------------------------------ fn text :: -> String / { gen.string ~alphabet: "ab" ~min: 1 ~max: 30 } /// Mostly a slice of the text (a likely match), sometimes anything. Never /// empty: an empty pattern has no matches, by design. fn pattern :: text: String -> String / { let n = string.length text if gen.int ~low: 1 ~high: 10 <= 6 { let start = gen.int ~low: 0 ~high: (n - 1) string.slice text start (start + gen.int ~low: 1 ~high: 4) } else { gen.string ~alphabet: "abc" ~min: 1 ~max: 4 } } fn case :: -> (String, String) / { let t = text () (t, pattern t) } /// The positions where `p` occurs in `t`, ascending, overlapping included. fn naive_positions :: t: String p: String -> List { let n = string.length t let m = string.length p list.filter (list.range 0 (n - m)) fn i { string.slice t i (i + m) == p } } fn count_and_locate_match_the_naive_scan :: case: (String, String) -> Bool { let (t, p) = case let idx = fm.build t let positions = naive_positions t p fm.locate idx p == positions && fm.count idx p == list.length positions && fm.contains idx p == (positions != []) } @test fn count_and_locate_match_the_naive_scan_of_the_text :: { check.seeded ~seed: 301 ~runs: 100 case count_and_locate_match_the_naive_scan } /// A random slice of the text. fn slice_case :: -> (String, Int, Int) / { let t = text () let n = string.length t let start = gen.int ~low: 0 ~high: (n - 1) (t, start, gen.int ~low: 0 ~high: (n - start)) } fn extract_matches_slice :: case: (String, Int, Int) -> Bool { let (t, start, len) = case fm.extract (fm.build t) start len == string.slice t start (start + len) } @test fn extract_matches_a_slice_of_the_text :: { check.seeded ~seed: 302 ~runs: 100 slice_case extract_matches_slice } // ---- corpora, against a naive scan --------------------------------------------- fn docs :: -> List / { gen.list ~min: 1 ~max: 4 fn { gen.string ~alphabet: "ab" ~min: 1 ~max: 12 } } fn corpus_case :: -> (List, String) / { let ds = docs () (ds, pattern (string.concat ds)) } /// The `(document, offset)` pairs of `p` over `docs`, in order. fn naive_corpus :: docs: List p: String -> List<(Int, Int)> { use (d, text) <- list.flat_map (list.zip (list.range 0 (list.length docs - 1)) docs) list.map (naive_positions text p) fn i { (d, i) } } fn corpus_matches_the_naive_scan :: case: (List, String) -> Bool { let (ds, p) = case let c = fm.build_corpus ds fm.corpus_locate c p == naive_corpus ds p && fm.corpus_count c p == list.length (naive_corpus ds p) } @test fn a_corpus_matches_the_naive_scan_of_its_documents :: { check.seeded ~seed: 303 ~runs: 100 corpus_case corpus_matches_the_naive_scan }