-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
25 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
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
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
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
57 changes: 57 additions & 0 deletions
57
core/src/commonTest/kotlin/it/unibo/tuprolog/utils/CacheTest.kt
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,57 @@ | ||
package it.unibo.tuprolog.utils | ||
|
||
import kotlin.test.assertEquals | ||
|
||
interface CacheTest { | ||
|
||
companion object { | ||
|
||
fun prototype(cacheConstructor: (Int) -> Cache<String, Int>): CacheTest = | ||
object : CacheTest { | ||
override fun cacheFactory(capacity: Int): Cache<String, Int> { | ||
return cacheConstructor(capacity) | ||
} | ||
} | ||
|
||
private val ITEMS: List<Pair<String, Int>> = (0 until 26).asSequence() | ||
.map { String(charArrayOf('a' + it)) to it } | ||
.toList() | ||
|
||
private const val DEFAULT_CAPACITY = 5 | ||
} | ||
|
||
fun cacheFactory(capacity: Int): Cache<String, Int> | ||
|
||
fun testKeysCaching() { | ||
val cache = cacheFactory(DEFAULT_CAPACITY) | ||
|
||
var i = 0 | ||
|
||
while (i < DEFAULT_CAPACITY) { | ||
assertEquals(i, cache.size) | ||
assertEquals(Optional.none(), cache.set(ITEMS[i].first, ITEMS[i].second)) | ||
i++ | ||
for (j in 0 until i) { | ||
assertEquals(Optional.some(ITEMS[j].second), cache[ITEMS[j].first]) | ||
} | ||
for (j in i until ITEMS.size) { | ||
assertEquals(Optional.none(), cache[ITEMS[j].first]) | ||
} | ||
} | ||
|
||
while (i < ITEMS.size) { | ||
assertEquals(DEFAULT_CAPACITY, cache.size) | ||
assertEquals(Optional.some(ITEMS[i - DEFAULT_CAPACITY]), cache.set(ITEMS[i].first, ITEMS[i].second)) | ||
i++ | ||
for (j in 0 until (i - DEFAULT_CAPACITY)) { | ||
assertEquals(Optional.none(), cache[ITEMS[j].first]) | ||
} | ||
for (j in (i - DEFAULT_CAPACITY) until i) { | ||
assertEquals(Optional.some(ITEMS[j].second), cache[ITEMS[j].first]) | ||
} | ||
for (j in i until ITEMS.size) { | ||
assertEquals(Optional.none(), cache[ITEMS[j].first]) | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
core/src/commonTest/kotlin/it/unibo/tuprolog/utils/CursorTest.kt
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,4 @@ | ||
package it.unibo.tuprolog.utils | ||
|
||
class CursorTest { | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/commonTest/kotlin/it/unibo/tuprolog/utils/LRUCacheTest.kt
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,12 @@ | ||
package it.unibo.tuprolog.utils | ||
|
||
import it.unibo.tuprolog.utils.impl.LRUCache | ||
import kotlin.test.Test | ||
|
||
class LRUCacheTest : CacheTest by CacheTest.prototype(::LRUCache) { | ||
|
||
@Test | ||
override fun testKeysCaching() { | ||
super.testKeysCaching() | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/commonTest/kotlin/it/unibo/tuprolog/utils/SimpleLRUCacheTest.kt
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,12 @@ | ||
package it.unibo.tuprolog.utils | ||
|
||
import it.unibo.tuprolog.utils.impl.SimpleLRUCache | ||
import kotlin.test.Test | ||
|
||
class SimpleLRUCacheTest : CacheTest by CacheTest.prototype(::SimpleLRUCache) { | ||
|
||
@Test | ||
override fun testKeysCaching() { | ||
super.testKeysCaching() | ||
} | ||
} |