Added MRUCache
This commit is contained in:
1 parent
cc8c9ecc5d
commit
1fd229fdb1
3 files changed
+110
-21
No files matched your search
@@ -1,7 +1,11 @@
|
||||
package bintools
|
||||
|
||||
import net.sergeych.bintools.MRUCache
|
||||
import net.sergeych.bintools.toDump
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class TestTools {
|
||||
@Test
|
||||
@@ -16,4 +20,42 @@ class TestTools {
|
||||
println(res.toDump())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCache() {
|
||||
val cache = MRUCache<Int,String>(3)
|
||||
cache.putAll( mapOf(1 to "one", 2 to "two", 3 to "three", 4 to "four" ) )
|
||||
assertNull(cache[0])
|
||||
|
||||
// this actually should reset MRU for 2:
|
||||
assertEquals("two", cache[2])
|
||||
assertNull(cache[1])
|
||||
assertEquals(3, cache.size)
|
||||
assertTrue { 3 in cache }
|
||||
assertTrue { 4 in cache }
|
||||
|
||||
// now MRU is 2 (checked in assertEquals above) so LRU to drop is 3!
|
||||
cache[5] = "five"
|
||||
assertEquals(3, cache.size)
|
||||
assertTrue { 2 in cache }
|
||||
assertTrue { 4 in cache }
|
||||
assertTrue { 5 in cache }
|
||||
|
||||
cache.getOrPut(3) { "new three"}
|
||||
assertEquals(3, cache.size)
|
||||
assertTrue { 2 in cache }
|
||||
assertTrue { 3 in cache }
|
||||
assertTrue { 5 in cache }
|
||||
|
||||
cache[2] = "New Two"
|
||||
cache[6] = "six"
|
||||
|
||||
// 2 is now second used
|
||||
// amd 6 is MRU, oldest is therefore 5
|
||||
|
||||
assertEquals(3, cache.size)
|
||||
assertTrue { 2 in cache }
|
||||
assertTrue { 6 in cache }
|
||||
assertTrue { 3 in cache }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user