From 020f625b3d94cdb00242ead397595eeff842533c Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 30 Mar 2017 21:37:41 -0400 Subject: - Implemented ordered sets by means of red-black trees. --- stdlib/test/test/lux/data/coll/ordered.lux | 68 ++++++++++++++++++++++++++++++ stdlib/test/tests.lux | 1 + 2 files changed, 69 insertions(+) create mode 100644 stdlib/test/test/lux/data/coll/ordered.lux (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/data/coll/ordered.lux b/stdlib/test/test/lux/data/coll/ordered.lux new file mode 100644 index 000000000..213a568c1 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/ordered.lux @@ -0,0 +1,68 @@ +(;module: + lux + (lux [io] + (control monad) + (data (coll ["&" ordered] + ["S" set] + [list "" Fold]) + [number] + text/format) + ["R" math/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) + (:: @ map (|>. S;to-list (&;from-list number;Ord)))) + setR (|> (R;set number;Hash sizeR gen-nat) + (:: @ map (|>. S;to-list (&;from-list number;Ord)))) + #let [(^open "&/") &;Eq]] + ($_ seq + (assert "I can query the size of a set." + (n.= sizeL (&;size setL))) + + (assert "Converting sets to/from lists can't change their values." + (|> setL + &;to-list (&;from-list number;Ord) + (&/= setL))) + + (assert "Order is preserved." + (let [listL (&;to-list setL) + (^open "L/") (list;Eq number;Eq)] + (L/= listL + (list;sort n.< listL)))) + + (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;Ord) + setL))) + + (assert "Intersection with the empty set results in the empty set." + (let [empty-set (&;new number;Ord)] + (&/= 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." + (list;every? (&;member? setL) (&;to-list setL))) + )) diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index c066e551e..ffe0628bf 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -45,6 +45,7 @@ [list] [queue] [set] + [ordered] [stack] ## [vector] (tree [rose] -- cgit v1.2.3