flan/test/programs/bytes2.flan

96 lines
5.2 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 str] ()
(print "[")
(print (trim (bytes-view s)))
(print "]"))
(defn main [] i32
(show-idx (index-of-bytes (bytes-view "hello world") (bytes-view "world"))) ; 6, at the end
(show-idx (index-of-bytes (bytes-view "hello world") (bytes-view "hello"))) ; 0, at the start
(show-idx (index-of-bytes (bytes-view "hello world") (bytes-view "o w"))) ; 4, in the middle
(show-idx (index-of-bytes (bytes-view "banana") (bytes-view "na"))) ; 2, first of two
(show-idx (index-of-bytes (bytes-view "aaab") (bytes-view "aab"))) ; 1, after false starts
(println "")
(show-idx (index-of-bytes (bytes-view "hello") (bytes-view "hellp"))) ; -1, last byte differs
(show-idx (index-of-bytes (bytes-view "hi") (bytes-view "hiya"))) ; -1, longer, no trap
(show-idx (index-of-bytes (bytes-view "") (bytes-view "a"))) ; -1, empty haystack
(show-idx (index-of-bytes (bytes-view "hello") (bytes-view ""))) ; 0, empty needle
(show-idx (index-of-bytes (bytes-view "") (bytes-view ""))) ; 0, both empty
(show-idx (index-of-bytes (bytes-view "hello") (bytes-view "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 (is-digit \0)) (show-bool (is-digit \9)) (show-bool (is-digit \/))
(show-bool (is-digit \:)) (show-bool (is-digit \a))
(println "")
(show-bool (is-space \space)) (show-bool (is-space \tab))
(show-bool (is-space \newline)) (show-bool (is-space \return))
(show-bool (is-space \a)) (show-bool (is-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-view "0")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "3.5")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "-3.5")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "+0.25")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1e3")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1.5E-2")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "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-view "")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "abc")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1x")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view ".")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1e")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1e+")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view " 1")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "1 ")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "0x10")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "nan")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view "+")) (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-view "1.")) (Some v) v None -999.0)) (print " ")
(print (match (parse-f64 (bytes-view ".5")) (Some v) v None -999.0))
(println "")
;; Parsing a trimmed field, which is why both exist.
(print (match (parse-f64 (trim (bytes-view " 2.25 "))) (Some v) v None -999.0))
(println "")
0)