/** Fails if the {@link AtomicLongMap} is empty. */ public void isNotEmpty() { if (actual().isEmpty()) { failWithoutActual(simpleFact("expected not to be empty")); } }
/** Fails if the {@link AtomicLongMap} is not empty. */ public void isEmpty() { if (!actual().isEmpty()) { failWithActual(simpleFact("expected to be empty")); } }
public void testClear() { AtomicLongMap<Object> map = AtomicLongMap.create(); for (int i = 0; i < ITERATIONS; i++) { map.put(new Object(), i); } assertEquals(ITERATIONS, map.size()); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testAddAndGet() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long addend = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.addAndGet(key, addend); long after = map.get(key); assertEquals(before + addend, after); assertEquals(after, result); addend = after; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testGetAndAdd() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long addend = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.getAndAdd(key, addend); long after = map.get(key); assertEquals(before + addend, after); assertEquals(before, result); addend = after; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testPut() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.put(key, newValue); long after = map.get(key); assertEquals(newValue, after); assertEquals(before, result); newValue += newValue; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testRemove() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertEquals(0L, map.remove(key)); long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { map.put(key, newValue); assertTrue(map.containsKey(key)); long before = map.get(key); long result = map.remove(key); long after = map.get(key); assertFalse(map.containsKey(key)); assertEquals(before, result); assertEquals(0L, after); newValue += newValue; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testCreate_map() { Map<String, Long> in = ImmutableMap.of("1", 1L, "2", 2L, "3", 3L); AtomicLongMap<String> map = AtomicLongMap.create(in); assertFalse(map.isEmpty()); assertSame(3, map.size()); assertTrue(map.containsKey("1")); assertTrue(map.containsKey("2")); assertTrue(map.containsKey("3")); assertEquals(1L, map.get("1")); assertEquals(2L, map.get("2")); assertEquals(3L, map.get("3")); }
public void testReplace() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); assertFalse(map.replace(key, before + 1, newValue + 1)); assertFalse(map.replace(key, before - 1, newValue - 1)); assertTrue(map.replace(key, before, newValue)); long after = map.get(key); assertEquals(newValue, after); newValue += newValue; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testEmpty() { AtomicLongMap<String> map = AtomicLongMap.create(); assertEquals(0L, map.get("a")); assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertFalse(map.remove("a", 1L)); assertFalse(map.remove("a", 0L)); assertFalse(map.replace("a", 1L, 0L)); }
public void testRemoveValue() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertFalse(map.remove(key, 0L)); long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { map.put(key, newValue); assertTrue(map.containsKey(key)); long before = map.get(key); assertFalse(map.remove(key, newValue + 1)); assertFalse(map.remove(key, newValue - 1)); assertTrue(map.remove(key, newValue)); long after = map.get(key); assertFalse(map.containsKey(key)); assertEquals(0L, after); newValue += newValue; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testIncrementAndGet() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.incrementAndGet(key); long after = map.get(key); assertEquals(before + 1, after); assertEquals(after, result); } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); assertEquals(ITERATIONS, (int) map.get(key)); }
public void testGetAndDecrement() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.getAndDecrement(key); long after = map.get(key); assertEquals(before - 1, after); assertEquals(before, result); } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); assertEquals(-1 * ITERATIONS, (int) map.get(key)); }
public void testGetAndIncrement() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.getAndIncrement(key); long after = map.get(key); assertEquals(before + 1, after); assertEquals(before, result); } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); assertEquals(ITERATIONS, (int) map.get(key)); }
public void testDecrementAndGet() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.decrementAndGet(key); long after = map.get(key); assertEquals(before - 1, after); assertEquals(after, result); } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); assertEquals(-1 * ITERATIONS, (int) map.get(key)); }
public void testPutAll() { Map<String, Long> in = ImmutableMap.of("1", 1L, "2", 2L, "3", 3L); AtomicLongMap<String> map = AtomicLongMap.create(); assertTrue(map.isEmpty()); assertSame(0, map.size()); assertFalse(map.containsKey("1")); assertFalse(map.containsKey("2")); assertFalse(map.containsKey("3")); assertEquals(0L, map.get("1")); assertEquals(0L, map.get("2")); assertEquals(0L, map.get("3")); map.putAll(in); assertFalse(map.isEmpty()); assertSame(3, map.size()); assertTrue(map.containsKey("1")); assertTrue(map.containsKey("2")); assertTrue(map.containsKey("3")); assertEquals(1L, map.get("1")); assertEquals(2L, map.get("2")); assertEquals(3L, map.get("3")); }
public void testPutIfAbsent() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.putIfAbsent(key, newValue); long after = map.get(key); assertEquals(before, result); assertEquals(before == 0 ? newValue : before, after); map.remove(key); before = map.get(key); result = map.putIfAbsent(key, newValue); after = map.get(key); assertEquals(0, before); assertEquals(before, result); assertEquals(newValue, after); map.put(key, 0L); before = map.get(key); result = map.putIfAbsent(key, newValue); after = map.get(key); assertEquals(0, before); assertEquals(before, result); assertEquals(newValue, after); newValue += newValue; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testRemoveIfZero() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertFalse(map.removeIfZero(key)); assertEquals(1, map.incrementAndGet(key)); assertFalse(map.removeIfZero(key)); assertEquals(2, map.incrementAndGet(key)); assertFalse(map.removeIfZero(key)); assertEquals(1, map.decrementAndGet(key)); assertFalse(map.removeIfZero(key)); assertEquals(0, map.decrementAndGet(key)); assertTrue(map.removeIfZero(key)); assertFalse(map.containsKey(key)); }
public void testClear() { AtomicLongMap<Object> map = AtomicLongMap.create(); for (int i = 0; i < ITERATIONS; i++) { map.put(new Object(), i); } assertEquals(ITERATIONS, map.size()); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testCreate_map() { Map<String, Long> in = ImmutableMap.of("1", 1L, "2", 2L, "3", 3L); AtomicLongMap<String> map = AtomicLongMap.create(in); assertFalse(map.isEmpty()); assertSame(3, map.size()); assertTrue(map.containsKey("1")); assertTrue(map.containsKey("2")); assertTrue(map.containsKey("3")); assertEquals(1L, map.get("1")); assertEquals(2L, map.get("2")); assertEquals(3L, map.get("3")); }