From bed22d326d1a7555dbcfd589da51a592bfa9113b Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 23 Jun 2017 19:48:36 -0400 Subject: - Added Fold structure for rose trees. --- stdlib/test/test/lux/data/coll/tree/rose.lux | 51 +++++++++++++++++----------- 1 file changed, 31 insertions(+), 20 deletions(-) (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/data/coll/tree/rose.lux b/stdlib/test/test/lux/data/coll/tree/rose.lux index 2933452f6..fe8ffa71b 100644 --- a/stdlib/test/test/lux/data/coll/tree/rose.lux +++ b/stdlib/test/test/lux/data/coll/tree/rose.lux @@ -2,33 +2,44 @@ lux (lux [io] (control monad) - (data (coll (tree ["&" rose]) - [list "List/" Monad]) - [number]) - ["R" math/random]) + (data [product] + [number] + [text "T/" Eq] + text/format + (coll (tree ["&" rose]) + [list "L/" Monad Fold])) + ["r" math/random]) lux/test) -(def: gen-nat - (R;Random Nat) - (|> R;nat - (:: R;Monad map (n.% +100)))) +(def: gen-tree + (r;Random [Nat (&;Tree Nat)]) + (r;rec + (function [gen-tree] + (r;either (:: r;Monad map (|>. &;leaf [+1]) r;nat) + (do r;Monad + [value r;nat + num-children (|> r;nat (:: @ map (n.% +3))) + children' (r;list num-children gen-tree) + #let [size' (L/fold n.+ +0 (L/map product;left children')) + children (L/map product;right children')]] + (wrap [(n.inc size') + (&;branch value children)])) + )))) (context: "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))] + [[size sample] gen-tree #let [(^open "&/") (&;Eq number;Eq) - (^open "List/") (list;Eq number;Eq)]] + (^open "&/") &;Fold + concat (function [addition partial] (format partial (%n addition)))]] ($_ seq (test "Can compare trees for equality." - (and (&/= leaf leaf) - (&/= branch branch) - (not (&/= leaf branch)) - (not (&/= leaf (&;branch branchV (List/map &;leaf (list;reverse branchC))))))) + (&/= sample sample)) (test "Can flatten a tree to get all the nodes as a flat tree." - (List/= (list& branchV branchC) - (&;flatten branch))) + (n.= size + (list;size (&;flatten sample)))) + + (test "Can fold trees." + (T/= (&/fold concat "" sample) + (L/fold concat "" (&;flatten sample)))) )) -- cgit v1.2.3