CrossCheckRemoveAllTest.java
package org.caffinitas.ohc.linked;
import org.caffinitas.ohc.Eviction;
import org.caffinitas.ohc.HashAlgorithm;
import org.caffinitas.ohc.OHCache;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.assertTrue;
public class CrossCheckRemoveAllTest extends CrossCheckTestBase {
@Test(dataProvider = "types")
public void testRemoveAll(Eviction eviction, HashAlgorithm hashAlgorithm) throws Exception
{
try (OHCache<Integer, String> cache = cache(eviction, hashAlgorithm))
{
for (int i = 0; i < 100; i++)
cache.put(i, Integer.toOctalString(i));
for (int i = 0; i < 100; i++)
Assert.assertEquals(cache.get(i), Integer.toOctalString(i));
long lastFree = cache.freeCapacity();
assertTrue(lastFree < cache.capacity());
assertTrue(cache.size() > 0);
List<Integer> coll = new ArrayList<>();
for (int i = 0; i < 10; i++)
coll.add(i);
cache.removeAll(coll);
assertTrue(cache.freeCapacity() > lastFree);
Assert.assertEquals(cache.size(), 90);
for (int i = 10; i < 50; i++)
coll.add(i);
cache.removeAll(coll);
assertTrue(cache.freeCapacity() > lastFree);
Assert.assertEquals(cache.size(), 50);
for (int i = 50; i < 100; i++)
coll.add(i);
cache.removeAll(coll);
Assert.assertEquals(cache.freeCapacity(), cache.capacity());
Assert.assertEquals(cache.size(), 0);
}
}
@Test(dataProvider = "types")
public void testClear(Eviction eviction, HashAlgorithm hashAlgorithm) throws Exception
{
try (OHCache<Integer, String> cache = cache(eviction, hashAlgorithm))
{
for (int i = 0; i < 100; i++)
cache.put(i, Integer.toOctalString(i));
for (int i = 0; i < 100; i++)
Assert.assertEquals(cache.get(i), Integer.toOctalString(i));
assertTrue(cache.freeCapacity() < cache.capacity());
assertTrue(cache.size() > 0);
cache.clear();
Assert.assertEquals(cache.freeCapacity(), cache.capacity());
Assert.assertEquals(cache.size(), 0);
}
}
}