flan/test/programs/signedness.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

22 lines
1.2 KiB
Plaintext

;;;; Signedness, which nothing in the corpus was exercising.
;;;;
;;;; Found by mutation testing: emit.ml chooses ashr vs lshr and slt vs ult
;;;; from the operand's type, and both choices could be hardcoded to one arm
;;;; with the whole suite still green — because no program shifted a negative
;;;; integer right, and none compared an unsigned value above 2^31.
(defn main [] i32
;; An arithmetic shift keeps the sign. A logical one on -8 gives a number
;; near 2^63, which is the wrong answer that looks like a huge right one.
(print (>> (i64 -8) 1)) (println "") ; -4
(print (>> (i64 -1) 40)) (println "") ; -1, still, however far it goes
;; And unsigned stays unsigned: 3000000000 has its top bit set, so a signed
;; compare reads it as negative and answers the other way on every operator.
(let [big (bit-or (<< (u32 1) 31) (u32 1000))] ; 2^31 + 1000
(println (if (< big (u32 5)) "wrong: signed compare" "big is not small"))
(println (if (> big (u32 5)) "big is large" "wrong: signed compare"))
;; The same value through >>, which is logical on an unsigned type: a
;; signed shift here would keep the top bit and answer near 2^31 again.
(print (>> big 31)) (println "")) ; 1
0)