packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#31 · main · 4 min 11 s
0Étoiles
0Forks
3Issues
| 1 | // std/string.find (and string.contains, which shares its kernel) against |
| 2 | // a naive byte-level scan, on generated strings (issue gabla108/louise#380). |
| 3 | |
| 4 | import check |
| 5 | import gen |
| 6 | import std/bytes as raw |
| 7 | import std/random.{type random} |
| 8 | import std/string |
| 9 | import std/test.{assert, assert_equal} |
| 10 | |
| 11 | /// The naive find: the first byte offset at which `needle`'s bytes occur |
| 12 | /// in `s`'s, or None (the empty needle at 0). |
| 13 | fn naive_find :: s: String needle: String -> Option<Int> |
| 14 | { |
| 15 | let h = raw.from_string s |
| 16 | let n = raw.from_string needle |
| 17 | if raw.length n == 0 |
| 18 | { |
| 19 | Some(0) |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | scan_from h n 0 (raw.length h - raw.length n) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn scan_from :: h: Bytes n: Bytes at: Int last: Int -> Option<Int> |
| 28 | { |
| 29 | if at > last |
| 30 | { |
| 31 | None |
| 32 | } |
| 33 | else |
| 34 | { |
| 35 | if matches_at h n at 0 |
| 36 | { |
| 37 | Some(at) |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | scan_from h n (at + 1) last |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn matches_at :: h: Bytes n: Bytes at: Int i: Int -> Bool |
| 47 | { |
| 48 | if i == raw.length n |
| 49 | { |
| 50 | True |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | raw.read_u8 h (at + i) == raw.read_u8 n i && matches_at h n at (i + 1) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// The property: find is the naive scan's answer, and contains agrees. |
| 59 | fn agrees :: pair: (String, String) -> Bool |
| 60 | { |
| 61 | let (s, n) = pair |
| 62 | let want = naive_find s n |
| 63 | string.find s n == want && string.contains s n == (want != None) |
| 64 | } |
| 65 | |
| 66 | /// A slice of `s` (a sure hit when it is not empty; empty when the bounds |
| 67 | /// meet). |
| 68 | fn needle_in :: s: String -> String / <random> |
| 69 | { |
| 70 | let n = string.length s |
| 71 | let from = gen.int ~low: 0 ~high: n |
| 72 | let to = gen.int ~low: from ~high: n |
| 73 | string.slice s from to |
| 74 | } |
| 75 | |
| 76 | /// Haystack and needle from a small alphabet: matches are dense, and the |
| 77 | /// needle crosses the kernel's dispatch boundaries (1/4/16/64 bytes). |
| 78 | fn short_pair :: -> (String, String) / <random> |
| 79 | { |
| 80 | ( gen.string ~alphabet: "aabc" ~min: 0 ~max: 200 |
| 81 | , gen.string ~alphabet: "aabc" ~min: 0 ~max: 70 |
| 82 | ) |
| 83 | } |
| 84 | |
| 85 | /// A haystack, and a needle cut from it (a sure hit when not empty). |
| 86 | fn cut_pair :: -> (String, String) / <random> |
| 87 | { |
| 88 | let s = gen.string ~alphabet: "aabc" ~min: 0 ~max: 200 |
| 89 | (s, needle_in s) |
| 90 | } |
| 91 | |
| 92 | /// Multibyte characters: byte matches stay on character boundaries. |
| 93 | fn multibyte_pair :: -> (String, String) / <random> |
| 94 | { |
| 95 | ( gen.string ~alphabet: "é🦀ab" ~min: 0 ~max: 120 |
| 96 | , gen.string ~alphabet: "é🦀ab" ~min: 0 ~max: 30 |
| 97 | ) |
| 98 | } |
| 99 | |
| 100 | /// A long haystack, a short needle: the wide loop, mostly missing. |
| 101 | fn long_pair :: -> (String, String) / <random> |
| 102 | { |
| 103 | ( gen.string ~alphabet: "aabc de" ~min: 1000 ~max: 3000 |
| 104 | , gen.string ~alphabet: "aabc de" ~min: 1 ~max: 6 |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | @test |
| 109 | fn directed_cases :: |
| 110 | { |
| 111 | // The empty needle is found at 0, a longer needle nowhere. |
| 112 | assert_equal (string.find "abc" "") (Some(0)) |
| 113 | assert_equal (string.find "" "") (Some(0)) |
| 114 | assert_equal (string.find "ab" "abc") None |
| 115 | // Overlaps: the first occurrence wins. |
| 116 | assert_equal (string.find "aaaa" "aa") (Some(0)) |
| 117 | assert_equal (string.find "abaaa" "aaa") (Some(2)) |
| 118 | // A byte index, on a character boundary. |
| 119 | assert_equal (string.find "héllo" "llo") (Some(3)) |
| 120 | // The dispatch boundaries, found at the end and missed. |
| 121 | assert_equal (string.find "abbbbc" "bbbc") (Some(2)) |
| 122 | assert_equal (string.find "abbbbc" "bbbbc") (Some(1)) |
| 123 | assert_equal (string.find "abbbbc" "bbx") None |
| 124 | assert |
| 125 | ( string.find (string.repeat "ab" 40 <> "z") (string.repeat "ab" 8) |
| 126 | == Some(0) |
| 127 | ) |
| 128 | "needle of 16 at 0" |
| 129 | assert |
| 130 | ( string.find (string.repeat "ab" 40 <> "z") (string.repeat "ab" 8 <> "z") |
| 131 | == Some(64) |
| 132 | ) |
| 133 | "needle of 17 at the end" |
| 134 | let long = string.repeat "x" 200 |
| 135 | assert_equal (string.find (long <> "mark") "mark") (Some(200)) |
| 136 | assert_equal (string.find long (string.repeat "y" 64)) None |
| 137 | } |
| 138 | |
| 139 | @test |
| 140 | fn find_is_the_naive_scan_on_short_strings :: |
| 141 | { |
| 142 | check.seeded ~seed: 3801 ~runs: 300 short_pair agrees |
| 143 | } |
| 144 | |
| 145 | @test |
| 146 | fn find_is_the_naive_scan_when_the_needle_is_cut_from_the_haystack :: |
| 147 | { |
| 148 | check.seeded ~seed: 3802 ~runs: 300 cut_pair agrees |
| 149 | } |
| 150 | |
| 151 | @test |
| 152 | fn find_is_the_naive_scan_on_multibyte_strings :: |
| 153 | { |
| 154 | check.seeded ~seed: 3803 ~runs: 200 multibyte_pair agrees |
| 155 | } |
| 156 | |
| 157 | @test |
| 158 | fn find_is_the_naive_scan_on_long_haystacks :: |
| 159 | { |
| 160 | check.seeded ~seed: 3804 ~runs: 50 long_pair agrees |
| 161 | } |