tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

189 lignes · 4892 octetsBrut · Historique
1// The FM-index's count/locate/extract agree with naive scans of the text,
2// on examples and on random texts and patterns.
3
4import fm
5import gen
6import gen/check
7import std/list
8import std/random.{type random}
9import std/string
10import std/test.{assert, assert_equal}
11
12@test
13fn banana_counts_and_locates_overlapping_occurrences ::
14{
15 let idx = fm.build "banana"
16 assert_equal (fm.count idx "ana") 2
17 assert_equal (fm.locate idx "ana") [1, 3]
18 assert_equal (fm.count idx "a") 3
19 assert_equal (fm.locate idx "a") [1, 3, 5]
20 assert_equal (fm.count idx "banana") 1
21 assert_equal (fm.count idx "bananas") 0
22 assert_equal (fm.count idx "x") 0
23 assert (fm.contains idx "nan") "nan"
24 assert (fm.contains idx "nana") "nana, at position 2"
25 assert (!fm.contains idx "nab") "nab"
26 assert_equal (fm.extract idx 1 3) "ana"
27 assert_equal (fm.length idx) 6
28}
29
30@test
31fn an_empty_pattern_has_no_matches ::
32{
33 let idx = fm.build "banana"
34 assert_equal (fm.count idx "") 0
35 assert_equal (fm.locate idx "") []
36}
37
38@test
39fn an_empty_text_matches_nothing ::
40{
41 let idx = fm.build ""
42 assert_equal (fm.length idx) 0
43 assert_equal (fm.count idx "a") 0
44 assert_equal (fm.locate idx "a") []
45}
46
47@test
48fn a_run_of_one_letter_overlaps ::
49{
50 let idx = fm.build "aaaa"
51 assert_equal (fm.count idx "aa") 3
52 assert_equal (fm.locate idx "aa") [0, 1, 2]
53}
54
55@test
56fn a_corpus_maps_occurrences_to_documents ::
57{
58 let c = fm.build_corpus ["banana", "bandana", "ananas"]
59 assert_equal (fm.corpus_count c "ana") 5
60 assert_equal
61 (fm.corpus_locate c "ana")
62 [(0, 1), (0, 3), (1, 4), (2, 0), (2, 2)]
63 assert_equal (fm.corpus_locate c "dan") [(1, 3)]
64 assert_equal (fm.corpus_count c "xyz") 0
65}
66
67@test
68fn a_pattern_with_the_separator_matches_nothing ::
69{
70 let c = fm.build_corpus ["ab", "cd"]
71 assert_equal (fm.corpus_count c "b\u{0}c") 0
72 assert_equal (fm.corpus_locate c "b\u{0}c") []
73}
74
75@test
76@should_panic "document separator"
77fn a_document_with_the_separator_panics ::
78{
79 let _ = fm.build_corpus ["ok", "not \u{0} ok"]
80 ()
81}
82
83// ---- against naive scans ------------------------------------------------------
84
85fn text :: -> String / <random>
86{
87 gen.string ~alphabet: "ab" ~min: 1 ~max: 30
88}
89
90/// Mostly a slice of the text (a likely match), sometimes anything. Never
91/// empty: an empty pattern has no matches, by design.
92fn pattern :: text: String -> String / <random>
93{
94 let n = string.length text
95 if gen.int ~low: 1 ~high: 10 <= 6
96 {
97 let start = gen.int ~low: 0 ~high: (n - 1)
98 string.slice text start (start + gen.int ~low: 1 ~high: 4)
99 }
100 else
101 {
102 gen.string ~alphabet: "abc" ~min: 1 ~max: 4
103 }
104}
105
106fn case :: -> (String, String) / <random>
107{
108 let t = text ()
109 (t, pattern t)
110}
111
112/// The positions where `p` occurs in `t`, ascending, overlapping included.
113fn naive_positions :: t: String p: String -> List<Int>
114{
115 let n = string.length t
116 let m = string.length p
117 list.filter (list.range 0 (n - m)) fn i { string.slice t i (i + m) == p }
118}
119
120fn count_and_locate_match_the_naive_scan :: case: (String, String) -> Bool
121{
122 let (t, p) = case
123 let idx = fm.build t
124 let positions = naive_positions t p
125 fm.locate idx p == positions && fm.count idx p == list.length positions
126 && fm.contains idx p == (positions != [])
127}
128
129@test
130fn count_and_locate_match_the_naive_scan_of_the_text ::
131{
132 check.seeded ~seed: 301 ~runs: 100 case count_and_locate_match_the_naive_scan
133}
134
135/// A random slice of the text.
136fn slice_case :: -> (String, Int, Int) / <random>
137{
138 let t = text ()
139 let n = string.length t
140 let start = gen.int ~low: 0 ~high: (n - 1)
141 (t, start, gen.int ~low: 0 ~high: (n - start))
142}
143
144fn extract_matches_slice :: case: (String, Int, Int) -> Bool
145{
146 let (t, start, len) = case
147 fm.extract (fm.build t) start len == string.slice t start (start + len)
148}
149
150@test
151fn extract_matches_a_slice_of_the_text ::
152{
153 check.seeded ~seed: 302 ~runs: 100 slice_case extract_matches_slice
154}
155
156// ---- corpora, against a naive scan ---------------------------------------------
157
158fn docs :: -> List<String> / <random>
159{
160 gen.list ~min: 1 ~max: 4 fn { gen.string ~alphabet: "ab" ~min: 1 ~max: 12 }
161}
162
163fn corpus_case :: -> (List<String>, String) / <random>
164{
165 let ds = docs ()
166 (ds, pattern (string.concat ds))
167}
168
169/// The `(document, offset)` pairs of `p` over `docs`, in order.
170fn naive_corpus :: docs: List<String> p: String -> List<(Int, Int)>
171{
172 use (d, text) <-
173 list.flat_map (list.zip (list.range 0 (list.length docs - 1)) docs)
174 list.map (naive_positions text p) fn i { (d, i) }
175}
176
177fn corpus_matches_the_naive_scan :: case: (List<String>, String) -> Bool
178{
179 let (ds, p) = case
180 let c = fm.build_corpus ds
181 fm.corpus_locate c p == naive_corpus ds p
182 && fm.corpus_count c p == list.length (naive_corpus ds p)
183}
184
185@test
186fn a_corpus_matches_the_naive_scan_of_its_documents ::
187{
188 check.seeded ~seed: 303 ~runs: 100 corpus_case corpus_matches_the_naive_scan
189}