|
| 1 | +import DisjointSet from '../DisjointSet'; |
| 2 | + |
| 3 | +describe('DisjointSet', () => { |
| 4 | + it('should throw error when trying to union and check not existing sets', () => { |
| 5 | + function mergeNotExistingSets() { |
| 6 | + const disjointSet = new DisjointSet(); |
| 7 | + |
| 8 | + disjointSet.union('A', 'B'); |
| 9 | + } |
| 10 | + |
| 11 | + function checkNotExistingSets() { |
| 12 | + const disjointSet = new DisjointSet(); |
| 13 | + |
| 14 | + disjointSet.inSameSet('A', 'B'); |
| 15 | + } |
| 16 | + |
| 17 | + expect(mergeNotExistingSets).toThrow(); |
| 18 | + expect(checkNotExistingSets).toThrow(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should do basic manipulations on disjoint set', () => { |
| 22 | + const disjointSet = new DisjointSet(); |
| 23 | + |
| 24 | + expect(disjointSet.find('A')).toBeNull(); |
| 25 | + expect(disjointSet.find('B')).toBeNull(); |
| 26 | + |
| 27 | + disjointSet.makeSet('A'); |
| 28 | + |
| 29 | + expect(disjointSet.find('A')).toBe('A'); |
| 30 | + expect(disjointSet.find('B')).toBeNull(); |
| 31 | + |
| 32 | + disjointSet.makeSet('B'); |
| 33 | + |
| 34 | + expect(disjointSet.find('A')).toBe('A'); |
| 35 | + expect(disjointSet.find('B')).toBe('B'); |
| 36 | + |
| 37 | + disjointSet.makeSet('C'); |
| 38 | + |
| 39 | + expect(disjointSet.inSameSet('A', 'B')).toBeFalsy(); |
| 40 | + |
| 41 | + disjointSet.union('A', 'B'); |
| 42 | + |
| 43 | + expect(disjointSet.find('A')).toBe('A'); |
| 44 | + expect(disjointSet.find('B')).toBe('A'); |
| 45 | + expect(disjointSet.inSameSet('A', 'B')).toBeTruthy(); |
| 46 | + expect(disjointSet.inSameSet('B', 'A')).toBeTruthy(); |
| 47 | + expect(disjointSet.inSameSet('A', 'C')).toBeFalsy(); |
| 48 | + |
| 49 | + disjointSet.union('A', 'A'); |
| 50 | + |
| 51 | + disjointSet.union('B', 'C'); |
| 52 | + |
| 53 | + expect(disjointSet.find('A')).toBe('A'); |
| 54 | + expect(disjointSet.find('B')).toBe('A'); |
| 55 | + expect(disjointSet.find('C')).toBe('A'); |
| 56 | + |
| 57 | + expect(disjointSet.inSameSet('A', 'B')).toBeTruthy(); |
| 58 | + expect(disjointSet.inSameSet('B', 'C')).toBeTruthy(); |
| 59 | + expect(disjointSet.inSameSet('A', 'C')).toBeTruthy(); |
| 60 | + |
| 61 | + disjointSet |
| 62 | + .makeSet('E') |
| 63 | + .makeSet('F') |
| 64 | + .makeSet('G') |
| 65 | + .makeSet('H') |
| 66 | + .makeSet('I'); |
| 67 | + |
| 68 | + disjointSet |
| 69 | + .union('E', 'F') |
| 70 | + .union('F', 'G') |
| 71 | + .union('G', 'H') |
| 72 | + .union('H', 'I'); |
| 73 | + |
| 74 | + expect(disjointSet.inSameSet('A', 'I')).toBeFalsy(); |
| 75 | + expect(disjointSet.inSameSet('E', 'I')).toBeTruthy(); |
| 76 | + |
| 77 | + disjointSet.union('I', 'C'); |
| 78 | + |
| 79 | + expect(disjointSet.find('I')).toBe('E'); |
| 80 | + expect(disjointSet.inSameSet('A', 'I')).toBeTruthy(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should union smaller set with bigger one making bigger one to be new root', () => { |
| 84 | + const disjointSet = new DisjointSet(); |
| 85 | + |
| 86 | + disjointSet |
| 87 | + .makeSet('A') |
| 88 | + .makeSet('B') |
| 89 | + .makeSet('C') |
| 90 | + .union('B', 'C') |
| 91 | + .union('A', 'C'); |
| 92 | + |
| 93 | + expect(disjointSet.find('A')).toBe('B'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should do basic manipulations on disjoint set with custom key extractor', () => { |
| 97 | + const keyExtractor = value => value.key; |
| 98 | + |
| 99 | + const disjointSet = new DisjointSet(keyExtractor); |
| 100 | + |
| 101 | + const itemA = { key: 'A', value: 1 }; |
| 102 | + const itemB = { key: 'B', value: 2 }; |
| 103 | + const itemC = { key: 'C', value: 3 }; |
| 104 | + |
| 105 | + expect(disjointSet.find(itemA)).toBeNull(); |
| 106 | + expect(disjointSet.find(itemB)).toBeNull(); |
| 107 | + |
| 108 | + disjointSet.makeSet(itemA); |
| 109 | + |
| 110 | + expect(disjointSet.find(itemA)).toBe('A'); |
| 111 | + expect(disjointSet.find(itemB)).toBeNull(); |
| 112 | + |
| 113 | + disjointSet.makeSet(itemB); |
| 114 | + |
| 115 | + expect(disjointSet.find(itemA)).toBe('A'); |
| 116 | + expect(disjointSet.find(itemB)).toBe('B'); |
| 117 | + |
| 118 | + disjointSet.makeSet(itemC); |
| 119 | + |
| 120 | + expect(disjointSet.inSameSet(itemA, itemB)).toBeFalsy(); |
| 121 | + |
| 122 | + disjointSet.union(itemA, itemB); |
| 123 | + |
| 124 | + expect(disjointSet.find(itemA)).toBe('A'); |
| 125 | + expect(disjointSet.find(itemB)).toBe('A'); |
| 126 | + expect(disjointSet.inSameSet(itemA, itemB)).toBeTruthy(); |
| 127 | + expect(disjointSet.inSameSet(itemB, itemA)).toBeTruthy(); |
| 128 | + expect(disjointSet.inSameSet(itemA, itemC)).toBeFalsy(); |
| 129 | + |
| 130 | + disjointSet.union(itemA, itemC); |
| 131 | + |
| 132 | + expect(disjointSet.find(itemA)).toBe('A'); |
| 133 | + expect(disjointSet.find(itemB)).toBe('A'); |
| 134 | + expect(disjointSet.find(itemC)).toBe('A'); |
| 135 | + |
| 136 | + expect(disjointSet.inSameSet(itemA, itemB)).toBeTruthy(); |
| 137 | + expect(disjointSet.inSameSet(itemB, itemC)).toBeTruthy(); |
| 138 | + expect(disjointSet.inSameSet(itemA, itemC)).toBeTruthy(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments