From 216a12cd02337c83c889a667063e0c06f2944e65 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 25 Jan 2017 20:30:44 -0400 Subject: - Moved the lux/data/struct/* modules to lux/data/coll/*. - Did the same for the lux/codata/struct/* modules. - Moved lux/codata/io to lux/io. - Moved lux/control/effect to lux/effect. - Renamed "sample" functions to "this" functions. - Renamed "at" functions to "nth" functions. --- stdlib/test/test/lux.lux | 2 +- stdlib/test/test/lux/cli.lux | 4 +- stdlib/test/test/lux/codata/coll/stream.lux | 106 +++++++++++ stdlib/test/test/lux/codata/cont.lux | 2 +- stdlib/test/test/lux/codata/env.lux | 2 +- stdlib/test/test/lux/codata/io.lux | 27 --- stdlib/test/test/lux/codata/state.lux | 2 +- stdlib/test/test/lux/codata/struct/stream.lux | 106 ----------- stdlib/test/test/lux/concurrency/actor.lux | 6 +- stdlib/test/test/lux/concurrency/atom.lux | 4 +- stdlib/test/test/lux/concurrency/frp.lux | 6 +- stdlib/test/test/lux/concurrency/promise.lux | 6 +- stdlib/test/test/lux/concurrency/stm.lux | 4 +- stdlib/test/test/lux/control/effect.lux | 77 -------- stdlib/test/test/lux/data/bit.lux | 4 +- stdlib/test/test/lux/data/bool.lux | 2 +- stdlib/test/test/lux/data/char.lux | 4 +- stdlib/test/test/lux/data/coll/array.lux | 135 ++++++++++++++ stdlib/test/test/lux/data/coll/dict.lux | 137 ++++++++++++++ stdlib/test/test/lux/data/coll/list.lux | 226 +++++++++++++++++++++++ stdlib/test/test/lux/data/coll/queue.lux | 55 ++++++ stdlib/test/test/lux/data/coll/set.lux | 68 +++++++ stdlib/test/test/lux/data/coll/stack.lux | 48 +++++ stdlib/test/test/lux/data/coll/tree/rose.lux | 40 ++++ stdlib/test/test/lux/data/coll/tree/zipper.lux | 128 +++++++++++++ stdlib/test/test/lux/data/coll/vector.lux | 78 ++++++++ stdlib/test/test/lux/data/error.lux | 2 +- stdlib/test/test/lux/data/error/exception.lux | 2 +- stdlib/test/test/lux/data/format/json.lux | 8 +- stdlib/test/test/lux/data/ident.lux | 2 +- stdlib/test/test/lux/data/identity.lux | 2 +- stdlib/test/test/lux/data/log.lux | 2 +- stdlib/test/test/lux/data/maybe.lux | 2 +- stdlib/test/test/lux/data/number.lux | 2 +- stdlib/test/test/lux/data/product.lux | 2 +- stdlib/test/test/lux/data/struct/array.lux | 135 -------------- stdlib/test/test/lux/data/struct/dict.lux | 137 -------------- stdlib/test/test/lux/data/struct/list.lux | 226 ----------------------- stdlib/test/test/lux/data/struct/queue.lux | 55 ------ stdlib/test/test/lux/data/struct/set.lux | 68 ------- stdlib/test/test/lux/data/struct/stack.lux | 48 ----- stdlib/test/test/lux/data/struct/tree/rose.lux | 40 ---- stdlib/test/test/lux/data/struct/tree/zipper.lux | 128 ------------- stdlib/test/test/lux/data/struct/vector.lux | 78 -------- stdlib/test/test/lux/data/sum.lux | 4 +- stdlib/test/test/lux/data/text.lux | 35 ++-- stdlib/test/test/lux/data/text/format.lux | 2 +- stdlib/test/test/lux/effect.lux | 77 ++++++++ stdlib/test/test/lux/host.lux | 6 +- stdlib/test/test/lux/io.lux | 27 +++ stdlib/test/test/lux/lexer.lux | 4 +- stdlib/test/test/lux/lexer/regex.lux | 2 +- stdlib/test/test/lux/macro/ast.lux | 2 +- stdlib/test/test/lux/macro/poly/eq.lux | 2 +- stdlib/test/test/lux/macro/poly/functor.lux | 2 +- stdlib/test/test/lux/macro/poly/text-encoder.lux | 2 +- stdlib/test/test/lux/macro/syntax.lux | 10 +- stdlib/test/test/lux/math.lux | 4 +- stdlib/test/test/lux/math/complex.lux | 4 +- stdlib/test/test/lux/math/logic/continuous.lux | 2 +- stdlib/test/test/lux/math/ratio.lux | 4 +- stdlib/test/test/lux/math/simple.lux | 4 +- stdlib/test/test/lux/pipe.lux | 2 +- stdlib/test/test/lux/type.lux | 4 +- stdlib/test/test/lux/type/auto.lux | 4 +- stdlib/test/test/lux/type/check.lux | 4 +- stdlib/test/tests.lux | 28 +-- 67 files changed, 1227 insertions(+), 1226 deletions(-) create mode 100644 stdlib/test/test/lux/codata/coll/stream.lux delete mode 100644 stdlib/test/test/lux/codata/io.lux delete mode 100644 stdlib/test/test/lux/codata/struct/stream.lux delete mode 100644 stdlib/test/test/lux/control/effect.lux create mode 100644 stdlib/test/test/lux/data/coll/array.lux create mode 100644 stdlib/test/test/lux/data/coll/dict.lux create mode 100644 stdlib/test/test/lux/data/coll/list.lux create mode 100644 stdlib/test/test/lux/data/coll/queue.lux create mode 100644 stdlib/test/test/lux/data/coll/set.lux create mode 100644 stdlib/test/test/lux/data/coll/stack.lux create mode 100644 stdlib/test/test/lux/data/coll/tree/rose.lux create mode 100644 stdlib/test/test/lux/data/coll/tree/zipper.lux create mode 100644 stdlib/test/test/lux/data/coll/vector.lux delete mode 100644 stdlib/test/test/lux/data/struct/array.lux delete mode 100644 stdlib/test/test/lux/data/struct/dict.lux delete mode 100644 stdlib/test/test/lux/data/struct/list.lux delete mode 100644 stdlib/test/test/lux/data/struct/queue.lux delete mode 100644 stdlib/test/test/lux/data/struct/set.lux delete mode 100644 stdlib/test/test/lux/data/struct/stack.lux delete mode 100644 stdlib/test/test/lux/data/struct/tree/rose.lux delete mode 100644 stdlib/test/test/lux/data/struct/tree/zipper.lux delete mode 100644 stdlib/test/test/lux/data/struct/vector.lux create mode 100644 stdlib/test/test/lux/effect.lux create mode 100644 stdlib/test/test/lux/io.lux (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux.lux b/stdlib/test/test/lux.lux index cd394ac76..947676665 100644 --- a/stdlib/test/test/lux.lux +++ b/stdlib/test/test/lux.lux @@ -7,7 +7,7 @@ lux lux/test (lux (control monad) - (codata [io]) + [io] [math] ["R" random] (data [text "T/" Eq] diff --git a/stdlib/test/test/lux/cli.lux b/stdlib/test/test/lux/cli.lux index 2aab87ae4..35b342000 100644 --- a/stdlib/test/test/lux/cli.lux +++ b/stdlib/test/test/lux/cli.lux @@ -5,14 +5,14 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data text/format [text "Text/" Eq] [number] [product] [sum] - (struct [list])) + (coll [list])) (codata function) ["&" cli] ["R" random] diff --git a/stdlib/test/test/lux/codata/coll/stream.lux b/stdlib/test/test/lux/codata/coll/stream.lux new file mode 100644 index 000000000..9dc10c07f --- /dev/null +++ b/stdlib/test/test/lux/codata/coll/stream.lux @@ -0,0 +1,106 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad + comonad) + (data [text "Text/" Monoid] + text/format + (coll [list]) + [number "Nat/" Codec]) + (codata function + [cont] + (coll ["&" stream])) + ["R" random] + pipe) + lux/test) + +(test: "Streams" + [size (|> R;nat (:: @ map (|>. (n.% +100) (n.max +2)))) + offset (|> R;nat (:: @ map (n.% +100))) + factor (|> R;nat (:: @ map (|>. (n.% +100) (n.max +2)))) + elem R;nat + cycle-seed (R;list size R;nat) + cycle-sample-idx (|> R;nat (:: @ map (n.% +1000))) + #let [(^open "List/") (list;Eq number;Eq) + sample0 (&;iterate n.inc +0) + sample1 (&;iterate n.inc offset)]] + ($_ seq + (assert "Can move along a stream and take slices off it." + (and (and (List/= (list;n.range +0 (n.dec size)) + (&;take size sample0)) + (List/= (list;n.range offset (n.dec (n.+ offset size))) + (&;take size (&;drop offset sample0))) + (let [[drops takes...] (&;split size sample0)] + (and (List/= (list;n.range +0 (n.dec size)) + drops) + (List/= (list;n.range size (n.dec (n.* +2 size))) + (&;take size takes...))))) + (and (List/= (list;n.range +0 (n.dec size)) + (&;take-while (n.< size) sample0)) + (List/= (list;n.range offset (n.dec (n.+ offset size))) + (&;take-while (n.< (n.+ offset size)) + (&;drop-while (n.< offset) sample0))) + (let [[drops takes...] (&;split-while (n.< size) sample0)] + (and (List/= (list;n.range +0 (n.dec size)) + drops) + (List/= (list;n.range size (n.dec (n.* +2 size))) + (&;take-while (n.< (n.* +2 size)) takes...))))) + )) + + (assert "Can repeat any element and infinite number of times." + (n.= elem (&;nth offset (&;repeat elem)))) + + (assert "Can obtain the head & tail of a stream." + (and (n.= offset (&;head sample1)) + (List/= (list;n.range (n.inc offset) (n.+ offset size)) + (&;take size (&;tail sample1))))) + + (assert "Can filter streams." + (and (n.= (n.* +2 offset) + (&;nth offset + (&;filter n.even? sample0))) + (let [[evens odds] (&;partition n.even? (&;iterate n.inc +0))] + (and (n.= (n.* +2 offset) + (&;nth offset evens)) + (n.= (n.inc (n.* +2 offset)) + (&;nth offset odds)))))) + + (assert "Functor goes over 'all' elements in a stream." + (let [(^open "&/") &;Functor + there (&/map (n.* factor) sample0) + back-again (&/map (n./ factor) there)] + (and (not (List/= (&;take size sample0) + (&;take size there))) + (List/= (&;take size sample0) + (&;take size back-again))))) + + (assert "CoMonad produces a value for every element in a stream." + (let [(^open "&/") &;Functor] + (List/= (&;take size (&/map (n.* factor) sample1)) + (&;take size + (be &;CoMonad + [inputs sample1] + (n.* factor (&;head inputs))))))) + + (assert "'unfold' generalizes 'iterate'." + (let [(^open "&/") &;Functor + (^open "List/") (list;Eq text;Eq)] + (List/= (&;take size + (&/map Nat/encode (&;iterate n.inc offset))) + (&;take size + (&;unfold (lambda [n] [(n.inc n) (Nat/encode n)]) + offset))))) + + (assert "Can cycle over the same elements as an infinite stream." + (|> (&;cycle cycle-seed) + (default (undefined)) + (&;nth cycle-sample-idx) + (n.= (default (undefined) + (list;nth (n.% size cycle-sample-idx) + cycle-seed))))) + )) diff --git a/stdlib/test/test/lux/codata/cont.lux b/stdlib/test/test/lux/codata/cont.lux index 39fdd6f42..b0e906248 100644 --- a/stdlib/test/test/lux/codata/cont.lux +++ b/stdlib/test/test/lux/codata/cont.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format diff --git a/stdlib/test/test/lux/codata/env.lux b/stdlib/test/test/lux/codata/env.lux index 4f5a37de9..2acad142f 100644 --- a/stdlib/test/test/lux/codata/env.lux +++ b/stdlib/test/test/lux/codata/env.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format diff --git a/stdlib/test/test/lux/codata/io.lux b/stdlib/test/test/lux/codata/io.lux deleted file mode 100644 index 7965869d0..000000000 --- a/stdlib/test/test/lux/codata/io.lux +++ /dev/null @@ -1,27 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (control monad) - (data [text "Text/" Monoid Eq] - text/format - [number]) - (codata function - ["&" io])) - lux/test) - -(test: "I/O" - ($_ seq - (assert "" (Text/= "YOLO" (&;run (&;io "YOLO")))) - (assert "" (i.= 11 (&;run (:: &;Functor map i.inc (&;io 10))))) - (assert "" (i.= 10 (&;run (:: &;Applicative wrap 10)))) - (assert "" (i.= 30 (&;run (let [(^open "&/") &;Applicative] - (&/apply (&/wrap (i.+ 10)) (&/wrap 20)))))) - (assert "" (i.= 30 (&;run (do &;Monad - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - (wrap (f x y)))))))) diff --git a/stdlib/test/test/lux/codata/state.lux b/stdlib/test/test/lux/codata/state.lux index 79a458cc0..d73ebaa58 100644 --- a/stdlib/test/test/lux/codata/state.lux +++ b/stdlib/test/test/lux/codata/state.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format diff --git a/stdlib/test/test/lux/codata/struct/stream.lux b/stdlib/test/test/lux/codata/struct/stream.lux deleted file mode 100644 index 41132b2ee..000000000 --- a/stdlib/test/test/lux/codata/struct/stream.lux +++ /dev/null @@ -1,106 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad - comonad) - (data [text "Text/" Monoid] - text/format - (struct [list]) - [number "Nat/" Codec]) - (codata function - [cont] - (struct ["&" stream])) - ["R" random] - pipe) - lux/test) - -(test: "Streams" - [size (|> R;nat (:: @ map (|>. (n.% +100) (n.max +2)))) - offset (|> R;nat (:: @ map (n.% +100))) - factor (|> R;nat (:: @ map (|>. (n.% +100) (n.max +2)))) - elem R;nat - cycle-seed (R;list size R;nat) - cycle-sample-idx (|> R;nat (:: @ map (n.% +1000))) - #let [(^open "List/") (list;Eq number;Eq) - sample0 (&;iterate n.inc +0) - sample1 (&;iterate n.inc offset)]] - ($_ seq - (assert "Can move along a stream and take slices off it." - (and (and (List/= (list;n.range +0 (n.dec size)) - (&;take size sample0)) - (List/= (list;n.range offset (n.dec (n.+ offset size))) - (&;take size (&;drop offset sample0))) - (let [[drops takes...] (&;split size sample0)] - (and (List/= (list;n.range +0 (n.dec size)) - drops) - (List/= (list;n.range size (n.dec (n.* +2 size))) - (&;take size takes...))))) - (and (List/= (list;n.range +0 (n.dec size)) - (&;take-while (n.< size) sample0)) - (List/= (list;n.range offset (n.dec (n.+ offset size))) - (&;take-while (n.< (n.+ offset size)) - (&;drop-while (n.< offset) sample0))) - (let [[drops takes...] (&;split-while (n.< size) sample0)] - (and (List/= (list;n.range +0 (n.dec size)) - drops) - (List/= (list;n.range size (n.dec (n.* +2 size))) - (&;take-while (n.< (n.* +2 size)) takes...))))) - )) - - (assert "Can repeat any element and infinite number of times." - (n.= elem (&;at offset (&;repeat elem)))) - - (assert "Can obtain the head & tail of a stream." - (and (n.= offset (&;head sample1)) - (List/= (list;n.range (n.inc offset) (n.+ offset size)) - (&;take size (&;tail sample1))))) - - (assert "Can filter streams." - (and (n.= (n.* +2 offset) - (&;at offset - (&;filter n.even? sample0))) - (let [[evens odds] (&;partition n.even? (&;iterate n.inc +0))] - (and (n.= (n.* +2 offset) - (&;at offset evens)) - (n.= (n.inc (n.* +2 offset)) - (&;at offset odds)))))) - - (assert "Functor goes over 'all' elements in a stream." - (let [(^open "&/") &;Functor - there (&/map (n.* factor) sample0) - back-again (&/map (n./ factor) there)] - (and (not (List/= (&;take size sample0) - (&;take size there))) - (List/= (&;take size sample0) - (&;take size back-again))))) - - (assert "CoMonad produces a value for every element in a stream." - (let [(^open "&/") &;Functor] - (List/= (&;take size (&/map (n.* factor) sample1)) - (&;take size - (be &;CoMonad - [inputs sample1] - (n.* factor (&;head inputs))))))) - - (assert "'unfold' generalizes 'iterate'." - (let [(^open "&/") &;Functor - (^open "List/") (list;Eq text;Eq)] - (List/= (&;take size - (&/map Nat/encode (&;iterate n.inc offset))) - (&;take size - (&;unfold (lambda [n] [(n.inc n) (Nat/encode n)]) - offset))))) - - (assert "Can cycle over the same elements as an infinite stream." - (|> (&;cycle cycle-seed) - (default (undefined)) - (&;at cycle-sample-idx) - (n.= (default (undefined) - (list;at (n.% size cycle-sample-idx) - cycle-seed))))) - )) diff --git a/stdlib/test/test/lux/concurrency/actor.lux b/stdlib/test/test/lux/concurrency/actor.lux index 7136ab30d..5ae191512 100644 --- a/stdlib/test/test/lux/concurrency/actor.lux +++ b/stdlib/test/test/lux/concurrency/actor.lux @@ -5,14 +5,14 @@ (;module: lux - (lux (control monad) + (lux [io #- run] + (control monad) (data [number] text/format [error #- fail]) (concurrency [promise #+ Promise Monad "Promise/" Monad] ["&" actor #+ actor:]) - (codata function - [io #- run])) + (codata function)) lux/test) (actor: Adder diff --git a/stdlib/test/test/lux/concurrency/atom.lux b/stdlib/test/test/lux/concurrency/atom.lux index 312bc0369..a2ef82562 100644 --- a/stdlib/test/test/lux/concurrency/atom.lux +++ b/stdlib/test/test/lux/concurrency/atom.lux @@ -5,10 +5,10 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [number] - (struct [list "" Functor]) + (coll [list "" Functor]) text/format) (concurrency ["&" atom]) ["R" random] diff --git a/stdlib/test/test/lux/concurrency/frp.lux b/stdlib/test/test/lux/concurrency/frp.lux index 6bec2ea37..2d0b75f95 100644 --- a/stdlib/test/test/lux/concurrency/frp.lux +++ b/stdlib/test/test/lux/concurrency/frp.lux @@ -5,14 +5,14 @@ (;module: lux - (lux (control monad) + (lux [io #- run] + (control monad) (data [number] text/format [error #- fail]) (concurrency [promise #+ Promise Monad "Promise/" Monad] ["&" frp]) - (codata function - [io #- run])) + (codata function)) lux/test) (def: (List->Chan values) diff --git a/stdlib/test/test/lux/concurrency/promise.lux b/stdlib/test/test/lux/concurrency/promise.lux index 53accdfcc..c217fccf9 100644 --- a/stdlib/test/test/lux/concurrency/promise.lux +++ b/stdlib/test/test/lux/concurrency/promise.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (control monad) + (lux [io #- run] + (control monad) (data [number] text/format [error #- fail]) (concurrency ["&" promise]) - (codata function - [io #- run]) + (codata function) ["R" random] pipe) lux/test) diff --git a/stdlib/test/test/lux/concurrency/stm.lux b/stdlib/test/test/lux/concurrency/stm.lux index 5b385c685..763804018 100644 --- a/stdlib/test/test/lux/concurrency/stm.lux +++ b/stdlib/test/test/lux/concurrency/stm.lux @@ -5,10 +5,10 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [number] - (struct [list "" Functor "List/" Fold]) + (coll [list "" Functor "List/" Fold]) text/format) (concurrency ["&" stm] [promise]) diff --git a/stdlib/test/test/lux/control/effect.lux b/stdlib/test/test/lux/control/effect.lux deleted file mode 100644 index be7eda3aa..000000000 --- a/stdlib/test/test/lux/control/effect.lux +++ /dev/null @@ -1,77 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io "IO/" Monad]) - (control monad - functor) - (data [text] - text/format) - [macro] - ["R" random] - pipe - (control effect)) - lux/test) - -(do-template [ ] - [(effect: - ( [Nat Text] Bool) - ( Nat))] - - [EffA opA fieldA] - [EffB opB fieldB] - [EffC opC fieldC] - ) - -(do-template [ ] - [(handler: _ - (=> [io;IO io;Monad]) - (def: ( size sample) - (IO/wrap ( size (text;size sample)))) - - (def: (IO/wrap )))] - - [EffA opA n.< fieldA +10] - [EffB opB n.= fieldB +20] - [EffC opC n.> fieldC +30] - ) - -(type: EffABC (|E EffA EffB EffC)) - -(def: Functor - (Functor EffABC) - (|F Functor Functor Functor)) - -(def: Handler - (Handler EffABC io;IO) - (|H io;Monad - Handler Handler Handler)) - -## [Tests] -(test: "Algebraic effects" - (let% [ (do-template [ ] - [(io;run (with-handler Handler - (doE Functor - [] - (lift ( "YOLO"))))) - (n.= (io;run (with-handler Handler - (doE Functor - [] - (lift )))))] - - [opA +10 fieldA +10] - [opB +4 fieldB +20] - [opC +2 fieldC +30])] - (assert "Can handle effects using handlers." - (and - - (n.= +60 (io;run (with-handler Handler - (doE Functor - [a (lift fieldA) - b (lift fieldB) - c (lift fieldC)] - (wrap ($_ n.+ a b c)))))) - )))) diff --git a/stdlib/test/test/lux/data/bit.lux b/stdlib/test/test/lux/data/bit.lux index 0b66b30f5..0da5cfd45 100644 --- a/stdlib/test/test/lux/data/bit.lux +++ b/stdlib/test/test/lux/data/bit.lux @@ -5,8 +5,8 @@ (;module: lux - (lux (control [monad]) - (codata [io]) + (lux [io] + (control [monad]) (data ["&" bit] number) ["R" random]) diff --git a/stdlib/test/test/lux/data/bool.lux b/stdlib/test/test/lux/data/bool.lux index 4c196546c..669232f47 100644 --- a/stdlib/test/test/lux/data/bool.lux +++ b/stdlib/test/test/lux/data/bool.lux @@ -6,7 +6,7 @@ (;module: lux (lux (control [monad]) - (codata [io]) + [io] (data bool) ["R" random]) lux/test) diff --git a/stdlib/test/test/lux/data/char.lux b/stdlib/test/test/lux/data/char.lux index 6482dd219..460e98b24 100644 --- a/stdlib/test/test/lux/data/char.lux +++ b/stdlib/test/test/lux/data/char.lux @@ -6,7 +6,7 @@ (;module: lux (lux (control [monad]) - (codata [io]) + [io] (data char [text] text/format) @@ -35,7 +35,7 @@ (#;Left _) false)) (|> value as-text - (text;at +0) (default (undefined)) + (text;nth +0) (default (undefined)) (:: Eq = value)))) (assert "Characters have an ordering relationship." diff --git a/stdlib/test/test/lux/data/coll/array.lux b/stdlib/test/test/lux/data/coll/array.lux new file mode 100644 index 000000000..236c2f915 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/array.lux @@ -0,0 +1,135 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux (control [monad]) + [io] + (data (coll ["&" array] + [list]) + [number]) + ["R" random] + pipe) + lux/test) + +(def: bounded-size + (R;Random Nat) + (|> R;nat + (:: R;Monad map (|>. (n.% +100) (n.+ +1))))) + +(test: "Arrays and their copies" + [size bounded-size + original (R;array size R;nat) + #let [clone (&;clone original) + copy (: (&;Array Nat) + (&;new size)) + manual-copy (: (&;Array Nat) + (&;new size))]] + ($_ seq + (assert "Size function must correctly return size of array." + (n.= size (&;size original))) + (assert "Cloning an array should yield and identical array, but not the same one." + (and (:: (&;Eq number;Eq) = original clone) + (not (is original clone)))) + (assert "Full-range manual copies should give the same result as cloning." + (exec (&;copy size +0 original +0 copy) + (and (:: (&;Eq number;Eq) = original copy) + (not (is original copy))))) + (assert "Array folding should go over all values." + (exec (:: &;Fold fold + (lambda [x idx] + (exec (&;put idx x manual-copy) + (n.inc idx))) + +0 + original) + (:: (&;Eq number;Eq) = original manual-copy))) + (assert "Transformations between (full) arrays and lists shouldn't cause lose or change any values." + (|> original + &;to-list &;from-list + (:: (&;Eq number;Eq) = original))) + )) + +(test: "Array mutation" + [size bounded-size + idx (:: @ map (n.% size) R;nat) + array (|> (R;array size R;nat) + (R;filter (|>. &;to-list (list;any? n.odd?)))) + #let [value (default (undefined) + (&;get idx array))]] + ($_ seq + (assert "Shouldn't be able to find a value in an unoccupied cell." + (case (&;get idx (&;remove idx array)) + (#;Some _) false + #;None true)) + (assert "You should be able to access values put into the array." + (case (&;get idx (&;put idx value array)) + (#;Some value') (n.= value' value) + #;None false)) + (assert "All cells should be occupied on a full array." + (and (n.= size (&;occupied array)) + (n.= +0 (&;vacant array)))) + (assert "Filtering mutates the array to remove invalid values." + (exec (&;filter n.even? array) + (and (n.< size (&;occupied array)) + (n.> +0 (&;vacant array)) + (n.= size (n.+ (&;occupied array) + (&;vacant array)))))) + )) + +(test: "Finding values." + [size bounded-size + array (|> (R;array size R;nat) + (R;filter (|>. &;to-list (list;any? n.even?))))] + ($_ seq + (assert "Can find values inside arrays." + (|> (&;find n.even? array) + (case> (#;Some _) true + #;None false))) + (assert "Can find values inside arrays (with access to indices)." + (|> (&;find+ (lambda [idx n] + (and (n.even? n) + (n.< size idx))) + array) + (case> (#;Some _) true + #;None false))))) + +(test: "Functor" + [size bounded-size + array (R;array size R;nat)] + (let [(^open) &;Functor + (^open) (&;Eq number;Eq)] + ($_ seq + (assert "Functor shouldn't alter original array." + (let [copy (map id array)] + (and (= array copy) + (not (is array copy))))) + (assert "Functor should go over all available array elements." + (let [there (map n.inc array) + back-again (map n.dec there)] + (and (not (= array there)) + (= array back-again))))))) + +(test: "Monoid" + [sizeL bounded-size + sizeR bounded-size + left (R;array sizeL R;nat) + right (R;array sizeR R;nat) + #let [(^open) &;Monoid + (^open) (&;Eq number;Eq) + fusion (append left right)]] + ($_ seq + (assert "Appending two arrays should produce a new one twice as large." + (n.= (n.+ sizeL sizeR) (&;size fusion))) + (assert "First elements of fused array should equal the first array." + (|> (: (&;Array Nat) + (&;new sizeL)) + (&;copy sizeL +0 fusion +0) + (= left))) + (assert "Last elements of fused array should equal the second array." + (|> (: (&;Array Nat) + (&;new sizeR)) + (&;copy sizeR sizeL fusion +0) + (= right))) + )) diff --git a/stdlib/test/test/lux/data/coll/dict.lux b/stdlib/test/test/lux/data/coll/dict.lux new file mode 100644 index 000000000..38b8e83b4 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/dict.lux @@ -0,0 +1,137 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad + [eq]) + (data [text "Text/" Monoid] + text/format + [number] + [char] + (coll ["&" dict] + [list "List/" Fold Functor])) + (codata function) + ["R" random] + pipe) + lux/test) + +(test: "Dictionaries." + [#let [capped-nat (:: R;Monad map (n.% +100) R;nat)] + size capped-nat + dict (R;dict char;Hash size R;char capped-nat) + non-key (|> R;char + (R;filter (lambda [key] (not (&;contains? key dict))))) + test-val (|> R;nat + (R;filter (lambda [val] (not (list;member? number;Eq (&;values dict) val)))))] + ($_ seq + (assert "Size function should correctly represent Dict size." + (n.= size (&;size dict))) + + (assert "Dicts of size 0 should be considered empty." + (if (n.= +0 size) + (&;empty? dict) + (not (&;empty? dict)))) + + (assert "The functions 'entries', 'keys' and 'values' should be synchronized." + (:: (list;Eq (eq;conj char;Eq number;Eq)) = + (&;entries dict) + (list;zip2 (&;keys dict) + (&;values dict)))) + + (assert "Dict should be able to recognize it's own keys." + (list;every? (lambda [key] (&;contains? key dict)) + (&;keys dict))) + + (assert "Should be able to get every key." + (list;every? (lambda [key] (case (&;get key dict) + (#;Some _) true + _ false)) + (&;keys dict))) + + (assert "Shouldn't be able to access non-existant keys." + (case (&;get non-key dict) + (#;Some _) false + _ true)) + + (assert "Should be able to put and then get a value." + (case (&;get non-key (&;put non-key test-val dict)) + (#;Some v) (n.= test-val v) + _ true)) + + (assert "Should be able to put~ and then get a value." + (case (&;get non-key (&;put~ non-key test-val dict)) + (#;Some v) (n.= test-val v) + _ true)) + + (assert "Shouldn't be able to put~ an existing key." + (or (n.= +0 size) + (let [first-key (|> dict &;keys list;head (default (undefined)))] + (case (&;get first-key (&;put~ first-key test-val dict)) + (#;Some v) (not (n.= test-val v)) + _ true)))) + + (assert "Removing a key should make it's value inaccessible." + (let [base (&;put non-key test-val dict)] + (and (&;contains? non-key base) + (not (&;contains? non-key (&;remove non-key base)))))) + + (assert "Should be possible to update values via their keys." + (let [base (&;put non-key test-val dict) + updt (&;update non-key n.inc base)] + (case [(&;get non-key base) (&;get non-key updt)] + [(#;Some x) (#;Some y)] + (n.= (n.inc x) y) + + _ + false))) + + (assert "Additions and removals to a Dict should affect its size." + (let [plus (&;put non-key test-val dict) + base (&;remove non-key plus)] + (and (n.= (n.inc (&;size dict)) (&;size plus)) + (n.= (n.dec (&;size plus)) (&;size base))))) + + (assert "A Dict should equal itself & going to<->from lists shouldn't change that." + (let [(^open) (&;Eq number;Eq)] + (and (= dict dict) + (|> dict &;entries (&;from-list char;Hash) (= dict))))) + + (assert "Merging a Dict to itself changes nothing." + (let [(^open) (&;Eq number;Eq)] + (= dict (&;merge dict dict)))) + + (assert "If you merge, and the second dict has overlapping keys, it should overwrite yours." + (let [dict' (|> dict &;entries + (List/map (lambda [[k v]] [k (n.inc v)])) + (&;from-list char;Hash)) + (^open) (&;Eq number;Eq)] + (= dict' (&;merge dict' dict)))) + + (assert "Can merge values in such a way that they become combined." + (list;every? (lambda [[x x*2]] (n.= (n.* +2 x) x*2)) + (list;zip2 (&;values dict) + (&;values (&;merge-with n.+ dict dict))))) + + (assert "Should be able to select subset of keys from dict." + (|> dict + (&;put non-key test-val) + (&;select (list non-key)) + &;size + (n.= +1))) + + (assert "Should be able to re-bind existing values to different keys." + (or (n.= +0 size) + (let [first-key (|> dict &;keys list;head (default (undefined))) + rebound (&;re-bind first-key non-key dict)] + (and (n.= (&;size dict) (&;size rebound)) + (&;contains? non-key rebound) + (not (&;contains? first-key rebound)) + (n.= (default (undefined) + (&;get first-key dict)) + (default (undefined) + (&;get non-key rebound))))))) + )) diff --git a/stdlib/test/test/lux/data/coll/list.lux b/stdlib/test/test/lux/data/coll/list.lux new file mode 100644 index 000000000..9df350b0b --- /dev/null +++ b/stdlib/test/test/lux/data/coll/list.lux @@ -0,0 +1,226 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" list]) + [text "Text/" Monoid] + [number] + [bool] + [product]) + ["R" random] + pipe) + lux/test) + +(def: bounded-size + (R;Random Nat) + (|> R;nat + (:: R;Monad map (|>. (n.% +100) (n.+ +10))))) + +(test: "Lists: Part 1" + [size bounded-size + idx (:: @ map (n.% size) R;nat) + sample (R;list size R;nat) + other-size bounded-size + other-sample (R;list other-size R;nat) + separator R;nat + #let [(^open) (&;Eq number;Eq) + (^open "&/") &;Functor]] + ($_ seq + (assert "The size function should correctly portray the size of the list." + (n.= size (&;size sample))) + + (assert "The repeat function should produce as many elements as asked of it." + (n.= size (&;size (&;repeat size [])))) + + (assert "Reversing a list does not change it's size." + (n.= (&;size sample) + (&;size (&;reverse sample)))) + + (assert "Reversing a list twice results in the original list." + (= sample + (&;reverse (&;reverse sample)))) + + (assert "Filtering by a predicate and its complement should result in a number of elements equal to the original list." + (and (n.= (&;size sample) + (n.+ (&;size (&;filter n.even? sample)) + (&;size (&;filter (bool;complement n.even?) sample)))) + (let [[plus minus] (&;partition n.even? sample)] + (n.= (&;size sample) + (n.+ (&;size plus) + (&;size minus)))))) + + (assert "If every element in a list satisfies a predicate, there can't be any that satisfy its complement." + (if (&;every? n.even? sample) + (and (not (&;any? (bool;complement n.even?) sample)) + (&;empty? (&;filter (bool;complement n.even?) sample))) + (&;any? (bool;complement n.even?) sample))) + + (assert "Any element of the list can be considered it's member." + (let [elem (default (undefined) + (&;nth idx sample))] + (&;member? number;Eq sample elem))) + )) + +(test: "Lists: Part 2" + [size bounded-size + idx (:: @ map (n.% size) R;nat) + sample (R;list size R;nat) + other-size bounded-size + other-sample (R;list other-size R;nat) + separator R;nat + #let [(^open) (&;Eq number;Eq) + (^open "&/") &;Functor]] + ($_ seq + (assert "Appending the head and the tail should yield the original list." + (let [head (default (undefined) + (&;head sample)) + tail (default (undefined) + (&;tail sample))] + (= sample + (#;Cons head tail)))) + + (assert "Appending the inits and the last should yield the original list." + (let [(^open) &;Monoid + inits (default (undefined) + (&;inits sample)) + last (default (undefined) + (&;last sample))] + (= sample + (append inits (list last))))) + + (assert "Functor should go over every element of the list." + (let [(^open) &;Functor + there (map n.inc sample) + back-again (map n.dec there)] + (and (not (= sample there)) + (= sample back-again)))) + + (assert "Splitting a list into chunks and re-appending them should yield the original list." + (let [(^open) &;Monoid + [left right] (&;split idx sample) + [left' right'] (&;split-with n.even? sample)] + (and (= sample + (append left right)) + (= sample + (append left' right')) + (= sample + (append (&;take idx sample) + (&;drop idx sample))) + (= sample + (append (&;take-while n.even? sample) + (&;drop-while n.even? sample))) + ))) + + (assert "Segmenting the list in pairs should yield as many elements as N/2." + (n.= (n./ +2 size) + (&;size (&;as-pairs sample)))) + + (assert "Sorting a list shouldn't change it's size." + (n.= (&;size sample) + (&;size (&;sort n.< sample)))) + + (assert "Sorting a list with one order should yield the reverse of sorting it with the opposite order." + (= (&;sort n.< sample) + (&;reverse (&;sort n.> sample)))) + )) + +(test: "Lists: Part 3" + [size bounded-size + idx (:: @ map (n.% size) R;nat) + sample (R;list size R;nat) + other-size bounded-size + other-sample (R;list other-size R;nat) + separator R;nat + #let [(^open) (&;Eq number;Eq) + (^open "&/") &;Functor]] + ($_ seq + (assert "If you zip 2 lists, the result's size will be that of the smaller list." + (n.= (&;size (&;zip2 sample other-sample)) + (n.min (&;size sample) (&;size other-sample)))) + + (assert "I can pair-up elements of a list in order." + (let [(^open) &;Functor + zipped (&;zip2 sample other-sample) + num-zipper (&;size zipped)] + (and (|> zipped (map product;left) (= (&;take num-zipper sample))) + (|> zipped (map product;right) (= (&;take num-zipper other-sample)))))) + + (assert "You can generate indices for any size, and they will be in ascending order." + (let [(^open) &;Functor + indices (&;indices size)] + (and (n.= size (&;size indices)) + (= indices + (&;sort n.< indices)) + (&;every? (n.= (n.dec size)) + (&;zip2-with n.+ + indices + (&;sort n.> indices))) + ))) + + (assert "The 'interpose' function places a value between every member of a list." + (let [(^open) &;Functor + sample+ (&;interpose separator sample)] + (and (n.= (|> size (n.* +2) n.dec) + (&;size sample+)) + (|> sample+ &;as-pairs (map product;right) (&;every? (n.= separator)))))) + + (assert "List append is a monoid." + (let [(^open) &;Monoid] + (and (= sample (append unit sample)) + (= sample (append sample unit)) + (let [[left right] (&;split size (append sample other-sample))] + (and (= sample left) + (= other-sample right)))))) + + (assert "Applicative allows you to create singleton lists, and apply lists of functions to lists of values." + (let [(^open) &;Applicative] + (and (= (list separator) (wrap separator)) + (= (map n.inc sample) + (apply (wrap n.inc) sample))))) + + (assert "List concatenation is a monad." + (let [(^open) &;Monad + (^open) &;Monoid] + (= (append sample other-sample) + (join (list sample other-sample))))) + + (assert "You can find any value that satisfies some criterium, if such values exist in the list." + (case (&;find n.even? sample) + (#;Some found) + (and (n.even? found) + (&;any? n.even? sample) + (not (&;every? (bool;complement n.even?) sample))) + + #;None + (and (not (&;any? n.even? sample)) + (&;every? (bool;complement n.even?) sample)))) + + (assert "You can iteratively construct a list, generating values until you're done." + (= (&;n.range +0 (n.dec size)) + (&;iterate (lambda [n] (if (n.< size n) (#;Some (n.inc n)) #;None)) + +0))) + + (assert "Can enumerate all elements in a list." + (let [enum-sample (&;enumerate sample)] + (and (= (&;indices (&;size enum-sample)) + (&/map product;left enum-sample)) + (= sample + (&/map product;right enum-sample))))) + )) + +(test: "Monad transformer" + (let [lift (&;lift-list io;Monad) + (^open "io/") io;Monad] + (assert "Can add list functionality to any monad." + (|> (io;run (do (&;ListT io;Monad) + [a (lift (io/wrap 123)) + b (wrap 456)] + (wrap (i.+ a b)))) + (case> (^ (list 579)) true + _ false))) + )) diff --git a/stdlib/test/test/lux/data/coll/queue.lux b/stdlib/test/test/lux/data/coll/queue.lux new file mode 100644 index 000000000..e65c4eb74 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/queue.lux @@ -0,0 +1,55 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" queue]) + [number]) + ["R" random] + pipe) + lux/test) + +(test: "Queues" + [size (:: @ map (n.% +100) R;nat) + sample (R;queue size R;nat) + non-member (|> R;nat + (R;filter (. not (&;member? number;Eq sample))))] + ($_ seq + (assert "I can query the size of a queue (and empty queues have size 0)." + (if (n.= +0 size) + (&;empty? sample) + (n.= size (&;size sample)))) + + (assert "Enqueueing and dequeing affects the size of queues." + (and (n.= (n.inc size) (&;size (&;push non-member sample))) + (or (&;empty? sample) + (n.= (n.dec size) (&;size (&;pop sample)))) + (n.= size (&;size (&;pop (&;push non-member sample)))))) + + (assert "Transforming to/from list can't change the queue." + (let [(^open "&/") (&;Eq number;Eq)] + (|> sample + &;to-list &;from-list + (&/= sample)))) + + (assert "I can always peek at a non-empty queue." + (case (&;peek sample) + #;None (&;empty? sample) + (#;Some _) true)) + + (assert "I can query whether an element belongs to a queue." + (and (not (&;member? number;Eq sample non-member)) + (&;member? number;Eq (&;push non-member sample) + non-member) + (case (&;peek sample) + #;None + (&;empty? sample) + + (#;Some first) + (and (&;member? number;Eq sample first) + (not (&;member? number;Eq (&;pop sample) first)))))) + )) diff --git a/stdlib/test/test/lux/data/coll/set.lux b/stdlib/test/test/lux/data/coll/set.lux new file mode 100644 index 000000000..d9540927e --- /dev/null +++ b/stdlib/test/test/lux/data/coll/set.lux @@ -0,0 +1,68 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" set] + [list "" Fold]) + [number]) + ["R" random] + pipe) + lux/test) + +(def: gen-nat + (R;Random Nat) + (|> R;nat + (:: R;Monad map (n.% +100)))) + +(test: "Sets" + [sizeL gen-nat + sizeR gen-nat + setL (R;set number;Hash sizeL gen-nat) + setR (R;set number;Hash sizeR gen-nat) + non-member (|> gen-nat + (R;filter (. not (&;member? setL)))) + #let [(^open "&/") &;Eq]] + ($_ seq + (assert "I can query the size of a set." + (and (n.= sizeL (&;size setL)) + (n.= sizeR (&;size setR)))) + + (assert "Converting sets to/from lists can't change their values." + (|> setL + &;to-list (&;from-list number;Hash) + (&/= setL))) + + (assert "Every set is a sub-set of the union of itself with another." + (let [setLR (&;union setL setR)] + (and (&;sub? setLR setL) + (&;sub? setLR setR)))) + + (assert "Every set is a super-set of the intersection of itself with another." + (let [setLR (&;intersection setL setR)] + (and (&;super? setLR setL) + (&;super? setLR setR)))) + + (assert "Union with the empty set leaves a set unchanged." + (&/= setL + (&;union (&;new number;Hash) + setL))) + + (assert "Intersection with the empty set results in the empty set." + (let [empty-set (&;new number;Hash)] + (&/= empty-set + (&;intersection empty-set setL)))) + + (assert "After substracting a set A from another B, no member of A can be a member of B." + (let [sub (&;difference setR setL)] + (not (list;any? (&;member? sub) (&;to-list setR))))) + + (assert "Every member of a set must be identifiable." + (and (not (&;member? setL non-member)) + (&;member? (&;add non-member setL) non-member) + (not (&;member? (&;remove non-member (&;add non-member setL)) non-member)))) + )) diff --git a/stdlib/test/test/lux/data/coll/stack.lux b/stdlib/test/test/lux/data/coll/stack.lux new file mode 100644 index 000000000..f8057b1f2 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/stack.lux @@ -0,0 +1,48 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" stack] + [list "" Fold]) + [number]) + ["R" random] + pipe) + lux/test) + +(def: gen-nat + (R;Random Nat) + (|> R;nat + (:: R;Monad map (n.% +100)))) + +(test: "Stacks" + [size gen-nat + sample (R;stack size gen-nat) + new-top gen-nat] + ($_ seq + (assert "Can query the size of a stack." + (n.= size (&;size sample))) + + (assert "Can peek inside non-empty stacks." + (case (&;peek sample) + #;None (&;empty? sample) + (#;Some _) (not (&;empty? sample)))) + + (assert "Popping empty stacks doesn't change anything. + But, if they're non-empty, the top of the stack is removed." + (let [sample' (&;pop sample)] + (or (n.= (&;size sample) (n.inc (&;size sample'))) + (and (&;empty? sample) (&;empty? sample'))) + )) + + (assert "Pushing onto a stack always increases it by 1, adding a new value at the top." + (and (is sample + (&;pop (&;push new-top sample))) + (n.= (n.inc (&;size sample)) (&;size (&;push new-top sample))) + (|> (&;push new-top sample) &;peek (default (undefined)) + (is new-top)))) + )) diff --git a/stdlib/test/test/lux/data/coll/tree/rose.lux b/stdlib/test/test/lux/data/coll/tree/rose.lux new file mode 100644 index 000000000..126a36678 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/tree/rose.lux @@ -0,0 +1,40 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll (tree ["&" rose]) + [list "List/" Monad]) + [number]) + ["R" random] + pipe) + lux/test) + +(def: gen-nat + (R;Random Nat) + (|> R;nat + (:: R;Monad map (n.% +100)))) + +(test: "Trees" + [leaf (:: @ map &;leaf R;nat) + branchS gen-nat + branchV R;nat + branchC (R;list branchS R;nat) + #let [branch (&;branch branchV (List/map &;leaf branchC))] + #let [(^open "&/") (&;Eq number;Eq) + (^open "List/") (list;Eq number;Eq)]] + ($_ seq + (assert "Can compare trees for equality." + (and (&/= leaf leaf) + (&/= branch branch) + (not (&/= leaf branch)) + (not (&/= leaf (&;branch branchV (List/map &;leaf (list;reverse branchC))))))) + + (assert "Can flatten a tree to get all the nodes as a flat tree." + (List/= (list& branchV branchC) + (&;flatten branch))) + )) diff --git a/stdlib/test/test/lux/data/coll/tree/zipper.lux b/stdlib/test/test/lux/data/coll/tree/zipper.lux new file mode 100644 index 000000000..949b558a7 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/tree/zipper.lux @@ -0,0 +1,128 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll [list "List/" Fold Functor] + (tree ["&" zipper] + [rose])) + [text "Text/" Monoid] + text/format + [number]) + (codata function) + ["R" random] + pipe) + lux/test) + +(def: gen-tree + (R;Random (rose;Tree Nat)) + (R;rec (lambda [gen-tree] + (do R;Monad + ## Each branch can have, at most, 1 child. + [size (|> R;nat (:: @ map (n.% +2)))] + (R;seq R;nat + (R;list size gen-tree)))))) + +(def: (to-end zipper) + (All [a] (-> (&;Zipper a) (&;Zipper a))) + (loop [zipper zipper] + (if (&;end? zipper) + zipper + (recur (&;next zipper))))) + +(test: "Zippers" + [sample gen-tree + new-val R;nat + pre-val R;nat + post-val R;nat + #let [(^open "Tree/") (rose;Eq number;Eq) + (^open "List/") (list;Eq number;Eq)]] + ($_ seq + (assert "Trees can be converted to/from zippers." + (|> sample + &;from-tree &;to-tree + (Tree/= sample))) + + (assert "Creating a zipper gives you a root node." + (|> sample &;from-tree &;root?)) + + (assert "Can move down inside branches. Can move up from lower nodes." + (let [zipper (&;from-tree sample)] + (if (&;branch? zipper) + (let [child (|> zipper &;down)] + (and (not (Tree/= sample (&;to-tree child))) + (|> child &;parent (default (undefined)) (is zipper)) + (|> child &;up (is zipper) not) + (|> child &;root (is zipper) not))) + (and (&;leaf? zipper) + (|> zipper (&;prepend-child new-val) &;branch?))))) + + (assert "Can prepend and append children." + (let [zipper (&;from-tree sample)] + (if (&;branch? zipper) + (let [mid-val (|> zipper &;down &;value) + zipper (|> zipper + (&;prepend-child pre-val) + (&;append-child post-val))] + (and (|> zipper &;down &;value (is pre-val)) + (|> zipper &;down &;right &;value (is mid-val)) + (|> zipper &;down &;right &;right &;value (is post-val)) + (|> zipper &;down &;rightmost &;leftmost &;value (is pre-val)) + (|> zipper &;down &;right &;left &;value (is pre-val)) + (|> zipper &;down &;rightmost &;value (is post-val)))) + true))) + + (assert "Can insert children around a node (unless it's root)." + (let [zipper (&;from-tree sample)] + (if (&;branch? zipper) + (let [mid-val (|> zipper &;down &;value) + zipper (|> zipper + &;down + (&;insert-left pre-val) + (default (undefined)) + (&;insert-right post-val) + (default (undefined)) + &;up)] + (and (|> zipper &;down &;value (is pre-val)) + (|> zipper &;down &;right &;value (is mid-val)) + (|> zipper &;down &;right &;right &;value (is post-val)) + (|> zipper &;down &;rightmost &;leftmost &;value (is pre-val)) + (|> zipper &;down &;right &;left &;value (is pre-val)) + (|> zipper &;down &;rightmost &;value (is post-val)))) + (and (|> zipper (&;insert-left pre-val) (case> (#;Some _) false + #;None true)) + (|> zipper (&;insert-right post-val) (case> (#;Some _) false + #;None true)))))) + + (assert "Can set and update the value of a node." + (|> sample &;from-tree (&;set new-val) &;value (n.= new-val))) + + (assert "Zipper traversal follows the outline of the tree depth-first." + (List/= (rose;flatten sample) + (loop [zipper (&;from-tree sample)] + (if (&;end? zipper) + (list (&;value zipper)) + (#;Cons (&;value zipper) + (recur (&;next zipper))))))) + + (assert "Backwards zipper traversal yield reverse tree flatten." + (List/= (list;reverse (rose;flatten sample)) + (loop [zipper (to-end (&;from-tree sample))] + (if (&;root? zipper) + (list (&;value zipper)) + (#;Cons (&;value zipper) + (recur (&;prev zipper))))))) + + (assert "Can remove nodes (except root nodes)." + (let [zipper (&;from-tree sample)] + (if (&;branch? zipper) + (and (|> zipper &;down &;root? not) + (|> zipper &;down &;remove (case> #;None false + (#;Some node) (&;root? node)))) + (|> zipper &;remove (case> #;None true + (#;Some _) false))))) + )) diff --git a/stdlib/test/test/lux/data/coll/vector.lux b/stdlib/test/test/lux/data/coll/vector.lux new file mode 100644 index 000000000..9e1c3c44b --- /dev/null +++ b/stdlib/test/test/lux/data/coll/vector.lux @@ -0,0 +1,78 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" vector] + [list "List/" Fold Functor]) + [text "Text/" Monoid] + text/format + [number]) + (codata function) + ["R" random] + pipe) + lux/test) + +(test: "Vectors" + [size (|> R;nat (:: @ map (n.% +100))) + idx (|> R;nat (:: @ map (n.% size))) + sample (R;vector size R;nat) + other-sample (R;vector size R;nat) + non-member (|> R;nat (R;filter (. not (&;member? number;Eq sample)))) + #let [(^open "&/") (&;Eq number;Eq) + (^open "&/") &;Monad + (^open "&/") &;Fold + (^open "&/") &;Monoid]] + ($_ seq + (assert "Can query size of vector." + (if (&;empty? sample) + (and (n.= +0 size) + (n.= +0 (&;size sample))) + (n.= size (&;size sample)))) + + (assert "Can add and remove elements to vectors." + (and (n.= (n.inc size) (&;size (&;add non-member sample))) + (n.= (n.dec size) (&;size (&;pop sample))))) + + (assert "Can put and get elements into vectors." + (|> sample + (&;put idx non-member) + (&;nth idx) + (default (undefined)) + (is non-member))) + + (assert "Can update elements of vectors." + (|> sample + (&;put idx non-member) (&;update idx n.inc) + (&;nth idx) (default (undefined)) + (n.= (n.inc non-member)))) + + (assert "Can safely transform to/from lists." + (|> sample &;to-list &;from-list (&/= sample))) + + (assert "Can identify members of a vector." + (and (not (&;member? number;Eq sample non-member)) + (&;member? number;Eq (&;add non-member sample) non-member))) + + (assert "Can fold over elements of vector." + (n.= (List/fold n.+ +0 (&;to-list sample)) + (&/fold n.+ +0 sample))) + + (assert "Functor goes over every element." + (let [there (&/map n.inc sample) + back-again (&/map n.dec there)] + (and (not (&/= sample there)) + (&/= sample back-again)))) + + (assert "Applicative allows you to create singleton vectors, and apply vectors of functions to vectors of values." + (and (&/= (&;vector non-member) (&/wrap non-member)) + (&/= (&/map n.inc sample) (&/apply (&/wrap n.inc) sample)))) + + (assert "Vector concatenation is a monad." + (&/= (&/append sample other-sample) + (&/join (&;vector sample other-sample)))) + )) diff --git a/stdlib/test/test/lux/data/error.lux b/stdlib/test/test/lux/data/error.lux index 880c2e0f1..a53d7faf2 100644 --- a/stdlib/test/test/lux/data/error.lux +++ b/stdlib/test/test/lux/data/error.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data text/format ["&" error]) diff --git a/stdlib/test/test/lux/data/error/exception.lux b/stdlib/test/test/lux/data/error/exception.lux index 312bca2a2..c3db059e6 100644 --- a/stdlib/test/test/lux/data/error/exception.lux +++ b/stdlib/test/test/lux/data/error/exception.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [error #- fail] (error ["&" exception #+ exception:]) diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index 06c29707f..3734192a0 100644 --- a/stdlib/test/test/lux/data/format/json.lux +++ b/stdlib/test/test/lux/data/format/json.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad codec eq) @@ -17,9 +17,9 @@ [maybe] [number "i/" Number] (format ["&" json]) - (struct [vector #+ vector] - [dict] - [list])) + (coll [vector #+ vector] + [dict] + [list])) [compiler #+ with-gensyms] [macro] (macro [ast] diff --git a/stdlib/test/test/lux/data/ident.lux b/stdlib/test/test/lux/data/ident.lux index 70a8d2a58..1f8cc0441 100644 --- a/stdlib/test/test/lux/data/ident.lux +++ b/stdlib/test/test/lux/data/ident.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data ["&" ident] [text "Text/" Eq] diff --git a/stdlib/test/test/lux/data/identity.lux b/stdlib/test/test/lux/data/identity.lux index 4f8c26cb1..2444b80b7 100644 --- a/stdlib/test/test/lux/data/identity.lux +++ b/stdlib/test/test/lux/data/identity.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad comonad) (data ["&" identity] diff --git a/stdlib/test/test/lux/data/log.lux b/stdlib/test/test/lux/data/log.lux index dd94b1efa..abc1112a2 100644 --- a/stdlib/test/test/lux/data/log.lux +++ b/stdlib/test/test/lux/data/log.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data ["&" log] [text "Text/" Monoid Eq] diff --git a/stdlib/test/test/lux/data/maybe.lux b/stdlib/test/test/lux/data/maybe.lux index b0f2b411c..de738e336 100644 --- a/stdlib/test/test/lux/data/maybe.lux +++ b/stdlib/test/test/lux/data/maybe.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data ["&" maybe] [text "Text/" Monoid] diff --git a/stdlib/test/test/lux/data/number.lux b/stdlib/test/test/lux/data/number.lux index 8b7267444..4de7d4c14 100644 --- a/stdlib/test/test/lux/data/number.lux +++ b/stdlib/test/test/lux/data/number.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data number [text "Text/" Monoid Eq] diff --git a/stdlib/test/test/lux/data/product.lux b/stdlib/test/test/lux/data/product.lux index 3d064f96e..69b1029b9 100644 --- a/stdlib/test/test/lux/data/product.lux +++ b/stdlib/test/test/lux/data/product.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data product [text "Text/" Monoid] diff --git a/stdlib/test/test/lux/data/struct/array.lux b/stdlib/test/test/lux/data/struct/array.lux deleted file mode 100644 index ae7ff4bbc..000000000 --- a/stdlib/test/test/lux/data/struct/array.lux +++ /dev/null @@ -1,135 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (control [monad]) - (codata [io]) - (data (struct ["&" array] - [list]) - [number]) - ["R" random] - pipe) - lux/test) - -(def: bounded-size - (R;Random Nat) - (|> R;nat - (:: R;Monad map (|>. (n.% +100) (n.+ +1))))) - -(test: "Arrays and their copies" - [size bounded-size - original (R;array size R;nat) - #let [clone (&;clone original) - copy (: (&;Array Nat) - (&;new size)) - manual-copy (: (&;Array Nat) - (&;new size))]] - ($_ seq - (assert "Size function must correctly return size of array." - (n.= size (&;size original))) - (assert "Cloning an array should yield and identical array, but not the same one." - (and (:: (&;Eq number;Eq) = original clone) - (not (is original clone)))) - (assert "Full-range manual copies should give the same result as cloning." - (exec (&;copy size +0 original +0 copy) - (and (:: (&;Eq number;Eq) = original copy) - (not (is original copy))))) - (assert "Array folding should go over all values." - (exec (:: &;Fold fold - (lambda [x idx] - (exec (&;put idx x manual-copy) - (n.inc idx))) - +0 - original) - (:: (&;Eq number;Eq) = original manual-copy))) - (assert "Transformations between (full) arrays and lists shouldn't cause lose or change any values." - (|> original - &;to-list &;from-list - (:: (&;Eq number;Eq) = original))) - )) - -(test: "Array mutation" - [size bounded-size - idx (:: @ map (n.% size) R;nat) - array (|> (R;array size R;nat) - (R;filter (|>. &;to-list (list;any? n.odd?)))) - #let [value (default (undefined) - (&;get idx array))]] - ($_ seq - (assert "Shouldn't be able to find a value in an unoccupied cell." - (case (&;get idx (&;remove idx array)) - (#;Some _) false - #;None true)) - (assert "You should be able to access values put into the array." - (case (&;get idx (&;put idx value array)) - (#;Some value') (n.= value' value) - #;None false)) - (assert "All cells should be occupied on a full array." - (and (n.= size (&;occupied array)) - (n.= +0 (&;vacant array)))) - (assert "Filtering mutates the array to remove invalid values." - (exec (&;filter n.even? array) - (and (n.< size (&;occupied array)) - (n.> +0 (&;vacant array)) - (n.= size (n.+ (&;occupied array) - (&;vacant array)))))) - )) - -(test: "Finding values." - [size bounded-size - array (|> (R;array size R;nat) - (R;filter (|>. &;to-list (list;any? n.even?))))] - ($_ seq - (assert "Can find values inside arrays." - (|> (&;find n.even? array) - (case> (#;Some _) true - #;None false))) - (assert "Can find values inside arrays (with access to indices)." - (|> (&;find+ (lambda [idx n] - (and (n.even? n) - (n.< size idx))) - array) - (case> (#;Some _) true - #;None false))))) - -(test: "Functor" - [size bounded-size - array (R;array size R;nat)] - (let [(^open) &;Functor - (^open) (&;Eq number;Eq)] - ($_ seq - (assert "Functor shouldn't alter original array." - (let [copy (map id array)] - (and (= array copy) - (not (is array copy))))) - (assert "Functor should go over all available array elements." - (let [there (map n.inc array) - back-again (map n.dec there)] - (and (not (= array there)) - (= array back-again))))))) - -(test: "Monoid" - [sizeL bounded-size - sizeR bounded-size - left (R;array sizeL R;nat) - right (R;array sizeR R;nat) - #let [(^open) &;Monoid - (^open) (&;Eq number;Eq) - fusion (append left right)]] - ($_ seq - (assert "Appending two arrays should produce a new one twice as large." - (n.= (n.+ sizeL sizeR) (&;size fusion))) - (assert "First elements of fused array should equal the first array." - (|> (: (&;Array Nat) - (&;new sizeL)) - (&;copy sizeL +0 fusion +0) - (= left))) - (assert "Last elements of fused array should equal the second array." - (|> (: (&;Array Nat) - (&;new sizeR)) - (&;copy sizeR sizeL fusion +0) - (= right))) - )) diff --git a/stdlib/test/test/lux/data/struct/dict.lux b/stdlib/test/test/lux/data/struct/dict.lux deleted file mode 100644 index b467e232a..000000000 --- a/stdlib/test/test/lux/data/struct/dict.lux +++ /dev/null @@ -1,137 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad - [eq]) - (data [text "Text/" Monoid] - text/format - [number] - [char] - (struct ["&" dict] - [list "List/" Fold Functor])) - (codata function) - ["R" random] - pipe) - lux/test) - -(test: "Dictionaries." - [#let [capped-nat (:: R;Monad map (n.% +100) R;nat)] - size capped-nat - dict (R;dict char;Hash size R;char capped-nat) - non-key (|> R;char - (R;filter (lambda [key] (not (&;contains? key dict))))) - test-val (|> R;nat - (R;filter (lambda [val] (not (list;member? number;Eq (&;values dict) val)))))] - ($_ seq - (assert "Size function should correctly represent Dict size." - (n.= size (&;size dict))) - - (assert "Dicts of size 0 should be considered empty." - (if (n.= +0 size) - (&;empty? dict) - (not (&;empty? dict)))) - - (assert "The functions 'entries', 'keys' and 'values' should be synchronized." - (:: (list;Eq (eq;conj char;Eq number;Eq)) = - (&;entries dict) - (list;zip2 (&;keys dict) - (&;values dict)))) - - (assert "Dict should be able to recognize it's own keys." - (list;every? (lambda [key] (&;contains? key dict)) - (&;keys dict))) - - (assert "Should be able to get every key." - (list;every? (lambda [key] (case (&;get key dict) - (#;Some _) true - _ false)) - (&;keys dict))) - - (assert "Shouldn't be able to access non-existant keys." - (case (&;get non-key dict) - (#;Some _) false - _ true)) - - (assert "Should be able to put and then get a value." - (case (&;get non-key (&;put non-key test-val dict)) - (#;Some v) (n.= test-val v) - _ true)) - - (assert "Should be able to put~ and then get a value." - (case (&;get non-key (&;put~ non-key test-val dict)) - (#;Some v) (n.= test-val v) - _ true)) - - (assert "Shouldn't be able to put~ an existing key." - (or (n.= +0 size) - (let [first-key (|> dict &;keys list;head (default (undefined)))] - (case (&;get first-key (&;put~ first-key test-val dict)) - (#;Some v) (not (n.= test-val v)) - _ true)))) - - (assert "Removing a key should make it's value inaccessible." - (let [base (&;put non-key test-val dict)] - (and (&;contains? non-key base) - (not (&;contains? non-key (&;remove non-key base)))))) - - (assert "Should be possible to update values via their keys." - (let [base (&;put non-key test-val dict) - updt (&;update non-key n.inc base)] - (case [(&;get non-key base) (&;get non-key updt)] - [(#;Some x) (#;Some y)] - (n.= (n.inc x) y) - - _ - false))) - - (assert "Additions and removals to a Dict should affect its size." - (let [plus (&;put non-key test-val dict) - base (&;remove non-key plus)] - (and (n.= (n.inc (&;size dict)) (&;size plus)) - (n.= (n.dec (&;size plus)) (&;size base))))) - - (assert "A Dict should equal itself & going to<->from lists shouldn't change that." - (let [(^open) (&;Eq number;Eq)] - (and (= dict dict) - (|> dict &;entries (&;from-list char;Hash) (= dict))))) - - (assert "Merging a Dict to itself changes nothing." - (let [(^open) (&;Eq number;Eq)] - (= dict (&;merge dict dict)))) - - (assert "If you merge, and the second dict has overlapping keys, it should overwrite yours." - (let [dict' (|> dict &;entries - (List/map (lambda [[k v]] [k (n.inc v)])) - (&;from-list char;Hash)) - (^open) (&;Eq number;Eq)] - (= dict' (&;merge dict' dict)))) - - (assert "Can merge values in such a way that they become combined." - (list;every? (lambda [[x x*2]] (n.= (n.* +2 x) x*2)) - (list;zip2 (&;values dict) - (&;values (&;merge-with n.+ dict dict))))) - - (assert "Should be able to select subset of keys from dict." - (|> dict - (&;put non-key test-val) - (&;select (list non-key)) - &;size - (n.= +1))) - - (assert "Should be able to re-bind existing values to different keys." - (or (n.= +0 size) - (let [first-key (|> dict &;keys list;head (default (undefined))) - rebound (&;re-bind first-key non-key dict)] - (and (n.= (&;size dict) (&;size rebound)) - (&;contains? non-key rebound) - (not (&;contains? first-key rebound)) - (n.= (default (undefined) - (&;get first-key dict)) - (default (undefined) - (&;get non-key rebound))))))) - )) diff --git a/stdlib/test/test/lux/data/struct/list.lux b/stdlib/test/test/lux/data/struct/list.lux deleted file mode 100644 index b40615036..000000000 --- a/stdlib/test/test/lux/data/struct/list.lux +++ /dev/null @@ -1,226 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct ["&" list]) - [text "Text/" Monoid] - [number] - [bool] - [product]) - ["R" random] - pipe) - lux/test) - -(def: bounded-size - (R;Random Nat) - (|> R;nat - (:: R;Monad map (|>. (n.% +100) (n.+ +10))))) - -(test: "Lists: Part 1" - [size bounded-size - idx (:: @ map (n.% size) R;nat) - sample (R;list size R;nat) - other-size bounded-size - other-sample (R;list other-size R;nat) - separator R;nat - #let [(^open) (&;Eq number;Eq) - (^open "&/") &;Functor]] - ($_ seq - (assert "The size function should correctly portray the size of the list." - (n.= size (&;size sample))) - - (assert "The repeat function should produce as many elements as asked of it." - (n.= size (&;size (&;repeat size [])))) - - (assert "Reversing a list does not change it's size." - (n.= (&;size sample) - (&;size (&;reverse sample)))) - - (assert "Reversing a list twice results in the original list." - (= sample - (&;reverse (&;reverse sample)))) - - (assert "Filtering by a predicate and its complement should result in a number of elements equal to the original list." - (and (n.= (&;size sample) - (n.+ (&;size (&;filter n.even? sample)) - (&;size (&;filter (bool;complement n.even?) sample)))) - (let [[plus minus] (&;partition n.even? sample)] - (n.= (&;size sample) - (n.+ (&;size plus) - (&;size minus)))))) - - (assert "If every element in a list satisfies a predicate, there can't be any that satisfy its complement." - (if (&;every? n.even? sample) - (and (not (&;any? (bool;complement n.even?) sample)) - (&;empty? (&;filter (bool;complement n.even?) sample))) - (&;any? (bool;complement n.even?) sample))) - - (assert "Any element of the list can be considered it's member." - (let [elem (default (undefined) - (&;at idx sample))] - (&;member? number;Eq sample elem))) - )) - -(test: "Lists: Part 2" - [size bounded-size - idx (:: @ map (n.% size) R;nat) - sample (R;list size R;nat) - other-size bounded-size - other-sample (R;list other-size R;nat) - separator R;nat - #let [(^open) (&;Eq number;Eq) - (^open "&/") &;Functor]] - ($_ seq - (assert "Appending the head and the tail should yield the original list." - (let [head (default (undefined) - (&;head sample)) - tail (default (undefined) - (&;tail sample))] - (= sample - (#;Cons head tail)))) - - (assert "Appending the inits and the last should yield the original list." - (let [(^open) &;Monoid - inits (default (undefined) - (&;inits sample)) - last (default (undefined) - (&;last sample))] - (= sample - (append inits (list last))))) - - (assert "Functor should go over every element of the list." - (let [(^open) &;Functor - there (map n.inc sample) - back-again (map n.dec there)] - (and (not (= sample there)) - (= sample back-again)))) - - (assert "Splitting a list into chunks and re-appending them should yield the original list." - (let [(^open) &;Monoid - [left right] (&;split idx sample) - [left' right'] (&;split-with n.even? sample)] - (and (= sample - (append left right)) - (= sample - (append left' right')) - (= sample - (append (&;take idx sample) - (&;drop idx sample))) - (= sample - (append (&;take-while n.even? sample) - (&;drop-while n.even? sample))) - ))) - - (assert "Segmenting the list in pairs should yield as many elements as N/2." - (n.= (n./ +2 size) - (&;size (&;as-pairs sample)))) - - (assert "Sorting a list shouldn't change it's size." - (n.= (&;size sample) - (&;size (&;sort n.< sample)))) - - (assert "Sorting a list with one order should yield the reverse of sorting it with the opposite order." - (= (&;sort n.< sample) - (&;reverse (&;sort n.> sample)))) - )) - -(test: "Lists: Part 3" - [size bounded-size - idx (:: @ map (n.% size) R;nat) - sample (R;list size R;nat) - other-size bounded-size - other-sample (R;list other-size R;nat) - separator R;nat - #let [(^open) (&;Eq number;Eq) - (^open "&/") &;Functor]] - ($_ seq - (assert "If you zip 2 lists, the result's size will be that of the smaller list." - (n.= (&;size (&;zip2 sample other-sample)) - (n.min (&;size sample) (&;size other-sample)))) - - (assert "I can pair-up elements of a list in order." - (let [(^open) &;Functor - zipped (&;zip2 sample other-sample) - num-zipper (&;size zipped)] - (and (|> zipped (map product;left) (= (&;take num-zipper sample))) - (|> zipped (map product;right) (= (&;take num-zipper other-sample)))))) - - (assert "You can generate indices for any size, and they will be in ascending order." - (let [(^open) &;Functor - indices (&;indices size)] - (and (n.= size (&;size indices)) - (= indices - (&;sort n.< indices)) - (&;every? (n.= (n.dec size)) - (&;zip2-with n.+ - indices - (&;sort n.> indices))) - ))) - - (assert "The 'interpose' function places a value between every member of a list." - (let [(^open) &;Functor - sample+ (&;interpose separator sample)] - (and (n.= (|> size (n.* +2) n.dec) - (&;size sample+)) - (|> sample+ &;as-pairs (map product;right) (&;every? (n.= separator)))))) - - (assert "List append is a monoid." - (let [(^open) &;Monoid] - (and (= sample (append unit sample)) - (= sample (append sample unit)) - (let [[left right] (&;split size (append sample other-sample))] - (and (= sample left) - (= other-sample right)))))) - - (assert "Applicative allows you to create singleton lists, and apply lists of functions to lists of values." - (let [(^open) &;Applicative] - (and (= (list separator) (wrap separator)) - (= (map n.inc sample) - (apply (wrap n.inc) sample))))) - - (assert "List concatenation is a monad." - (let [(^open) &;Monad - (^open) &;Monoid] - (= (append sample other-sample) - (join (list sample other-sample))))) - - (assert "You can find any value that satisfies some criterium, if such values exist in the list." - (case (&;find n.even? sample) - (#;Some found) - (and (n.even? found) - (&;any? n.even? sample) - (not (&;every? (bool;complement n.even?) sample))) - - #;None - (and (not (&;any? n.even? sample)) - (&;every? (bool;complement n.even?) sample)))) - - (assert "You can iteratively construct a list, generating values until you're done." - (= (&;n.range +0 (n.dec size)) - (&;iterate (lambda [n] (if (n.< size n) (#;Some (n.inc n)) #;None)) - +0))) - - (assert "Can enumerate all elements in a list." - (let [enum-sample (&;enumerate sample)] - (and (= (&;indices (&;size enum-sample)) - (&/map product;left enum-sample)) - (= sample - (&/map product;right enum-sample))))) - )) - -(test: "Monad transformer" - (let [lift (&;lift-list io;Monad) - (^open "io/") io;Monad] - (assert "Can add list functionality to any monad." - (|> (io;run (do (&;ListT io;Monad) - [a (lift (io/wrap 123)) - b (wrap 456)] - (wrap (i.+ a b)))) - (case> (^ (list 579)) true - _ false))) - )) diff --git a/stdlib/test/test/lux/data/struct/queue.lux b/stdlib/test/test/lux/data/struct/queue.lux deleted file mode 100644 index d92fecf10..000000000 --- a/stdlib/test/test/lux/data/struct/queue.lux +++ /dev/null @@ -1,55 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct ["&" queue]) - [number]) - ["R" random] - pipe) - lux/test) - -(test: "Queues" - [size (:: @ map (n.% +100) R;nat) - sample (R;queue size R;nat) - non-member (|> R;nat - (R;filter (. not (&;member? number;Eq sample))))] - ($_ seq - (assert "I can query the size of a queue (and empty queues have size 0)." - (if (n.= +0 size) - (&;empty? sample) - (n.= size (&;size sample)))) - - (assert "Enqueueing and dequeing affects the size of queues." - (and (n.= (n.inc size) (&;size (&;push non-member sample))) - (or (&;empty? sample) - (n.= (n.dec size) (&;size (&;pop sample)))) - (n.= size (&;size (&;pop (&;push non-member sample)))))) - - (assert "Transforming to/from list can't change the queue." - (let [(^open "&/") (&;Eq number;Eq)] - (|> sample - &;to-list &;from-list - (&/= sample)))) - - (assert "I can always peek at a non-empty queue." - (case (&;peek sample) - #;None (&;empty? sample) - (#;Some _) true)) - - (assert "I can query whether an element belongs to a queue." - (and (not (&;member? number;Eq sample non-member)) - (&;member? number;Eq (&;push non-member sample) - non-member) - (case (&;peek sample) - #;None - (&;empty? sample) - - (#;Some first) - (and (&;member? number;Eq sample first) - (not (&;member? number;Eq (&;pop sample) first)))))) - )) diff --git a/stdlib/test/test/lux/data/struct/set.lux b/stdlib/test/test/lux/data/struct/set.lux deleted file mode 100644 index ae709384f..000000000 --- a/stdlib/test/test/lux/data/struct/set.lux +++ /dev/null @@ -1,68 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct ["&" set] - [list "" Fold]) - [number]) - ["R" random] - pipe) - lux/test) - -(def: gen-nat - (R;Random Nat) - (|> R;nat - (:: R;Monad map (n.% +100)))) - -(test: "Sets" - [sizeL gen-nat - sizeR gen-nat - setL (R;set number;Hash sizeL gen-nat) - setR (R;set number;Hash sizeR gen-nat) - non-member (|> gen-nat - (R;filter (. not (&;member? setL)))) - #let [(^open "&/") &;Eq]] - ($_ seq - (assert "I can query the size of a set." - (and (n.= sizeL (&;size setL)) - (n.= sizeR (&;size setR)))) - - (assert "Converting sets to/from lists can't change their values." - (|> setL - &;to-list (&;from-list number;Hash) - (&/= setL))) - - (assert "Every set is a sub-set of the union of itself with another." - (let [setLR (&;union setL setR)] - (and (&;sub? setLR setL) - (&;sub? setLR setR)))) - - (assert "Every set is a super-set of the intersection of itself with another." - (let [setLR (&;intersection setL setR)] - (and (&;super? setLR setL) - (&;super? setLR setR)))) - - (assert "Union with the empty set leaves a set unchanged." - (&/= setL - (&;union (&;new number;Hash) - setL))) - - (assert "Intersection with the empty set results in the empty set." - (let [empty-set (&;new number;Hash)] - (&/= empty-set - (&;intersection empty-set setL)))) - - (assert "After substracting a set A from another B, no member of A can be a member of B." - (let [sub (&;difference setR setL)] - (not (list;any? (&;member? sub) (&;to-list setR))))) - - (assert "Every member of a set must be identifiable." - (and (not (&;member? setL non-member)) - (&;member? (&;add non-member setL) non-member) - (not (&;member? (&;remove non-member (&;add non-member setL)) non-member)))) - )) diff --git a/stdlib/test/test/lux/data/struct/stack.lux b/stdlib/test/test/lux/data/struct/stack.lux deleted file mode 100644 index 001eb1af1..000000000 --- a/stdlib/test/test/lux/data/struct/stack.lux +++ /dev/null @@ -1,48 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct ["&" stack] - [list "" Fold]) - [number]) - ["R" random] - pipe) - lux/test) - -(def: gen-nat - (R;Random Nat) - (|> R;nat - (:: R;Monad map (n.% +100)))) - -(test: "Stacks" - [size gen-nat - sample (R;stack size gen-nat) - new-top gen-nat] - ($_ seq - (assert "Can query the size of a stack." - (n.= size (&;size sample))) - - (assert "Can peek inside non-empty stacks." - (case (&;peek sample) - #;None (&;empty? sample) - (#;Some _) (not (&;empty? sample)))) - - (assert "Popping empty stacks doesn't change anything. - But, if they're non-empty, the top of the stack is removed." - (let [sample' (&;pop sample)] - (or (n.= (&;size sample) (n.inc (&;size sample'))) - (and (&;empty? sample) (&;empty? sample'))) - )) - - (assert "Pushing onto a stack always increases it by 1, adding a new value at the top." - (and (is sample - (&;pop (&;push new-top sample))) - (n.= (n.inc (&;size sample)) (&;size (&;push new-top sample))) - (|> (&;push new-top sample) &;peek (default (undefined)) - (is new-top)))) - )) diff --git a/stdlib/test/test/lux/data/struct/tree/rose.lux b/stdlib/test/test/lux/data/struct/tree/rose.lux deleted file mode 100644 index 21592aba9..000000000 --- a/stdlib/test/test/lux/data/struct/tree/rose.lux +++ /dev/null @@ -1,40 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct (tree ["&" rose]) - [list "List/" Monad]) - [number]) - ["R" random] - pipe) - lux/test) - -(def: gen-nat - (R;Random Nat) - (|> R;nat - (:: R;Monad map (n.% +100)))) - -(test: "Trees" - [leaf (:: @ map &;leaf R;nat) - branchS gen-nat - branchV R;nat - branchC (R;list branchS R;nat) - #let [branch (&;branch branchV (List/map &;leaf branchC))] - #let [(^open "&/") (&;Eq number;Eq) - (^open "List/") (list;Eq number;Eq)]] - ($_ seq - (assert "Can compare trees for equality." - (and (&/= leaf leaf) - (&/= branch branch) - (not (&/= leaf branch)) - (not (&/= leaf (&;branch branchV (List/map &;leaf (list;reverse branchC))))))) - - (assert "Can flatten a tree to get all the nodes as a flat tree." - (List/= (list& branchV branchC) - (&;flatten branch))) - )) diff --git a/stdlib/test/test/lux/data/struct/tree/zipper.lux b/stdlib/test/test/lux/data/struct/tree/zipper.lux deleted file mode 100644 index f2d7fe708..000000000 --- a/stdlib/test/test/lux/data/struct/tree/zipper.lux +++ /dev/null @@ -1,128 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct [list "List/" Fold Functor] - (tree ["&" zipper] - [rose])) - [text "Text/" Monoid] - text/format - [number]) - (codata function) - ["R" random] - pipe) - lux/test) - -(def: gen-tree - (R;Random (rose;Tree Nat)) - (R;rec (lambda [gen-tree] - (do R;Monad - ## Each branch can have, at most, 1 child. - [size (|> R;nat (:: @ map (n.% +2)))] - (R;seq R;nat - (R;list size gen-tree)))))) - -(def: (to-end zipper) - (All [a] (-> (&;Zipper a) (&;Zipper a))) - (loop [zipper zipper] - (if (&;end? zipper) - zipper - (recur (&;next zipper))))) - -(test: "Zippers" - [sample gen-tree - new-val R;nat - pre-val R;nat - post-val R;nat - #let [(^open "Tree/") (rose;Eq number;Eq) - (^open "List/") (list;Eq number;Eq)]] - ($_ seq - (assert "Trees can be converted to/from zippers." - (|> sample - &;from-tree &;to-tree - (Tree/= sample))) - - (assert "Creating a zipper gives you a root node." - (|> sample &;from-tree &;root?)) - - (assert "Can move down inside branches. Can move up from lower nodes." - (let [zipper (&;from-tree sample)] - (if (&;branch? zipper) - (let [child (|> zipper &;down)] - (and (not (Tree/= sample (&;to-tree child))) - (|> child &;parent (default (undefined)) (is zipper)) - (|> child &;up (is zipper) not) - (|> child &;root (is zipper) not))) - (and (&;leaf? zipper) - (|> zipper (&;prepend-child new-val) &;branch?))))) - - (assert "Can prepend and append children." - (let [zipper (&;from-tree sample)] - (if (&;branch? zipper) - (let [mid-val (|> zipper &;down &;value) - zipper (|> zipper - (&;prepend-child pre-val) - (&;append-child post-val))] - (and (|> zipper &;down &;value (is pre-val)) - (|> zipper &;down &;right &;value (is mid-val)) - (|> zipper &;down &;right &;right &;value (is post-val)) - (|> zipper &;down &;rightmost &;leftmost &;value (is pre-val)) - (|> zipper &;down &;right &;left &;value (is pre-val)) - (|> zipper &;down &;rightmost &;value (is post-val)))) - true))) - - (assert "Can insert children around a node (unless it's root)." - (let [zipper (&;from-tree sample)] - (if (&;branch? zipper) - (let [mid-val (|> zipper &;down &;value) - zipper (|> zipper - &;down - (&;insert-left pre-val) - (default (undefined)) - (&;insert-right post-val) - (default (undefined)) - &;up)] - (and (|> zipper &;down &;value (is pre-val)) - (|> zipper &;down &;right &;value (is mid-val)) - (|> zipper &;down &;right &;right &;value (is post-val)) - (|> zipper &;down &;rightmost &;leftmost &;value (is pre-val)) - (|> zipper &;down &;right &;left &;value (is pre-val)) - (|> zipper &;down &;rightmost &;value (is post-val)))) - (and (|> zipper (&;insert-left pre-val) (case> (#;Some _) false - #;None true)) - (|> zipper (&;insert-right post-val) (case> (#;Some _) false - #;None true)))))) - - (assert "Can set and update the value of a node." - (|> sample &;from-tree (&;set new-val) &;value (n.= new-val))) - - (assert "Zipper traversal follows the outline of the tree depth-first." - (List/= (rose;flatten sample) - (loop [zipper (&;from-tree sample)] - (if (&;end? zipper) - (list (&;value zipper)) - (#;Cons (&;value zipper) - (recur (&;next zipper))))))) - - (assert "Backwards zipper traversal yield reverse tree flatten." - (List/= (list;reverse (rose;flatten sample)) - (loop [zipper (to-end (&;from-tree sample))] - (if (&;root? zipper) - (list (&;value zipper)) - (#;Cons (&;value zipper) - (recur (&;prev zipper))))))) - - (assert "Can remove nodes (except root nodes)." - (let [zipper (&;from-tree sample)] - (if (&;branch? zipper) - (and (|> zipper &;down &;root? not) - (|> zipper &;down &;remove (case> #;None false - (#;Some node) (&;root? node)))) - (|> zipper &;remove (case> #;None true - (#;Some _) false))))) - )) diff --git a/stdlib/test/test/lux/data/struct/vector.lux b/stdlib/test/test/lux/data/struct/vector.lux deleted file mode 100644 index 35663c63a..000000000 --- a/stdlib/test/test/lux/data/struct/vector.lux +++ /dev/null @@ -1,78 +0,0 @@ -## Copyright (c) Eduardo Julian. All rights reserved. -## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. -## If a copy of the MPL was not distributed with this file, -## You can obtain one at http://mozilla.org/MPL/2.0/. - -(;module: - lux - (lux (codata [io]) - (control monad) - (data (struct ["&" vector] - [list "List/" Fold Functor]) - [text "Text/" Monoid] - text/format - [number]) - (codata function) - ["R" random] - pipe) - lux/test) - -(test: "Vectors" - [size (|> R;nat (:: @ map (n.% +100))) - idx (|> R;nat (:: @ map (n.% size))) - sample (R;vector size R;nat) - other-sample (R;vector size R;nat) - non-member (|> R;nat (R;filter (. not (&;member? number;Eq sample)))) - #let [(^open "&/") (&;Eq number;Eq) - (^open "&/") &;Monad - (^open "&/") &;Fold - (^open "&/") &;Monoid]] - ($_ seq - (assert "Can query size of vector." - (if (&;empty? sample) - (and (n.= +0 size) - (n.= +0 (&;size sample))) - (n.= size (&;size sample)))) - - (assert "Can add and remove elements to vectors." - (and (n.= (n.inc size) (&;size (&;add non-member sample))) - (n.= (n.dec size) (&;size (&;pop sample))))) - - (assert "Can put and get elements into vectors." - (|> sample - (&;put idx non-member) - (&;at idx) - (default (undefined)) - (is non-member))) - - (assert "Can update elements of vectors." - (|> sample - (&;put idx non-member) (&;update idx n.inc) - (&;at idx) (default (undefined)) - (n.= (n.inc non-member)))) - - (assert "Can safely transform to/from lists." - (|> sample &;to-list &;from-list (&/= sample))) - - (assert "Can identify members of a vector." - (and (not (&;member? number;Eq sample non-member)) - (&;member? number;Eq (&;add non-member sample) non-member))) - - (assert "Can fold over elements of vector." - (n.= (List/fold n.+ +0 (&;to-list sample)) - (&/fold n.+ +0 sample))) - - (assert "Functor goes over every element." - (let [there (&/map n.inc sample) - back-again (&/map n.dec there)] - (and (not (&/= sample there)) - (&/= sample back-again)))) - - (assert "Applicative allows you to create singleton vectors, and apply vectors of functions to vectors of values." - (and (&/= (&;vector non-member) (&/wrap non-member)) - (&/= (&/map n.inc sample) (&/apply (&/wrap n.inc) sample)))) - - (assert "Vector concatenation is a monad." - (&/= (&/append sample other-sample) - (&/join (&;vector sample other-sample)))) - )) diff --git a/stdlib/test/test/lux/data/sum.lux b/stdlib/test/test/lux/data/sum.lux index 2ff10dbf5..abb00dbfa 100644 --- a/stdlib/test/test/lux/data/sum.lux +++ b/stdlib/test/test/lux/data/sum.lux @@ -5,12 +5,12 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data sum [text "Text/" Monoid] [number] - (struct [list])) + (coll [list])) (codata function) pipe) lux/test) diff --git a/stdlib/test/test/lux/data/text.lux b/stdlib/test/test/lux/data/text.lux index 14f58aedb..fc1d17e64 100644 --- a/stdlib/test/test/lux/data/text.lux +++ b/stdlib/test/test/lux/data/text.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data ["&" text] [char] text/format [number] - (struct [list])) + (coll [list])) (codata function) ["R" random] pipe) @@ -34,7 +34,7 @@ idx (:: @ map (n.% size) R;nat) sample (R;text size)] (assert "" (|> sample - (&;at idx) + (&;nth idx) (case> (^=> (#;Some char) [(char;as-text char) char'] [[(&;index-of char' sample) @@ -132,20 +132,21 @@ ))) (test: "Structures" - ($_ seq - (assert "" (:: &;Ord < "bcd" "abc")) - (assert "" (not (:: &;Ord < "abc" "abc"))) - (assert "" (not (:: &;Ord < "abc" "bcd"))) - (assert "" (:: &;Ord <= "bcd" "abc")) - (assert "" (:: &;Ord <= "abc" "abc")) - (assert "" (not (:: &;Ord <= "abc" "bcd"))) - (assert "" (:: &;Ord > "abc" "bcd")) - (assert "" (not (:: &;Ord > "abc" "abc"))) - (assert "" (not (:: &;Ord > "bcd" "abc"))) - (assert "" (:: &;Ord >= "abc" "bcd")) - (assert "" (:: &;Ord >= "abc" "abc")) - (assert "" (not (:: &;Ord >= "bcd" "abc"))) - )) + (let [(^open "&/") &;Ord] + ($_ seq + (assert "" (&/< "bcd" "abc")) + (assert "" (not (&/< "abc" "abc"))) + (assert "" (not (&/< "abc" "bcd"))) + (assert "" (&/<= "bcd" "abc")) + (assert "" (&/<= "abc" "abc")) + (assert "" (not (&/<= "abc" "bcd"))) + (assert "" (&/> "abc" "bcd")) + (assert "" (not (&/> "abc" "abc"))) + (assert "" (not (&/> "bcd" "abc"))) + (assert "" (&/>= "abc" "bcd")) + (assert "" (&/>= "abc" "abc")) + (assert "" (not (&/>= "bcd" "abc"))) + ))) (test: "Codec" [size bounded-size diff --git a/stdlib/test/test/lux/data/text/format.lux b/stdlib/test/test/lux/data/text/format.lux index 12516a9ca..7ec6baf98 100644 --- a/stdlib/test/test/lux/data/text/format.lux +++ b/stdlib/test/test/lux/data/text/format.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data text/format [text] diff --git a/stdlib/test/test/lux/effect.lux b/stdlib/test/test/lux/effect.lux new file mode 100644 index 000000000..275e1e66a --- /dev/null +++ b/stdlib/test/test/lux/effect.lux @@ -0,0 +1,77 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux [io "IO/" Monad] + (control monad + functor) + (data [text] + text/format) + [macro] + ["R" random] + pipe + effect) + lux/test) + +(do-template [ ] + [(effect: + ( [Nat Text] Bool) + ( Nat))] + + [EffA opA fieldA] + [EffB opB fieldB] + [EffC opC fieldC] + ) + +(do-template [ ] + [(handler: _ + (=> [io;IO io;Monad]) + (def: ( size sample) + (IO/wrap ( size (text;size sample)))) + + (def: (IO/wrap )))] + + [EffA opA n.< fieldA +10] + [EffB opB n.= fieldB +20] + [EffC opC n.> fieldC +30] + ) + +(type: EffABC (|E EffA EffB EffC)) + +(def: Functor + (Functor EffABC) + (|F Functor Functor Functor)) + +(def: Handler + (Handler EffABC io;IO) + (|H io;Monad + Handler Handler Handler)) + +## [Tests] +(test: "Algebraic effects" + (let% [ (do-template [ ] + [(io;run (with-handler Handler + (doE Functor + [] + (lift ( "YOLO"))))) + (n.= (io;run (with-handler Handler + (doE Functor + [] + (lift )))))] + + [opA +10 fieldA +10] + [opB +4 fieldB +20] + [opC +2 fieldC +30])] + (assert "Can handle effects using handlers." + (and + + (n.= +60 (io;run (with-handler Handler + (doE Functor + [a (lift fieldA) + b (lift fieldB) + c (lift fieldC)] + (wrap ($_ n.+ a b c)))))) + )))) diff --git a/stdlib/test/test/lux/host.lux b/stdlib/test/test/lux/host.lux index 03ed87772..61f6aa7ec 100644 --- a/stdlib/test/test/lux/host.lux +++ b/stdlib/test/test/lux/host.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (control monad) + (lux [io] + (control monad) (data text/format [number] [product] [text "Text/" Eq]) - (codata function - [io]) + (codata function) ["&" host #+ jvm-import class: interface: object] ["R" random] pipe) diff --git a/stdlib/test/test/lux/io.lux b/stdlib/test/test/lux/io.lux new file mode 100644 index 000000000..0d7a3e5a9 --- /dev/null +++ b/stdlib/test/test/lux/io.lux @@ -0,0 +1,27 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;module: + lux + (lux ["&" io] + (control monad) + (data [text "Text/" Monoid Eq] + text/format + [number]) + (codata function)) + lux/test) + +(test: "I/O" + ($_ seq + (assert "" (Text/= "YOLO" (&;run (&;io "YOLO")))) + (assert "" (i.= 11 (&;run (:: &;Functor map i.inc (&;io 10))))) + (assert "" (i.= 10 (&;run (:: &;Applicative wrap 10)))) + (assert "" (i.= 30 (&;run (let [(^open "&/") &;Applicative] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20)))))) + (assert "" (i.= 30 (&;run (do &;Monad + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) diff --git a/stdlib/test/test/lux/lexer.lux b/stdlib/test/test/lux/lexer.lux index 3c459ab8e..f08841a93 100644 --- a/stdlib/test/test/lux/lexer.lux +++ b/stdlib/test/test/lux/lexer.lux @@ -6,12 +6,12 @@ (;module: lux (lux (control monad) - (codata [io]) + [io] (data [error #- fail] [text "T/" Eq] text/format [char "C/" Eq] - (struct [list])) + (coll [list])) ["R" random] pipe ["&" lexer]) diff --git a/stdlib/test/test/lux/lexer/regex.lux b/stdlib/test/test/lux/lexer/regex.lux index 4a9f01c27..fd8e563ec 100644 --- a/stdlib/test/test/lux/lexer/regex.lux +++ b/stdlib/test/test/lux/lexer/regex.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [error #- fail] [product] diff --git a/stdlib/test/test/lux/macro/ast.lux b/stdlib/test/test/lux/macro/ast.lux index 61127bef7..9c010c1e4 100644 --- a/stdlib/test/test/lux/macro/ast.lux +++ b/stdlib/test/test/lux/macro/ast.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (codata function) (control monad) (data [text "T/" Eq] diff --git a/stdlib/test/test/lux/macro/poly/eq.lux b/stdlib/test/test/lux/macro/poly/eq.lux index 4f4c296d6..ed34369a9 100644 --- a/stdlib/test/test/lux/macro/poly/eq.lux +++ b/stdlib/test/test/lux/macro/poly/eq.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad eq) (data text/format diff --git a/stdlib/test/test/lux/macro/poly/functor.lux b/stdlib/test/test/lux/macro/poly/functor.lux index 23d42c78d..ca269e15e 100644 --- a/stdlib/test/test/lux/macro/poly/functor.lux +++ b/stdlib/test/test/lux/macro/poly/functor.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad [functor] eq) diff --git a/stdlib/test/test/lux/macro/poly/text-encoder.lux b/stdlib/test/test/lux/macro/poly/text-encoder.lux index 154d9ab10..ef9474f42 100644 --- a/stdlib/test/test/lux/macro/poly/text-encoder.lux +++ b/stdlib/test/test/lux/macro/poly/text-encoder.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad eq) (data text/format diff --git a/stdlib/test/test/lux/macro/syntax.lux b/stdlib/test/test/lux/macro/syntax.lux index 2755bbf8e..1d96883de 100644 --- a/stdlib/test/test/lux/macro/syntax.lux +++ b/stdlib/test/test/lux/macro/syntax.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad eq) (data [text "Text/" Monoid] @@ -73,8 +73,8 @@ (let% [ (do-template [ ] [(assert (and (is? (list ( ))) - (found? (s;sample? ( )) (list ( ))) - (enforced? (s;sample! ( )) (list ( )))))] + (found? (s;this? ( )) (list ( ))) + (enforced? (s;this! ( )) (list ( )))))] ["Can parse Bool syntax." true ast;bool bool;Eq s;bool] ["Can parse Nat syntax." +123 ast;nat number;Eq s;nat] @@ -252,8 +252,8 @@ (assert "Can parse while taking separators into account." (and (match (list 123 456 789) (s;run (list (ast;int 123) (ast;text "YOLO") (ast;int 456) (ast;text "YOLO") (ast;int 789)) - (s;sep-by (s;sample! (' "YOLO")) s;int))) + (s;sep-by (s;this! (' "YOLO")) s;int))) (match (list 123 456) (s;run (list (ast;int 123) (ast;text "YOLO") (ast;int 456) (ast;int 789)) - (s;sep-by (s;sample! (' "YOLO")) s;int))))) + (s;sep-by (s;this! (' "YOLO")) s;int))))) )) diff --git a/stdlib/test/test/lux/math.lux b/stdlib/test/test/lux/math.lux index 1ffba0aa2..f16517d85 100644 --- a/stdlib/test/test/lux/math.lux +++ b/stdlib/test/test/lux/math.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format [bool "b/" Eq] [number "r/" Number] - (struct [list "List/" Fold Functor]) + (coll [list "List/" Fold Functor]) [product]) (codata function) ["R" random] diff --git a/stdlib/test/test/lux/math/complex.lux b/stdlib/test/test/lux/math/complex.lux index f9743e9ec..0fb327425 100644 --- a/stdlib/test/test/lux/math/complex.lux +++ b/stdlib/test/test/lux/math/complex.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format [bool "b/" Eq] [number "r/" Number] - (struct [list "List/" Fold Functor]) + (coll [list "List/" Fold Functor]) [product]) (codata function) [math] diff --git a/stdlib/test/test/lux/math/logic/continuous.lux b/stdlib/test/test/lux/math/logic/continuous.lux index 883d54a06..57905a4d7 100644 --- a/stdlib/test/test/lux/math/logic/continuous.lux +++ b/stdlib/test/test/lux/math/logic/continuous.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (codata function) ["R" random] diff --git a/stdlib/test/test/lux/math/ratio.lux b/stdlib/test/test/lux/math/ratio.lux index 0cdbf4f93..cd9c6138b 100644 --- a/stdlib/test/test/lux/math/ratio.lux +++ b/stdlib/test/test/lux/math/ratio.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format [bool "b/" Eq] [number "r/" Number] - (struct [list "List/" Fold Functor]) + (coll [list "List/" Fold Functor]) [product]) (codata function) ["R" random] diff --git a/stdlib/test/test/lux/math/simple.lux b/stdlib/test/test/lux/math/simple.lux index 235723f25..0fc6ee2be 100644 --- a/stdlib/test/test/lux/math/simple.lux +++ b/stdlib/test/test/lux/math/simple.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format [bool "b/" Eq] [number "r/" Number] - (struct [list "List/" Fold Functor]) + (coll [list "List/" Fold Functor]) [product]) (codata function) ["R" random] diff --git a/stdlib/test/test/lux/pipe.lux b/stdlib/test/test/lux/pipe.lux index 86f45079f..0fc95add2 100644 --- a/stdlib/test/test/lux/pipe.lux +++ b/stdlib/test/test/lux/pipe.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data text/format [number] diff --git a/stdlib/test/test/lux/type.lux b/stdlib/test/test/lux/type.lux index 1670f3146..081d9d444 100644 --- a/stdlib/test/test/lux/type.lux +++ b/stdlib/test/test/lux/type.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid] text/format [number] maybe - (struct [list])) + (coll [list])) ["R" random] pipe ["&" type]) diff --git a/stdlib/test/test/lux/type/auto.lux b/stdlib/test/test/lux/type/auto.lux index fdc1ec51a..12b52292e 100644 --- a/stdlib/test/test/lux/type/auto.lux +++ b/stdlib/test/test/lux/type/auto.lux @@ -5,7 +5,7 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad functor [eq]) @@ -14,7 +14,7 @@ [number] [bool "B/" Eq] maybe - (struct [list])) + (coll [list])) ["R" random] pipe [type] diff --git a/stdlib/test/test/lux/type/check.lux b/stdlib/test/test/lux/type/check.lux index 2dadd92f2..e69bcfc45 100644 --- a/stdlib/test/test/lux/type/check.lux +++ b/stdlib/test/test/lux/type/check.lux @@ -5,13 +5,13 @@ (;module: lux - (lux (codata [io]) + (lux [io] (control monad) (data [text "Text/" Monoid Eq] text/format [number] maybe - (struct [list])) + (coll [list])) ["R" random] pipe [type] diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index fdcf01457..d6a21cae4 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -6,27 +6,27 @@ (;module: lux (lux (control monad) - (codata [io]) + [io] (concurrency [promise]) [cli #+ program:] [test]) (test lux (lux ["_;" cli] ["_;" host] + ["_;" io] ["_;" pipe] ["_;" lexer] (lexer ["_;" regex]) - (codata ["_;" io] + (codata ["_;" cont] ["_;" env] ["_;" state] - ["_;" cont] - (struct ["_;" stream])) + (coll ["_;" stream])) (concurrency ["_;" actor] ["_;" atom] ["_;" frp] ["_;" promise] ["_;" stm]) - (control [effect]) + ["_;" effect] (data [bit] [bool] [char] @@ -41,15 +41,15 @@ [text] (error [exception]) (format [json]) - (struct [array] - [dict] - [list] - [queue] - [set] - [stack] - ## [vector] - (tree [rose] - [zipper])) + (coll [array] + [dict] + [list] + [queue] + [set] + [stack] + ## [vector] + (tree [rose] + [zipper])) (text [format]) ) ["_;" math] -- cgit v1.2.3