From 00c5bad77dda7e204642976e67da536f82f0cbcf Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 1 May 2018 21:51:02 -0400 Subject: - Re-organized dict modules. --- stdlib/test/test/lux/data/coll/dict.lux | 128 ---------------------- stdlib/test/test/lux/data/coll/dict/ordered.lux | 88 +++++++++++++++ stdlib/test/test/lux/data/coll/dict/unordered.lux | 128 ++++++++++++++++++++++ stdlib/test/test/lux/data/coll/ordered/dict.lux | 88 --------------- stdlib/test/test/lux/data/format/json.lux | 2 +- stdlib/test/test/lux/data/format/xml.lux | 6 +- stdlib/test/test/lux/lang/syntax.lux | 2 +- stdlib/test/test/lux/math/random.lux | 2 +- stdlib/test/tests.lux | 6 +- 9 files changed, 225 insertions(+), 225 deletions(-) delete mode 100644 stdlib/test/test/lux/data/coll/dict.lux create mode 100644 stdlib/test/test/lux/data/coll/dict/ordered.lux create mode 100644 stdlib/test/test/lux/data/coll/dict/unordered.lux delete mode 100644 stdlib/test/test/lux/data/coll/ordered/dict.lux (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/data/coll/dict.lux b/stdlib/test/test/lux/data/coll/dict.lux deleted file mode 100644 index 01074b6fc..000000000 --- a/stdlib/test/test/lux/data/coll/dict.lux +++ /dev/null @@ -1,128 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad] - [eq]) - (data [text] - text/format - [number] - [maybe] - (coll ["&" dict] - [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.dict 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.Eq (&.values dict) val)))))] - ($_ seq - (test "Size function should correctly represent Dict size." - (n/= size (&.size dict))) - - (test "Dicts 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.Eq (eq.product number.Eq number.Eq)) = - (&.entries dict) - (list.zip2 (&.keys dict) - (&.values dict)))) - - (test "Dict 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 n/inc base)] - (case [(&.get non-key base) (&.get non-key updt)] - [(#.Some x) (#.Some y)] - (n/= (n/inc x) y) - - _ - false))) - - (test "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))))) - - (test "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 number.Hash) (= dict))))) - - (test "Merging a Dict to itself changes nothing." - (let [(^open) (&.Eq number.Eq)] - (= 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 (n/inc v)])) - (&.from-list number.Hash)) - (^open) (&.Eq number.Eq)] - (= 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/dict/ordered.lux b/stdlib/test/test/lux/data/coll/dict/ordered.lux new file mode 100644 index 000000000..73a1ebcad --- /dev/null +++ b/stdlib/test/test/lux/data/coll/dict/ordered.lux @@ -0,0 +1,88 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad] + [eq #+ Eq]) + (data [product] + [number] + (coll (set ["s" unordered]) + (dict ["dict" unordered] + ["&" ordered]) + [list "L/" Functor])) + ["r" math/random]) + lux/test) + +(context: "Dict" + (<| (times +100) + (do @ + [size (|> r.nat (:: @ map (n/% +100))) + keys (r.set number.Hash size r.nat) + values (r.set number.Hash size r.nat) + extra-key (|> r.nat (r.filter (|>> (s.member? keys) not))) + extra-value r.nat + #let [pairs (list.zip2 (s.to-list keys) + (s.to-list values)) + sample (&.from-list number.Order pairs) + sorted-pairs (list.sort (function (_ [left _] [right _]) + (n/< left right)) + pairs) + sorted-values (L/map product.right sorted-pairs) + (^open "&/") (&.Eq number.Eq)]] + ($_ seq + (test "Can query the size of a dictionary." + (n/= size (&.size sample))) + + (test "Can query value for minimum key." + (case [(&.min sample) (list.head sorted-values)] + [#.None #.None] + true + + [(#.Some reference) (#.Some sample)] + (n/= reference sample) + + _ + false)) + + (test "Can query value for maximum key." + (case [(&.max sample) (list.last sorted-values)] + [#.None #.None] + true + + [(#.Some reference) (#.Some sample)] + (n/= reference sample) + + _ + false)) + + (test "Converting dictionaries to/from lists cannot change their values." + (|> sample + &.entries (&.from-list number.Order) + (&/= sample))) + + (test "Order is preserved." + (let [(^open "L/") (list.Eq (: (Eq [Nat Nat]) + (function (_ [kr vr] [ks vs]) + (and (n/= kr ks) + (n/= vr vs)))))] + (L/= (&.entries sample) + sorted-pairs))) + + (test "Every key in a dictionary must be identifiable." + (list.every? (function (_ key) (&.contains? key sample)) + (&.keys sample))) + + (test "Can add and remove elements in a dictionary." + (and (not (&.contains? extra-key sample)) + (let [sample' (&.put extra-key extra-value sample) + sample'' (&.remove extra-key sample')] + (and (&.contains? extra-key sample') + (not (&.contains? extra-key sample'')) + (case [(&.get extra-key sample') + (&.get extra-key sample'')] + [(#.Some found) #.None] + (n/= extra-value found) + + _ + false))) + )) + )))) diff --git a/stdlib/test/test/lux/data/coll/dict/unordered.lux b/stdlib/test/test/lux/data/coll/dict/unordered.lux new file mode 100644 index 000000000..73b0df822 --- /dev/null +++ b/stdlib/test/test/lux/data/coll/dict/unordered.lux @@ -0,0 +1,128 @@ +(.module: + lux + (lux [io] + (control [monad #+ do Monad] + [eq]) + (data [text] + text/format + [number] + [maybe] + (coll (dict ["&" 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.dict 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.Eq (&.values dict) val)))))] + ($_ seq + (test "Size function should correctly represent Dict size." + (n/= size (&.size dict))) + + (test "Dicts 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.Eq (eq.product number.Eq number.Eq)) = + (&.entries dict) + (list.zip2 (&.keys dict) + (&.values dict)))) + + (test "Dict 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 n/inc base)] + (case [(&.get non-key base) (&.get non-key updt)] + [(#.Some x) (#.Some y)] + (n/= (n/inc x) y) + + _ + false))) + + (test "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))))) + + (test "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 number.Hash) (= dict))))) + + (test "Merging a Dict to itself changes nothing." + (let [(^open) (&.Eq number.Eq)] + (= 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 (n/inc v)])) + (&.from-list number.Hash)) + (^open) (&.Eq number.Eq)] + (= 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/ordered/dict.lux b/stdlib/test/test/lux/data/coll/ordered/dict.lux deleted file mode 100644 index 301e64927..000000000 --- a/stdlib/test/test/lux/data/coll/ordered/dict.lux +++ /dev/null @@ -1,88 +0,0 @@ -(.module: - lux - (lux [io] - (control [monad #+ do Monad] - [eq #+ Eq]) - (data [product] - [number] - (coll (ordered ["&" dict]) - (set ["s" unordered]) - ["d" dict] - [list "L/" Functor])) - ["r" math/random]) - lux/test) - -(context: "Dict" - (<| (times +100) - (do @ - [size (|> r.nat (:: @ map (n/% +100))) - keys (r.set number.Hash size r.nat) - values (r.set number.Hash size r.nat) - extra-key (|> r.nat (r.filter (|>> (s.member? keys) not))) - extra-value r.nat - #let [pairs (list.zip2 (s.to-list keys) - (s.to-list values)) - sample (&.from-list number.Order pairs) - sorted-pairs (list.sort (function (_ [left _] [right _]) - (n/< left right)) - pairs) - sorted-values (L/map product.right sorted-pairs) - (^open "&/") (&.Eq number.Eq)]] - ($_ seq - (test "Can query the size of a dictionary." - (n/= size (&.size sample))) - - (test "Can query value for minimum key." - (case [(&.min sample) (list.head sorted-values)] - [#.None #.None] - true - - [(#.Some reference) (#.Some sample)] - (n/= reference sample) - - _ - false)) - - (test "Can query value for maximum key." - (case [(&.max sample) (list.last sorted-values)] - [#.None #.None] - true - - [(#.Some reference) (#.Some sample)] - (n/= reference sample) - - _ - false)) - - (test "Converting dictionaries to/from lists cannot change their values." - (|> sample - &.entries (&.from-list number.Order) - (&/= sample))) - - (test "Order is preserved." - (let [(^open "L/") (list.Eq (: (Eq [Nat Nat]) - (function (_ [kr vr] [ks vs]) - (and (n/= kr ks) - (n/= vr vs)))))] - (L/= (&.entries sample) - sorted-pairs))) - - (test "Every key in a dictionary must be identifiable." - (list.every? (function (_ key) (&.contains? key sample)) - (&.keys sample))) - - (test "Can add and remove elements in a dictionary." - (and (not (&.contains? extra-key sample)) - (let [sample' (&.put extra-key extra-value sample) - sample'' (&.remove extra-key sample')] - (and (&.contains? extra-key sample') - (not (&.contains? extra-key sample'')) - (case [(&.get extra-key sample') - (&.get extra-key sample'')] - [(#.Some found) #.None] - (n/= extra-value found) - - _ - false))) - )) - )))) diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index eec53b9cc..d302c63b0 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 [sequence #+ sequence] - ["d" dict] + (dict ["d" unordered]) [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 ed337a0b7..80ac5bcb1 100644 --- a/stdlib/test/test/lux/data/format/xml.lux +++ b/stdlib/test/test/lux/data/format/xml.lux @@ -10,8 +10,8 @@ ["E" error] [maybe] (format ["&" xml]) - (coll [dict] - [list "L/" Functor])) + (coll (dict ["dict" unordered]) + [list "list/" Functor])) ["r" math/random "r/" Monad] test) ) @@ -85,7 +85,7 @@ value (xml-text^ +1 +10) #let [node (#&.Node tag (dict.put attr value &.attrs) - (L/map (|>> #&.Text) children))]] + (list/map (|>> #&.Text) children))]] ($_ seq (test "Can parse text." (E.default false diff --git a/stdlib/test/test/lux/lang/syntax.lux b/stdlib/test/test/lux/lang/syntax.lux index 2b4d6789f..49967e071 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] - [dict #+ Dict])) + (dict ["dict" unordered #+ Dict]))) ["r" math/random "r/" Monad] (macro [code]) (lang ["&" syntax]) diff --git a/stdlib/test/test/lux/math/random.lux b/stdlib/test/test/lux/math/random.lux index eed882ea7..45c83016c 100644 --- a/stdlib/test/test/lux/math/random.lux +++ b/stdlib/test/test/lux/math/random.lux @@ -10,7 +10,7 @@ [queue] [stack] (set ["set" unordered]) - [dict])) + (dict ["dict" unordered]))) (math ["r" random])) lux/test) diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index 9739c63e5..2c462e248 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -43,7 +43,8 @@ ["_." xml]) (coll ["_." array] ["_." bits] - ["_." dict] + ["_." dict/unordered] + ["_." dict/ordered] ["_." list] ["_." queue] ["_." set/unordered] @@ -53,8 +54,7 @@ ["_." priority-queue] ["_." stream] (tree ["tree_." rose] - ["tree_." zipper]) - (ordered ["ordered_." dict])) + ["tree_." zipper])) (text ["_." format] ["_." lexer] ["_." regex])) -- cgit v1.2.3