Skip to content

Commit ae626bc

Browse files
author
valeriotrinca
committed
#3263 LRUCacheTest and MRUCacheTest
1 parent 9c418ba commit ae626bc

File tree

4 files changed

+116
-24
lines changed

4 files changed

+116
-24
lines changed

src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,4 @@ public void setValue(J value) {
168168
this.value = value;
169169
}
170170
}
171-
172-
public static void main(String[] args) {
173-
final LRUCache<String, Integer> cache = new LRUCache<>(2);
174-
cache.put("Key1", 1);
175-
cache.put("Key2", 2);
176-
cache.put("Key3", 3);
177-
cache.put("Key4", 4);
178-
System.out.println("getValue(Key1): " + cache.get("Key1"));
179-
System.out.println("getValue(Key2): " + cache.get("Key2"));
180-
System.out.println("getValue(Key3): " + cache.get("Key3"));
181-
System.out.println("getValue(Key4): " + cache.get("Key4"));
182-
}
183171
}

src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,4 @@ public void setValue(J value) {
166166
this.value = value;
167167
}
168168
}
169-
170-
public static void main(String[] args) {
171-
final MRUCache<String, Integer> cache = new MRUCache<>(2);
172-
cache.put("Key1", 1);
173-
cache.put("Key2", 2);
174-
cache.put("Key3", 3);
175-
cache.put("Key4", 4);
176-
System.out.println("getValue(Key1): " + cache.get("Key1"));
177-
System.out.println("getValue(Key2): " + cache.get("Key2"));
178-
System.out.println("getValue(Key3): " + cache.get("Key3"));
179-
System.out.println("getValue(Key4): " + cache.get("Key4"));
180-
}
181169
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.datastructures.caches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class LRUCacheTest {
9+
10+
private static final int SIZE = 5;
11+
12+
@Test
13+
public void putAndGetIntegerValues() {
14+
LRUCache<Integer, Integer> lruCache = new LRUCache<>(SIZE);
15+
16+
for(int i = 0; i < SIZE; i++) {
17+
lruCache.put(i, i);
18+
}
19+
20+
for(int i = 0; i < SIZE; i++) {
21+
assertEquals(i, lruCache.get(i));
22+
}
23+
}
24+
25+
@Test
26+
public void putAndGetStringValues() {
27+
LRUCache<String, String> lruCache = new LRUCache<>(SIZE);
28+
29+
for(int i = 0; i < SIZE; i++) {
30+
lruCache.put("key" + i, "value" + i);
31+
}
32+
33+
for(int i = 0; i < SIZE; i++) {
34+
assertEquals("value" + i, lruCache.get("key" + i));
35+
}
36+
}
37+
38+
@Test
39+
public void nullKeysAndValues() {
40+
LRUCache<Integer, Integer> mruCache = new LRUCache<>(SIZE);
41+
mruCache.put(null, 2);
42+
mruCache.put(6, null);
43+
44+
assertEquals(2, mruCache.get(null));
45+
assertNull(mruCache.get(6));
46+
}
47+
48+
@Test
49+
public void overCapacity() {
50+
LRUCache<Integer, Integer> mruCache = new LRUCache<>(SIZE);
51+
52+
for(int i = 0; i < 10; i++) {
53+
mruCache.put(i, i);
54+
}
55+
56+
assertEquals(9, mruCache.get(9));
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.datastructures.caches;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
8+
public class MRUCacheTest {
9+
10+
private static final int SIZE = 5;
11+
12+
@Test
13+
public void putAndGetIntegerValues() {
14+
MRUCache<Integer, Integer> lruCache = new MRUCache<>(SIZE);
15+
16+
for(int i = 0; i < SIZE; i++) {
17+
lruCache.put(i, i);
18+
}
19+
20+
for(int i = 0; i < SIZE; i++) {
21+
assertEquals(i, lruCache.get(i));
22+
}
23+
}
24+
25+
@Test
26+
public void putAndGetStringValues() {
27+
MRUCache<String, String> lruCache = new MRUCache<>(SIZE);
28+
29+
for(int i = 0; i < SIZE; i++) {
30+
lruCache.put("key" + i, "value" + i);
31+
}
32+
33+
for(int i = 0; i < SIZE; i++) {
34+
assertEquals("value" + i, lruCache.get("key" + i));
35+
}
36+
}
37+
38+
@Test
39+
public void nullKeysAndValues() {
40+
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
41+
mruCache.put(null, 2);
42+
mruCache.put(6, null);
43+
44+
assertEquals(2, mruCache.get(null));
45+
assertNull(mruCache.get(6));
46+
}
47+
48+
@Test
49+
public void overCapacity() {
50+
MRUCache<Integer, Integer> mruCache = new MRUCache<>(SIZE);
51+
52+
for(int i = 0; i < 10; i++) {
53+
mruCache.put(i, i);
54+
}
55+
56+
assertEquals(9, mruCache.get(9));
57+
}
58+
}

0 commit comments

Comments
 (0)