From d56eaa58af12ea54c4db7a974074c19f886bc332 Mon Sep 17 00:00:00 2001 From: valjamwo Date: Mon, 19 Sep 2022 19:55:15 +0200 Subject: [PATCH] Add LRUCacheTest and MRUCacheTest (fixes #3263) (#3277) --- .../datastructures/caches/LRUCache.java | 12 ---- .../datastructures/caches/MRUCache.java | 12 ---- .../datastructures/caches/LRUCacheTest.java | 58 +++++++++++++++++++ .../datastructures/caches/MRUCacheTest.java | 58 +++++++++++++++++++ 4 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/caches/LRUCacheTest.java create mode 100644 src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java b/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java index b905499b..fcb7d975 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java @@ -168,16 +168,4 @@ public class LRUCache { this.value = value; } } - - public static void main(String[] args) { - final LRUCache cache = new LRUCache<>(2); - cache.put("Key1", 1); - cache.put("Key2", 2); - cache.put("Key3", 3); - cache.put("Key4", 4); - System.out.println("getValue(Key1): " + cache.get("Key1")); - System.out.println("getValue(Key2): " + cache.get("Key2")); - System.out.println("getValue(Key3): " + cache.get("Key3")); - System.out.println("getValue(Key4): " + cache.get("Key4")); - } } diff --git a/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java b/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java index 5d77865c..30f91496 100644 --- a/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java +++ b/src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java @@ -166,16 +166,4 @@ public class MRUCache { this.value = value; } } - - public static void main(String[] args) { - final MRUCache cache = new MRUCache<>(2); - cache.put("Key1", 1); - cache.put("Key2", 2); - cache.put("Key3", 3); - cache.put("Key4", 4); - System.out.println("getValue(Key1): " + cache.get("Key1")); - System.out.println("getValue(Key2): " + cache.get("Key2")); - System.out.println("getValue(Key3): " + cache.get("Key3")); - System.out.println("getValue(Key4): " + cache.get("Key4")); - } } diff --git a/src/test/java/com/thealgorithms/datastructures/caches/LRUCacheTest.java b/src/test/java/com/thealgorithms/datastructures/caches/LRUCacheTest.java new file mode 100644 index 00000000..ca44f2ca --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/caches/LRUCacheTest.java @@ -0,0 +1,58 @@ +package com.thealgorithms.datastructures.caches; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +public class LRUCacheTest { + + private static final int SIZE = 5; + + @Test + public void putAndGetIntegerValues() { + LRUCache lruCache = new LRUCache<>(SIZE); + + for(int i = 0; i < SIZE; i++) { + lruCache.put(i, i); + } + + for(int i = 0; i < SIZE; i++) { + assertEquals(i, lruCache.get(i)); + } + } + + @Test + public void putAndGetStringValues() { + LRUCache lruCache = new LRUCache<>(SIZE); + + for(int i = 0; i < SIZE; i++) { + lruCache.put("key" + i, "value" + i); + } + + for(int i = 0; i < SIZE; i++) { + assertEquals("value" + i, lruCache.get("key" + i)); + } + } + + @Test + public void nullKeysAndValues() { + LRUCache mruCache = new LRUCache<>(SIZE); + mruCache.put(null, 2); + mruCache.put(6, null); + + assertEquals(2, mruCache.get(null)); + assertNull(mruCache.get(6)); + } + + @Test + public void overCapacity() { + LRUCache mruCache = new LRUCache<>(SIZE); + + for(int i = 0; i < 10; i++) { + mruCache.put(i, i); + } + + assertEquals(9, mruCache.get(9)); + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java b/src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java new file mode 100644 index 00000000..6ff77aeb --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java @@ -0,0 +1,58 @@ +package com.thealgorithms.datastructures.caches; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class MRUCacheTest { + + private static final int SIZE = 5; + + @Test + public void putAndGetIntegerValues() { + MRUCache lruCache = new MRUCache<>(SIZE); + + for(int i = 0; i < SIZE; i++) { + lruCache.put(i, i); + } + + for(int i = 0; i < SIZE; i++) { + assertEquals(i, lruCache.get(i)); + } + } + + @Test + public void putAndGetStringValues() { + MRUCache lruCache = new MRUCache<>(SIZE); + + for(int i = 0; i < SIZE; i++) { + lruCache.put("key" + i, "value" + i); + } + + for(int i = 0; i < SIZE; i++) { + assertEquals("value" + i, lruCache.get("key" + i)); + } + } + + @Test + public void nullKeysAndValues() { + MRUCache mruCache = new MRUCache<>(SIZE); + mruCache.put(null, 2); + mruCache.put(6, null); + + assertEquals(2, mruCache.get(null)); + assertNull(mruCache.get(6)); + } + + @Test + public void overCapacity() { + MRUCache mruCache = new MRUCache<>(SIZE); + + for(int i = 0; i < 10; i++) { + mruCache.put(i, i); + } + + assertEquals(9, mruCache.get(9)); + } +}