-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_hash.kl
78 lines (62 loc) · 3.38 KB
/
test_hash.kl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
PROGRAM test_hash
%NOLOCKGROUP
CONST
%INCLUDE includes/hash.c
TYPE
%INCLUDE includes/hash.t
VAR
smallTable : ARRAY[1] OF hashNode
twoTable : ARRAY[2] OF hashNode
%INCLUDE includes/kunit.h
%INCLUDE includes/hash.h
ROUTINE clearTables
VAR
i : INTEGER
BEGIN
smallTable[1].key = ''
smallTable[1].val = ''
FOR i=1 TO 2 DO
twoTable[i].key = ''
twoTable[i].val = ''
ENDFOR
END clearTables
-- private methods
ROUTINE djb2(str : STRING) : INTEGER FROM hash
ROUTINE jsStrHash(str : STRING) : INTEGER FROM hash
ROUTINE hPut(key : STRING; value : STRING; tbl : ARRAY OF hashNode) : BOOLEAN FROM hash
ROUTINE hGet(key : STRING; tbl : ARRAY OF hashNode) : STRING FROM hash
ROUTINE hDel(key : STRING; tbl : ARRAY OF hashNode) : BOOLEAN FROM hash
ROUTINE hModHash(key : STRING; len : INTEGER) : INTEGER FROM hash
ROUTINE hGetIndex(key : STRING; get : BOOLEAN; tbl : ARRAY OF hashNode) : INTEGER FROM hash
BEGIN
kunit_test('djb2("a")', kunit_eq_int(177670, djb2('a')))
kunit_test('djb2("asdf")', kunit_eq_int(2090088131, djb2('asdf')))
-- djb2 overflows INTEGER
--kunit_test('djb2("really long string")', kunit_eq_int((-3 022 999 531), djb2('really long string')))
kunit_test('jsStrHash("a")', kunit_eq_int(97, jsStrHash('a')))
kunit_test('jsStrHash("asdf")', kunit_eq_int(3003444, jsStrHash('asdf')))
kunit_test('jsStrHash("a really really really long string")', kunit_eq_int(1765648255, jsStrHash('a really really really long string')))
clearTables
kunit_test('hashGet with empty table returns empty string', kunit_eq_str('', hashGet('foo', 'test_hash', 'smallTable')))
kunit_test('hashDel with empty string returns false', kunit_assert(NOT(hashDel('', 'test_hash', 'smallTable'))))
kunit_test('hashPut with empty string returns false', kunit_assert(NOT(hashPut('', 'foo', 'test_hash', 'smallTable'))))
kunit_test('hashGet with empty string returns empty string', kunit_eq_str('', hashGet('', 'test_hash', 'smallTable')))
kunit_test('put returns true', kunit_assert(hashPut('foo', 'bar', 'test_hash', 'smallTable')))
kunit_test('hashPut puts a value', kunit_eq_str('bar', hashGet('foo', 'test_hash', 'smallTable')))
kunit_test('put works', kunit_assert(hashPut('foo', 'baz', 'test_hash', 'smallTable')))
kunit_test('hashPut overwrites', kunit_eq_str('baz', hashGet('foo', 'test_hash', 'smallTable')))
-- can't put this... table full
kunit_test('put returns false when full', NOT(kunit_assert(hashPut('bar', 'foo', 'test_hash', 'smallTable'))))
kunit_test('hashPut does not put if tbl full', kunit_eq_str('', hashGet('bar', 'test_hash', 'smallTable')))
kunit_test('hashDel returns true if it deletes key', kunit_assert(hashDel('foo', 'test_hash', 'smallTable')))
kunit_test('hashDel actually deletes', kunit_eq_str('', hashGet('foo', 'test_hash', 'smallTable')))
kunit_test('hashDel returns false if it cannot find the key', NOT(kunit_assert(hashDel('asdfasdf', 'test_hash', 'smallTable'))))
-- test collisions
kunit_test('mod hash of a is 2', kunit_eq_int(2, hModHash('a', 2)))
kunit_test('mod hash of c is 2', kunit_eq_int(2, hModHash('c', 2)))
kunit_test('index of a is 2', kunit_eq_int(2, hGetIndex('a', false, twoTable)))
kunit_test('index of c is 2 normally', kunit_eq_int(2, hGetIndex('c', false, twoTable)))
kunit_test('put a', kunit_assert(hashPut('a', 'foo', 'test_hash', 'twoTable')))
kunit_test('index of c is 1 if 2 is full', kunit_eq_int(1, hGetIndex('c', false, twoTable)))
kunit_done
END test_hash