Revamped caching again, this time, without sorting.
Implemented cache changes.
This commit is contained in:
@@ -2,9 +2,10 @@ package ca.recrown.islandsurvivalcraft.caching;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -31,14 +32,14 @@ public class CacheTest {
|
||||
assertTrue(val == null);
|
||||
val = "first";
|
||||
integerCache.setValue(0, val);
|
||||
assertTrue(integerCache.hasValue(0));
|
||||
assertTrue(integerCache.getValue(0) != null);
|
||||
assertSame(val, integerCache.getValue(0));
|
||||
|
||||
val = integerCache.getValue(1);
|
||||
assertTrue(val == null);
|
||||
val = "second";
|
||||
integerCache.setValue(1, val);
|
||||
assertTrue(integerCache.hasValue(1));
|
||||
assertTrue(integerCache.getValue(1) != null);
|
||||
assertSame(val, integerCache.getValue(1));
|
||||
}
|
||||
|
||||
@@ -51,11 +52,57 @@ public class CacheTest {
|
||||
assertEquals("first", integerCache.getValue(0));
|
||||
|
||||
integerCache.setValue(3, "fourth");
|
||||
|
||||
assertEquals("first", integerCache.getValue(0));
|
||||
assertTrue(integerCache.hasValue(1));
|
||||
|
||||
integerCache.setValue(5, "sixth");
|
||||
assertFalse(integerCache.hasValue(2));
|
||||
integerCache.setValue(4, "fifth");
|
||||
assertTrue(integerCache.getValue(3) != null);
|
||||
assertTrue(integerCache.getValue(0) != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsageLargeData() {
|
||||
int amount = 1024;
|
||||
Random random = new Random();
|
||||
|
||||
Cache<Integer, Integer> largeCache = new Cache<>(amount/2);
|
||||
|
||||
int[] expected = new int[amount];
|
||||
for (int i = 0; i < amount; i++) {
|
||||
expected[i] = random.nextInt();
|
||||
largeCache.setValue(i, expected[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < amount /2; i++) {
|
||||
assertEquals(null, largeCache.getValue(i), "Current accessor: " + i);
|
||||
}
|
||||
|
||||
for (int i = amount /2; i < amount; i++) {
|
||||
assertEquals(expected[i], largeCache.getValue(i), "Current accessor: " + i);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsageLargeDataImportance() {
|
||||
int amount = 1024;
|
||||
Random random = new Random();
|
||||
|
||||
Cache<Integer, Integer> largeCache = new Cache<>(amount/2);
|
||||
|
||||
int[] expected = new int[amount];
|
||||
for (int i = 0; i < amount; i++) {
|
||||
expected[i] = random.nextInt();
|
||||
largeCache.setValue(i, expected[i]);
|
||||
largeCache.getValue(0);
|
||||
}
|
||||
|
||||
for (int i = 1; i < (amount/2) + 1; i++) {
|
||||
assertEquals(null, largeCache.getValue(i), "Current accessor: " + i);
|
||||
}
|
||||
|
||||
for (int i = (amount /2) + 1; i < amount; i++) {
|
||||
assertEquals(expected[i], largeCache.getValue(i), "Current accessor: " + i);
|
||||
}
|
||||
|
||||
assertEquals(expected[0], largeCache.getValue(0));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user