flan/test/programs/bytes2.flan
Joseph Ferano 96ab4c9cf0 Retire the per-type printers, since print says all of it
print-str, print-i64, print-f64, print-bytes, print-line and newline leave
the prelude. print and println are the whole printing surface now, and print
is the better call at every one of the sites that used them: it is the same
structural walk without the newline, so the no-newline case the family was
kept for is covered, and it takes the value as it is. The old print-i64
forced an explicit (i64 x) at every call site, because this language widens
nothing implicitly; that cast is gone from 127 places.

Dropping it moves one answer. hash-grid returns u64, and the cast through
the signed printer showed sand-headless's hash as -2851001042534928384.
print routes a u64 through flan_u64_to_bytes, so it now prints
15595743031174623232 — the same 64 bits, read as the unsigned number they
are. The pinned expectation follows the correction.

test-flan-dev.el and test_session.ml both reached for print-line as "a name
the prelude has"; they reach for rand-seed instead.
2026-09-12 05:32:25 +07:00

96 lines
5.0 KiB
Plaintext

;;;; index-of-bytes, trim, the two byte classes, and parse-f64.
;;;;
;;;; The search cases are the ones a naive loop gets wrong rather than the
;;;; ones it gets right: a needle that matches only at the very end, one that
;;;; matches only at index 0, one whose first byte occurs repeatedly before
;;;; the real match ("aab" in "aaab"), a needle longer than the haystack
;;;; (which must answer None and must not trap building the window), the
;;;; empty needle, and a near-miss that shares every byte but the last.
;;;;
;;;; The parse-f64 cases are every shape strtod answers a plausible number
;;;; for and a caller cannot tell from a real one: "", "abc", "1x", ".",
;;;; "1e", " 1", "0x10" and "nan". Each must be None.
(defn show-idx [o (Option i32)]
(print (match o (Some i) i None -1))
(print " "))
(defn show-bool [b bool]
(print (if b "t" "f")))
;; Brackets around the result so an empty trim is visible as [] rather than
;; as nothing at all — the all-whitespace case is otherwise indistinguishable
;; from a trim that printed the wrong slice of length zero.
(defn show-trim [s string]
(print "[")
(print (trim (bytes s)))
(print "]"))
(defn main [] i32
(show-idx (index-of-bytes (bytes "hello world") (bytes "world"))) ; 6, at the end
(show-idx (index-of-bytes (bytes "hello world") (bytes "hello"))) ; 0, at the start
(show-idx (index-of-bytes (bytes "hello world") (bytes "o w"))) ; 4, in the middle
(show-idx (index-of-bytes (bytes "banana") (bytes "na"))) ; 2, first of two
(show-idx (index-of-bytes (bytes "aaab") (bytes "aab"))) ; 1, after false starts
(println "")
(show-idx (index-of-bytes (bytes "hello") (bytes "hellp"))) ; -1, last byte differs
(show-idx (index-of-bytes (bytes "hi") (bytes "hiya"))) ; -1, longer, no trap
(show-idx (index-of-bytes (bytes "") (bytes "a"))) ; -1, empty haystack
(show-idx (index-of-bytes (bytes "hello") (bytes ""))) ; 0, empty needle
(show-idx (index-of-bytes (bytes "") (bytes ""))) ; 0, both empty
(show-idx (index-of-bytes (bytes "hello") (bytes "hello"))) ; 0, whole string
(println "")
(show-trim " hi ") ; [hi]
(show-trim "hi") ; [hi] nothing to remove
(show-trim "\thi\n") ; [hi] tab and newline count
(show-trim " ") ; [] all whitespace, must not run backwards
(show-trim "") ; []
(show-trim " a b ") ; [a b] the inner space survives
(show-trim " x") ; [x] one-sided
(show-trim "x ") ; [x]
(println "")
(show-bool (digit? \0)) (show-bool (digit? \9)) (show-bool (digit? \/))
(show-bool (digit? \:)) (show-bool (digit? \a))
(println "")
(show-bool (space? \space)) (show-bool (space? \tab))
(show-bool (space? \newline)) (show-bool (space? \return))
(show-bool (space? \a)) (show-bool (space? \0))
(println "")
;; Accepted. The last is the round trip through %g that proves the value and
;; not merely the acceptance is right.
(print (match (parse-f64 (bytes "0")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "3.5")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "-3.5")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "+0.25")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1e3")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1.5E-2")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "12")) (Some v) v None -999.0))
(println "")
;; Refused. Every one of these is a number out of strtod, which is the point.
(print (match (parse-f64 (bytes "")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "abc")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1x")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes ".")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1e")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1e+")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes " 1")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "1 ")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "0x10")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "nan")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes "+")) (Some v) v None -999.0))
(println "")
;; A trailing dot with no fraction is a C float literal and is accepted; a
;; leading one is too. Both are here because they are the boundary the
;; digit counter, not the position, decides.
(print (match (parse-f64 (bytes "1.")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes ".5")) (Some v) v None -999.0))
(println "")
;; Parsing a trimmed field, which is why both exist.
(print (match (parse-f64 (trim (bytes " 2.25 "))) (Some v) v None -999.0))
(println "")
0)