From: Jérôme Benoit Date: Tue, 30 Apr 2024 08:35:33 +0000 (+0200) Subject: test: add priority queue dequeue() with defined k X-Git-Tag: v4.0.0~1^2~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a4092a1586bd17e72e76d8a596427e0de6466077;p=poolifier.git test: add priority queue dequeue() with defined k Signed-off-by: Jérôme Benoit --- diff --git a/tests/priority-queue.test.mjs b/tests/priority-queue.test.mjs index 5014b3a4..58931859 100644 --- a/tests/priority-queue.test.mjs +++ b/tests/priority-queue.test.mjs @@ -117,7 +117,7 @@ describe('Priority queue test suite', () => { { data: 3, priority: 0 }, { data: 1, priority: 1 } ]) - rtSize = priorityQueue.enqueue(2, -2) + rtSize = priorityQueue.enqueue(3, -2) expect(priorityQueue.size).toBe(6) expect(priorityQueue.maxSize).toBe(6) expect(rtSize).toBe(priorityQueue.size) @@ -126,12 +126,12 @@ describe('Priority queue test suite', () => { { data: 2, priority: 0 }, { data: 3, priority: -1 }, { data: 3, priority: 0 }, - { data: 2, priority: -2 }, + { data: 3, priority: -2 }, { data: 1, priority: 1 } ]) }) - it('Verify dequeue() behavior', () => { + it('Verify default k dequeue() behavior', () => { const priorityQueue = new PriorityQueue() priorityQueue.enqueue(1) priorityQueue.enqueue(2, -1) @@ -156,6 +156,64 @@ describe('Priority queue test suite', () => { expect(priorityQueue.nodeArray).toStrictEqual([]) }) + it('Verify k=2 dequeue() behavior', () => { + const priorityQueue = new PriorityQueue(2) + priorityQueue.enqueue(1) + priorityQueue.enqueue(2) + priorityQueue.enqueue(3) + priorityQueue.enqueue(3, -1) + priorityQueue.enqueue(1, 1) + priorityQueue.enqueue(3, -2) + let rtItem = priorityQueue.dequeue(3) + expect(priorityQueue.size).toBe(5) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(3) + expect(priorityQueue.nodeArray).toStrictEqual([ + { data: 1, priority: 0 }, + { data: 2, priority: 0 }, + { data: 3, priority: -1 }, + { data: 3, priority: 0 }, + { data: 1, priority: 1 } + ]) + rtItem = priorityQueue.dequeue() + expect(priorityQueue.size).toBe(4) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(1) + expect(priorityQueue.nodeArray).toStrictEqual([ + { data: 2, priority: 0 }, + { data: 3, priority: -1 }, + { data: 3, priority: 0 }, + { data: 1, priority: 1 } + ]) + rtItem = priorityQueue.dequeue(2) + expect(priorityQueue.size).toBe(3) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(3) + expect(priorityQueue.nodeArray).toStrictEqual([ + { data: 2, priority: 0 }, + { data: 3, priority: -1 }, + { data: 1, priority: 1 } + ]) + rtItem = priorityQueue.dequeue(2) + expect(priorityQueue.size).toBe(2) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(1) + expect(priorityQueue.nodeArray).toStrictEqual([ + { data: 2, priority: 0 }, + { data: 3, priority: -1 } + ]) + rtItem = priorityQueue.dequeue(2) + expect(priorityQueue.size).toBe(1) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(2) + expect(priorityQueue.nodeArray).toStrictEqual([{ data: 3, priority: -1 }]) + rtItem = priorityQueue.dequeue() + expect(priorityQueue.size).toBe(0) + expect(priorityQueue.maxSize).toBe(6) + expect(rtItem).toBe(3) + expect(priorityQueue.nodeArray).toStrictEqual([]) + }) + it('Verify peekFirst() behavior', () => { const priorityQueue = new PriorityQueue() priorityQueue.enqueue(1)