Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 2605d30

Browse files
committed
test: add unit tests for FastFairPriorityQueue functionality
1 parent 3e4d718 commit 2605d30

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/util/queue/FastFairPriorityQueue.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ class FastFairPriorityQueue<T>(private val prioCmp: Comparator<in T>) : Abstract
167167
fun snapshot(): ObjectArrayList<T> {
168168
val size = heap.size()
169169
if (size == 0) return ObjectArrayList()
170-
val buf = heap.backing().copyOfRange(0, size + 1)
170+
171+
val raw = heap.backing()
172+
val buf: Array<Entry<T>?> = arrayOfNulls(size)
173+
System.arraycopy(raw, 0, buf, 0, size)
171174
ObjectArrays.stableSort(buf, entryCmp)
172175

173176
val result = ObjectArrayList<T>(size)
@@ -192,7 +195,7 @@ class FastFairPriorityQueue<T>(private val prioCmp: Comparator<in T>) : Abstract
192195
private class ExposedHeap<E>(
193196
cmp: Comparator<E>
194197
) : ObjectHeapPriorityQueue<E>(cmp) {
195-
fun backing(): Array<E?> = heap
198+
fun backing(): Array<Any?> = heap as Array<Any?>
196199

197200
companion object {
198201
@Serial
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dev.slne.surf.cloud.api.common.util.queue
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class FastFairPriorityQueueTest {
7+
8+
@Test
9+
fun `fifo order for equal priority`() {
10+
val queue = FastFairPriorityQueue<Int>(Comparator.naturalOrder())
11+
queue.offer(1)
12+
queue.offer(1)
13+
queue.offer(1)
14+
15+
val first = queue.poll()
16+
val second = queue.poll()
17+
val third = queue.poll()
18+
19+
assertEquals(listOf(1, 1, 1), listOf(first, second, third))
20+
}
21+
22+
@Test
23+
fun `addFirst inserts ahead of others`() {
24+
val queue = FastFairPriorityQueue<Int>(Comparator.naturalOrder())
25+
queue.offer(1)
26+
queue.addFirst(1)
27+
queue.offer(2)
28+
29+
assertEquals(1, queue.poll())
30+
assertEquals(1, queue.poll())
31+
assertEquals(2, queue.poll())
32+
}
33+
34+
@Test
35+
fun `snapshot returns ordered list`() {
36+
val queue = FastFairPriorityQueue<Int>(Comparator.naturalOrder())
37+
queue.offer(3)
38+
queue.offer(1)
39+
queue.offer(2)
40+
41+
assertEquals(listOf(1, 2, 3), queue.snapshot().toList())
42+
}
43+
44+
}

0 commit comments

Comments
 (0)