generated from SmartOperatingBlock/kotlin-template-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement a simple scheduler using list
- Loading branch information
1 parent
38a0ad1
commit 3b64326
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
}) |