--- jsr166/src/test/tck/AtomicIntegerArrayTest.java 2005/05/25 14:27:37 1.10 +++ jsr166/src/test/tck/AtomicIntegerArrayTest.java 2009/11/16 05:30:07 1.13 @@ -2,8 +2,8 @@ * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import junit.framework.*; @@ -26,7 +26,7 @@ public class AtomicIntegerArrayTest exte */ public void testConstructor() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(0,ai.get(i)); } @@ -50,30 +50,30 @@ public class AtomicIntegerArrayTest exte int[] a = { 17, 3, -42, 99, -7}; AtomicIntegerArray ai = new AtomicIntegerArray(a); assertEquals(a.length, ai.length()); - for (int i = 0; i < a.length; ++i) + for (int i = 0; i < a.length; ++i) assertEquals(a[i], ai.get(i)); } /** * get and set for out of bound indices throw IndexOutOfBoundsException */ - public void testIndexing(){ + public void testIndexing() { AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); try { ai.get(SIZE); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success) { } try { ai.get(-1); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success) { } try { ai.set(SIZE, 0); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success) { } try { ai.set(-1, 0); - } catch(IndexOutOfBoundsException success){ + } catch (IndexOutOfBoundsException success) { } } @@ -81,7 +81,7 @@ public class AtomicIntegerArrayTest exte * get returns the last value set at index */ public void testGetSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.get(i)); @@ -96,7 +96,7 @@ public class AtomicIntegerArrayTest exte * get returns the last value lazySet at index by same thread */ public void testGetLazySet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.lazySet(i, 1); assertEquals(1,ai.get(i)); @@ -111,7 +111,7 @@ public class AtomicIntegerArrayTest exte * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertTrue(ai.compareAndSet(i, 1,2)); @@ -133,7 +133,7 @@ public class AtomicIntegerArrayTest exte a.set(0, 1); Thread t = new Thread(new Runnable() { public void run() { - while(!a.compareAndSet(0, 2, 3)) Thread.yield(); + while (!a.compareAndSet(0, 2, 3)) Thread.yield(); }}); try { t.start(); @@ -142,23 +142,23 @@ public class AtomicIntegerArrayTest exte assertFalse(t.isAlive()); assertEquals(a.get(0), 3); } - catch(Exception e) { + catch (Exception e) { unexpectedException(); } } /** * repeated weakCompareAndSet succeeds in changing value when equal - * to expected + * to expected */ public void testWeakCompareAndSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); - while(!ai.weakCompareAndSet(i, 1,2)); - while(!ai.weakCompareAndSet(i, 2,-4)); + while (!ai.weakCompareAndSet(i, 1,2)); + while (!ai.weakCompareAndSet(i, 2,-4)); assertEquals(-4,ai.get(i)); - while(!ai.weakCompareAndSet(i, -4,7)); + while (!ai.weakCompareAndSet(i, -4,7)); assertEquals(7,ai.get(i)); } } @@ -167,7 +167,7 @@ public class AtomicIntegerArrayTest exte * getAndSet returns previous value and sets to given value at given index */ public void testGetAndSet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndSet(i,0)); @@ -180,7 +180,7 @@ public class AtomicIntegerArrayTest exte * getAndAdd returns previous value and adds given value */ public void testGetAndAdd() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndAdd(i,2)); @@ -194,7 +194,7 @@ public class AtomicIntegerArrayTest exte * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndDecrement(i)); @@ -207,7 +207,7 @@ public class AtomicIntegerArrayTest exte * getAndIncrement returns previous value and increments */ public void testGetAndIncrement() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(1,ai.getAndIncrement(i)); @@ -224,7 +224,7 @@ public class AtomicIntegerArrayTest exte * addAndGet adds given value to current, and returns current value */ public void testAddAndGet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(3,ai.addAndGet(i,2)); @@ -238,7 +238,7 @@ public class AtomicIntegerArrayTest exte * decrementAndGet decrements and returns current value */ public void testDecrementAndGet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(0,ai.decrementAndGet(i)); @@ -252,7 +252,7 @@ public class AtomicIntegerArrayTest exte * incrementAndGet increments and returns current value */ public void testIncrementAndGet() { - AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); for (int i = 0; i < SIZE; ++i) { ai.set(i, 1); assertEquals(2,ai.incrementAndGet(i)); @@ -266,7 +266,7 @@ public class AtomicIntegerArrayTest exte } static final int COUNTDOWN = 100000; - + class Counter implements Runnable { final AtomicIntegerArray ai; volatile int counts; @@ -295,8 +295,8 @@ public class AtomicIntegerArrayTest exte */ public void testCountingInMultipleThreads() { try { - final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) + final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; ++i) ai.set(i, COUNTDOWN); Counter c1 = new Counter(ai); Counter c2 = new Counter(ai); @@ -308,7 +308,7 @@ public class AtomicIntegerArrayTest exte t2.join(); assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); } - catch(InterruptedException ie) { + catch (InterruptedException ie) { unexpectedException(); } } @@ -318,8 +318,8 @@ public class AtomicIntegerArrayTest exte * a deserialized serialized array holds same values */ public void testSerialization() { - AtomicIntegerArray l = new AtomicIntegerArray(SIZE); - for (int i = 0; i < SIZE; ++i) + AtomicIntegerArray l = new AtomicIntegerArray(SIZE); + for (int i = 0; i < SIZE; ++i) l.set(i, -i); try { @@ -334,7 +334,7 @@ public class AtomicIntegerArrayTest exte for (int i = 0; i < SIZE; ++i) { assertEquals(l.get(i), r.get(i)); } - } catch(Exception e){ + } catch (Exception e) { e.printStackTrace(); unexpectedException(); } @@ -343,7 +343,7 @@ public class AtomicIntegerArrayTest exte /** * toString returns current value. - */ + */ public void testToString() { int[] a = { 17, 3, -42, 99, -7}; AtomicIntegerArray ai = new AtomicIntegerArray(a);