1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.annotations.J2ktIncompatible; 22 import com.google.common.collect.ImmutableMap; 23 import com.google.common.collect.Sets; 24 import com.google.common.testing.NullPointerTester; 25 import com.google.common.testing.SerializableTester; 26 import java.util.Map; 27 import java.util.Random; 28 import java.util.Set; 29 import junit.framework.TestCase; 30 31 /** 32 * Tests for {@link AtomicLongMap}. 33 * 34 * @author mike nonemacher 35 */ 36 @GwtCompatible(emulated = true) 37 public class AtomicLongMapTest extends TestCase { 38 private static final int ITERATIONS = 100; 39 private static final int MAX_ADDEND = 100; 40 41 private final Random random = new Random(301); 42 43 @J2ktIncompatible 44 @GwtIncompatible // NullPointerTester testNulls()45 public void testNulls() { 46 NullPointerTester tester = new NullPointerTester(); 47 tester.testAllPublicConstructors(AtomicLongMap.class); 48 tester.testAllPublicStaticMethods(AtomicLongMap.class); 49 AtomicLongMap<Object> map = AtomicLongMap.create(); 50 tester.testAllPublicInstanceMethods(map); 51 } 52 testCreate_map()53 public void testCreate_map() { 54 Map<String, Long> in = ImmutableMap.of("1", 1L, "2", 2L, "3", 3L); 55 AtomicLongMap<String> map = AtomicLongMap.create(in); 56 assertFalse(map.isEmpty()); 57 assertEquals(3, map.size()); 58 assertTrue(map.containsKey("1")); 59 assertTrue(map.containsKey("2")); 60 assertTrue(map.containsKey("3")); 61 assertEquals(1L, map.get("1")); 62 assertEquals(2L, map.get("2")); 63 assertEquals(3L, map.get("3")); 64 } 65 testIncrementAndGet()66 public void testIncrementAndGet() { 67 AtomicLongMap<String> map = AtomicLongMap.create(); 68 String key = "key"; 69 for (int i = 0; i < ITERATIONS; i++) { 70 long before = map.get(key); 71 long result = map.incrementAndGet(key); 72 long after = map.get(key); 73 assertEquals(before + 1, after); 74 assertEquals(after, result); 75 } 76 assertEquals(1, map.size()); 77 assertTrue(!map.isEmpty()); 78 assertTrue(map.containsKey(key)); 79 assertEquals(ITERATIONS, (int) map.get(key)); 80 } 81 testIncrementAndGet_zero()82 public void testIncrementAndGet_zero() { 83 AtomicLongMap<String> map = AtomicLongMap.create(); 84 String key = "key"; 85 assertEquals(0L, map.get(key)); 86 assertFalse(map.containsKey(key)); 87 88 assertEquals(1L, map.incrementAndGet(key)); 89 assertEquals(1L, map.get(key)); 90 91 assertEquals(0L, map.decrementAndGet(key)); 92 assertEquals(0L, map.get(key)); 93 assertTrue(map.containsKey(key)); 94 95 assertEquals(1L, map.incrementAndGet(key)); 96 assertEquals(1L, map.get(key)); 97 } 98 testGetAndIncrement()99 public void testGetAndIncrement() { 100 AtomicLongMap<String> map = AtomicLongMap.create(); 101 String key = "key"; 102 for (int i = 0; i < ITERATIONS; i++) { 103 long before = map.get(key); 104 long result = map.getAndIncrement(key); 105 long after = map.get(key); 106 assertEquals(before + 1, after); 107 assertEquals(before, result); 108 } 109 assertEquals(1, map.size()); 110 assertTrue(!map.isEmpty()); 111 assertTrue(map.containsKey(key)); 112 assertEquals(ITERATIONS, (int) map.get(key)); 113 } 114 testGetAndIncrement_zero()115 public void testGetAndIncrement_zero() { 116 AtomicLongMap<String> map = AtomicLongMap.create(); 117 String key = "key"; 118 assertEquals(0L, map.get(key)); 119 assertFalse(map.containsKey(key)); 120 121 assertEquals(0L, map.getAndIncrement(key)); 122 assertEquals(1L, map.get(key)); 123 124 assertEquals(1L, map.getAndDecrement(key)); 125 assertEquals(0L, map.get(key)); 126 assertTrue(map.containsKey(key)); 127 128 assertEquals(0L, map.getAndIncrement(key)); 129 assertEquals(1L, map.get(key)); 130 } 131 testDecrementAndGet()132 public void testDecrementAndGet() { 133 AtomicLongMap<String> map = AtomicLongMap.create(); 134 String key = "key"; 135 for (int i = 0; i < ITERATIONS; i++) { 136 long before = map.get(key); 137 long result = map.decrementAndGet(key); 138 long after = map.get(key); 139 assertEquals(before - 1, after); 140 assertEquals(after, result); 141 } 142 assertEquals(1, map.size()); 143 assertTrue(!map.isEmpty()); 144 assertTrue(map.containsKey(key)); 145 assertEquals(-1 * ITERATIONS, (int) map.get(key)); 146 } 147 testDecrementAndGet_zero()148 public void testDecrementAndGet_zero() { 149 AtomicLongMap<String> map = AtomicLongMap.create(); 150 String key = "key"; 151 assertEquals(0L, map.get(key)); 152 assertFalse(map.containsKey(key)); 153 154 assertEquals(-1L, map.decrementAndGet(key)); 155 assertEquals(-1L, map.get(key)); 156 157 assertEquals(0L, map.incrementAndGet(key)); 158 assertEquals(0L, map.get(key)); 159 assertTrue(map.containsKey(key)); 160 161 assertEquals(-1L, map.decrementAndGet(key)); 162 assertEquals(-1L, map.get(key)); 163 } 164 testGetAndDecrement()165 public void testGetAndDecrement() { 166 AtomicLongMap<String> map = AtomicLongMap.create(); 167 String key = "key"; 168 for (int i = 0; i < ITERATIONS; i++) { 169 long before = map.get(key); 170 long result = map.getAndDecrement(key); 171 long after = map.get(key); 172 assertEquals(before - 1, after); 173 assertEquals(before, result); 174 } 175 assertEquals(1, map.size()); 176 assertTrue(!map.isEmpty()); 177 assertTrue(map.containsKey(key)); 178 assertEquals(-1 * ITERATIONS, (int) map.get(key)); 179 } 180 testGetAndDecrement_zero()181 public void testGetAndDecrement_zero() { 182 AtomicLongMap<String> map = AtomicLongMap.create(); 183 String key = "key"; 184 assertEquals(0L, map.get(key)); 185 assertFalse(map.containsKey(key)); 186 187 assertEquals(0L, map.getAndDecrement(key)); 188 assertEquals(-1L, map.get(key)); 189 190 assertEquals(-1L, map.getAndIncrement(key)); 191 assertEquals(0L, map.get(key)); 192 assertTrue(map.containsKey(key)); 193 194 assertEquals(0L, map.getAndDecrement(key)); 195 assertEquals(-1L, map.get(key)); 196 } 197 testAddAndGet()198 public void testAddAndGet() { 199 AtomicLongMap<String> map = AtomicLongMap.create(); 200 String key = "key"; 201 long addend = random.nextInt(MAX_ADDEND); 202 for (int i = 0; i < ITERATIONS; i++) { 203 long before = map.get(key); 204 long result = map.addAndGet(key, addend); 205 long after = map.get(key); 206 assertEquals(before + addend, after); 207 assertEquals(after, result); 208 addend = after; 209 } 210 assertEquals(1, map.size()); 211 assertTrue(!map.isEmpty()); 212 assertTrue(map.containsKey(key)); 213 } 214 testAddAndGet_zero()215 public void testAddAndGet_zero() { 216 AtomicLongMap<String> map = AtomicLongMap.create(); 217 String key = "key"; 218 long value = random.nextInt(MAX_ADDEND); 219 assertEquals(0L, map.get(key)); 220 assertFalse(map.containsKey(key)); 221 222 assertEquals(value, map.addAndGet(key, value)); 223 assertEquals(value, map.get(key)); 224 225 assertEquals(0L, map.addAndGet(key, -1 * value)); 226 assertEquals(0L, map.get(key)); 227 assertTrue(map.containsKey(key)); 228 229 assertEquals(value, map.addAndGet(key, value)); 230 assertEquals(value, map.get(key)); 231 } 232 testGetAndAdd()233 public void testGetAndAdd() { 234 AtomicLongMap<String> map = AtomicLongMap.create(); 235 String key = "key"; 236 long addend = random.nextInt(MAX_ADDEND); 237 for (int i = 0; i < ITERATIONS; i++) { 238 long before = map.get(key); 239 long result = map.getAndAdd(key, addend); 240 long after = map.get(key); 241 assertEquals(before + addend, after); 242 assertEquals(before, result); 243 addend = after; 244 } 245 assertEquals(1, map.size()); 246 assertTrue(!map.isEmpty()); 247 assertTrue(map.containsKey(key)); 248 } 249 testGetAndAdd_zero()250 public void testGetAndAdd_zero() { 251 AtomicLongMap<String> map = AtomicLongMap.create(); 252 String key = "key"; 253 long value = random.nextInt(MAX_ADDEND); 254 assertEquals(0L, map.get(key)); 255 assertFalse(map.containsKey(key)); 256 257 assertEquals(0L, map.getAndAdd(key, value)); 258 assertEquals(value, map.get(key)); 259 260 assertEquals(value, map.getAndAdd(key, -1 * value)); 261 assertEquals(0L, map.get(key)); 262 assertTrue(map.containsKey(key)); 263 264 assertEquals(0L, map.getAndAdd(key, value)); 265 assertEquals(value, map.get(key)); 266 } 267 testPut()268 public void testPut() { 269 AtomicLongMap<String> map = AtomicLongMap.create(); 270 String key = "key"; 271 long newValue = random.nextInt(MAX_ADDEND); 272 for (int i = 0; i < ITERATIONS; i++) { 273 long before = map.get(key); 274 long result = map.put(key, newValue); 275 long after = map.get(key); 276 assertEquals(newValue, after); 277 assertEquals(before, result); 278 newValue += newValue; 279 } 280 assertEquals(1, map.size()); 281 assertTrue(!map.isEmpty()); 282 assertTrue(map.containsKey(key)); 283 } 284 testPut_zero()285 public void testPut_zero() { 286 AtomicLongMap<String> map = AtomicLongMap.create(); 287 String key = "key"; 288 long value = random.nextInt(MAX_ADDEND); 289 assertEquals(0L, map.get(key)); 290 assertFalse(map.containsKey(key)); 291 292 assertEquals(0L, map.put(key, value)); 293 assertEquals(value, map.get(key)); 294 295 assertEquals(value, map.put(key, 0L)); 296 assertEquals(0L, map.get(key)); 297 assertTrue(map.containsKey(key)); 298 299 assertEquals(0L, map.put(key, value)); 300 assertEquals(value, map.get(key)); 301 } 302 testPutAll()303 public void testPutAll() { 304 Map<String, Long> in = ImmutableMap.of("1", 1L, "2", 2L, "3", 3L); 305 AtomicLongMap<String> map = AtomicLongMap.create(); 306 assertTrue(map.isEmpty()); 307 assertEquals(0, map.size()); 308 assertFalse(map.containsKey("1")); 309 assertFalse(map.containsKey("2")); 310 assertFalse(map.containsKey("3")); 311 assertEquals(0L, map.get("1")); 312 assertEquals(0L, map.get("2")); 313 assertEquals(0L, map.get("3")); 314 315 map.putAll(in); 316 assertFalse(map.isEmpty()); 317 assertEquals(3, map.size()); 318 assertTrue(map.containsKey("1")); 319 assertTrue(map.containsKey("2")); 320 assertTrue(map.containsKey("3")); 321 assertEquals(1L, map.get("1")); 322 assertEquals(2L, map.get("2")); 323 assertEquals(3L, map.get("3")); 324 } 325 testPutIfAbsent()326 public void testPutIfAbsent() { 327 AtomicLongMap<String> map = AtomicLongMap.create(); 328 String key = "key"; 329 long newValue = random.nextInt(MAX_ADDEND); 330 for (int i = 0; i < ITERATIONS; i++) { 331 long before = map.get(key); 332 long result = map.putIfAbsent(key, newValue); 333 long after = map.get(key); 334 assertEquals(before, result); 335 assertEquals(before == 0 ? newValue : before, after); 336 337 map.remove(key); 338 before = map.get(key); 339 result = map.putIfAbsent(key, newValue); 340 after = map.get(key); 341 assertEquals(0, before); 342 assertEquals(before, result); 343 assertEquals(newValue, after); 344 345 map.put(key, 0L); 346 before = map.get(key); 347 result = map.putIfAbsent(key, newValue); 348 after = map.get(key); 349 assertEquals(0, before); 350 assertEquals(before, result); 351 assertEquals(newValue, after); 352 353 newValue += newValue; 354 } 355 assertEquals(1, map.size()); 356 assertTrue(!map.isEmpty()); 357 assertTrue(map.containsKey(key)); 358 } 359 testPutIfAbsent_zero()360 public void testPutIfAbsent_zero() { 361 AtomicLongMap<String> map = AtomicLongMap.create(); 362 String key = "key"; 363 long value = random.nextInt(MAX_ADDEND); 364 assertEquals(0L, map.get(key)); 365 assertFalse(map.containsKey(key)); 366 367 assertEquals(0L, map.putIfAbsent(key, value)); 368 assertEquals(value, map.get(key)); 369 370 assertEquals(value, map.put(key, 0L)); 371 assertEquals(0L, map.get(key)); 372 assertTrue(map.containsKey(key)); 373 374 assertEquals(0L, map.putIfAbsent(key, value)); 375 assertEquals(value, map.get(key)); 376 } 377 testReplace()378 public void testReplace() { 379 AtomicLongMap<String> map = AtomicLongMap.create(); 380 String key = "key"; 381 long newValue = random.nextInt(MAX_ADDEND); 382 for (int i = 0; i < ITERATIONS; i++) { 383 long before = map.get(key); 384 assertFalse(map.replace(key, before + 1, newValue + 1)); 385 assertFalse(map.replace(key, before - 1, newValue - 1)); 386 assertTrue(map.replace(key, before, newValue)); 387 long after = map.get(key); 388 assertEquals(newValue, after); 389 newValue += newValue; 390 } 391 assertEquals(1, map.size()); 392 assertTrue(!map.isEmpty()); 393 assertTrue(map.containsKey(key)); 394 } 395 testReplace_zero()396 public void testReplace_zero() { 397 AtomicLongMap<String> map = AtomicLongMap.create(); 398 String key = "key"; 399 long value = random.nextInt(MAX_ADDEND); 400 assertEquals(0L, map.get(key)); 401 assertFalse(map.containsKey(key)); 402 403 assertTrue(map.replace(key, 0L, value)); 404 assertEquals(value, map.get(key)); 405 406 assertTrue(map.replace(key, value, 0L)); 407 assertEquals(0L, map.get(key)); 408 assertTrue(map.containsKey(key)); 409 410 assertTrue(map.replace(key, 0L, value)); 411 assertEquals(value, map.get(key)); 412 } 413 testRemove()414 public void testRemove() { 415 AtomicLongMap<String> map = AtomicLongMap.create(); 416 String key = "key"; 417 assertEquals(0, map.size()); 418 assertTrue(map.isEmpty()); 419 assertEquals(0L, map.remove(key)); 420 421 long newValue = random.nextInt(MAX_ADDEND); 422 for (int i = 0; i < ITERATIONS; i++) { 423 map.put(key, newValue); 424 assertTrue(map.containsKey(key)); 425 426 long before = map.get(key); 427 long result = map.remove(key); 428 long after = map.get(key); 429 assertFalse(map.containsKey(key)); 430 assertEquals(before, result); 431 assertEquals(0L, after); 432 newValue += newValue; 433 } 434 assertEquals(0, map.size()); 435 assertTrue(map.isEmpty()); 436 } 437 testRemove_zero()438 public void testRemove_zero() { 439 AtomicLongMap<String> map = AtomicLongMap.create(); 440 String key = "key"; 441 assertEquals(0L, map.get(key)); 442 assertFalse(map.containsKey(key)); 443 444 assertEquals(0L, map.remove(key)); 445 assertEquals(0L, map.get(key)); 446 assertFalse(map.containsKey(key)); 447 448 assertEquals(0L, map.put(key, 0L)); 449 assertEquals(0L, map.get(key)); 450 assertTrue(map.containsKey(key)); 451 452 assertEquals(0L, map.remove(key)); 453 assertEquals(0L, map.get(key)); 454 assertFalse(map.containsKey(key)); 455 } 456 testRemoveIfZero()457 public void testRemoveIfZero() { 458 AtomicLongMap<String> map = AtomicLongMap.create(); 459 String key = "key"; 460 assertEquals(0, map.size()); 461 assertTrue(map.isEmpty()); 462 assertFalse(map.removeIfZero(key)); 463 464 assertEquals(1, map.incrementAndGet(key)); 465 assertFalse(map.removeIfZero(key)); 466 assertEquals(2, map.incrementAndGet(key)); 467 assertFalse(map.removeIfZero(key)); 468 assertEquals(1, map.decrementAndGet(key)); 469 assertFalse(map.removeIfZero(key)); 470 assertEquals(0, map.decrementAndGet(key)); 471 assertTrue(map.removeIfZero(key)); 472 assertFalse(map.containsKey(key)); 473 } 474 testRemoveValue()475 public void testRemoveValue() { 476 AtomicLongMap<String> map = AtomicLongMap.create(); 477 String key = "key"; 478 assertEquals(0, map.size()); 479 assertTrue(map.isEmpty()); 480 assertFalse(map.remove(key, 0L)); 481 482 long newValue = random.nextInt(MAX_ADDEND); 483 for (int i = 0; i < ITERATIONS; i++) { 484 map.put(key, newValue); 485 assertTrue(map.containsKey(key)); 486 487 long before = map.get(key); 488 assertFalse(map.remove(key, newValue + 1)); 489 assertFalse(map.remove(key, newValue - 1)); 490 assertTrue(map.remove(key, newValue)); 491 long after = map.get(key); 492 assertFalse(map.containsKey(key)); 493 assertEquals(0L, after); 494 newValue += newValue; 495 } 496 assertEquals(0, map.size()); 497 assertTrue(map.isEmpty()); 498 } 499 testRemoveValue_zero()500 public void testRemoveValue_zero() { 501 AtomicLongMap<String> map = AtomicLongMap.create(); 502 String key = "key"; 503 assertEquals(0L, map.get(key)); 504 assertFalse(map.containsKey(key)); 505 506 assertFalse(map.remove(key, 0L)); 507 assertEquals(0L, map.get(key)); 508 assertFalse(map.containsKey(key)); 509 510 assertEquals(0L, map.put(key, 0L)); 511 assertEquals(0L, map.get(key)); 512 assertTrue(map.containsKey(key)); 513 514 assertTrue(map.remove(key, 0L)); 515 assertEquals(0L, map.get(key)); 516 assertFalse(map.containsKey(key)); 517 } 518 testRemoveZeros()519 public void testRemoveZeros() { 520 AtomicLongMap<Object> map = AtomicLongMap.create(); 521 Set<Object> nonZeroKeys = Sets.newHashSet(); 522 for (int i = 0; i < ITERATIONS; i++) { 523 Object key = new Object(); 524 long value = i % 2; 525 map.put(key, value); 526 if (value != 0L) { 527 nonZeroKeys.add(key); 528 } 529 } 530 assertEquals(ITERATIONS, map.size()); 531 assertTrue(map.asMap().containsValue(0L)); 532 533 map.removeAllZeros(); 534 assertFalse(map.asMap().containsValue(0L)); 535 assertEquals(ITERATIONS / 2, map.size()); 536 assertEquals(nonZeroKeys, map.asMap().keySet()); 537 } 538 testClear()539 public void testClear() { 540 AtomicLongMap<Object> map = AtomicLongMap.create(); 541 for (int i = 0; i < ITERATIONS; i++) { 542 map.put(new Object(), i); 543 } 544 assertEquals(ITERATIONS, map.size()); 545 546 map.clear(); 547 assertEquals(0, map.size()); 548 assertTrue(map.isEmpty()); 549 } 550 testSum()551 public void testSum() { 552 AtomicLongMap<Object> map = AtomicLongMap.create(); 553 long sum = 0; 554 for (int i = 0; i < ITERATIONS; i++) { 555 map.put(new Object(), i); 556 sum += i; 557 } 558 assertEquals(ITERATIONS, map.size()); 559 assertEquals(sum, map.sum()); 560 } 561 testEmpty()562 public void testEmpty() { 563 AtomicLongMap<String> map = AtomicLongMap.create(); 564 assertEquals(0L, map.get("a")); 565 assertEquals(0, map.size()); 566 assertTrue(map.isEmpty()); 567 assertFalse(map.remove("a", 1L)); 568 assertFalse(map.remove("a", 0L)); 569 assertFalse(map.replace("a", 1L, 0L)); 570 } 571 testSerialization()572 public void testSerialization() { 573 AtomicLongMap<String> map = AtomicLongMap.create(); 574 map.put("key", 1L); 575 AtomicLongMap<String> reserialized = SerializableTester.reserialize(map); 576 assertEquals(map.asMap(), reserialized.asMap()); 577 } 578 } 579