Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,4 @@ public void setValue(J value) {
this.value = value;
}
}

public static void main(String[] args) {
final LRUCache<String, Integer> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,4 @@ public void setValue(J value) {
this.value = value;
}
}

public static void main(String[] args) {
final MRUCache<String, Integer> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<String, String> 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<Integer, Integer> 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<Integer, Integer> mruCache = new LRUCache<>(SIZE);

for(int i = 0; i < 10; i++) {
mruCache.put(i, i);
}

assertEquals(9, mruCache.get(9));
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<String, String> 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<Integer, Integer> 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<Integer, Integer> mruCache = new MRUCache<>(SIZE);

for(int i = 0; i < 10; i++) {
mruCache.put(i, i);
}

assertEquals(9, mruCache.get(9));
}
}