Skip to content

Commit

Permalink
Extend LruCache unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth J. Shackleton <[email protected]>
  • Loading branch information
kennethshackleton committed Jun 1, 2024
1 parent 311c091 commit 0317839
Showing 1 changed file with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package com.bloomberg.selekt
package com.bloomberg.selekt.cache

import com.bloomberg.selekt.cache.LruCache
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.inOrder
Expand All @@ -30,8 +30,50 @@ import org.mockito.kotlin.whenever
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
import kotlin.test.fail

internal class LruCacheTest {
@Test
fun maxSizeAtLeastOne() {
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
assertThrows<IllegalArgumentException> {
LruCache(0, disposal)
}
}

@Test
fun get() {
val first = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(1, disposal)
cache["1", { first }]
assertSame(first, cache["1", { fail() }])
}

@Test
fun getTwo() {
val first = Any()
val second = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(2, disposal)
cache["1", { first }]
cache["2", { second }]
assertSame(first, cache["1", { fail() }])
assertSame(second, cache["2", { fail() }])
}

@Test
fun getAfterEvict() {
val first = Any()
val second = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(1, disposal)
cache["1", { first }]
cache["2", { second }]
assertFalse(cache.containsKey("1"))
assertSame(second, cache["2", { fail() }])
}

@Test
fun evict() {
val first = Any()
Expand All @@ -44,6 +86,7 @@ internal class LruCacheTest {
inOrder(disposal) {
verify(disposal, times(1)).invoke(same(first))
}
assertSame(second, cache["2", { fail() }])
}

@Test
Expand Down

0 comments on commit 0317839

Please sign in to comment.