// The trigram index answers exactly the documents containing the query // (a substring match on the normalized text), checked against a naive // scan on random corpora. import gen import gen/check import search import std/list import std/random.{type random} import std/string import std/test.{assert, assert_equal} fn notes :: -> search.Index { search.build [ (1, "Toki Pona notes") , (2, "TODO: plant the toki pona stickers") , (3, "Shopping: milk, eggs") ] } @test fn normalize_lowercases_and_trims :: { assert_equal (search.normalize " ToKi Pona\t") "toki pona" assert_equal (search.normalize "") "" } @test fn trigrams_are_the_distinct_three_character_windows :: { assert_equal (search.trigrams "abcd") ["abc", "bcd"] assert_equal (search.trigrams "aaaa") ["aaa"] assert_equal (search.trigrams "ab") [] assert_equal (search.trigrams "") [] } @test fn search_is_a_case_insensitive_substring_match :: { let idx = notes () assert_equal (search.search idx "toki") [1, 2] assert_equal (search.search idx "PONA") [1, 2] assert_equal (search.search idx "toki pona") [1, 2] assert_equal (search.search idx "milk") [3] assert_equal (search.search idx " bread ") [] assert_equal (search.size idx) 3 } @test fn an_empty_query_matches_everything :: { assert_equal (search.search (notes ()) "") [1, 2, 3] assert_equal (search.search (notes ()) " ") [1, 2, 3] } @test fn a_short_query_scans_the_documents :: { assert_equal (search.search (notes ()) "to") [1, 2] assert_equal (search.search (notes ()) "M") [3] assert_equal (search.search (notes ()) "z") [] } @test fn trigrams_alone_do_not_decide_a_candidate_is_verified :: { // Every trigram of "abxy" occurs in the document, but "abxy" does not. let idx = search.build [(1, "abx bxy")] assert_equal (search.search idx "abxy") [] assert_equal (search.search idx "bxy") [1] } @test fn add_stores_and_replaces_a_document :: { let idx = search.add (notes ()) 4 "a bread recipe" assert_equal (search.search idx "bread") [4] // Adding an existing id replaces its text. let idx = search.add idx 4 "a cake recipe" assert_equal (search.search idx "bread") [] assert_equal (search.search idx "cake") [4] assert_equal (search.size idx) 4 } @test fn remove_drops_a_document :: { let idx = search.remove (notes ()) 2 assert_equal (search.search idx "toki") [1] assert_equal (search.size idx) 2 // Removing an absent id changes nothing. assert_equal (search.search (search.remove idx 99) "toki") [1] } @test fn a_duplicate_id_in_build_keeps_the_last_text :: { let idx = search.build [(1, "first text"), (1, "second text")] assert_equal (search.search idx "second") [1] assert_equal (search.search idx "first") [] } @test @should_panic "negative id" fn a_negative_id_panics :: { let _ = search.build [(0 - 1, "nope")] () } // ---- against a naive scan --------------------------------------------------- /// A small corpus; the ids are the positions. fn corpus :: -> List<(Int, String)> / { let texts = gen.list ~min: 0 ~max: 14 fn { gen.string ~alphabet: "abcAB " ~min: 0 ~max: 24 } list.zip (list.range 0 (list.length texts - 1)) texts } /// Mostly a slice of a document (a likely match), sometimes anything. fn query :: docs: List<(Int, String)> -> String / { if docs != [] && gen.int ~low: 1 ~high: 10 <= 6 { let (_, text) = gen.pick docs let n = string.length text let start = gen.int ~low: 0 ~high: n string.slice text start (start + gen.int ~low: 0 ~high: 8) } else { gen.string ~alphabet: "abcAB " ~min: 0 ~max: 8 } } fn case :: -> (List<(Int, String)>, String) / { let docs = corpus () (docs, query docs) } /// The ids of the documents containing `q`, ascending: the scan `search` /// must agree with. fn naive :: docs: List<(Int, String)> q: String -> List { let q = search.normalize q list.sort ( list.map (list.filter docs fn d { string.contains (search.normalize d.1) q }) fn d { d.0 } ) } fn matches_the_naive_scan :: case: (List<(Int, String)>, String) -> Bool { let (docs, q) = case let expected = naive docs q let built = search.build docs let added = list.fold docs (search.empty ()) fn idx d { search.add idx d.0 d.1 } search.search built q == expected && search.search added q == expected } @test fn search_matches_the_naive_scan :: { check.seeded ~seed: 201 ~runs: 100 case matches_the_naive_scan } /// A random document of the corpus is removed; the rest still match. fn removed_case :: -> (List<(Int, String)>, Int, String) / { let docs = corpus () let gone = gen.int ~low: 0 ~high: 20 (docs, gone, query docs) } fn remove_matches_the_naive_scan :: case: (List<(Int, String)>, Int, String) -> Bool { let (docs, gone, q) = case let idx = search.remove (search.build docs) gone let expected = naive (list.filter docs fn d { d.0 != gone }) q search.search idx q == expected } @test fn remove_matches_the_naive_scan_of_the_rest :: { check.seeded ~seed: 202 ~runs: 100 removed_case remove_matches_the_naive_scan }