From 51c3b8b81b4cd453cd42efa5d98cb368afba40e5 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:52:56 +0100 Subject: [PATCH] conform/ needs only the core: library tests go to test/run, generic tests use local modules --- diff --git a/conform/err/BadProc.raz b/conform/err/BadProc.raz index 0930581..98d3aa6 100644 --- a/conform/err/BadProc.raz +++ b/conform/err/BadProc.raz @@ -1,6 +1,6 @@ module BadProc; -import Str, M := Map([]U8, I64, Str.cmp, Str.eq); +import Str, M := Table([]U8, I64, Str.cmp, Str.eq); -var m: M.Map; +var m: M.Table; end BadProc. diff --git a/conform/err/FirstUseLate.raz b/conform/err/FirstUseLate.raz index 13f88da..439b837 100644 --- a/conform/err/FirstUseLate.raz +++ b/conform/err/FirstUseLate.raz @@ -1,7 +1,7 @@ module FirstUseLate; -import L := List(Later); +import L := Pile(Later); -var l: L.List; (* made here, before Later is declared *) +var l: L.Pile; (* made here, before Later is declared *) type Later = I64; diff --git a/conform/err/NoArgs.raz b/conform/err/NoArgs.raz index f472904..a0095ef 100644 --- a/conform/err/NoArgs.raz +++ b/conform/err/NoArgs.raz @@ -1,3 +1,3 @@ module NoArgs; -import List; +import Pile; end NoArgs. diff --git a/conform/err/StmtArgKind.raz b/conform/err/StmtArgKind.raz index c88e4ba..736bf49 100644 --- a/conform/err/StmtArgKind.raz +++ b/conform/err/StmtArgKind.raz @@ -1,6 +1,6 @@ module StmtArgKind; -import Mem, L := List(Mem.Page); +import Mem, L := Pile(Mem.Page); -var l: L.List; +var l: L.Pile; end StmtArgKind. diff --git a/conform/err/StmtManyTypeArgs.raz b/conform/err/StmtManyTypeArgs.raz index e9847cb..d7dbaa0 100644 --- a/conform/err/StmtManyTypeArgs.raz +++ b/conform/err/StmtManyTypeArgs.raz @@ -1,6 +1,6 @@ module StmtManyTypeArgs; -import L := List(I64, I64); +import L := Pile(I64, I64); -var l: L.List; +var l: L.Pile; end StmtManyTypeArgs. diff --git a/conform/err/StmtNeedProc.raz b/conform/err/StmtNeedProc.raz index 01f87bc..841c6f0 100644 --- a/conform/err/StmtNeedProc.raz +++ b/conform/err/StmtNeedProc.raz @@ -1,6 +1,6 @@ module StmtNeedProc; -import Str, M := Map([]U8, I64, I64, Str.eq); +import Str, M := Table([]U8, I64, I64, Str.eq); -var m: M.Map; +var m: M.Table; end StmtNeedProc. diff --git a/conform/err/StmtNeedType.raz b/conform/err/StmtNeedType.raz index a3d751e..cb54891 100644 --- a/conform/err/StmtNeedType.raz +++ b/conform/err/StmtNeedType.raz @@ -1,6 +1,6 @@ module StmtNeedType; -import Str, M := Map(Str.cmp, I64, Str.cmp, Str.eq); +import Str, M := Table(Str.cmp, I64, Str.cmp, Str.eq); -var m: M.Map; +var m: M.Table; end StmtNeedType. diff --git a/conform/err/TwoTypes.raz b/conform/err/TwoTypes.raz index 3269f35..7a1a27a 100644 --- a/conform/err/TwoTypes.raz +++ b/conform/err/TwoTypes.raz @@ -1,7 +1,7 @@ module TwoTypes; -import A := List(I64), B := List(I32); +import A := Pile(I64), B := Pile(I32); proc main*; -var a: A.List; b: B.List; +var a: A.Pile; b: B.Pile; begin a := b; end; diff --git a/conform/err/mods/Pile.raz b/conform/err/mods/Pile.raz new file mode 100644 index 0000000..f86e1e2 --- /dev/null +++ b/conform/err/mods/Pile.raz @@ -0,0 +1,36 @@ +(* A generic module that needs no library: a stack of at most eight + values of T. *) +module Pile(T); + +const Max = 8; + +type Pile* = record + items: [Max]T; + len*: INT; + end; + +(* x on top; false if the pile is full. *) +proc push*(p: ^Pile; x: T): BOOL; +begin + if p^.len = Max then return false; end; + p^.items[p^.len] := x; + INC(p^.len); + return true; +end; + +(* The top, taken off; false if the pile is empty. *) +proc pop*(p: ^Pile): (T, BOOL); +var zero: T; +begin + if p^.len = 0 then return zero, false; end; + DEC(p^.len); + return p^.items[p^.len], true; +end; + +(* The top, left where it is; the pile must not be empty. *) +proc top*(p: ^Pile): T; +begin + return p^.items[p^.len - 1]; +end; + +end Pile. diff --git a/conform/err/mods/Table.raz b/conform/err/mods/Table.raz new file mode 100644 index 0000000..cbb965f --- /dev/null +++ b/conform/err/mods/Table.raz @@ -0,0 +1,48 @@ +(* A generic module with procedure parameters and no library: a table + of sixteen slots, filled by linear probing. *) +module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); + +const Slots = 16; + +type Table* = record + used: [Slots]BOOL; + keys: [Slots]K; + vals: [Slots]V; + len*: INT; + end; + +(* v for k, a new entry or a change; false if the table is full. *) +proc put*(t: ^Table; k: K; v: V): BOOL; +var i, n: INT; +begin + i := INT(hash(k) mod Slots); + for n := 1 to Slots do + if not t^.used[i] then + t^.used[i] := true; + t^.keys[i] := k; + t^.vals[i] := v; + INC(t^.len); + return true; + elsif eq(t^.keys[i], k) then + t^.vals[i] := v; + return true; + end; + i := (i + 1) mod Slots; + end; + return false; +end; + +(* What k has; false if it has nothing. *) +proc get*(t: ^Table; k: K): (V, BOOL); +var i, n: INT; zero: V; +begin + i := INT(hash(k) mod Slots); + for n := 1 to Slots do + if not t^.used[i] then return zero, false; end; + if eq(t^.keys[i], k) then return t^.vals[i], true; end; + i := (i + 1) mod Slots; + end; + return zero, false; +end; + +end Table. diff --git a/conform/run/Alloc.expect b/conform/run/Alloc.expect deleted file mode 100644 index db5ee40..0000000 --- a/conform/run/Alloc.expect +++ /dev/null @@ -1,5 +0,0 @@ -1001000 -1001000 -7 -10100 -nil again 0 diff --git a/conform/run/Alloc.raz b/conform/run/Alloc.raz deleted file mode 100644 index 6d5245c..0000000 --- a/conform/run/Alloc.raz +++ /dev/null @@ -1,66 +0,0 @@ -module Alloc; -import Out, Mem, Arena, Heap; - -type - Node = record next: ^Node; val: I64; pad: [5]I64; end; - Big = record data: [1000]I64; end; - -proc build(a: ^Mem.Allocator; n: I64): ^Node; -var head, p: ^Node; i: I64; -begin - head := nil; - for i := 1 to n do - NEW(p, a); - if p = nil then return nil; end; - if p^.val # 0 then Out.str("not zeroed "); end; - p^.val := i; - p^.pad[4] := i; - p^.next := head; - head := p; - end; - return head; -end; - -proc sum(p: ^Node): I64; -var s: I64; -begin - s := 0; - while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end; - return s; -end; - -proc release(a: ^Mem.Allocator; p: ^Node); -var next: ^Node; -begin - while p # nil do - next := p^.next; - Mem.free(a, ^U8(p)); - p := next; - end; -end; - -proc main*; -var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node; -begin - l := build(Heap.heap, 1000); - Out.int(sum(l)); Out.ln; - release(Heap.heap, l); - m := build(Heap.heap, 1000); - Out.int(sum(m)); Out.ln; - NEW(b, Heap.heap); - b^.data[999] := 7; - Out.int(b^.data[999] + b^.data[0]); Out.ln; - Mem.free(Heap.heap, ^U8(b)); - if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end; - l := build(ADR(ar.base), 100); - Out.int(sum(l)); Out.ln; - l := build(ADR(ar.base), 1000); - if l = nil then Out.str("nil "); end; - Arena.reset(ADR(ar)); - NEW(again, ADR(ar.base)); - if again # nil then Out.str("again "); Out.int(again^.val); end; - Out.ln; - Arena.done(ADR(ar)); -end; - -end Alloc. diff --git a/conform/run/FirstUse.raz b/conform/run/FirstUse.raz index 1044a97..35a8c36 100644 --- a/conform/run/FirstUse.raz +++ b/conform/run/FirstUse.raz @@ -1,10 +1,10 @@ module FirstUse; -import Out, Str, Heap, Table := Map([]U8, Colour, Str.hash, Str.eq), - Ints := List(I64), More := List(I64), Never := Loud(I64), Ones := Loud(U8); +import Out, Str, Table := Table([]U8, Colour, Str.hash, Str.eq), + Ints := Pile(I64), More := Pile(I64), Never := Loud(I64), Ones := Loud(U8); type Colour = (red, green, blue); -var names: Table.Map; (* Table is made here, after Colour *) +var names: Table.Table; (* Table is made here, after Colour *) proc show(k: []U8); var c: Colour; found: BOOL; @@ -17,10 +17,10 @@ (* Ones is first used in a procedure, and made before its code *) proc main*; -var a: Ints.List; b: More.List; +var a: Ints.Pile; b: More.Pile; begin a := b; (* two imports, one instance *) - if a.head = nil then Out.str("an empty list\n"); end; + if a.len = 0 then Out.str("an empty list\n"); end; Out.str("in main, Ones has size "); Out.int(Ones.size); Out.ln; show("sky"); show("rose"); @@ -28,7 +28,6 @@ end; begin - Table.init(ADR(names), Heap.heap); _ := Table.put(ADR(names), "rose", red); _ := Table.put(ADR(names), "sky", blue); end FirstUse. diff --git a/conform/run/Generic.expect b/conform/run/Generic.expect index 5b3656e..4253b9d 100644 --- a/conform/run/Generic.expect +++ b/conform/run/Generic.expect @@ -2,5 +2,3 @@ 8 1 abtrue [] false -157 152 -1 -{ BEEF } x18446744073709551615 diff --git a/conform/run/Generic.raz b/conform/run/Generic.raz index e634f4a..7503b71 100644 --- a/conform/run/Generic.raz +++ b/conform/run/Generic.raz @@ -1,30 +1,26 @@ module Generic; -import Fmt, Buf, Str, Heap, Stack, Ints := List(I64), More := List(I64), Strs := List([]U8); +import Out, Stack, Ints := Pile(I64), More := Pile(I64), Strs := Pile([]U8); -proc twice(x: I64): I64; begin return x * 2; end; +proc yes(b: BOOL); +begin + if b then Out.str("true"); else Out.str("false"); end; +end; proc main*; -var l: Strs.List; v: I64; s: []U8; ok: BOOL; b: Buf.Buf; n: ^Ints.Node; +var l: Strs.Pile; v: I64; s: []U8; ok: BOOL; begin ok := Ints.push(Stack.p, 7); - ok := More.push(Stack.p, 8); - Fmt.print("{} {} {}\n", [Fmt.int(Stack.top()), Fmt.int(Stack.p^.len), Fmt.bool(ok)]); - n := Stack.p^.head; + ok := More.push(Stack.p, 8); (* two imports, one instance: one pile *) + Out.int(Stack.top()); Out.char(' '); Out.int(Stack.p^.len); Out.char(' '); yes(ok); Out.ln; v, ok := Ints.pop(Stack.p); - Fmt.print("{} {}\n", [Fmt.int(v), Fmt.int(Stack.p^.len)]); - Strs.init(ADR(l), Heap.heap); + Out.int(v); Out.char(' '); Out.int(Stack.p^.len); Out.ln; ok := Strs.push(ADR(l), "b"); ok := Strs.push(ADR(l), "a"); s, ok := Strs.pop(ADR(l)); - Fmt.print("{}{}{}\n", [Fmt.str(s), Fmt.str(l.head^.val), Fmt.bool(ok)]); + Out.str(s); Out.str(Strs.top(ADR(l))); yes(ok); Out.ln; s, ok := Strs.pop(ADR(l)); s, ok := Strs.pop(ADR(l)); - Fmt.print("[{}] {}\n", [Fmt.str(s), Fmt.bool(ok)]); - Buf.init(ADR(b), Heap.heap); - for v := 1 to 40 do Fmt.write(ADR(b), "{},", [Fmt.int(v * v)]); end; - Fmt.print("{} {} {}\n", [Fmt.int(b.len), Fmt.int(Str.find(Buf.bytes(ADR(b)), "1600")), Fmt.int(Str.cmp("ab", "b"))]); - Buf.done(ADR(b)); - Fmt.print("{{ {} }} {}{}\n", [Fmt.hex(48879), Fmt.char('x'), Fmt.uint(0FFFFFFFFFFFFFFFFH)]); + Out.char('['); Out.str(s); Out.str("] "); yes(ok); Out.ln; end; end Generic. diff --git a/conform/run/HeapCheck.expect b/conform/run/HeapCheck.expect deleted file mode 100644 index 1bffa8d..0000000 --- a/conform/run/HeapCheck.expect +++ /dev/null @@ -1,10 +0,0 @@ -nil -nil -nil -nil -nil -nil -a block -aligned -a block -freeing it again diff --git a/conform/run/HeapCheck.raz b/conform/run/HeapCheck.raz deleted file mode 100644 index 94a8b56..0000000 --- a/conform/run/HeapCheck.raz +++ /dev/null @@ -1,34 +0,0 @@ -(* The heap refuses bad sizes and alignments, and stops the program - when a block is freed twice. *) -module HeapCheck; -import Out, Heap, Mem; - -proc show(p: ^U8); -begin - if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end; -end; - -proc main*; -var p, q: ^U8; -begin - show(Mem.alloc(Heap.heap, -1, 8)); - show(Mem.alloc(Heap.heap, 1 shl 60, 8)); - show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8)); - show(Mem.alloc(Heap.heap, 10, 3)); - show(Mem.alloc(Heap.heap, 10, 0)); - show(Mem.alloc(Heap.heap, 10, 8192)); - p := Mem.alloc(Heap.heap, 10, 64); - show(p); - if INT(p) mod 64 = 0 then Out.str("aligned\n"); end; - Mem.free(Heap.heap, p); - q := Mem.alloc(Heap.heap, 100000, 8); - show(q); - Mem.free(Heap.heap, q); - p := Mem.alloc(Heap.heap, 24, 8); - Mem.free(Heap.heap, p); - Out.str("freeing it again\n"); - Mem.free(Heap.heap, p); - Out.str("not reached\n"); -end; - -end HeapCheck. diff --git a/conform/run/HeapCheck.status b/conform/run/HeapCheck.status deleted file mode 100644 index 0cfbf08..0000000 --- a/conform/run/HeapCheck.status +++ /dev/null @@ -1 +0,0 @@ -2 diff --git a/conform/run/HeapStray.expect b/conform/run/HeapStray.expect deleted file mode 100644 index 3102032..0000000 --- a/conform/run/HeapStray.expect +++ /dev/null @@ -1 +0,0 @@ -freeing a stray pointer diff --git a/conform/run/HeapStray.raz b/conform/run/HeapStray.raz deleted file mode 100644 index c8f69f9..0000000 --- a/conform/run/HeapStray.raz +++ /dev/null @@ -1,14 +0,0 @@ -(* Freeing what the heap never gave out stops the program. *) -module HeapStray; -import Out, Heap, Mem; - -var a: [4]I64; - -proc main*; -begin - Out.str("freeing a stray pointer\n"); - Mem.free(Heap.heap, ^U8(ADR(a[2]))); - Out.str("not reached\n"); -end; - -end HeapStray. diff --git a/conform/run/HeapStray.status b/conform/run/HeapStray.status deleted file mode 100644 index 0cfbf08..0000000 --- a/conform/run/HeapStray.status +++ /dev/null @@ -1 +0,0 @@ -2 diff --git a/conform/run/MapRemove.expect b/conform/run/MapRemove.expect deleted file mode 100644 index 2d3431b..0000000 --- a/conform/run/MapRemove.expect +++ /dev/null @@ -1,2 +0,0 @@ -341 entries, 341 of them, summing 6658308 -walked 341, summing 6658308 diff --git a/conform/run/MapRemove.raz b/conform/run/MapRemove.raz deleted file mode 100644 index d40f37d..0000000 --- a/conform/run/MapRemove.raz +++ /dev/null @@ -1,56 +0,0 @@ -(* Map's remove and next, against an array that holds the same: keys - hashed into runs of eight, so that removing moves entries back, - around the end of the slots too. *) -module MapRemove; -import Fmt, Heap, Rt, Ints := Map(I64, I64, near, same); - -const N = 500; - -proc near(k: I64): U64; -begin - return U64(k div 8 * 977); -end; - -proc same(a, b: I64): BOOL; -begin - return a = b; -end; - -proc main*; -var - m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64; - found, _ok: BOOL; -begin - Ints.init(ADR(m), Heap.heap); - seed := 1; - for i := 1 to 20000 do - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - k := seed mod N; - if seed div N mod 3 = 0 then - if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; - want[k] := 0; - else - _ok := Ints.put(ADR(m), k, i); - want[k] := i; - end; - end; - for k := 0 to N - 1 do - v, found := Ints.get(ADR(m), k); - if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end; - if found then INC(n); INC(sum, v); end; - end; - if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end; - Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]); - (* the walk sees each entry once *) - n := 0; sum := 0; - i, k, v := Ints.next(ADR(m), 0); - while i >= 0 do - if want[k] # v then Rt.stop("next: not what was put"); end; - want[k] := 0; - INC(n); INC(sum, v); - i, k, v := Ints.next(ADR(m), i); - end; - Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]); -end; - -end MapRemove. diff --git a/conform/run/RealRead.expect b/conform/run/RealRead.expect index 9255818..d3e1321 100644 --- a/conform/run/RealRead.expect +++ b/conform/run/RealRead.expect @@ -1,10 +1,10 @@ 7506AC5B262CA1FF 3FB999999999999A -FFFFFFFFFFFFF -1 -1 -0 -0 +000FFFFFFFFFFFFF +0000000000000001 +0000000000000001 +0000000000000000 +0000000000000000 7FEFFFFFFFFFFFFF 4340000000000000 4340000000000002 diff --git a/conform/run/RealRead.raz b/conform/run/RealRead.raz index 3f71900..b971f50 100644 --- a/conform/run/RealRead.raz +++ b/conform/run/RealRead.raz @@ -1,7 +1,7 @@ (* Real literals read to the nearest F64, ties to the even one: their bits, for numbers a reader of decimals easily gets one off. *) module RealRead; -import Fmt; +import Out; proc bits(x: F64): U64; var p: ^U64; @@ -12,7 +12,7 @@ proc show(x: F64); begin - Fmt.print("{}\n", [Fmt.hex(bits(x))]); + Out.hex(bits(x), 16); Out.ln; end; proc main*; diff --git a/conform/run/Sorted.raz b/conform/run/Sorted.raz index 9658ae9..6190949 100644 --- a/conform/run/Sorted.raz +++ b/conform/run/Sorted.raz @@ -1,7 +1,7 @@ -(* Sort: slices in many orders and of every length up to 40 come out +(* A generic sort: slices in many orders and of every length up to 40 come out in order, holding what they held; and a sort by a record's field. *) module Sorted; -import Fmt, Rt, Ints := Sort(I64, below), ByAge := Sort(Person, younger); +import Out, Rt, Ints := Order(I64, below), ByAge := Order(Person, younger); type Person = record name: []U8; age: I64; end; @@ -45,7 +45,7 @@ end; people := [{name: "Ana", age: 41}, {name: "Bo", age: 7}, {name: "Cy", age: 90}, {name: "Di", age: 23}]; ByAge.sort(people); - for i := 0 to 3 do Fmt.print("{} {}\n", [Fmt.str(people[i].name), Fmt.int(people[i].age)]); end; + for i := 0 to 3 do Out.str(people[i].name); Out.char(' '); Out.int(people[i].age); Out.ln; end; end; end Sorted. diff --git a/conform/run/mods/Order.raz b/conform/run/mods/Order.raz new file mode 100644 index 0000000..5ab18ec --- /dev/null +++ b/conform/run/mods/Order.raz @@ -0,0 +1,19 @@ +(* A generic module whose procedure parameter orders its values: an + insertion sort of a slice, in place and stable. *) +module Order(T; less: proc(a, b: T): BOOL); + +proc sort*(s: []T); +var i, j: INT; t: T; +begin + for i := 1 to LEN(s) - 1 do + t := s[i]; + j := i - 1; + while (j >= 0) and less(t, s[j]) do + s[j + 1] := s[j]; + DEC(j); + end; + s[j + 1] := t; + end; +end; + +end Order. diff --git a/conform/run/mods/Pile.raz b/conform/run/mods/Pile.raz new file mode 100644 index 0000000..f86e1e2 --- /dev/null +++ b/conform/run/mods/Pile.raz @@ -0,0 +1,36 @@ +(* A generic module that needs no library: a stack of at most eight + values of T. *) +module Pile(T); + +const Max = 8; + +type Pile* = record + items: [Max]T; + len*: INT; + end; + +(* x on top; false if the pile is full. *) +proc push*(p: ^Pile; x: T): BOOL; +begin + if p^.len = Max then return false; end; + p^.items[p^.len] := x; + INC(p^.len); + return true; +end; + +(* The top, taken off; false if the pile is empty. *) +proc pop*(p: ^Pile): (T, BOOL); +var zero: T; +begin + if p^.len = 0 then return zero, false; end; + DEC(p^.len); + return p^.items[p^.len], true; +end; + +(* The top, left where it is; the pile must not be empty. *) +proc top*(p: ^Pile): T; +begin + return p^.items[p^.len - 1]; +end; + +end Pile. diff --git a/conform/run/mods/Stack.raz b/conform/run/mods/Stack.raz index c47aea8..b8d4512 100644 --- a/conform/run/mods/Stack.raz +++ b/conform/run/mods/Stack.raz @@ -1,16 +1,15 @@ module Stack; -import Ints := List(I64), Heap; +import Ints := Pile(I64); var - s: Ints.List; - p*: ^Ints.List; (* s, which only this module could change by its name *) + s: Ints.Pile; + p*: ^Ints.Pile; (* s, which only this module could change by its name *) proc top*(): I64; begin - return s.head^.val; + return Ints.top(p); end; begin - Ints.init(ADR(s), Heap.heap); p := ADR(s); end Stack. diff --git a/conform/run/mods/Table.raz b/conform/run/mods/Table.raz new file mode 100644 index 0000000..cbb965f --- /dev/null +++ b/conform/run/mods/Table.raz @@ -0,0 +1,48 @@ +(* A generic module with procedure parameters and no library: a table + of sixteen slots, filled by linear probing. *) +module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); + +const Slots = 16; + +type Table* = record + used: [Slots]BOOL; + keys: [Slots]K; + vals: [Slots]V; + len*: INT; + end; + +(* v for k, a new entry or a change; false if the table is full. *) +proc put*(t: ^Table; k: K; v: V): BOOL; +var i, n: INT; +begin + i := INT(hash(k) mod Slots); + for n := 1 to Slots do + if not t^.used[i] then + t^.used[i] := true; + t^.keys[i] := k; + t^.vals[i] := v; + INC(t^.len); + return true; + elsif eq(t^.keys[i], k) then + t^.vals[i] := v; + return true; + end; + i := (i + 1) mod Slots; + end; + return false; +end; + +(* What k has; false if it has nothing. *) +proc get*(t: ^Table; k: K): (V, BOOL); +var i, n: INT; zero: V; +begin + i := INT(hash(k) mod Slots); + for n := 1 to Slots do + if not t^.used[i] then return zero, false; end; + if eq(t^.keys[i], k) then return t^.vals[i], true; end; + i := (i + 1) mod Slots; + end; + return zero, false; +end; + +end Table. diff --git a/test/msg/BadProc.err b/test/msg/BadProc.err index b3a0e1a..248e361 100644 --- a/test/msg/BadProc.err +++ b/test/msg/BadProc.err @@ -1,7 +1,7 @@ -conform/err/BadProc.raz:2:33: error: cmp doesn't have the signature of hash - 2 | import Str, M := Map([]U8, I64, Str.cmp, Str.eq); - | ^^^^^^^ -lib/Map.raz:4:18: note: the signature is here - 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); - | ^^^^ +conform/err/BadProc.raz:2:35: error: cmp doesn't have the signature of hash + 2 | import Str, M := Table([]U8, I64, Str.cmp, Str.eq); + | ^^^^^^^ +conform/err/mods/Table.raz:3:20: note: the signature is here + 3 | module Table(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); + | ^^^^ raz: 1 error diff --git a/test/msg/FirstUseLate.err b/test/msg/FirstUseLate.err index eeab7b1..eff7e45 100644 --- a/test/msg/FirstUseLate.err +++ b/test/msg/FirstUseLate.err @@ -1,7 +1,7 @@ conform/err/FirstUseLate.raz:2:18: error: Later isn't declared - 2 | import L := List(Later); + 2 | import L := Pile(Later); | ^^^^^ conform/err/FirstUseLate.raz:4:8: note: the instance is made here, where it is first used - 4 | var l: L.List; (* made here, before Later is declared *) + 4 | var l: L.Pile; (* made here, before Later is declared *) | ^ raz: 1 error diff --git a/test/msg/NoArgs.err b/test/msg/NoArgs.err index 5666fcc..ccd919a 100644 --- a/test/msg/NoArgs.err +++ b/test/msg/NoArgs.err @@ -1,4 +1,4 @@ -conform/err/NoArgs.raz:2:8: error: List needs a type for T - 2 | import List; +conform/err/NoArgs.raz:2:8: error: Pile needs a type for T + 2 | import Pile; | ^^^^ raz: 1 error diff --git a/test/msg/StmtArgKind.err b/test/msg/StmtArgKind.err index c791821..757dad8 100644 --- a/test/msg/StmtArgKind.err +++ b/test/msg/StmtArgKind.err @@ -1,4 +1,4 @@ conform/err/StmtArgKind.raz:2:23: error: Page isn't a type or a procedure - 2 | import Mem, L := List(Mem.Page); + 2 | import Mem, L := Pile(Mem.Page); | ^^^^^^^^ raz: 1 error diff --git a/test/msg/StmtManyTypeArgs.err b/test/msg/StmtManyTypeArgs.err index 1daf2f7..7d88e80 100644 --- a/test/msg/StmtManyTypeArgs.err +++ b/test/msg/StmtManyTypeArgs.err @@ -1,4 +1,4 @@ -conform/err/StmtManyTypeArgs.raz:2:8: error: too many type arguments for List - 2 | import L := List(I64, I64); +conform/err/StmtManyTypeArgs.raz:2:8: error: too many type arguments for Pile + 2 | import L := Pile(I64, I64); | ^^^^^^^^^^^^^^^^^^^ raz: 1 error diff --git a/test/msg/StmtNeedProc.err b/test/msg/StmtNeedProc.err index 975a5d8..ab18423 100644 --- a/test/msg/StmtNeedProc.err +++ b/test/msg/StmtNeedProc.err @@ -1,4 +1,4 @@ -conform/err/StmtNeedProc.raz:2:33: error: Map needs a procedure for hash - 2 | import Str, M := Map([]U8, I64, I64, Str.eq); - | ^^^ +conform/err/StmtNeedProc.raz:2:35: error: Table needs a procedure for hash + 2 | import Str, M := Table([]U8, I64, I64, Str.eq); + | ^^^ raz: 1 error diff --git a/test/msg/StmtNeedType.err b/test/msg/StmtNeedType.err index 9c2a054..6f1f4e2 100644 --- a/test/msg/StmtNeedType.err +++ b/test/msg/StmtNeedType.err @@ -1,4 +1,4 @@ -conform/err/StmtNeedType.raz:2:22: error: Map needs a type for K - 2 | import Str, M := Map(Str.cmp, I64, Str.cmp, Str.eq); - | ^^^^^^^ +conform/err/StmtNeedType.raz:2:24: error: Table needs a type for K + 2 | import Str, M := Table(Str.cmp, I64, Str.cmp, Str.eq); + | ^^^^^^^ raz: 1 error diff --git a/test/msg/TwoTypes.err b/test/msg/TwoTypes.err index c965196..8308745 100644 --- a/test/msg/TwoTypes.err +++ b/test/msg/TwoTypes.err @@ -1,4 +1,4 @@ -conform/err/TwoTypes.raz:6:10: error: can't use List(I32).List as List(I64).List +conform/err/TwoTypes.raz:6:10: error: can't use Pile(I32).Pile as Pile(I64).Pile 6 | a := b; | ^ raz: 1 error diff --git a/test/run/Alloc.expect b/test/run/Alloc.expect new file mode 100644 index 0000000..db5ee40 --- /dev/null +++ b/test/run/Alloc.expect @@ -0,0 +1,5 @@ +1001000 +1001000 +7 +10100 +nil again 0 diff --git a/test/run/Alloc.raz b/test/run/Alloc.raz new file mode 100644 index 0000000..6d5245c --- /dev/null +++ b/test/run/Alloc.raz @@ -0,0 +1,66 @@ +module Alloc; +import Out, Mem, Arena, Heap; + +type + Node = record next: ^Node; val: I64; pad: [5]I64; end; + Big = record data: [1000]I64; end; + +proc build(a: ^Mem.Allocator; n: I64): ^Node; +var head, p: ^Node; i: I64; +begin + head := nil; + for i := 1 to n do + NEW(p, a); + if p = nil then return nil; end; + if p^.val # 0 then Out.str("not zeroed "); end; + p^.val := i; + p^.pad[4] := i; + p^.next := head; + head := p; + end; + return head; +end; + +proc sum(p: ^Node): I64; +var s: I64; +begin + s := 0; + while p # nil do s := s + p^.val + p^.pad[4]; p := p^.next; end; + return s; +end; + +proc release(a: ^Mem.Allocator; p: ^Node); +var next: ^Node; +begin + while p # nil do + next := p^.next; + Mem.free(a, ^U8(p)); + p := next; + end; +end; + +proc main*; +var ar: Arena.Arena; l, m: ^Node; b: ^Big; i: I64; small: ^U8; again: ^Node; +begin + l := build(Heap.heap, 1000); + Out.int(sum(l)); Out.ln; + release(Heap.heap, l); + m := build(Heap.heap, 1000); + Out.int(sum(m)); Out.ln; + NEW(b, Heap.heap); + b^.data[999] := 7; + Out.int(b^.data[999] + b^.data[0]); Out.ln; + Mem.free(Heap.heap, ^U8(b)); + if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end; + l := build(ADR(ar.base), 100); + Out.int(sum(l)); Out.ln; + l := build(ADR(ar.base), 1000); + if l = nil then Out.str("nil "); end; + Arena.reset(ADR(ar)); + NEW(again, ADR(ar.base)); + if again # nil then Out.str("again "); Out.int(again^.val); end; + Out.ln; + Arena.done(ADR(ar)); +end; + +end Alloc. diff --git a/test/run/HeapCheck.expect b/test/run/HeapCheck.expect new file mode 100644 index 0000000..1bffa8d --- /dev/null +++ b/test/run/HeapCheck.expect @@ -0,0 +1,10 @@ +nil +nil +nil +nil +nil +nil +a block +aligned +a block +freeing it again diff --git a/test/run/HeapCheck.raz b/test/run/HeapCheck.raz new file mode 100644 index 0000000..94a8b56 --- /dev/null +++ b/test/run/HeapCheck.raz @@ -0,0 +1,34 @@ +(* The heap refuses bad sizes and alignments, and stops the program + when a block is freed twice. *) +module HeapCheck; +import Out, Heap, Mem; + +proc show(p: ^U8); +begin + if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end; +end; + +proc main*; +var p, q: ^U8; +begin + show(Mem.alloc(Heap.heap, -1, 8)); + show(Mem.alloc(Heap.heap, 1 shl 60, 8)); + show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8)); + show(Mem.alloc(Heap.heap, 10, 3)); + show(Mem.alloc(Heap.heap, 10, 0)); + show(Mem.alloc(Heap.heap, 10, 8192)); + p := Mem.alloc(Heap.heap, 10, 64); + show(p); + if INT(p) mod 64 = 0 then Out.str("aligned\n"); end; + Mem.free(Heap.heap, p); + q := Mem.alloc(Heap.heap, 100000, 8); + show(q); + Mem.free(Heap.heap, q); + p := Mem.alloc(Heap.heap, 24, 8); + Mem.free(Heap.heap, p); + Out.str("freeing it again\n"); + Mem.free(Heap.heap, p); + Out.str("not reached\n"); +end; + +end HeapCheck. diff --git a/test/run/HeapCheck.status b/test/run/HeapCheck.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/HeapCheck.status @@ -0,0 +1 @@ +2 diff --git a/test/run/HeapStray.expect b/test/run/HeapStray.expect new file mode 100644 index 0000000..3102032 --- /dev/null +++ b/test/run/HeapStray.expect @@ -0,0 +1 @@ +freeing a stray pointer diff --git a/test/run/HeapStray.raz b/test/run/HeapStray.raz new file mode 100644 index 0000000..c8f69f9 --- /dev/null +++ b/test/run/HeapStray.raz @@ -0,0 +1,14 @@ +(* Freeing what the heap never gave out stops the program. *) +module HeapStray; +import Out, Heap, Mem; + +var a: [4]I64; + +proc main*; +begin + Out.str("freeing a stray pointer\n"); + Mem.free(Heap.heap, ^U8(ADR(a[2]))); + Out.str("not reached\n"); +end; + +end HeapStray. diff --git a/test/run/HeapStray.status b/test/run/HeapStray.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/HeapStray.status @@ -0,0 +1 @@ +2 diff --git a/test/run/MapRemove.expect b/test/run/MapRemove.expect new file mode 100644 index 0000000..2d3431b --- /dev/null +++ b/test/run/MapRemove.expect @@ -0,0 +1,2 @@ +341 entries, 341 of them, summing 6658308 +walked 341, summing 6658308 diff --git a/test/run/MapRemove.raz b/test/run/MapRemove.raz new file mode 100644 index 0000000..d40f37d --- /dev/null +++ b/test/run/MapRemove.raz @@ -0,0 +1,56 @@ +(* Map's remove and next, against an array that holds the same: keys + hashed into runs of eight, so that removing moves entries back, + around the end of the slots too. *) +module MapRemove; +import Fmt, Heap, Rt, Ints := Map(I64, I64, near, same); + +const N = 500; + +proc near(k: I64): U64; +begin + return U64(k div 8 * 977); +end; + +proc same(a, b: I64): BOOL; +begin + return a = b; +end; + +proc main*; +var + m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64; + found, _ok: BOOL; +begin + Ints.init(ADR(m), Heap.heap); + seed := 1; + for i := 1 to 20000 do + seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; + k := seed mod N; + if seed div N mod 3 = 0 then + if Ints.remove(ADR(m), k) # (want[k] # 0) then Rt.stop("remove: wrong answer"); end; + want[k] := 0; + else + _ok := Ints.put(ADR(m), k, i); + want[k] := i; + end; + end; + for k := 0 to N - 1 do + v, found := Ints.get(ADR(m), k); + if (found # (want[k] # 0)) or (v # want[k]) then Rt.stop("get: not what was put"); end; + if found then INC(n); INC(sum, v); end; + end; + if Ints.remove(ADR(m), N) then Rt.stop("remove: a key never put"); end; + Fmt.print("{} entries, {} of them, summing {}\n", [Fmt.int(m.len), Fmt.int(n), Fmt.int(sum)]); + (* the walk sees each entry once *) + n := 0; sum := 0; + i, k, v := Ints.next(ADR(m), 0); + while i >= 0 do + if want[k] # v then Rt.stop("next: not what was put"); end; + want[k] := 0; + INC(n); INC(sum, v); + i, k, v := Ints.next(ADR(m), i); + end; + Fmt.print("walked {}, summing {}\n", [Fmt.int(n), Fmt.int(sum)]); +end; + +end MapRemove.