@@ -17,19 +17,23 @@ describe('HashTable', () => {
17
17
expect ( hashTable . hash ( 'abc' ) ) . toBe ( 6 ) ;
18
18
} ) ;
19
19
20
- it ( 'should insert , read and delete data with collisions' , ( ) => {
20
+ it ( 'should set , read and delete data with collisions' , ( ) => {
21
21
const hashTable = new HashTable ( 3 ) ;
22
22
23
23
expect ( hashTable . hash ( 'a' ) ) . toBe ( 1 ) ;
24
24
expect ( hashTable . hash ( 'b' ) ) . toBe ( 2 ) ;
25
25
expect ( hashTable . hash ( 'c' ) ) . toBe ( 0 ) ;
26
26
expect ( hashTable . hash ( 'd' ) ) . toBe ( 1 ) ;
27
27
28
- hashTable . insert ( 'a' , 'sky-old' ) ;
29
- hashTable . insert ( 'a' , 'sky' ) ;
30
- hashTable . insert ( 'b' , 'sea' ) ;
31
- hashTable . insert ( 'c' , 'earth' ) ;
32
- hashTable . insert ( 'd' , 'ocean' ) ;
28
+ hashTable . set ( 'a' , 'sky-old' ) ;
29
+ hashTable . set ( 'a' , 'sky' ) ;
30
+ hashTable . set ( 'b' , 'sea' ) ;
31
+ hashTable . set ( 'c' , 'earth' ) ;
32
+ hashTable . set ( 'd' , 'ocean' ) ;
33
+
34
+ expect ( hashTable . has ( 'x' ) ) . toBeFalsy ( ) ;
35
+ expect ( hashTable . has ( 'b' ) ) . toBeTruthy ( ) ;
36
+ expect ( hashTable . has ( 'c' ) ) . toBeTruthy ( ) ;
33
37
34
38
const stringifier = value => `${ value . key } :${ value . value } ` ;
35
39
@@ -47,18 +51,38 @@ describe('HashTable', () => {
47
51
expect ( hashTable . get ( 'a' ) ) . toBeNull ( ) ;
48
52
expect ( hashTable . get ( 'd' ) ) . toBe ( 'ocean' ) ;
49
53
50
- hashTable . insert ( 'd' , 'ocean-new' ) ;
54
+ hashTable . set ( 'd' , 'ocean-new' ) ;
51
55
expect ( hashTable . get ( 'd' ) ) . toBe ( 'ocean-new' ) ;
52
56
} ) ;
53
57
54
58
it ( 'should be possible to add objects to hash table' , ( ) => {
55
59
const hashTable = new HashTable ( ) ;
56
60
57
- hashTable . insert ( 'objectKey' , { prop1 : 'a' , prop2 : 'b' } ) ;
61
+ hashTable . set ( 'objectKey' , { prop1 : 'a' , prop2 : 'b' } ) ;
58
62
59
63
const object = hashTable . get ( 'objectKey' ) ;
60
64
expect ( object ) . toBeDefined ( ) ;
61
65
expect ( object . prop1 ) . toBe ( 'a' ) ;
62
66
expect ( object . prop2 ) . toBe ( 'b' ) ;
63
67
} ) ;
68
+
69
+ it ( 'should track actual keys' , ( ) => {
70
+ const hashTable = new HashTable ( 3 ) ;
71
+
72
+ hashTable . set ( 'a' , 'sky-old' ) ;
73
+ hashTable . set ( 'a' , 'sky' ) ;
74
+ hashTable . set ( 'b' , 'sea' ) ;
75
+ hashTable . set ( 'c' , 'earth' ) ;
76
+ hashTable . set ( 'd' , 'ocean' ) ;
77
+
78
+ expect ( hashTable . getKeys ( ) ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' ] ) ;
79
+ expect ( hashTable . has ( 'a' ) ) . toBeTruthy ( ) ;
80
+ expect ( hashTable . has ( 'x' ) ) . toBeFalsy ( ) ;
81
+
82
+ hashTable . delete ( 'a' ) ;
83
+
84
+ expect ( hashTable . has ( 'a' ) ) . toBeFalsy ( ) ;
85
+ expect ( hashTable . has ( 'b' ) ) . toBeTruthy ( ) ;
86
+ expect ( hashTable . has ( 'x' ) ) . toBeFalsy ( ) ;
87
+ } ) ;
64
88
} ) ;
0 commit comments