Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DeterministicPriorityCollection<T, Priority>(private val comparator: Compa
assert({ !priorityQueue.any { (e, _) -> e == element } }) { "Element already exists in priority queue" }

val currentTop = topElement
if (currentTop != null && comparator.compare(currentTop.second, priority) < 0) {
if (currentTop != null && comparator.compare(currentTop.second, priority) > 0) {
topElement = element to priority
priorityQueue.add(currentTop)
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.usvm.algorithms

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

internal class DeterministicPriorityCollectionTest {
@Test
fun `adding a worse priority keeps the cached minimum`() {
val collection = DeterministicPriorityCollection<String, Int>(naturalOrder())
collection.add(element = "first", priority = 1)
assertEquals("first", collection.peek())

collection.add(element = "second", priority = 2)

assertEquals("first", collection.peek())
collection.remove("first")
assertEquals("second", collection.peek())
assertEquals(1, collection.count)
}

@Test
fun `adding a better priority replaces the cached minimum`() {
val collection = DeterministicPriorityCollection<String, Int>(naturalOrder())
collection.add(element = "second", priority = 2)
assertEquals("second", collection.peek())

collection.add(element = "first", priority = 1)

assertEquals("first", collection.peek())
collection.remove("first")
assertEquals("second", collection.peek())
assertEquals(1, collection.count)
}

@Test
fun `adding a worse priority keeps the cached maximum with reverse order`() {
val collection = DeterministicPriorityCollection<String, Int>(reverseOrder())
collection.add(element = "first", priority = 2)
assertEquals("first", collection.peek())

collection.add(element = "second", priority = 1)

assertEquals("first", collection.peek())
collection.remove("first")
assertEquals("second", collection.peek())
assertEquals(1, collection.count)
}

@Test
fun `adding a better priority replaces the cached maximum with reverse order`() {
val collection = DeterministicPriorityCollection<String, Int>(reverseOrder())
collection.add(element = "second", priority = 1)
assertEquals("second", collection.peek())

collection.add(element = "first", priority = 2)

assertEquals("first", collection.peek())
collection.remove("first")
assertEquals("second", collection.peek())
assertEquals(1, collection.count)
}
}
Loading