|
1 | 1 | import { PriorityQueue } from '../src'; |
2 | 2 | import { IPriorityQueue } from '../typing/priority-queue'; |
3 | | -import { testCase1, testCase2 } from './fixtures/test-case'; |
| 3 | +import { testCase1, testCase2, testCase3 } from './fixtures/test-case'; |
4 | 4 |
|
5 | 5 | describe('testing priority queue', () => { |
6 | 6 | let p: IPriorityQueue<number>; |
@@ -80,6 +80,98 @@ describe('testing priority queue', () => { |
80 | 80 |
|
81 | 81 | }); |
82 | 82 |
|
| 83 | + describe('testing with array of object', () => { |
| 84 | + it('should return max heap of test case 3', () => { |
| 85 | + p = new PriorityQueue(function (a: any, b: any) { |
| 86 | + return a.value < b.value; |
| 87 | + }); |
| 88 | + |
| 89 | + for (let i = 0; i < testCase3.length; i++) { |
| 90 | + p.push(testCase3[i] as any); |
| 91 | + } |
| 92 | + const actual = p.toArray(); |
| 93 | + const expected = [ |
| 94 | + { text: 'e', value: 8 }, |
| 95 | + { text: 'b', value: 7 }, |
| 96 | + { text: 'c', value: 4 }, |
| 97 | + { text: 'd', value: 1 }, |
| 98 | + { text: 'a', value: 2 }, |
| 99 | + { text: 'f', value: 1 }, |
| 100 | + ]; |
| 101 | + expect(actual).toEqual(expected); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return min heap of test case 3', () => { |
| 105 | + p = new PriorityQueue(function (a: any, b: any) { |
| 106 | + return a.value > b.value; |
| 107 | + }); |
| 108 | + |
| 109 | + for (let i = 0; i < testCase3.length; i++) { |
| 110 | + p.push(testCase3[i] as any); |
| 111 | + } |
| 112 | + const actual = p.toArray(); |
| 113 | + const expected = [ |
| 114 | + { text: 'd', value: 1 }, |
| 115 | + { text: 'a', value: 2 }, |
| 116 | + { text: 'f', value: 1 }, |
| 117 | + { text: 'b', value: 7 }, |
| 118 | + { text: 'e', value: 8 }, |
| 119 | + { text: 'c', value: 4 } |
| 120 | + ]; |
| 121 | + expect(actual).toEqual(expected); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should extract the "max" value & rebuild "max" heap of test case 3', () => { |
| 125 | + p = new PriorityQueue(function (a: any, b: any) { |
| 126 | + return a.value < b.value; |
| 127 | + }); |
| 128 | + |
| 129 | + for (let i = 0; i < testCase3.length; i++) { |
| 130 | + p.push(testCase3[i] as any); |
| 131 | + } |
| 132 | + |
| 133 | + expect(p.size()).toEqual(testCase3.length); |
| 134 | + const max = p.pop(); |
| 135 | + expect(max).toEqual({ text: 'e', value: 8 }); |
| 136 | + expect(p.size()).toEqual(testCase3.length - 1); |
| 137 | + |
| 138 | + const actual = p.toArray(); |
| 139 | + const expected = [ |
| 140 | + { text: 'b', value: 7 }, |
| 141 | + { text: 'a', value: 2 }, |
| 142 | + { text: 'c', value: 4 }, |
| 143 | + { text: 'd', value: 1 }, |
| 144 | + { text: 'f', value: 1 } |
| 145 | + ]; |
| 146 | + expect(actual).toEqual(expected); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should extract the "min" value & rebuild "min" heap of test case 3', () => { |
| 150 | + p = new PriorityQueue(function (a: any, b: any) { |
| 151 | + return a.value > b.value; |
| 152 | + }); |
| 153 | + |
| 154 | + for (let i = 0; i < testCase3.length; i++) { |
| 155 | + p.push(testCase3[i] as any); |
| 156 | + } |
| 157 | + |
| 158 | + expect(p.size()).toEqual(testCase3.length); |
| 159 | + const min = p.pop(); |
| 160 | + expect(min).toEqual({ text: 'd', value: 1 }); |
| 161 | + expect(p.size()).toEqual(testCase3.length - 1); |
| 162 | + |
| 163 | + const actual = p.toArray(); |
| 164 | + const expected = [ |
| 165 | + { text: 'f', value: 1 }, |
| 166 | + { text: 'a', value: 2 }, |
| 167 | + { text: 'c', value: 4 }, |
| 168 | + { text: 'b', value: 7 }, |
| 169 | + { text: 'e', value: 8 } |
| 170 | + ]; |
| 171 | + expect(actual).toEqual(expected); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
83 | 175 | describe('min priority queue', () => { |
84 | 176 | beforeEach(() => { |
85 | 177 | p = new PriorityQueue(function (a, b) { |
|
0 commit comments