From 3a32ba13ae6507c0b842853cc9a83fb443c2f480 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 5 Dec 2016 16:40:33 -0400 Subject: - Updated tests in the lux/codata branch. --- stdlib/test/test/lux/codata/env.lux | 32 +++--- stdlib/test/test/lux/codata/io.lux | 32 +++--- stdlib/test/test/lux/codata/state.lux | 51 +++++---- stdlib/test/test/lux/codata/struct/stream.lux | 150 ++++++++++++++++---------- stdlib/test/tests.lux | 8 +- 5 files changed, 164 insertions(+), 109 deletions(-) (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/codata/env.lux b/stdlib/test/test/lux/codata/env.lux index 0e24ddf75..a2a90a3af 100644 --- a/stdlib/test/test/lux/codata/env.lux +++ b/stdlib/test/test/lux/codata/env.lux @@ -1,3 +1,8 @@ +## 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]) @@ -6,18 +11,19 @@ text/format [number]) (codata function - env)) + ["&" env])) lux/test) -(test: "lux/codata/env exports" - (test-all (match 123 (run 123 ask)) - (match 246 (run 123 (local (i.* 2) ask))) - (match 134 (run 123 (:: Functor map i.inc (i.+ 10)))) - (match 10 (run 123 (:: Applicative wrap 10))) - (match 30 (run 123 (let [(^open) Applicative] - (apply (wrap (i.+ 10)) (wrap 20))))) - (match 30 (run 123 (do Monad - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - (wrap (f x y))))))) +(test: "Envs" + ($_ seq + (assert "" (i.= 123 (&;run 123 &;ask))) + (assert "" (i.= 246 (&;run 123 (&;local (i.* 2) &;ask)))) + (assert "" (i.= 134 (&;run 123 (:: &;Functor map i.inc (i.+ 10))))) + (assert "" (i.= 10 (&;run 123 (:: &;Applicative wrap 10)))) + (assert "" (i.= 30 (&;run 123 (let [(^open "&/") &;Applicative] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20)))))) + (assert "" (i.= 30 (&;run 123 (do &;Monad + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) diff --git a/stdlib/test/test/lux/codata/io.lux b/stdlib/test/test/lux/codata/io.lux index 916609226..7965869d0 100644 --- a/stdlib/test/test/lux/codata/io.lux +++ b/stdlib/test/test/lux/codata/io.lux @@ -1,21 +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 (control monad) - (data [text "Text/" Monoid] + (data [text "Text/" Monoid Eq] text/format [number]) (codata function - io)) + ["&" io])) lux/test) -(test: "lux/codata/io exports" - (test-all (match "YOLO" (run (io "YOLO"))) - (match 11 (run (:: Functor map i.inc (io 10)))) - (match 10 (run (:: Applicative wrap 10))) - (match 30 (run (let [(^open) Applicative] - (apply (wrap (i.+ 10)) (wrap 20))))) - (match 30 (run (do Monad - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - (wrap (f x y))))))) +(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 84c90b5b1..4c4b808b3 100644 --- a/stdlib/test/test/lux/codata/state.lux +++ b/stdlib/test/test/lux/codata/state.lux @@ -1,3 +1,8 @@ +## 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]) @@ -7,28 +12,28 @@ [number] [product]) (codata function - state)) + ["&" state])) lux/test) -(test: "lux/codata/state exports" - (test-all (match 123 (product;right (run 123 get))) - (match 321 (product;right (run 123 (do Monad - [_ (put 321)] - get)))) - (match 369 (product;right (run 123 (do Monad - [_ (update (i.* 3))] - get)))) - (match 124 (product;right (run 123 (use i.inc)))) - (match 246 (product;right (run 123 (local (i.* 2) get)))) - (match 124 (product;right (run 123 (:: Functor map i.inc get)))) - (match 10 (product;right (run 123 (:: Applicative wrap 10)))) - (match 30 (product;right (run 123 (let [(^open) Applicative] - (apply (wrap (i.+ 10)) (wrap 20)))))) - (match 30 (product;right (run 123 (: (State Int Int) - (do Monad - [f (wrap i.+) - x (wrap 10) - y (wrap 20)] - - (wrap (f x y))))))) - )) +(test: "State" + ($_ seq + (assert "" (i.= 123 (product;right (&;run 123 &;get)))) + (assert "" (i.= 321 (product;right (&;run 123 (do &;Monad + [_ (&;put 321)] + &;get))))) + (assert "" (i.= 369 (product;right (&;run 123 (do &;Monad + [_ (&;update (i.* 3))] + &;get))))) + (assert "" (i.= 124 (product;right (&;run 123 (&;use i.inc))))) + (assert "" (i.= 246 (product;right (&;run 123 (&;local (i.* 2) &;get))))) + (assert "" (i.= 124 (product;right (&;run 123 (:: &;Functor map i.inc &;get))))) + (assert "" (i.= 10 (product;right (&;run 123 (:: &;Applicative wrap 10))))) + (assert "" (i.= 30 (product;right (&;run 123 (let [(^open "&/") &;Applicative] + (&/apply (&/wrap (i.+ 10)) (&/wrap 20))))))) + (assert "" (i.= 30 (product;right (&;run 123 (: (&;State Int Int) + (do &;Monad + [f (wrap i.+) + x (wrap 10) + y (wrap 20)] + (wrap (f x y)))))))) + )) diff --git a/stdlib/test/test/lux/codata/struct/stream.lux b/stdlib/test/test/lux/codata/struct/stream.lux index 1da81aa02..c9397b1a3 100644 --- a/stdlib/test/test/lux/codata/struct/stream.lux +++ b/stdlib/test/test/lux/codata/struct/stream.lux @@ -1,3 +1,8 @@ +## 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]) @@ -5,64 +10,97 @@ comonad) (data [text "Text/" Monoid] text/format - [number "Int/" Codec]) + (struct [list]) + [number "Nat/" Codec]) (codata function [cont] - (struct stream))) + (struct ["&" stream])) + (math ["R" random]) + pipe) lux/test) -(test: "lux/codata/stream exports" - (let% [ (do-template [ ] - [(match (^ (list 0 1 2)) - ( (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [3 4 5 6]}) - ( (iterate i.inc 0))) - (match (^=> (^ [(list 0 1 2) _stream_]) - {_stream_ (^stream& w x y z ...)} - {[w x y z] [3 4 5 6]}) - ( (iterate i.inc 0)))] +(test: "Streams" + [size (|> R;nat (:: @ map (|>. (n.% +100) (n.max +1)))) + offset (|> R;nat (:: @ map (n.% +100))) + factor (|> R;nat (:: @ map (|>. (n.% +100) (n.max +1)))) + 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 size) + (&;take size sample0)) + (List/= (list;n.range offset (n.+ offset size)) + (&;take size (&;drop offset sample0))) + (let [[drops takes...] (&;split size sample0)] + (and (List/= (list;n.range +0 size) + drops) + (List/= (list;n.range size (n.* +2 size)) + (&;take size takes...))))) + (and (List/= (list;n.range +0 size) + (&;take-while (n.< size) sample0)) + (List/= (list;n.range offset (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 size) + drops) + (List/= (list;n.range size (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 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))))) - [take drop split +3] - [take-while drop-while split-with (i.< 3)]) - ] - (test-all (match (^=> (^stream& w x y z ...) - {[w x y z] [0 1 2 3]}) - (iterate i.inc 0)) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 0 0 0]}) - (repeat 0)) - (match (^=> (#;Some the-stream) - {the-stream (^stream& w x y z ...)} - {[w x y z] [0 1 0 1]}) - (cycle (list 0 1))) - (match 0 (head (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [1 2 3 4]}) - (tail (iterate i.inc 0))) - (match 9 (at +9 (iterate i.inc 0))) - (match 0 (at +0 (iterate i.inc 0))) - - (match (^=> (^stream& w x y z ...) - {[w x y z] ["0" "1" "2" "3"]}) - (unfold (lambda [n] [(i.inc n) (Int/encode n)]) - 0)) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 2 4 6]}) - (filter even? (iterate i.inc 0))) - (match (^=> [e_stream o_stream] - {e_stream (^stream& w x y z ...)} - {o_stream (^stream& a b c d ...)} - {[w x y z a b c d] [0 2 4 6 1 3 5 7]}) - (partition even? (iterate i.inc 0))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [0 1 4 9]}) - (let [square (lambda [n] (i.* n n))] - (:: Functor map square (iterate i.inc 0)))) - (match (^=> (^stream& w x y z ...) - {[w x y z] [4 9 16 25]}) - (let [square (lambda [n] (i.* n n))] - (be CoMonad - [inputs (iterate i.inc 2)] - (square (head inputs))))) - ))) + (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/tests.lux b/stdlib/test/tests.lux index 5314c2923..de1487b34 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -12,6 +12,10 @@ [test]) (test lux (lux ["_;" cli] + (codata ["_;" io] + [env] + [state] + (struct [stream])) (data [bit] [bool] [char] @@ -38,10 +42,6 @@ ) (text [format]) ) - ## (codata ["_;" io] - ## [env] - ## [state] - ## (struct [stream])) ## (macro [ast] ## [syntax]) ## [type] -- cgit v1.2.3