|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var mod = require('../../src/data-structures/hash-table.js'); |
| 4 | +var Node = mod.Node; |
| 5 | +var Hashtable = mod.Hashtable; |
| 6 | + |
| 7 | +describe('Node', function () { |
| 8 | + it('should be a constructor function', function () { |
| 9 | + expect(typeof Node).toBe('function'); |
| 10 | + }); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Hash table', function () { |
| 14 | + it('should be a constructor function.', function () { |
| 15 | + expect(typeof Hashtable).toBe('function'); |
| 16 | + }); |
| 17 | + it('should start with empty table.', function () { |
| 18 | + expect(new Hashtable().buckets.length).toBe(0); |
| 19 | + }); |
| 20 | + it('should put() K(int):V in table properly.', function () { |
| 21 | + var hashTable = new Hashtable(); |
| 22 | + hashTable.put(10, 'value'); |
| 23 | + expect(hashTable.buckets[10].data).toBe('value'); |
| 24 | + }); |
| 25 | + it('should put() K(str):V in table properly.', function () { |
| 26 | + var hashTable = new Hashtable(); |
| 27 | + hashTable.put('key', 'value'); |
| 28 | + /* |
| 29 | + 'key' hashCode()'s to 106079. Then the hash is adjusted to fit |
| 30 | + the number of configurable buckets (array size). |
| 31 | + 106079 % 100 (100 is default maxBucketCount) |
| 32 | + result is 79. |
| 33 | + This is done to avoid using get() since it's untested at this point. |
| 34 | + */ |
| 35 | + expect(hashTable.buckets[79].data).toBe('value'); |
| 36 | + }); |
| 37 | + it('should put() multiple K(int):Vs with hash collisions in properly (1).', function () { |
| 38 | + var hashTable = new Hashtable(); |
| 39 | + // Same hash so going to same bucket, but different keys. Collision. |
| 40 | + hashTable.put(10, 'value', 'someHash'); |
| 41 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 42 | + /* |
| 43 | + 'someHash' hashCode()'s to 1504481314. Then the hash is adjusted to fit |
| 44 | + the number of configurable buckets (array size). |
| 45 | + 1504481314 % 100 (100 is default maxBucketCount) |
| 46 | + result is 14. |
| 47 | + This is done to avoid using get() since it's untested at this point. |
| 48 | + */ |
| 49 | + expect(hashTable.buckets[14].data).toBe('value'); |
| 50 | + expect(hashTable.buckets[14].next.data).toBe('anotherValue'); |
| 51 | + }); |
| 52 | + it('should put() multiple K:Vs with hash collisions in properly (2).', function () { |
| 53 | + var hashTable = new Hashtable(); |
| 54 | + hashTable.put(10, 'value', 'someHash'); |
| 55 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 56 | + hashTable.put(77, 'lastValue', 'someHash'); |
| 57 | + expect(hashTable.buckets[14].data).toBe('value'); |
| 58 | + expect(hashTable.buckets[14].next.data).toBe('anotherValue'); |
| 59 | + expect(hashTable.buckets[14].next.next.data).toBe('lastValue'); |
| 60 | + }); |
| 61 | + it('should get() a k:v from table properly.', function () { |
| 62 | + var hashTable = new Hashtable(); |
| 63 | + hashTable.put(10, 'value'); |
| 64 | + expect(hashTable.get(10)).toBe('value'); |
| 65 | + }); |
| 66 | + it('should get() a k:v with collisions from table properly (1).', function () { |
| 67 | + var hashTable = new Hashtable(); |
| 68 | + hashTable.put(10, 'value', 'someHash'); |
| 69 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 70 | + expect(hashTable.get(35, 'someHash')).toBe('anotherValue'); |
| 71 | + }); |
| 72 | + it('should get() a k:v with collisions from table properly (2).', function () { |
| 73 | + var hashTable = new Hashtable(); |
| 74 | + hashTable.put(10, 'value', 'someHash'); |
| 75 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 76 | + hashTable.put(77, 'lastValue', 'someHash'); |
| 77 | + expect(hashTable.get(77, 'someHash')).toBe('lastValue'); |
| 78 | + }); |
| 79 | + it('should get() a k:v with collisions from table properly (3).', function () { |
| 80 | + var hashTable = new Hashtable(); |
| 81 | + hashTable.put(10, 'value', 'someHash'); |
| 82 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 83 | + hashTable.put(77, 'lastValue', 'someHash'); |
| 84 | + expect(hashTable.get(35, 'someHash')).toBe('anotherValue'); |
| 85 | + }); |
| 86 | + it('should get() a k:v with collisions from table properly (4).', function () { |
| 87 | + var hashTable = new Hashtable(); |
| 88 | + hashTable.put(10, 'value', 'someHash'); |
| 89 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 90 | + hashTable.put(77, 'lastValue', 'someHash'); |
| 91 | + expect(hashTable.get(10, 'someHash')).toBe('value'); |
| 92 | + }); |
| 93 | + it('should remove() a k:v from table properly.', function () { |
| 94 | + // remove only node/link in bucket : (B) |
| 95 | + var hashTable = new Hashtable(); |
| 96 | + hashTable.put(10, 'value'); |
| 97 | + hashTable.remove(10); |
| 98 | + expect(hashTable.get(10)).toBe(undefined); |
| 99 | + }); |
| 100 | + it('should remove() a k:v with collisions from table properly (2).', function () { |
| 101 | + // remove start node/link in bucket : (B) - A |
| 102 | + var hashTable = new Hashtable(); |
| 103 | + hashTable.put(10, 'value', 'someHash'); |
| 104 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 105 | + expect(hashTable.remove(10, 'someHash')).toBe('value'); |
| 106 | + expect(hashTable.get(35, 'someHash')).toBe('anotherValue'); |
| 107 | + expect(hashTable.get(10, 'someHash')).toBe(undefined); |
| 108 | + }); |
| 109 | + it('should remove() a k:v with collisions from table properly (3).', function () { |
| 110 | + // remove start node/link in bucket : (B) - A - C |
| 111 | + var hashTable = new Hashtable(); |
| 112 | + hashTable.put(10, 'value', 'someHash'); |
| 113 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 114 | + hashTable.put(66, 'lastValue', 'someHash'); |
| 115 | + expect(hashTable.remove(10, 'someHash')).toBe('value'); |
| 116 | + expect(hashTable.get(35, 'someHash')).toBe('anotherValue'); |
| 117 | + expect(hashTable.get(66, 'someHash')).toBe('lastValue'); |
| 118 | + }); |
| 119 | + it('should remove() a k:v with collisions from table properly (4).', function () { |
| 120 | + // remove middle node/link in bucket : A - (B) - C |
| 121 | + var hashTable = new Hashtable(); |
| 122 | + hashTable.put(10, 'value', 'someHash'); |
| 123 | + hashTable.put(35, 'anotherValue', 'someHash'); |
| 124 | + hashTable.put(66, 'lastValue', 'someHash'); |
| 125 | + expect(hashTable.remove(35, 'someHash')).toBe('anotherValue'); |
| 126 | + expect(hashTable.get(10, 'someHash')).toBe('value'); |
| 127 | + expect(hashTable.get(66, 'someHash')).toBe('lastValue'); |
| 128 | + }); |
| 129 | +}); |
0 commit comments