From 2f4233ded0dce94c12f52db5fef0769670c78fdd Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 10 Jul 2018 20:12:58 -0400 Subject: - Re-organized dictionary & set modules a bit. --- stdlib/test/test/lux/control/interval.lux | 2 +- stdlib/test/test/lux/data/coll/dictionary.lux | 128 +++++++++++++++++++++ .../test/test/lux/data/coll/dictionary/ordered.lux | 6 +- .../test/lux/data/coll/dictionary/unordered.lux | 128 --------------------- stdlib/test/test/lux/data/coll/set.lux | 64 +++++++++++ stdlib/test/test/lux/data/coll/set/ordered.lux | 8 +- stdlib/test/test/lux/data/coll/set/unordered.lux | 64 ----------- stdlib/test/test/lux/data/format/json.lux | 2 +- stdlib/test/test/lux/data/format/xml.lux | 2 +- .../test/test/lux/lang/compiler/analysis/case.lux | 2 +- .../lang/compiler/analysis/procedure/host.jvm.lux | 2 +- .../test/lux/lang/compiler/analysis/structure.lux | 2 +- .../test/lux/lang/compiler/synthesis/function.lux | 4 +- stdlib/test/test/lux/lang/syntax.lux | 2 +- stdlib/test/test/lux/lang/type/check.lux | 2 +- stdlib/test/test/lux/math/logic/fuzzy.lux | 2 +- stdlib/test/test/lux/math/random.lux | 4 +- 17 files changed, 212 insertions(+), 212 deletions(-) create mode 100644 stdlib/test/test/lux/data/coll/dictionary.lux delete mode 100644 stdlib/test/test/lux/data/coll/dictionary/unordered.lux create mode 100644 stdlib/test/test/lux/data/coll/set.lux delete mode 100644 stdlib/test/test/lux/data/coll/set/unordered.lux (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/control/interval.lux b/stdlib/test/test/lux/control/interval.lux index 06fe5cbde..6b6e96789 100644 --- a/stdlib/test/test/lux/control/interval.lux +++ b/stdlib/test/test/lux/control/interval.lux @@ -8,7 +8,7 @@ ["r" math/random] (data text/format [number] - (coll ["S" set/unordered] + (coll ["S" set] ["L" list])))) (context: "Equivalence." diff --git a/stdlib/test/test/lux/data/coll/dictionary.lux b/stdlib/test/test/lux/data/coll/dictionary.lux new file mode 100644 index 000000000..9c652ee7a --- /dev/null +++ b/stdlib/test/test/lux/data/coll/dictionary.lux @@ -0,0 +1,128 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad] + ["eq" equivalence]) + (data [text] + text/format + [number] + [maybe] + (coll ["&" dictionary] + [list "list/" Fold Functor])) + ["r" math/random]) + lux/test) + +(context: "Dictionaries." + (<| (times +100) + (do @ + [#let [capped-nat (:: r.Monad map (n/% +100) r.nat)] + size capped-nat + dict (r.dictionary number.Hash size r.nat capped-nat) + non-key (|> r.nat (r.filter (function (_ key) (not (&.contains? key dict))))) + test-val (|> r.nat (r.filter (function (_ val) (not (list.member? number.Equivalence (&.values dict) val)))))] + ($_ seq + (test "Size function should correctly represent Dictionary size." + (n/= size (&.size dict))) + + (test "Dictionaries of size 0 should be considered empty." + (if (n/= +0 size) + (&.empty? dict) + (not (&.empty? dict)))) + + (test "The functions 'entries', 'keys' and 'values' should be synchronized." + (:: (list.Equivalence (eq.product number.Equivalence number.Equivalence)) = + (&.entries dict) + (list.zip2 (&.keys dict) + (&.values dict)))) + + (test "Dictionary should be able to recognize it's own keys." + (list.every? (function (_ key) (&.contains? key dict)) + (&.keys dict))) + + (test "Should be able to get every key." + (list.every? (function (_ key) (case (&.get key dict) + (#.Some _) true + _ false)) + (&.keys dict))) + + (test "Shouldn't be able to access non-existant keys." + (case (&.get non-key dict) + (#.Some _) false + _ true)) + + (test "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)) + + (test "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)) + + (test "Shouldn't be able to put~ an existing key." + (or (n/= +0 size) + (let [first-key (|> dict &.keys list.head maybe.assume)] + (case (&.get first-key (&.put~ first-key test-val dict)) + (#.Some v) (not (n/= test-val v)) + _ true)))) + + (test "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)))))) + + (test "Should be possible to update values via their keys." + (let [base (&.put non-key test-val dict) + updt (&.update non-key inc base)] + (case [(&.get non-key base) (&.get non-key updt)] + [(#.Some x) (#.Some y)] + (n/= (inc x) y) + + _ + false))) + + (test "Additions and removals to a Dictionary should affect its size." + (let [plus (&.put non-key test-val dict) + base (&.remove non-key plus)] + (and (n/= (inc (&.size dict)) (&.size plus)) + (n/= (dec (&.size plus)) (&.size base))))) + + (test "A Dictionary should equal itself & going to<->from lists shouldn't change that." + (let [(^open) (&.Equivalence number.Equivalence)] + (and (= dict dict) + (|> dict &.entries (&.from-list number.Hash) (= dict))))) + + (test "Merging a Dictionary to itself changes nothing." + (let [(^open) (&.Equivalence number.Equivalence)] + (= dict (&.merge dict dict)))) + + (test "If you merge, and the second dict has overlapping keys, it should overwrite yours." + (let [dict' (|> dict &.entries + (list/map (function (_ [k v]) [k (inc v)])) + (&.from-list number.Hash)) + (^open) (&.Equivalence number.Equivalence)] + (= dict' (&.merge dict' dict)))) + + (test "Can merge values in such a way that they become combined." + (list.every? (function (_ [x x*2]) (n/= (n/* +2 x) x*2)) + (list.zip2 (&.values dict) + (&.values (&.merge-with n/+ dict dict))))) + + (test "Should be able to select subset of keys from dict." + (|> dict + (&.put non-key test-val) + (&.select (list non-key)) + &.size + (n/= +1))) + + (test "Should be able to re-bind existing values to different keys." + (or (n/= +0 size) + (let [first-key (|> dict &.keys list.head maybe.assume) + rebound (&.re-bind first-key non-key dict)] + (and (n/= (&.size dict) (&.size rebound)) + (&.contains? non-key rebound) + (not (&.contains? first-key rebound)) + (n/= (maybe.assume (&.get first-key dict)) + (maybe.assume (&.get non-key rebound))))))) + )))) diff --git a/stdlib/test/test/lux/data/coll/dictionary/ordered.lux b/stdlib/test/test/lux/data/coll/dictionary/ordered.lux index bfcd4b569..548fd7f83 100644 --- a/stdlib/test/test/lux/data/coll/dictionary/ordered.lux +++ b/stdlib/test/test/lux/data/coll/dictionary/ordered.lux @@ -5,9 +5,9 @@ [equivalence #+ Equivalence]) (data [product] [number] - (coll (set ["s" unordered]) - (dictionary ["dict" unordered] - ["&" ordered]) + (coll ["s" set] + ["dict" dictionary] + (dictionary ["&" ordered]) [list "L/" Functor])) ["r" math/random]) lux/test) diff --git a/stdlib/test/test/lux/data/coll/dictionary/unordered.lux b/stdlib/test/test/lux/data/coll/dictionary/unordered.lux deleted file mode 100644 index 3476898b6..000000000 --- a/stdlib/test/test/lux/data/coll/dictionary/unordered.lux +++ /dev/null @@ -1,128 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad] - ["eq" equivalence]) - (data [text] - text/format - [number] - [maybe] - (coll (dictionary ["&" unordered]) - [list "list/" Fold Functor])) - ["r" math/random]) - lux/test) - -(context: "Dictionaries." - (<| (times +100) - (do @ - [#let [capped-nat (:: r.Monad map (n/% +100) r.nat)] - size capped-nat - dict (r.dictionary number.Hash size r.nat capped-nat) - non-key (|> r.nat (r.filter (function (_ key) (not (&.contains? key dict))))) - test-val (|> r.nat (r.filter (function (_ val) (not (list.member? number.Equivalence (&.values dict) val)))))] - ($_ seq - (test "Size function should correctly represent Dictionary size." - (n/= size (&.size dict))) - - (test "Dictionaries of size 0 should be considered empty." - (if (n/= +0 size) - (&.empty? dict) - (not (&.empty? dict)))) - - (test "The functions 'entries', 'keys' and 'values' should be synchronized." - (:: (list.Equivalence (eq.product number.Equivalence number.Equivalence)) = - (&.entries dict) - (list.zip2 (&.keys dict) - (&.values dict)))) - - (test "Dictionary should be able to recognize it's own keys." - (list.every? (function (_ key) (&.contains? key dict)) - (&.keys dict))) - - (test "Should be able to get every key." - (list.every? (function (_ key) (case (&.get key dict) - (#.Some _) true - _ false)) - (&.keys dict))) - - (test "Shouldn't be able to access non-existant keys." - (case (&.get non-key dict) - (#.Some _) false - _ true)) - - (test "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)) - - (test "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)) - - (test "Shouldn't be able to put~ an existing key." - (or (n/= +0 size) - (let [first-key (|> dict &.keys list.head maybe.assume)] - (case (&.get first-key (&.put~ first-key test-val dict)) - (#.Some v) (not (n/= test-val v)) - _ true)))) - - (test "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)))))) - - (test "Should be possible to update values via their keys." - (let [base (&.put non-key test-val dict) - updt (&.update non-key inc base)] - (case [(&.get non-key base) (&.get non-key updt)] - [(#.Some x) (#.Some y)] - (n/= (inc x) y) - - _ - false))) - - (test "Additions and removals to a Dictionary should affect its size." - (let [plus (&.put non-key test-val dict) - base (&.remove non-key plus)] - (and (n/= (inc (&.size dict)) (&.size plus)) - (n/= (dec (&.size plus)) (&.size base))))) - - (test "A Dictionary should equal itself & going to<->from lists shouldn't change that." - (let [(^open) (&.Equivalence number.Equivalence)] - (and (= dict dict) - (|> dict &.entries (&.from-list number.Hash) (= dict))))) - - (test "Merging a Dictionary to itself changes nothing." - (let [(^open) (&.Equivalence number.Equivalence)] - (= dict (&.merge dict dict)))) - - (test "If you merge, and the second dict has overlapping keys, it should overwrite yours." - (let [dict' (|> dict &.entries - (list/map (function (_ [k v]) [k (inc v)])) - (&.from-list number.Hash)) - (^open) (&.Equivalence number.Equivalence)] - (= dict' (&.merge dict' dict)))) - - (test "Can merge values in such a way that they become combined." - (list.every? (function (_ [x x*2]) (n/= (n/* +2 x) x*2)) - (list.zip2 (&.values dict) - (&.values (&.merge-with n/+ dict dict))))) - - (test "Should be able to select subset of keys from dict." - (|> dict - (&.put non-key test-val) - (&.select (list non-key)) - &.size - (n/= +1))) - - (test "Should be able to re-bind existing values to different keys." - (or (n/= +0 size) - (let [first-key (|> dict &.keys list.head maybe.assume) - rebound (&.re-bind first-key non-key dict)] - (and (n/= (&.size dict) (&.size rebound)) - (&.contains? non-key rebound) - (not (&.contains? first-key rebound)) - (n/= (maybe.assume (&.get first-key dict)) - (maybe.assume (&.get non-key rebound))))))) - )))) 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..6b341ee3a --- /dev/null +++ b/stdlib/test/test/lux/data/coll/set.lux @@ -0,0 +1,64 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad]) + (data (coll ["&" set #+ Set] + [list "" Fold]) + [number]) + ["r" math/random]) + lux/test) + +(def: gen-nat + (r.Random Nat) + (|> r.nat + (:: r.Monad map (n/% +100)))) + +(context: "Sets" + (<| (times +100) + (do @ + [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 (|>> (&.member? setL) not))) + #let [(^open "&/") &.Equivalence]] + ($_ seq + (test "I can query the size of a set." + (and (n/= sizeL (&.size setL)) + (n/= sizeR (&.size setR)))) + + (test "Converting sets to/from lists can't change their values." + (|> setL + &.to-list (&.from-list number.Hash) + (&/= setL))) + + (test "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)))) + + (test "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)))) + + (test "Union with the empty set leaves a set unchanged." + (&/= setL + (&.union (&.new number.Hash) + setL))) + + (test "Intersection with the empty set results in the empty set." + (let [empty-set (&.new number.Hash)] + (&/= empty-set + (&.intersection empty-set setL)))) + + (test "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))))) + + (test "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/set/ordered.lux b/stdlib/test/test/lux/data/coll/set/ordered.lux index fd4fb5579..6833bf4a6 100644 --- a/stdlib/test/test/lux/data/coll/set/ordered.lux +++ b/stdlib/test/test/lux/data/coll/set/ordered.lux @@ -2,8 +2,8 @@ lux (lux [io] (control [monad #+ do Monad]) - (data (coll (set ["s" unordered] - ["&" ordered]) + (data (coll [set] + (set ["&" ordered]) [list "" Fold]) [number] text/format) @@ -20,8 +20,8 @@ (do @ [sizeL gen-nat sizeR gen-nat - listL (|> (r.set number.Hash sizeL gen-nat) (:: @ map s.to-list)) - listR (|> (r.set number.Hash sizeR gen-nat) (:: @ map s.to-list)) + listL (|> (r.set number.Hash sizeL gen-nat) (:: @ map set.to-list)) + listR (|> (r.set number.Hash sizeR gen-nat) (:: @ map set.to-list)) #let [(^open "&/") &.Equivalence setL (&.from-list number.Order listL) setR (&.from-list number.Order listR) diff --git a/stdlib/test/test/lux/data/coll/set/unordered.lux b/stdlib/test/test/lux/data/coll/set/unordered.lux deleted file mode 100644 index f17867665..000000000 --- a/stdlib/test/test/lux/data/coll/set/unordered.lux +++ /dev/null @@ -1,64 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad]) - (data (coll (set ["&" unordered #+ Set]) - [list "" Fold]) - [number]) - ["r" math/random]) - lux/test) - -(def: gen-nat - (r.Random Nat) - (|> r.nat - (:: r.Monad map (n/% +100)))) - -(context: "Sets" - (<| (times +100) - (do @ - [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 (|>> (&.member? setL) not))) - #let [(^open "&/") &.Equivalence]] - ($_ seq - (test "I can query the size of a set." - (and (n/= sizeL (&.size setL)) - (n/= sizeR (&.size setR)))) - - (test "Converting sets to/from lists can't change their values." - (|> setL - &.to-list (&.from-list number.Hash) - (&/= setL))) - - (test "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)))) - - (test "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)))) - - (test "Union with the empty set leaves a set unchanged." - (&/= setL - (&.union (&.new number.Hash) - setL))) - - (test "Intersection with the empty set results in the empty set." - (let [empty-set (&.new number.Hash)] - (&/= empty-set - (&.intersection empty-set setL)))) - - (test "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))))) - - (test "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/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index 27e9850e0..02a82bc63 100644 --- a/stdlib/test/test/lux/data/format/json.lux +++ b/stdlib/test/test/lux/data/format/json.lux @@ -14,7 +14,7 @@ [number] (format ["@" json]) (coll [row #+ row] - (dictionary ["d" unordered]) + ["d" dictionary] [list])) [macro #+ with-gensyms] (macro [code] diff --git a/stdlib/test/test/lux/data/format/xml.lux b/stdlib/test/test/lux/data/format/xml.lux index acdd2aec8..dd82c2e14 100644 --- a/stdlib/test/test/lux/data/format/xml.lux +++ b/stdlib/test/test/lux/data/format/xml.lux @@ -10,7 +10,7 @@ ["E" error] [maybe] (format ["&" xml]) - (coll (dictionary ["dict" unordered]) + (coll ["dict" dictionary] [list "list/" Functor])) ["r" math/random "r/" Monad] test) diff --git a/stdlib/test/test/lux/lang/compiler/analysis/case.lux b/stdlib/test/test/lux/lang/compiler/analysis/case.lux index 21fa2b9f9..2088a775b 100644 --- a/stdlib/test/test/lux/lang/compiler/analysis/case.lux +++ b/stdlib/test/test/lux/lang/compiler/analysis/case.lux @@ -10,7 +10,7 @@ [text "T/" Equivalence] text/format (coll [list "list/" Monad] - (set ["set" unordered]))) + [set])) ["r" math/random "r/" Monad] [macro #+ Monad] (macro [code]) diff --git a/stdlib/test/test/lux/lang/compiler/analysis/procedure/host.jvm.lux b/stdlib/test/test/lux/lang/compiler/analysis/procedure/host.jvm.lux index af4741918..7aa527c93 100644 --- a/stdlib/test/test/lux/lang/compiler/analysis/procedure/host.jvm.lux +++ b/stdlib/test/test/lux/lang/compiler/analysis/procedure/host.jvm.lux @@ -11,7 +11,7 @@ text/format (coll [array] [list "list/" Fold] - (dictionary ["dict" unordered]))) + ["dict" dictionary])) ["r" math/random "r/" Monad] [macro #+ Monad] (macro [code]) diff --git a/stdlib/test/test/lux/lang/compiler/analysis/structure.lux b/stdlib/test/test/lux/lang/compiler/analysis/structure.lux index d9d029d31..0fc97dfbe 100644 --- a/stdlib/test/test/lux/lang/compiler/analysis/structure.lux +++ b/stdlib/test/test/lux/lang/compiler/analysis/structure.lux @@ -10,7 +10,7 @@ [text] text/format (coll [list "list/" Functor] - (set ["set" unordered]))) + [set])) ["r" math/random "r/" Monad] [macro] (macro [code]) diff --git a/stdlib/test/test/lux/lang/compiler/synthesis/function.lux b/stdlib/test/test/lux/lang/compiler/synthesis/function.lux index ba7d015e2..44df282b9 100644 --- a/stdlib/test/test/lux/lang/compiler/synthesis/function.lux +++ b/stdlib/test/test/lux/lang/compiler/synthesis/function.lux @@ -9,8 +9,8 @@ [number] text/format (coll [list "list/" Functor Fold] - (dictionary ["dict" unordered #+ Dictionary]) - (set ["set" unordered]))) + ["dict" dictionary #+ Dictionary] + [set])) (lang ["///." reference #+ Variable "variable/" Equivalence] ["///." compiler] [".L" analysis #+ Arity Analysis] diff --git a/stdlib/test/test/lux/lang/syntax.lux b/stdlib/test/test/lux/lang/syntax.lux index d9a16b2c3..f3066368e 100644 --- a/stdlib/test/test/lux/lang/syntax.lux +++ b/stdlib/test/test/lux/lang/syntax.lux @@ -8,7 +8,7 @@ (text format ["l" lexer]) (coll [list] - (dictionary ["dict" unordered #+ Dictionary]))) + ["dict" dictionary #+ Dictionary])) ["r" math/random "r/" Monad] (macro [code]) (lang ["&" syntax]) diff --git a/stdlib/test/test/lux/lang/type/check.lux b/stdlib/test/test/lux/lang/type/check.lux index 2ffee1318..7a65782de 100644 --- a/stdlib/test/test/lux/lang/type/check.lux +++ b/stdlib/test/test/lux/lang/type/check.lux @@ -9,7 +9,7 @@ [text "text/" Monoid Equivalence] text/format (coll [list "list/" Functor] - (set ["set" unordered]))) + [set])) ["r" math/random] (lang [type "type/" Equivalence] ["@" type/check])) diff --git a/stdlib/test/test/lux/math/logic/fuzzy.lux b/stdlib/test/test/lux/math/logic/fuzzy.lux index 3fa7c66cc..d4a8ced61 100644 --- a/stdlib/test/test/lux/math/logic/fuzzy.lux +++ b/stdlib/test/test/lux/math/logic/fuzzy.lux @@ -3,7 +3,7 @@ (lux [io] (control [monad #+ do Monad]) (data (coll [list] - (set ["set" unordered])) + [set]) [bool "B/" Equivalence] [number] text/format) diff --git a/stdlib/test/test/lux/math/random.lux b/stdlib/test/test/lux/math/random.lux index 46d9edef4..1e85636d5 100644 --- a/stdlib/test/test/lux/math/random.lux +++ b/stdlib/test/test/lux/math/random.lux @@ -9,8 +9,8 @@ [array] [queue] [stack] - (set ["set" unordered]) - (dictionary ["dict" unordered]))) + [set] + ["dict" dictionary])) (math ["r" random])) lux/test) -- cgit v1.2.3