// Tests for packages/stringzilla. **Native-only**: the interpreter cannot // call C externs, so run them with `louise test --native` (the argon2 // package's pattern; ci.sh runs it the same way). // // `gen/check.property` cannot help here: its `holds` is pure, and every // function of this package performs ``. `for_all` below is its // shrunk-less stand-in: cases come from a seeded generator, the seed is // drawn from `random` (fresh cases each run) and reported on a failure. import gen import std/list import std/random.{type random} import std/string import std/test.{assert, assert_equal} import stringzilla /// Runs `holds` on `runs` cases of `generate`, from a seed drawn once /// per run and named in the failure message for replay. fn for_all :: runs: Int generate: fn() -> a / holds: fn(a) -> Bool / -> () / { let seed = random.int 1000000000 random.with_seed seed fn { each_case seed runs 0 generate holds } } fn each_case :: seed: Int runs: Int i: Int generate: fn() -> a / holds: fn(a) -> Bool / -> () / { if i >= runs { () } else { assert (holds (generate ())) "case {i + 1} failed (replay: seed {seed})" each_case seed runs (i + 1) generate holds } } // Oracles: naive character scans over `string.slice`/`string.length`. // The generators use ASCII alphabets, so character offsets are byte // offsets. fn oracle_find :: s: String needle: String -> Option { scan s needle 0 1 } fn oracle_find_last :: s: String needle: String -> Option { scan s needle (string.length s - string.length needle) (-1) } /// `Some(i)` if `needle` is `slice(s, i, i + length(needle))`: a naive /// character scan from `i` by `step`. (The `split`-based oracle is wrong /// for `find_last`: split matches non-overlapping left to right, but the /// last occurrence may overlap the last match — "abaabbaaabbbaabaa" /// ends in "bb" at 10.) fn scan :: s: String needle: String i: Int step: Int -> Option { let past = string.length s - string.length needle if i < 0 || i > past { None } else { if string.slice s i (i + string.length needle) == needle { Some(i) } else { scan s needle (i + step) step } } } /// Non-overlapping occurrences: on a match at `i`, continue at /// `i + length(needle)`. fn oracle_count :: s: String needle: String -> Int { let step = if needle == "" { 1 } else { string.length needle } count_from s needle 0 step 0 } fn count_from :: s: String needle: String i: Int step: Int found: Int -> Int { let past = string.length s - string.length needle if i > past { found } else { if string.slice s i (i + string.length needle) == needle { count_from s needle (i + step) step (found + 1) } else { count_from s needle (i + 1) step found } } } @test fn find_matches_a_naive_scan :: -> () / { for_all 300 ( fn { ( gen.string ~alphabet: "ab" ~min: 0 ~max: 60 , gen.string ~alphabet: "ab" ~min: 0 ~max: 6 ) } ) fn case { stringzilla.find case.0 case.1 == oracle_find case.0 case.1 } } @test fn find_last_matches_a_naive_scan :: -> () / { for_all 300 ( fn { ( gen.string ~alphabet: "ab" ~min: 0 ~max: 60 , gen.string ~alphabet: "ab" ~min: 0 ~max: 6 ) } ) fn case { stringzilla.find_last case.0 case.1 == oracle_find_last case.0 case.1 } } @test fn count_matches_a_naive_scan :: -> () / { for_all 300 ( fn { ( gen.string ~alphabet: "ab" ~min: 0 ~max: 60 , gen.string ~alphabet: "ab" ~min: 0 ~max: 6 ) } ) fn case { stringzilla.count case.0 case.1 == oracle_count case.0 case.1 } } @test fn finds_agree_with_contains :: -> () / { for_all 300 ( fn { ( gen.string ~alphabet: "ab" ~min: 0 ~max: 60 , gen.string ~alphabet: "ab" ~min: 0 ~max: 6 ) } ) fn case { (stringzilla.find case.0 case.1 != None) == string.contains case.0 case.1 } } @test fn find_byte_finds_the_byte :: -> () / { let alphabet = "abcde" for_all 300 ( fn { ( gen.string ~alphabet: alphabet ~min: 0 ~max: 60 , gen.int ~low: 0 ~high: 4 ) } ) fn case { // Byte `i` of the alphabet as a one-character string; ASCII, so // byte offsets are character offsets. let needle = string.slice alphabet case.1 (case.1 + 1) let byte = 97 + case.1 stringzilla.find_byte case.0 byte == stringzilla.find case.0 needle && stringzilla.find_byte_last case.0 byte == stringzilla.find_last case.0 needle } } @test fn compare_matches_std :: -> () / { // "é" and "ä" are two-byte UTF-8 starting at 0xC3: byte order beyond // ASCII is exactly what this checks (StringZilla v4.6.3 ordered bytes // signed there; the vendored copy carries the v5 fix — PATCHES.md). for_all 300 ( fn { ( gen.string ~alphabet: "abczéä" ~min: 0 ~max: 12 , gen.string ~alphabet: "abczéä" ~min: 0 ~max: 12 ) } ) fn case { let std = if case.0 < case.1 { -1 } else { if case.0 > case.1 { 1 } else { 0 } } let sz = match stringzilla.compare case.0 case.1 { Lt -> -1, Eq -> 0, Gt -> 1 } std == sz } } @test fn sort_matches_list_sort :: -> () / { // Short lists take the argsort's insertion path, long ones its radix // path; both must agree with `list.sort`, past 32 strings too, with // non-ASCII bytes in the mix. for_all 100 ( fn { gen.list ~min: 0 ~max: 70 (fn { gen.string ~alphabet: "abczéä" ~min: 0 ~max: 12 }) } ) fn xs { stringzilla.sort xs == Ok(list.sort xs) } } @test fn fixed_cases :: -> () / { // Needle longer than the haystack; overlapping occurrences; Unicode. assert_equal (stringzilla.find "ab" "abab") None assert_equal (stringzilla.count "aaaa" "aa") 2 assert_equal (stringzilla.find "日本語日本語" "本") (Some(3)) assert_equal (stringzilla.find_last "日本語日本語" "本") (Some(12)) assert_equal (stringzilla.count "日本語日本語" "日本") 2 // "é" is 0xC3 0xA9: a byte search finds a continuation byte too. assert_equal (stringzilla.find_byte "héllo" 0xA9) (Some(2)) assert_equal (stringzilla.find_byte "banana" 97) (Some(1)) assert_equal (stringzilla.find_byte_last "banana" 97) (Some(5)) assert_equal (stringzilla.find_byte "" 97) None } @test fn sort_on_fixed_lists :: -> () / { assert_equal (stringzilla.sort []) (Ok([])) assert_equal (stringzilla.sort ["b", "a", "c"]) (Ok(["a", "b", "c"])) assert_equal (stringzilla.sort ["bb", "b", "ba", "a", ""]) (Ok(["", "a", "b", "ba", "bb"])) assert_equal (stringzilla.sort ["x", "x", "x"]) (Ok(["x", "x", "x"])) assert_equal (stringzilla.sort ["é", "z", "ä"]) (Ok(["z", "ä", "é"])) let down = list.map (list.reverse (list.range 1 1000)) fn i { "k{i}" } assert_equal (stringzilla.sort down) (Ok(list.sort down)) } @test fn hashes_are_deterministic_and_seeded :: -> () / { assert_equal (stringzilla.hash "hello" 0) (stringzilla.hash "hello" 0) assert_equal (stringzilla.bytesum "hello") (stringzilla.bytesum "hello") assert (stringzilla.hash "hello" 0 != stringzilla.hash "hello" 1) "the seed matters" assert (stringzilla.hash "hello" 0 != stringzilla.hash "hellp" 0) "the text matters" // Known answers, recorded from the vendored StringZilla v4.6.3: the // low 62 bits of its 64-bit hash. Its kernels agree across SIMD tiers // (checked serial through haswell). assert_equal (stringzilla.hash "" 0) 463414023684379206 assert_equal (stringzilla.hash "hello" 0) 2641555913580541882 assert_equal (stringzilla.hash "hello" 42) 551789246955265504 assert_equal (stringzilla.bytesum "hello") 532 assert_equal (stringzilla.bytesum "") 0 } @test fn version_and_capabilities :: -> () / { assert_equal (stringzilla.version ()) "4.6.3" assert (string.contains (stringzilla.capabilities ()) "serial") (stringzilla.capabilities ()) }