Skip to content

Commit

Permalink
chore: implement a simple scheduler using list
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomoaccursi committed Nov 13, 2023
1 parent 38a0ad1 commit 3b64326
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/kotlin/control/ListScheduler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2023. Accursi Giacomo
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

package control

import entity.Event

/**
* Simple implementation of a scheduler using an ArrayList.
*/
class ListScheduler(
/**
* A list of all events.
*/
val events: ArrayList<Event> = ArrayList(),
) : Scheduler {
init {
sortListByTau()
}
override fun addEvent(event: Event) {
events.add(event)
this.sortListByTau()
}

override fun getNext(): Event? {
return events.firstOrNull()
}

override fun removeEvent(event: Event) {
events.remove(event)
}

override fun eventsUpdated() {
this.sortListByTau()
}

private fun sortListByTau() {
events.sortBy { it.tau.toDouble() }
}
}
56 changes: 56 additions & 0 deletions src/test/kotlin/control/TestListScheduler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023. Accursi Giacomo
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

package control

import entity.DoubleTime
import entity.EventImpl
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class TestListScheduler : StringSpec({
"test scheduler with no events" {
val emptyScheduler = ListScheduler()
emptyScheduler.getNext() shouldBe null
}

"test scheduler with one event" {
val event = EventImpl()
val scheduler = ListScheduler(ArrayList(listOf(event)))
scheduler.getNext() shouldBe event
}

"it must be possible to remove events from the scheduler" {
val event = EventImpl()
val emptyScheduler = ListScheduler(ArrayList(listOf(event)))
emptyScheduler.removeEvent(event)
emptyScheduler.getNext() shouldBe null
}

"scheduler with more than one event must return the event with smaller execution time" {
val currentTime = DoubleTime()
val event1 = EventImpl().also { it.initializationComplete(currentTime) }
val event2 = EventImpl().also { it.initializationComplete(currentTime) }
val scheduler = ListScheduler(ArrayList(listOf(event1, event2)))
val smaller = listOf(event1, event2).sortedBy { it.tau.toDouble() }.first()
scheduler.getNext() shouldBe smaller
}

"scheduler must reorder the events execution after them update" {
val currentTime = DoubleTime()
val event1 = EventImpl().also { it.initializationComplete(currentTime) }
val event2 = EventImpl().also { it.initializationComplete(currentTime) }
val event3 = EventImpl().also { it.initializationComplete(currentTime) }
var inverseSortedEventList =
listOf(event1, event2, event3)
inverseSortedEventList = inverseSortedEventList.sortedByDescending { event -> event.tau.toDouble() }
val scheduler = ListScheduler(ArrayList(inverseSortedEventList))
scheduler.eventsUpdated()
scheduler.events shouldBe inverseSortedEventList.sortedBy { it.tau.toDouble() }
}
})

0 comments on commit 3b64326

Please sign in to comment.