Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions lagoon/desk/tests/lib/lagoon-arithmetic.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/- *lagoon
/+ *test
/+ *lagoon
:::: /tests/lib/lagoon-arithmetic -- elementwise arithmetic
::
:: Table-driven: each op is asserted once across all precisions and a few
:: representative shapes (scalar, rectangular, square) via `fill`-built
:: rays, rather than hand-written per (op, bloq, shape, kind).
::
^|
|_ $: atol=_.1e-3 :: absolute tolerance for precision of operations
rtol=_.1e-5 :: relative tolerance for precision of operations
==
++ is-equal
|= [a=ray b=ray] ^- tang
?: =(a b) ~
:~ [%palm [": " ~ ~ ~] [leaf+"expected" "{<`ray`a>}"]]
[%palm [": " ~ ~ ~] [leaf+"actual " "{<`ray`b>}"]]
==
::
:: Representative shapes: a 1x1 scalar, a 2x3 rectangle, a 3x3 square.
++ shapes ^- (list (list @)) ~[~[1 1] ~[2 3] ~[3 3]]
:: Per-precision sample values, written once here instead of inline in
:: every arm. [bloq zero one two three five].
+$ rrow [bloq=@ zero=@ one=@ two=@ three=@ five=@]
++ reals
^- (list rrow)
:~ [4 .~~0 .~~1 .~~2 .~~3 .~~5]
[5 .0 .1 .2 .3 .5]
[6 .~0 .~1 .~2 .~3 .~5]
[7 .~~~0 .~~~1 .~~~2 .~~~3 .~~~5]
==
++ uint-bloqs ^- (list @) ~[3 4 5 6]
:: Run an %i754 assertion over reals x shapes.
++ each-real
|= fun=$-([rrow meta] tang)
^- tang
%- zing
%+ turn reals
|= r=rrow
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun r [shp bloq.r %i754 ~])
:: Run a %uint assertion over bloqs x shapes.
++ each-uint
|= fun=$-(meta tang)
^- tang
%- zing
%+ turn uint-bloqs
|= b=@
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun [shp b %uint ~])
::
:: ones + ones = twos
++ test-add ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m two.r) (add:la (fill:la m one.r) (fill:la m one.r)))
++ test-add-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 2) (add:la (fill:la m 1) (fill:la m 1)))
:: ones - ones = zeros
++ test-sub ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (sub:la (fill:la m one.r) (fill:la m one.r)))
++ test-sub-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 0) (sub:la (fill:la m 1) (fill:la m 1)))
:: fives - threes = twos (asymmetric: distinguishes x-y from y-x and from
:: returning an unmodified operand -- the bug fixed in the sub jet)
++ test-sub-asym ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m two.r) (sub:la (fill:la m five.r) (fill:la m three.r)))
:: ones * ones = ones
++ test-mul ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (mul:la (fill:la m one.r) (fill:la m one.r)))
++ test-mul-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 1) (mul:la (fill:la m 1) (fill:la m 1)))
:: ones / ones = ones
++ test-div ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (div:la (fill:la m one.r) (fill:la m one.r)))
++ test-div-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 1) (div:la (fill:la m 1) (fill:la m 1)))
:: ones mod ones = zeros
++ test-mod ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (mod:la (fill:la m one.r) (fill:la m one.r)))
++ test-mod-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 0) (mod:la (fill:la m 1) (fill:la m 1)))
::
:: Regression: mod must round the quotient with the active mode (default
:: %n, round-nearest), not truncate. 5 mod 3 = 5 - 3*round(5/3) = -1.
++ test-mod-nearest-6r ^- tang
=/ m `meta`[~[1 1] 6 %i754 ~]
(is-equal (fill:la m .~-1) (mod:la (fill:la m .~5) (fill:la m .~3)))
:: Regression: quad mod-scalar reciprocal constant (was 0.0, returned x).
:: 7 mod 3 = 1, 5 mod 3 = 2.
++ test-mods-7r ^- tang
=/ input-mods-1x2-7r (en-ray:la [meta=[shape=~[1 2] bloq=7 kind=%i754 tail=~] baum=~[~[.~~~7.0 .~~~5.0]]])
=/ canon-mods-1x2-7r (en-ray:la [meta=[shape=~[1 2] bloq=7 kind=%i754 tail=~] baum=~[~[.~~~1.0 .~~~2.0]]])
%+ is-equal
canon-mods-1x2-7r
(mod-scalar:la input-mods-1x2-7r .~~~3.0)
--
102 changes: 102 additions & 0 deletions lagoon/desk/tests/lib/lagoon-builders.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/- *lagoon
/+ *test
/+ *lagoon
:::: /tests/lib/lagoon-builders -- array builders
::
:: Table-driven across precisions and representative shapes. zeros/ones
:: are cross-checked against `fill`; eye/range/linspace against small
:: explicitly-built canons.
::
^|
|_ $: atol=_.1e-3 :: absolute tolerance for precision of operations
rtol=_.1e-5 :: relative tolerance for precision of operations
==
++ is-equal
|= [a=ray b=ray] ^- tang
?: =(a b) ~
:~ [%palm [": " ~ ~ ~] [leaf+"expected" "{<`ray`a>}"]]
[%palm [": " ~ ~ ~] [leaf+"actual " "{<`ray`b>}"]]
==
::
++ shapes ^- (list (list @)) ~[~[1 1] ~[2 3] ~[3 3]]
+$ rrow [bloq=@ zero=@ one=@ two=@ three=@ five=@]
++ reals
^- (list rrow)
:~ [4 .~~0 .~~1 .~~2 .~~3 .~~5]
[5 .0 .1 .2 .3 .5]
[6 .~0 .~1 .~2 .~3 .~5]
[7 .~~~0 .~~~1 .~~~2 .~~~3 .~~~5]
==
++ uint-bloqs ^- (list @) ~[3 4 5 6]
++ each-real
|= fun=$-([rrow meta] tang)
^- tang
%- zing
%+ turn reals
|= r=rrow
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun r [shp bloq.r %i754 ~])
++ each-uint
|= fun=$-(meta tang)
^- tang
%- zing
%+ turn uint-bloqs
|= b=@
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun [shp b %uint ~])
:: iterate just the precisions (for builders whose shape is fixed)
++ each-prec
|= fun=$-(rrow tang)
^- tang
%- zing
(turn reals fun)
::
:: zeros == a fill with the zero value; ones == a fill with the one value.
++ test-zeros ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (zeros:la m))
++ test-zeros-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 0) (zeros:la m))
++ test-ones ^- tang
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (ones:la m))
++ test-ones-uint ^- tang
%- each-uint
|= m=meta
(is-equal (fill:la m 1) (ones:la m))
:: eye 2x2 == [[1 0] [0 1]] and eye 3x3 == identity, per precision.
++ test-eye-2x2 ^- tang
%- each-prec
|= r=rrow
=/ m `meta`[~[2 2] bloq.r %i754 ~]
(is-equal (en-ray:la [m ~[~[one.r zero.r] ~[zero.r one.r]]]) (eye:la m))
++ test-eye-3x3 ^- tang
%- each-prec
|= r=rrow
=/ m `meta`[~[3 3] bloq.r %i754 ~]
%+ is-equal
(en-ray:la [m ~[~[one.r zero.r zero.r] ~[zero.r one.r zero.r] ~[zero.r zero.r one.r]]])
(eye:la m)
:: range [0, 3) step 1 == [0 1 2], per precision.
++ test-range ^- tang
%- each-prec
|= r=rrow
=/ m `meta`[~[3] bloq.r %i754 ~]
(is-equal (en-ray:la [m ~[zero.r one.r two.r]]) (range:la m [zero.r three.r] one.r))
:: linspace [0, 2] in 3 steps == [0 1 2], per precision.
++ test-linspace ^- tang
%- each-prec
|= r=rrow
=/ m `meta`[~[3] bloq.r %i754 ~]
(is-equal (en-ray:la [m ~[zero.r one.r two.r]]) (linspace:la m [zero.r two.r] 3))
--
169 changes: 169 additions & 0 deletions lagoon/desk/tests/lib/lagoon-compare-reduce.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/- *lagoon
/+ *test
/+ *lagoon
:::: /tests/lib/lagoon-compare-reduce -- comparisons and reductions
::
:: Table-driven. Comparisons run over precisions x shapes and cover all
:: three branches (>, <, =) per op rather than only the symmetric ones-vs-
:: ones case the old suite used. Reductions run over precisions on a
:: distinct-valued vector and read the reduced scalar via its ravel head
:: (independent of min/max's output shape).
::
^|
|_ $: atol=_.1e-3 :: absolute tolerance for precision of operations
rtol=_.1e-5 :: relative tolerance for precision of operations
==
++ is-equal
|= [a=ray b=ray] ^- tang
?: =(a b) ~
:~ [%palm [": " ~ ~ ~] [leaf+"expected" "{<`ray`a>}"]]
[%palm [": " ~ ~ ~] [leaf+"actual " "{<`ray`b>}"]]
==
::
++ shapes ^- (list (list @)) ~[~[1 1] ~[2 3] ~[3 3]]
+$ rrow [bloq=@ zero=@ one=@ two=@ three=@ five=@]
++ reals
^- (list rrow)
:~ [4 .~~0 .~~1 .~~2 .~~3 .~~5]
[5 .0 .1 .2 .3 .5]
[6 .~0 .~1 .~2 .~3 .~5]
[7 .~~~0 .~~~1 .~~~2 .~~~3 .~~~5]
==
++ uint-bloqs ^- (list @) ~[3 4 5 6]
:: elementwise: %i754 over reals x shapes
++ each-real
|= fun=$-([rrow meta] tang)
^- tang
%- zing
%+ turn reals
|= r=rrow
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun r [shp bloq.r %i754 ~])
:: elementwise: %uint over bloqs x shapes
++ each-uint
|= fun=$-(meta tang)
^- tang
%- zing
%+ turn uint-bloqs
|= b=@
^- tang
%- zing
%+ turn shapes
|= shp=(list @)
(fun [shp b %uint ~])
:: reductions: one distinct-valued 1-D vector [zero one two] per precision
++ each-vec
|= fun=$-([rrow ray] tang)
^- tang
%- zing
%+ turn reals
|= r=rrow
(fun r (en-ray:la [`meta`[~[3] bloq.r %i754 ~] ~[zero.r one.r two.r]]))
:: head scalar of a reduced ray, rank-independent
++ hed |=(a=ray ^-(@ -:(ravel:la a)))
::
:: Comparisons (numeric convention true=1, false=0). Both branches of
:: each op are covered -- one true-producing and one false-producing
:: arm -- across all precisions and shapes. (The old suite only tested
:: the symmetric ones-vs-ones case, exercising one branch per op.)
++ test-gth-t ^- tang :: 2 > 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (gth:la (fill:la m two.r) (fill:la m one.r)))
++ test-gth-f ^- tang :: 1 > 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (gth:la (fill:la m one.r) (fill:la m one.r)))
++ test-gte-t ^- tang :: 1 >= 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (gte:la (fill:la m one.r) (fill:la m one.r)))
++ test-gte-f ^- tang :: 1 >= 2
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (gte:la (fill:la m one.r) (fill:la m two.r)))
++ test-lth-t ^- tang :: 1 < 2
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (lth:la (fill:la m one.r) (fill:la m two.r)))
++ test-lth-f ^- tang :: 1 < 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (lth:la (fill:la m one.r) (fill:la m one.r)))
++ test-lte-t ^- tang :: 1 <= 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m one.r) (lte:la (fill:la m one.r) (fill:la m one.r)))
++ test-lte-f ^- tang :: 2 <= 1
%- each-real
|= [r=rrow m=meta]
(is-equal (fill:la m zero.r) (lte:la (fill:la m two.r) (fill:la m one.r)))
++ test-gth-uint-t ^- tang :: 2 > 1
%- each-uint
|= m=meta
(is-equal (fill:la m 1) (gth:la (fill:la m 2) (fill:la m 1)))
++ test-gth-uint-f ^- tang :: 1 > 1
%- each-uint
|= m=meta
(is-equal (fill:la m 0) (gth:la (fill:la m 1) (fill:la m 1)))
++ test-lte-uint-t ^- tang :: 1 <= 1
%- each-uint
|= m=meta
(is-equal (fill:la m 1) (lte:la (fill:la m 1) (fill:la m 1)))
++ test-lte-uint-f ^- tang :: 2 <= 1
%- each-uint
|= m=meta
(is-equal (fill:la m 0) (lte:la (fill:la m 2) (fill:la m 1)))
::
:: Reductions over [zero one two]: min=zero, max=two, cumsum=three (0+1+2),
:: argmin=0, argmax=2. argmin/argmax assert the forward (ravel) index.
++ test-min ^- tang
%- each-vec
|= [r=rrow a=ray]
(expect-eq !>(`@`zero.r) !>((hed (min:la a))))
++ test-max ^- tang
%- each-vec
|= [r=rrow a=ray]
(expect-eq !>(`@`two.r) !>((hed (max:la a))))
++ test-cumsum ^- tang
%- each-vec
|= [r=rrow a=ray]
(expect-eq !>(`@`three.r) !>((hed (cumsum:la a))))
++ test-argmin ^- tang
%- each-vec
|= [r=rrow a=ray]
(expect-eq !>(`@ud`0) !>(`@ud`(argmin:la a)))
++ test-argmax ^- tang
%- each-vec
|= [r=rrow a=ray]
(expect-eq !>(`@ud`2) !>(`@ud`(argmax:la a)))
::
:: any/all over boolean rays (numeric true=1.0, false=0.0).
++ test-any-all ^- tang
=/ all-true (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.1.0 .1.0 .1.0]]])
=/ has-false (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.1.0 .0.0 .1.0]]])
=/ all-false (en-ray:la [meta=[shape=~[1 3] bloq=5 kind=%i754 tail=~] baum=~[~[.0.0 .0.0 .0.0]]])
;: weld
%+ expect-eq !>(%.y) !>((all:la all-true))
%+ expect-eq !>(%.y) !>((any:la all-true))
%+ expect-eq !>(%.n) !>((all:la has-false))
%+ expect-eq !>(%.y) !>((any:la has-false))
%+ expect-eq !>(%.n) !>((all:la all-false))
%+ expect-eq !>(%.n) !>((any:la all-false))
==
++ test-any-all-1d ^- tang
=/ all-true (en-ray:la [meta=[shape=~[3] bloq=5 kind=%i754 tail=~] baum=~[.1.0 .1.0 .1.0]])
=/ has-false (en-ray:la [meta=[shape=~[3] bloq=5 kind=%i754 tail=~] baum=~[.1.0 .0.0 .1.0]])
=/ all-false (en-ray:la [meta=[shape=~[3] bloq=5 kind=%i754 tail=~] baum=~[.0.0 .0.0 .0.0]])
;: weld
%+ expect-eq !>(%.y) !>((all:la all-true))
%+ expect-eq !>(%.y) !>((any:la all-true))
%+ expect-eq !>(%.n) !>((all:la has-false))
%+ expect-eq !>(%.y) !>((any:la has-false))
%+ expect-eq !>(%.n) !>((all:la all-false))
%+ expect-eq !>(%.n) !>((any:la all-false))
==
--
Loading