Skip to content

Commit 37df827

Browse files
committed
unit test for hashmap
1 parent 611764a commit 37df827

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,28 @@ print(s:delMax()) -- return 10
117117
print(s:isEmpty()) -- return true
118118
```
119119

120+
### HashMap
121+
122+
```lua
123+
local hashmap = require('data.hashmap')
124+
local hash_func = function(x) return x % 1000 end
125+
local s = hashmap.create(hash_func)
126+
s:put(100, 2)
127+
s:put(200, 4)
128+
s:put(450, 2)
129+
print(s:get(100)) -- return 2
130+
print(s:get(200)) -- return 4
131+
print(s:get(450)) -- return 2
132+
print(s:get(99)) -- return nil
133+
print(s:containsKey(99)) -- return false
134+
print(s:containsKey(100)) -- return true
135+
print(s:size()) -- return 3
136+
print(s:isEmpty()) -- return false
137+
print(s:remove(100)) -- return 2
138+
print(s:containsKey(100)) -- return false
139+
print(s:size()) -- return 2
140+
s:remove(200)
141+
s:remove(450)
142+
print(s:isEmpty()) -- return true
143+
```
144+

spec/hashmap_spec.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 26/6/2017
5+
-- Time: 12:03 PM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
describe('hashmap', function()
10+
describe('hashmap()', function()
11+
it('should put and get and remove correctly', function()
12+
local hashmap = require('data.hashmap')
13+
local hash_func = function(x) return x % 1000 end
14+
local s = hashmap.create(hash_func)
15+
s:put(100, 2)
16+
s:put(200, 4)
17+
s:put(450, 2)
18+
assert.equal(s:get(100), 2)
19+
assert.equal(s:get(200), 4)
20+
assert.equal(s:get(450), 2)
21+
assert.equal(s:get(99), nil)
22+
assert.equal(s:containsKey(99), false)
23+
assert.equal(s:containsKey(100), true)
24+
assert.equal(s:size(), 3)
25+
assert.equal(s:isEmpty(), false)
26+
assert.equal(s:remove(100), 2)
27+
assert.equal(s:containsKey(100), false)
28+
assert.equal(s:size(), 2)
29+
s:remove(200)
30+
s:remove(450)
31+
assert.equal(s:isEmpty(), true)
32+
end)
33+
end)
34+
end)
35+

src/data/hashmap.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ function hashmap.create(hash)
3434

3535
s.s = {}
3636
s.N = 0
37+
s.hash = hash
3738

3839
return s
3940
end
4041

4142
function hashmap:put(key, value)
42-
local h = self:hash(key)
43+
local h = self.hash(key)
4344
local x = self.s[h]
4445
local found = false
4546
while x ~= nil do
@@ -61,7 +62,7 @@ function hashmap:put(key, value)
6162
end
6263

6364
function hashmap:get(key)
64-
local h = self:hash(key)
65+
local h = self.hash(key)
6566
local x = self.s[h]
6667
while x ~= nil do
6768
if x.key == key then
@@ -73,7 +74,7 @@ function hashmap:get(key)
7374
end
7475

7576
function hashmap:containsKey(key)
76-
local h = self:hash(key)
77+
local h = self.hash(key)
7778
local x = self.s[h]
7879
while x ~= nil do
7980
if x.key == key then
@@ -88,12 +89,12 @@ function hashmap:size()
8889
return self.N
8990
end
9091

91-
function hashmap:isEmpy()
92+
function hashmap:isEmpty()
9293
return self.N == 0
9394
end
9495

9596
function hashmap:remove(key)
96-
local h = self:hash(key)
97+
local h = self.hash(key)
9798
local x = self.s[h]
9899
local prev_x = nil
99100
while x ~= nil do
@@ -104,6 +105,7 @@ function hashmap:remove(key)
104105
else
105106
prev_x.next = x.next
106107
end
108+
self.N = self.N - 1
107109
return value
108110
end
109111
prev_x = x

0 commit comments

Comments
 (0)