Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2019-01-06 11:14:16 +0300
committerThijs Schreijer <thijs@thijsschreijer.nl>2019-01-06 11:27:26 +0300
commit0990ef8ad134869529b9eb2f51a73a7f70a95ccd (patch)
tree94c0fb0ca45db8e495156fb6f58f084f17177c69
parentf7982974cd47b667436cf168ec28444dbbe7e82e (diff)
split test-set.lua in separate files to match modules
-rw-r--r--tests/test-map.lua6
-rw-r--r--tests/test-multimap.lua11
-rw-r--r--tests/test-orderedmap.lua98
-rw-r--r--tests/test-set.lua152
4 files changed, 132 insertions, 135 deletions
diff --git a/tests/test-map.lua b/tests/test-map.lua
index 793c9c1..906b2e6 100644
--- a/tests/test-map.lua
+++ b/tests/test-map.lua
@@ -17,3 +17,9 @@ assert (cmp(m:keys(),{'alpha','beta','gamma'}))
asserteq (m:items(),{{'alpha',1},{'beta',2},{'gamma',3}})
asserteq (m:getvalues {'alpha','gamma'}, {1,3})
+
+
+m = Map{one=1,two=2}
+asserteq(m,Map{one=1,two=2})
+m:update {three=3,four=4}
+asserteq(m,Map{one=1,two=2,three=3,four=4})
diff --git a/tests/test-multimap.lua b/tests/test-multimap.lua
new file mode 100644
index 0000000..1cb2925
--- /dev/null
+++ b/tests/test-multimap.lua
@@ -0,0 +1,11 @@
+local asserteq = require 'pl.test' . asserteq
+local MultiMap = require 'pl.MultiMap'
+
+m = MultiMap()
+m:set('john',1)
+m:set('jane',3)
+m:set('john',2)
+
+local ms = MultiMap{john={1,2},jane={3}}
+
+asserteq(m,ms)
diff --git a/tests/test-orderedmap.lua b/tests/test-orderedmap.lua
new file mode 100644
index 0000000..1d0fc67
--- /dev/null
+++ b/tests/test-orderedmap.lua
@@ -0,0 +1,98 @@
+local List = require 'pl.List'
+
+local asserteq = require 'pl.test' . asserteq
+local asserteq2 = require 'pl.test' . asserteq2
+local OrderedMap = require 'pl.OrderedMap'
+
+
+m = OrderedMap()
+m:set('one',1)
+m:set('two',2)
+m:set('three',3)
+
+asserteq(m:values(),List{1,2,3})
+
+-- usually exercized like this:
+--for k,v in m:iter() do print(k,v) end
+
+local fn = m:iter()
+asserteq2 ('one',1,fn())
+asserteq2 ('two',2,fn())
+asserteq2 ('three',3,fn())
+
+-- Keys overriding methods can be used.
+m:set('set', 4)
+asserteq(m:values(),List{1,2,3,4})
+
+local o1 = OrderedMap {{z=2},{beta=1},{name='fred'}}
+asserteq(tostring(o1),'{z=2,beta=1,name="fred"}')
+
+-- order of keys is not preserved here!
+local o2 = OrderedMap {z=4,beta=1.1,name='alice',extra='dolly'}
+
+o1:update(o2)
+asserteq(tostring(o1),'{z=4,beta=1.1,name="alice",extra="dolly"}')
+
+o1:set('beta',nil)
+asserteq(o1,OrderedMap{{z=4},{name='alice'},{extra='dolly'}})
+
+local o3 = OrderedMap()
+o3:set('dog',10)
+o3:set('cat',20)
+o3:set('mouse',30)
+
+asserteq(o3:keys(),{'dog','cat','mouse'})
+
+o3:set('dog',nil)
+
+asserteq(o3:keys(),{'cat','mouse'})
+
+-- Vadim found a problem when clearing a key which did not exist already.
+-- The keys list would then contain the key, although the map would not
+o3:set('lizard',nil)
+
+asserteq(o3:keys(),{'cat','mouse'})
+asserteq(o3:values(), {20,30})
+asserteq(tostring(o3),'{cat=20,mouse=30}')
+
+-- copy constructor
+local o4 = OrderedMap(o3)
+
+asserteq(o4,o3)
+
+-- constructor throws an error if the argument is bad
+-- (errors same as OrderedMap:update)
+asserteq(false,pcall(function()
+ m = OrderedMap('string')
+end))
+
+---- changing order of key/value pairs ----
+
+o3 = OrderedMap{{cat=20},{mouse=30}}
+
+o3:insert(1,'bird',5) -- adds key/value before specified position
+o3:insert(1,'mouse') -- moves key keeping old value
+asserteq(o3:keys(),{'mouse','bird','cat'})
+asserteq(tostring(o3),'{mouse=30,bird=5,cat=20}')
+o3:insert(2,'cat',21) -- moves key and sets new value
+asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
+-- if you don't specify a value for an unknown key, nothing happens to the map
+o3:insert(3,'alligator')
+asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
+
+---- short-cut notation
+
+local o5 = OrderedMap()
+o5.alpha = 1
+o5.beta = 2
+o5.gamma = 3
+
+asserteq(o5,OrderedMap{{alpha=1},{beta=2},{gamma=3}})
+
+o5.alpha = 10
+o5.beta = 20
+o5.gamma = 30
+o5.delta = 40
+o5.checked = false
+
+asserteq(o5,OrderedMap{{alpha=10},{beta=20},{gamma=30},{delta=40},{checked=false}})
diff --git a/tests/test-set.lua b/tests/test-set.lua
index b049e72..6c12321 100644
--- a/tests/test-set.lua
+++ b/tests/test-set.lua
@@ -1,155 +1,37 @@
-class = require 'pl.class'
-M = require 'pl.Map'
-S = require 'pl.Set'
-List = require 'pl.List'
+local Set = require 'pl.Set'
+local asserteq = require 'pl.test' . asserteq
-asserteq = require 'pl.test' . asserteq
-asserteq2 = require 'pl.test' . asserteq2
-MultiMap = require 'pl.MultiMap'
-OrderedMap = require 'pl.OrderedMap'
-
-s1 = S{1,2}
-s2 = S{1,2}
+local s1 = Set{1,2}
+local s2 = Set{1,2}
-- equality
asserteq(s1,s2)
-- union
-asserteq(S{1,2} + S{2,3}, S{1,2,3})
-asserteq(S{1,2} + 3, S{1,2,3})
+asserteq(Set{1,2} + Set{2,3}, Set{1,2,3})
+asserteq(Set{1,2} + 3, Set{1,2,3})
-- intersection
-asserteq(S{1,2} * S{2,3}, S{2})
+asserteq(Set{1,2} * Set{2,3}, Set{2})
-- difference
-fruit = S{'apple','banana','orange','apricots'}
-tropical = S{'banana','orange'}
+local fruit = Set{'apple','banana','orange','apricots'}
+local tropical = Set{'banana','orange'}
-asserteq(fruit - tropical, S{'apple','apricots'})
-asserteq(tropical - 'orange', S{'banana'})
+asserteq(fruit - tropical, Set{'apple','apricots'})
+asserteq(tropical - 'orange', Set{'banana'})
-- symmetric_difference
-asserteq(S{1,2} ^ S{2,3}, S{1,3})
+asserteq(Set{1,2} ^ Set{2,3}, Set{1,3})
-- tostring - illustrative, because these assertions may or may not work,
-- due to no ordering in set elements
--asserteq(tostring(S{1,2}),'[1,2]')
--asserteq(tostring(S{1,S{2,3}}),'[1,[2,3]]')
-s3 = S()
-asserteq(S.isempty(s3),true)
+local s3 = Set()
+asserteq(Set.isempty(s3),true)
-s4 = S{1,2,3}
+local s4 = Set{1,2,3}
-- subsets/supersets
asserteq(s4 > s1,true)
-S.set(s3,'one',true)
+Set.set(s3,'one',true)
s3.two = true
-asserteq(s3,S{'one','two'})
-
-m = M{one=1,two=2}
-asserteq(m,M{one=1,two=2})
-m:update {three=3,four=4}
-asserteq(m,M{one=1,two=2,three=3,four=4})
-
-m = MultiMap()
-m:set('john',1)
-m:set('jane',3)
-m:set('john',2)
-
-ms = MultiMap{john={1,2},jane={3}}
-
-asserteq(m,ms)
-
-m = OrderedMap()
-m:set('one',1)
-m:set('two',2)
-m:set('three',3)
-
-asserteq(m:values(),List{1,2,3})
-
--- usually exercized like this:
---for k,v in m:iter() do print(k,v) end
-
-fn = m:iter()
-asserteq2 ('one',1,fn())
-asserteq2 ('two',2,fn())
-asserteq2 ('three',3,fn())
-
--- Keys overriding methods can be used.
-m:set('set', 4)
-asserteq(m:values(),List{1,2,3,4})
-
-o1 = OrderedMap {{z=2},{beta=1},{name='fred'}}
-asserteq(tostring(o1),'{z=2,beta=1,name="fred"}')
-
--- order of keys is not preserved here!
-o2 = OrderedMap {z=4,beta=1.1,name='alice',extra='dolly'}
-
-o1:update(o2)
-asserteq(tostring(o1),'{z=4,beta=1.1,name="alice",extra="dolly"}')
-
-o1:set('beta',nil)
-asserteq(o1,OrderedMap{{z=4},{name='alice'},{extra='dolly'}})
-
-o3 = OrderedMap()
-o3:set('dog',10)
-o3:set('cat',20)
-o3:set('mouse',30)
-
-asserteq(o3:keys(),{'dog','cat','mouse'})
-
-o3:set('dog',nil)
-
-asserteq(o3:keys(),{'cat','mouse'})
-
--- Vadim found a problem when clearing a key which did not exist already.
--- The keys list would then contain the key, although the map would not
-o3:set('lizard',nil)
-
-asserteq(o3:keys(),{'cat','mouse'})
-asserteq(o3:values(), {20,30})
-asserteq(tostring(o3),'{cat=20,mouse=30}')
-
--- copy constructor
-o4 = OrderedMap(o3)
-
-asserteq(o4,o3)
-
--- constructor throws an error if the argument is bad
--- (errors same as OrderedMap:update)
-asserteq(false,pcall(function()
- m = OrderedMap('string')
-end))
-
----- changing order of key/value pairs ----
-
-o3 = OrderedMap{{cat=20},{mouse=30}}
-
-o3:insert(1,'bird',5) -- adds key/value before specified position
-o3:insert(1,'mouse') -- moves key keeping old value
-asserteq(o3:keys(),{'mouse','bird','cat'})
-asserteq(tostring(o3),'{mouse=30,bird=5,cat=20}')
-o3:insert(2,'cat',21) -- moves key and sets new value
-asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
--- if you don't specify a value for an unknown key, nothing happens to the map
-o3:insert(3,'alligator')
-asserteq(tostring(o3),'{mouse=30,cat=21,bird=5}')
-
----- short-cut notation
-
-o5 = OrderedMap()
-o5.alpha = 1
-o5.beta = 2
-o5.gamma = 3
-
-asserteq(o5,OrderedMap{{alpha=1},{beta=2},{gamma=3}})
-
-o5.alpha = 10
-o5.beta = 20
-o5.gamma = 30
-o5.delta = 40
-o5.checked = false
-
-asserteq(o5,OrderedMap{{alpha=10},{beta=20},{gamma=30},{delta=40},{checked=false}})
-
-
-
-
-
+asserteq(s3,Set{'one','two'})