@@ -2,131 +2,133 @@ const HashMap = require('./hash-map');
22// import HashMap from './hash-map';
33
44
5- describe ( 'without collisions' , ( ) => {
6- let hashMap ;
7-
8- beforeEach ( ( ) => {
9- hashMap = new HashMap ( ) ;
10- } ) ;
11-
12- it ( 'gets values' , ( ) => {
13- hashMap . set ( 'test' , 'one' ) ;
14- expect ( hashMap . get ( 'test' ) ) . toBe ( 'one' ) ;
15- } ) ;
16-
17- it ( 'should increase load factor and size' , ( ) => {
18- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 0 ) ;
19- expect ( hashMap . size ) . toBe ( 0 ) ;
20- hashMap . set ( 'test' , 'one' ) ;
21- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 1 / 16 ) ;
22- expect ( hashMap . size ) . toBe ( 1 ) ;
23- } ) ;
24-
25- it ( 'should overwrite values and keep same size' , ( ) => {
26- hashMap . set ( 'test' , 'uno' ) ;
27- expect ( hashMap . get ( 'test' ) ) . toBe ( 'uno' ) ;
28- hashMap . set ( 'test' , 'dos' ) ;
29- expect ( hashMap . get ( 'test' ) ) . toBe ( 'dos' ) ;
30- expect ( hashMap . size ) . toBe ( 1 ) ;
31- } ) ;
32-
33- it ( 'should return with has' , ( ) => {
34- expect ( hashMap . has ( 'test' ) ) . toBe ( false ) ;
35- hashMap . set ( 'test' , 'uno' ) ;
36- expect ( hashMap . has ( 'test' ) ) . toBe ( true ) ;
37- } ) ;
38-
39- it ( 'should update keys on deletes' , ( ) => {
40- hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
41- hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
42- hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
43-
44- expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( true ) ;
45- expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( false ) ;
46- expect ( hashMap . get ( 'Bailando' ) ) . toBe ( undefined ) ;
47-
48- expect ( hashMap . keys ( ) ) . toEqual ( [ 'Despacito' , 'Dura' ] ) ;
49- } ) ;
50- } ) ;
51-
52- describe ( 'with many values (and collisions)' , ( ) => {
53- let hashMap ;
54-
55- beforeEach ( ( ) => {
56- hashMap = new HashMap ( 2 , 0 ) ;
57-
58- hashMap . set ( 'Pineapple' , 'Pen Pineapple Apple Pen' ) ;
59- hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
60- hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
61- hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
62- hashMap . set ( 'Lean On' , 'Major Lazer' ) ;
63- hashMap . set ( 'Hello' , 'Adele' ) ;
64- hashMap . set ( 'All About That Bass' , 'Meghan Trainor' ) ;
65- hashMap . set ( 'This Is What You Came For' , 'Calvin Harris ' ) ;
66- } ) ;
67-
68- it ( 'gets values' , ( ) => {
69- hashMap . set ( 'test' , 'one' ) ;
70- expect ( hashMap . get ( 'test' ) ) . toBe ( 'one' ) ;
71- expect ( hashMap . get ( 'Dura' ) ) . toBe ( 'Daddy Yankee' ) ;
72- expect ( hashMap . get ( 'Bailando' ) ) . toBe ( 'Enrique Iglesias' ) ;
73- } ) ;
74-
75- it ( 'should increase load factor and size' , ( ) => {
76- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 4 ) ;
77- expect ( hashMap . size ) . toBe ( 8 ) ;
78- hashMap . set ( 'test' , 'one' ) ;
79- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 9 / 2 ) ;
80- expect ( hashMap . size ) . toBe ( 9 ) ;
5+ describe ( 'HashMap Tests' , ( ) => {
6+ describe ( 'without collisions' , ( ) => {
7+ let hashMap ;
8+
9+ beforeEach ( ( ) => {
10+ hashMap = new HashMap ( ) ;
11+ } ) ;
12+
13+ it ( 'gets values' , ( ) => {
14+ hashMap . set ( 'test' , 'one' ) ;
15+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'one' ) ;
16+ } ) ;
17+
18+ it ( 'should increase load factor and size' , ( ) => {
19+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 0 ) ;
20+ expect ( hashMap . size ) . toBe ( 0 ) ;
21+ hashMap . set ( 'test' , 'one' ) ;
22+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 1 / 16 ) ;
23+ expect ( hashMap . size ) . toBe ( 1 ) ;
24+ } ) ;
25+
26+ it ( 'should overwrite values and keep same size' , ( ) => {
27+ hashMap . set ( 'test' , 'uno' ) ;
28+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'uno' ) ;
29+ hashMap . set ( 'test' , 'dos' ) ;
30+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'dos' ) ;
31+ expect ( hashMap . size ) . toBe ( 1 ) ;
32+ } ) ;
33+
34+ it ( 'should return with has' , ( ) => {
35+ expect ( hashMap . has ( 'test' ) ) . toBe ( false ) ;
36+ hashMap . set ( 'test' , 'uno' ) ;
37+ expect ( hashMap . has ( 'test' ) ) . toBe ( true ) ;
38+ } ) ;
39+
40+ it ( 'should update keys on deletes' , ( ) => {
41+ hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
42+ hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
43+ hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
44+
45+ expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( true ) ;
46+ expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( false ) ;
47+ expect ( hashMap . get ( 'Bailando' ) ) . toBe ( undefined ) ;
48+
49+ expect ( hashMap . keys ( ) ) . toEqual ( [ 'Despacito' , 'Dura' ] ) ;
50+ } ) ;
8151 } ) ;
8252
83- it ( 'should overwrite values and keep same size' , ( ) => {
84- hashMap . set ( 'test' , 'uno' ) ;
85- expect ( hashMap . get ( 'test' ) ) . toBe ( 'uno' ) ;
86- hashMap . set ( 'test' , 'dos' ) ;
87- expect ( hashMap . get ( 'test' ) ) . toBe ( 'dos' ) ;
88- expect ( hashMap . size ) . toBe ( 9 ) ;
53+ describe ( 'with many values (and collisions)' , ( ) => {
54+ let hashMap ;
55+
56+ beforeEach ( ( ) => {
57+ hashMap = new HashMap ( 2 , 0 ) ;
58+
59+ hashMap . set ( 'Pineapple' , 'Pen Pineapple Apple Pen' ) ;
60+ hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
61+ hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
62+ hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
63+ hashMap . set ( 'Lean On' , 'Major Lazer' ) ;
64+ hashMap . set ( 'Hello' , 'Adele' ) ;
65+ hashMap . set ( 'All About That Bass' , 'Meghan Trainor' ) ;
66+ hashMap . set ( 'This Is What You Came For' , 'Calvin Harris ' ) ;
67+ } ) ;
68+
69+ it ( 'gets values' , ( ) => {
70+ hashMap . set ( 'test' , 'one' ) ;
71+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'one' ) ;
72+ expect ( hashMap . get ( 'Dura' ) ) . toBe ( 'Daddy Yankee' ) ;
73+ expect ( hashMap . get ( 'Bailando' ) ) . toBe ( 'Enrique Iglesias' ) ;
74+ } ) ;
75+
76+ it ( 'should increase load factor and size' , ( ) => {
77+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 4 ) ;
78+ expect ( hashMap . size ) . toBe ( 8 ) ;
79+ hashMap . set ( 'test' , 'one' ) ;
80+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 9 / 2 ) ;
81+ expect ( hashMap . size ) . toBe ( 9 ) ;
82+ } ) ;
83+
84+ it ( 'should overwrite values and keep same size' , ( ) => {
85+ hashMap . set ( 'test' , 'uno' ) ;
86+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'uno' ) ;
87+ hashMap . set ( 'test' , 'dos' ) ;
88+ expect ( hashMap . get ( 'test' ) ) . toBe ( 'dos' ) ;
89+ expect ( hashMap . size ) . toBe ( 9 ) ;
90+ } ) ;
91+
92+ it ( 'should return with has' , ( ) => {
93+ expect ( hashMap . has ( 'test' ) ) . toBe ( false ) ;
94+ hashMap . set ( 'test' , 'uno' ) ;
95+ expect ( hashMap . has ( 'test' ) ) . toBe ( true ) ;
96+ } ) ;
8997 } ) ;
9098
91- it ( 'should return with has' , ( ) => {
92- expect ( hashMap . has ( 'test' ) ) . toBe ( false ) ;
93- hashMap . set ( 'test' , 'uno' ) ;
94- expect ( hashMap . has ( 'test' ) ) . toBe ( true ) ;
95- } ) ;
96- } ) ;
97-
98- describe ( '#rehash' , ( ) => {
99- let hashMap ;
100-
101- beforeEach ( ( ) => {
102- hashMap = new HashMap ( ) ;
103-
104- hashMap . set ( 'Pineapple' , 'Pen Pineapple Apple Pen' ) ;
105- hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
106- hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
107- hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
108- hashMap . set ( 'Lean On' , 'Major Lazer' ) ;
109- hashMap . set ( 'Hello' , 'Adele' ) ;
110- hashMap . set ( 'All About That Bass' , 'Meghan Trainor' ) ;
111- hashMap . set ( 'Wake Me Up' , 'Avicii' ) ;
112- hashMap . set ( 'Brother' , 'Avicii' ) ;
113- hashMap . set ( 'Faded' , 'Alan Walker' ) ;
114- hashMap . set ( 'The Spectre' , 'Alan Walker' ) ;
115- } ) ;
116-
117- it ( 'should rehash after 12 items by default' , ( ) => {
118- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 11 / 16 ) ;
119- expect ( hashMap . buckets . length ) . toBe ( 16 ) ;
120- hashMap . set ( 'Alone' , 'Alan Walker' ) ;
121- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 0.75 ) ;
122-
123- hashMap . set ( 'Levels' , 'Avicii' ) ;
124-
125- expect ( hashMap . getLoadFactor ( ) ) . toBe ( 13 / 32 ) ;
126- expect ( hashMap . buckets . length ) . toBe ( 32 ) ;
127-
128- expect ( hashMap . get ( 'Dura' ) ) . toBe ( 'Daddy Yankee' ) ;
129- expect ( hashMap . get ( 'Bailando' ) ) . toBe ( 'Enrique Iglesias' ) ;
130- expect ( hashMap . get ( 'Levels' ) ) . toBe ( 'Avicii' ) ;
99+ describe ( '#rehash' , ( ) => {
100+ let hashMap ;
101+
102+ beforeEach ( ( ) => {
103+ hashMap = new HashMap ( ) ;
104+
105+ hashMap . set ( 'Pineapple' , 'Pen Pineapple Apple Pen' ) ;
106+ hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
107+ hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
108+ hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
109+ hashMap . set ( 'Lean On' , 'Major Lazer' ) ;
110+ hashMap . set ( 'Hello' , 'Adele' ) ;
111+ hashMap . set ( 'All About That Bass' , 'Meghan Trainor' ) ;
112+ hashMap . set ( 'Wake Me Up' , 'Avicii' ) ;
113+ hashMap . set ( 'Brother' , 'Avicii' ) ;
114+ hashMap . set ( 'Faded' , 'Alan Walker' ) ;
115+ hashMap . set ( 'The Spectre' , 'Alan Walker' ) ;
116+ } ) ;
117+
118+ it ( 'should rehash after 12 items by default' , ( ) => {
119+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 11 / 16 ) ;
120+ expect ( hashMap . buckets . length ) . toBe ( 16 ) ;
121+ hashMap . set ( 'Alone' , 'Alan Walker' ) ;
122+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 0.75 ) ;
123+
124+ hashMap . set ( 'Levels' , 'Avicii' ) ;
125+
126+ expect ( hashMap . getLoadFactor ( ) ) . toBe ( 13 / 32 ) ;
127+ expect ( hashMap . buckets . length ) . toBe ( 32 ) ;
128+
129+ expect ( hashMap . get ( 'Dura' ) ) . toBe ( 'Daddy Yankee' ) ;
130+ expect ( hashMap . get ( 'Bailando' ) ) . toBe ( 'Enrique Iglesias' ) ;
131+ expect ( hashMap . get ( 'Levels' ) ) . toBe ( 'Avicii' ) ;
132+ } ) ;
131133 } ) ;
132134} ) ;
0 commit comments