From 3b6432633b78f0583851d6f382f842bced9bfea1 Mon Sep 17 00:00:00 2001 From: giacomoaccursi Date: Fri, 10 Nov 2023 18:43:14 +0100 Subject: [PATCH] chore: implement a simple scheduler using list --- src/main/kotlin/control/ListScheduler.kt | 45 ++++++++++++++++ src/test/kotlin/control/TestListScheduler.kt | 56 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/main/kotlin/control/ListScheduler.kt create mode 100644 src/test/kotlin/control/TestListScheduler.kt diff --git a/src/main/kotlin/control/ListScheduler.kt b/src/main/kotlin/control/ListScheduler.kt new file mode 100644 index 0000000..8eb6c59 --- /dev/null +++ b/src/main/kotlin/control/ListScheduler.kt @@ -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 = 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() } + } +} diff --git a/src/test/kotlin/control/TestListScheduler.kt b/src/test/kotlin/control/TestListScheduler.kt new file mode 100644 index 0000000..a81cc14 --- /dev/null +++ b/src/test/kotlin/control/TestListScheduler.kt @@ -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() } + } +})