bytes->i64 is strtoll behind a primitive, and strtoll returns 0 for "", for "abc", for a lone "-", and for the "12" in "12x". None of those is distinguishable from a real 0, so any program that parses input it did not write is already wrong and has no way to find out. parse-i64 takes the whole slice or refuses it and says so with None. It is also the version that answers the same on wasm32: strtoll is libc's and locale-sensitive, which is the same argument that put the PRNG in the prelude rather than leaving it to rand(). The byte predicates are over [u8] rather than over string on purpose. (bytes s) is one call at the call site, and in exchange one copy of each function serves strings and byte slices both — which is as near a generic as this gets. Each tests its length before it slices, and `and` short-circuits, so a prefix longer than the subject answers false instead of tripping the slice bounds check. sign-f32 and lerp are the only two numeric helpers here, because they are the only two that decide something. clamp is (min hi (max lo x)) and abs is (max x (- 0 x)) over builtins that already exist — a prelude wrapper is a function emitted into every program to save a caller nothing. sign-f32 answers 0.0 for NaN, which is a choice and is written down. lerp is the weighted sum and not a + t*(b - a): the latter does not land on b exactly at t = 1.0, and a position that never quite arrives is what interpolation gets bug reports for. floor, ceil and round are deliberately absent. (f32 (i32 x)) is fptosi, which is poison out of range, and shipping that as a documented limitation is the same class of bug NEXT.md already records twice under Sharp edges. Correct lowering is llvm.floor.f32 in emit.ml, which is not this lane. sqrt is absent for a different reason: it is an extern to libm, and what libm means on wasm32 is a decision the FFI owns, not the prelude. rand-i32-range answers lo for an empty or reversed range rather than dividing by zero, which is immediate undefined behaviour and not merely a wrong number. Both range functions draw exactly one rand-u32 and neither changes it, so the sand hash still pins the generator; the new test pins the derivations off a fixed seed, which nothing else would have caught.
84 lines
3.7 KiB
Plaintext
84 lines
3.7 KiB
Plaintext
;;;; The prelude's byte predicates, parse-i64, and the two number helpers.
|
|
;;;;
|
|
;;;; The cases are chosen so a wrong implementation fails one: a prefix longer
|
|
;;;; than the string (which must answer false, not trap), the empty prefix and
|
|
;;;; the whole string as its own prefix, a prefix that matches at the wrong
|
|
;;;; end, and for parse-i64 every shape strtoll answers 0 for — "", "abc",
|
|
;;;; "12x", "-" — each of which a caller could not tell from a real 0.
|
|
|
|
(defn show-bool [b bool]
|
|
(print-str (if b "t" "f")))
|
|
|
|
(defn main [] i32
|
|
(show-bool (bytes=? (bytes "abc") (bytes "abc"))) ; t
|
|
(show-bool (bytes=? (bytes "abc") (bytes "abd"))) ; f same length
|
|
(show-bool (bytes=? (bytes "abc") (bytes "ab"))) ; f prefix, not equal
|
|
(show-bool (bytes=? (bytes "") (bytes ""))) ; t
|
|
(newline)
|
|
|
|
(show-bool (starts-with? (bytes "hello") (bytes "hel"))) ; t
|
|
(show-bool (starts-with? (bytes "hello") (bytes "llo"))) ; f matches the end
|
|
(show-bool (starts-with? (bytes "hi") (bytes "hiya"))) ; f longer, no trap
|
|
(show-bool (starts-with? (bytes "hello") (bytes ""))) ; t
|
|
(show-bool (starts-with? (bytes "hello") (bytes "hello"))) ; t
|
|
(newline)
|
|
|
|
(show-bool (ends-with? (bytes "hello") (bytes "llo"))) ; t
|
|
(show-bool (ends-with? (bytes "hello") (bytes "hel"))) ; f matches the start
|
|
(show-bool (ends-with? (bytes "hi") (bytes "hiya"))) ; f longer, no trap
|
|
(show-bool (ends-with? (bytes "hello") (bytes ""))) ; t
|
|
(show-bool (ends-with? (bytes "hello") (bytes "hello"))) ; t
|
|
(newline)
|
|
|
|
;; First occurrence, and None for a byte that is not there.
|
|
(print-i64 (i64 (match (index-of-byte (bytes "banana") \a) (Some i) i None -1)))
|
|
(print-str " ")
|
|
(print-i64 (i64 (match (index-of-byte (bytes "banana") \z) (Some i) i None -1)))
|
|
(print-str " ")
|
|
(print-i64 (i64 (match (index-of-byte (bytes "") \a) (Some i) i None -1)))
|
|
(newline)
|
|
|
|
;; Accepted.
|
|
(print-i64 (match (parse-i64 (bytes "0")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "42")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "-42")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "+7")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "9007199254740993")) (Some v) v None -999))
|
|
(newline)
|
|
;; Refused. Each of these is a 0 out of strtoll, which is the point.
|
|
(print-i64 (match (parse-i64 (bytes "")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "abc")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "12x")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes "-")) (Some v) v None -999)) (print-str " ")
|
|
(print-i64 (match (parse-i64 (bytes " 1")) (Some v) v None -999))
|
|
(newline)
|
|
|
|
(print-f64 (f64 (sign-f32 3.5))) (print-str " ")
|
|
(print-f64 (f64 (sign-f32 -3.5))) (print-str " ")
|
|
(print-f64 (f64 (sign-f32 0.0)))
|
|
(newline)
|
|
|
|
;; t = 1.0 must return b exactly, which a + t*(b - a) does not always do.
|
|
(print-f64 (f64 (lerp 0.0 10.0 0.0))) (print-str " ")
|
|
(print-f64 (f64 (lerp 0.0 10.0 0.25))) (print-str " ")
|
|
(print-f64 (f64 (lerp 0.0 10.0 1.0))) (print-str " ")
|
|
(print-f64 (f64 (lerp 2.0 -2.0 0.5)))
|
|
(newline)
|
|
|
|
;; The RNG ranges, off a fixed seed, so the numbers are the sequence and not
|
|
;; just "something in range". An empty range answers lo and must not divide.
|
|
(rand-seed 7)
|
|
(dotimes [i 5]
|
|
(when (> i 0) (print-str " "))
|
|
(print-i64 (i64 (rand-i32-range 10 20))))
|
|
(newline)
|
|
(print-i64 (i64 (rand-i32-range 5 5))) (print-str " ")
|
|
(print-i64 (i64 (rand-i32-range 5 -5)))
|
|
(newline)
|
|
(rand-seed 7)
|
|
(dotimes [i 3]
|
|
(when (> i 0) (print-str " "))
|
|
(print-f64 (f64 (rand-f32-range 0.0 1.0))))
|
|
(newline)
|
|
0)
|