You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/24 05:42:33 UTC
svn commit: r434296 [12/19] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/concurrent/ modules/concurrent/.settings/
modules/concurrent/META-INF/ modules/concurrent/make/
modules/concurrent/src/ modules/concurrent/src/main/ modules/conc...
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerFieldUpdaterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerFieldUpdaterTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerFieldUpdaterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerFieldUpdaterTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,273 @@
+/*
+ * 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.
+ */
+
+import java.util.concurrent.atomic.*;
+import junit.framework.*;
+import java.util.*;
+
+public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
+ volatile int x = 0;
+ int w;
+ long z;
+ public static void main(String[] args){
+ junit.textui.TestRunner.run(suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
+ }
+
+ /**
+ * Construction with non-existent field throws RuntimeException
+ */
+ public void testConstructor() {
+ try{
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
+ a = AtomicIntegerFieldUpdater.newUpdater
+ (AtomicIntegerFieldUpdaterTest.class, "y");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * construction with field not of given type throws RuntimeException
+ */
+ public void testConstructor2() {
+ try{
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
+ a = AtomicIntegerFieldUpdater.newUpdater
+ (AtomicIntegerFieldUpdaterTest.class, "z");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * construction with non-volatile field throws RuntimeException
+ */
+ public void testConstructor3() {
+ try{
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
+ a = AtomicIntegerFieldUpdater.newUpdater
+ (AtomicIntegerFieldUpdaterTest.class, "w");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * get returns the last value set or assigned
+ */
+ public void testGetSet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.get(this));
+ a.set(this,2);
+ assertEquals(2,a.get(this));
+ a.set(this,-3);
+ assertEquals(-3,a.get(this));
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertTrue(a.compareAndSet(this,1,2));
+ assertTrue(a.compareAndSet(this,2,-4));
+ assertEquals(-4,a.get(this));
+ assertFalse(a.compareAndSet(this,-5,7));
+ assertFalse((7 == a.get(this)));
+ assertTrue(a.compareAndSet(this,-4,7));
+ assertEquals(7,a.get(this));
+ }
+
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ x = 1;
+ final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(a.compareAndSet(this, 1, 2));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(a.get(this), 3);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ while(!a.weakCompareAndSet(this,1,2));
+ while(!a.weakCompareAndSet(this,2,-4));
+ assertEquals(-4,a.get(this));
+ while(!a.weakCompareAndSet(this,-4,7));
+ assertEquals(7,a.get(this));
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndSet(this, 0));
+ assertEquals(0,a.getAndSet(this,-10));
+ assertEquals(-10,a.getAndSet(this,1));
+ }
+
+ /**
+ * getAndAdd returns previous value and adds given value
+ */
+ public void testGetAndAdd() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndAdd(this,2));
+ assertEquals(3,a.get(this));
+ assertEquals(3,a.getAndAdd(this,-4));
+ assertEquals(-1,a.get(this));
+ }
+
+ /**
+ * getAndDecrement returns previous value and decrements
+ */
+ public void testGetAndDecrement() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndDecrement(this));
+ assertEquals(0,a.getAndDecrement(this));
+ assertEquals(-1,a.getAndDecrement(this));
+ }
+
+ /**
+ * getAndIncrement returns previous value and increments
+ */
+ public void testGetAndIncrement() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndIncrement(this));
+ assertEquals(2,a.get(this));
+ a.set(this,-2);
+ assertEquals(-2,a.getAndIncrement(this));
+ assertEquals(-1,a.getAndIncrement(this));
+ assertEquals(0,a.getAndIncrement(this));
+ assertEquals(1,a.get(this));
+ }
+
+ /**
+ * addAndGet adds given value to current, and returns current value
+ */
+ public void testAddAndGet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(3,a.addAndGet(this,2));
+ assertEquals(3,a.get(this));
+ assertEquals(-1,a.addAndGet(this,-4));
+ assertEquals(-1,a.get(this));
+ }
+
+ /**
+ * decrementAndGet decrements and returns current value
+ */
+ public void testDecrementAndGet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(0,a.decrementAndGet(this));
+ assertEquals(-1,a.decrementAndGet(this));
+ assertEquals(-2,a.decrementAndGet(this));
+ assertEquals(-2,a.get(this));
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndGet() {
+ AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
+ try {
+ a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(2,a.incrementAndGet(this));
+ assertEquals(2,a.get(this));
+ a.set(this,-2);
+ assertEquals(-1,a.incrementAndGet(this));
+ assertEquals(0,a.incrementAndGet(this));
+ assertEquals(1,a.incrementAndGet(this));
+ assertEquals(1,a.get(this));
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerFieldUpdaterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,259 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+
+public class AtomicIntegerTest extends JSR166TestCase {
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicIntegerTest.class);
+ }
+
+ /**
+ * constructor initializes to given value
+ */
+ public void testConstructor(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor2(){
+ AtomicInteger ai = new AtomicInteger();
+ assertEquals(0,ai.get());
+ }
+
+ /**
+ * get returns the last value set
+ */
+ public void testGetSet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.get());
+ ai.set(2);
+ assertEquals(2,ai.get());
+ ai.set(-3);
+ assertEquals(-3,ai.get());
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertTrue(ai.compareAndSet(1,2));
+ assertTrue(ai.compareAndSet(2,-4));
+ assertEquals(-4,ai.get());
+ assertFalse(ai.compareAndSet(-5,7));
+ assertFalse((7 == ai.get()));
+ assertTrue(ai.compareAndSet(-4,7));
+ assertEquals(7,ai.get());
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicInteger ai = new AtomicInteger(1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(2, 3)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(1, 2));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.get(), 3);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ while(!ai.weakCompareAndSet(1,2));
+ while(!ai.weakCompareAndSet(2,-4));
+ assertEquals(-4,ai.get());
+ while(!ai.weakCompareAndSet(-4,7));
+ assertEquals(7,ai.get());
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.getAndSet(0));
+ assertEquals(0,ai.getAndSet(-10));
+ assertEquals(-10,ai.getAndSet(1));
+ }
+
+ /**
+ * getAndAdd returns previous value and adds given value
+ */
+ public void testGetAndAdd(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.getAndAdd(2));
+ assertEquals(3,ai.get());
+ assertEquals(3,ai.getAndAdd(-4));
+ assertEquals(-1,ai.get());
+ }
+
+ /**
+ * getAndDecrement returns previous value and decrements
+ */
+ public void testGetAndDecrement(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.getAndDecrement());
+ assertEquals(0,ai.getAndDecrement());
+ assertEquals(-1,ai.getAndDecrement());
+ }
+
+ /**
+ * getAndIncrement returns previous value and increments
+ */
+ public void testGetAndIncrement(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(1,ai.getAndIncrement());
+ assertEquals(2,ai.get());
+ ai.set(-2);
+ assertEquals(-2,ai.getAndIncrement());
+ assertEquals(-1,ai.getAndIncrement());
+ assertEquals(0,ai.getAndIncrement());
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * addAndGet adds given value to current, and returns current value
+ */
+ public void testAddAndGet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(3,ai.addAndGet(2));
+ assertEquals(3,ai.get());
+ assertEquals(-1,ai.addAndGet(-4));
+ assertEquals(-1,ai.get());
+ }
+
+ /**
+ * decrementAndGet decrements and returns current value
+ */
+ public void testDecrementAndGet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(0,ai.decrementAndGet());
+ assertEquals(-1,ai.decrementAndGet());
+ assertEquals(-2,ai.decrementAndGet());
+ assertEquals(-2,ai.get());
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndGet(){
+ AtomicInteger ai = new AtomicInteger(1);
+ assertEquals(2,ai.incrementAndGet());
+ assertEquals(2,ai.get());
+ ai.set(-2);
+ assertEquals(-1,ai.incrementAndGet());
+ assertEquals(0,ai.incrementAndGet());
+ assertEquals(1,ai.incrementAndGet());
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * a deserialized serialized atomic holds same value
+ */
+ public void testSerialization() {
+ AtomicInteger l = new AtomicInteger();
+
+ try {
+ l.set(22);
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(l);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ AtomicInteger r = (AtomicInteger) in.readObject();
+ assertEquals(l.get(), r.get());
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ AtomicInteger ai = new AtomicInteger();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals(ai.toString(), Integer.toString(i));
+ }
+ }
+
+ /**
+ * intValue returns current value.
+ */
+ public void testIntValue() {
+ AtomicInteger ai = new AtomicInteger();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals(i, ai.intValue());
+ }
+ }
+
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ AtomicInteger ai = new AtomicInteger();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((long)i, ai.longValue());
+ }
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ AtomicInteger ai = new AtomicInteger();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((float)i, ai.floatValue());
+ }
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ AtomicInteger ai = new AtomicInteger();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((double)i, ai.doubleValue());
+ }
+ }
+
+
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicIntegerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongArrayTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongArrayTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongArrayTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongArrayTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,334 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+import java.util.*;
+
+public class AtomicLongArrayTest extends JSR166TestCase {
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicLongArrayTest.class);
+ }
+
+ /**
+ * constructor creates array of given size with all elements zero
+ */
+ public void testConstructor(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i)
+ assertEquals(0,ai.get(i));
+ }
+
+ /**
+ * constructor with null array throws NPE
+ */
+ public void testConstructor2NPE() {
+ try {
+ long[] a = null;
+ AtomicLongArray ai = new AtomicLongArray(a);
+ } catch (NullPointerException success) {
+ } catch (Exception ex) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * constructor with array is of same size and has all elements
+ */
+ public void testConstructor2() {
+ long[] a = { 17L, 3L, -42L, 99L, -7L};
+ AtomicLongArray ai = new AtomicLongArray(a);
+ assertEquals(a.length, ai.length());
+ 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(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ try {
+ ai.get(SIZE);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.get(-1);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.set(SIZE, 0);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.set(-1, 0);
+ } catch(IndexOutOfBoundsException success){
+ }
+ }
+
+ /**
+ * get returns the last value set at index
+ */
+ public void testGetSet(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(1,ai.get(i));
+ ai.set(i, 2);
+ assertEquals(2,ai.get(i));
+ ai.set(i, -3);
+ assertEquals(-3,ai.get(i));
+ }
+ }
+
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertTrue(ai.compareAndSet(i, 1,2));
+ assertTrue(ai.compareAndSet(i, 2,-4));
+ assertEquals(-4,ai.get(i));
+ assertFalse(ai.compareAndSet(i, -5,7));
+ assertFalse((7 == ai.get(i)));
+ assertTrue(ai.compareAndSet(i, -4,7));
+ assertEquals(7,ai.get(i));
+ }
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicLongArray a = new AtomicLongArray(1);
+ a.set(0, 1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!a.compareAndSet(0, 2, 3)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(a.compareAndSet(0, 1, 2));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(a.get(0), 3);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ while(!ai.weakCompareAndSet(i, 1,2));
+ while(!ai.weakCompareAndSet(i, 2,-4));
+ assertEquals(-4,ai.get(i));
+ while(!ai.weakCompareAndSet(i, -4,7));
+ assertEquals(7,ai.get(i));
+ }
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value at given index
+ */
+ public void testGetAndSet(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(1,ai.getAndSet(i,0));
+ assertEquals(0,ai.getAndSet(i,-10));
+ assertEquals(-10,ai.getAndSet(i,1));
+ }
+ }
+
+ /**
+ * getAndAdd returns previous value and adds given value
+ */
+ public void testGetAndAdd(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(1,ai.getAndAdd(i,2));
+ assertEquals(3,ai.get(i));
+ assertEquals(3,ai.getAndAdd(i,-4));
+ assertEquals(-1,ai.get(i));
+ }
+ }
+
+ /**
+ * getAndDecrement returns previous value and decrements
+ */
+ public void testGetAndDecrement(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(1,ai.getAndDecrement(i));
+ assertEquals(0,ai.getAndDecrement(i));
+ assertEquals(-1,ai.getAndDecrement(i));
+ }
+ }
+
+ /**
+ * getAndIncrement returns previous value and increments
+ */
+ public void testGetAndIncrement(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(1,ai.getAndIncrement(i));
+ assertEquals(2,ai.get(i));
+ ai.set(i,-2);
+ assertEquals(-2,ai.getAndIncrement(i));
+ assertEquals(-1,ai.getAndIncrement(i));
+ assertEquals(0,ai.getAndIncrement(i));
+ assertEquals(1,ai.get(i));
+ }
+ }
+
+ /**
+ * addAndGet adds given value to current, and returns current value
+ */
+ public void testAddAndGet() {
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(3,ai.addAndGet(i,2));
+ assertEquals(3,ai.get(i));
+ assertEquals(-1,ai.addAndGet(i,-4));
+ assertEquals(-1,ai.get(i));
+ }
+ }
+
+ /**
+ * decrementAndGet decrements and returns current value
+ */
+ public void testDecrementAndGet(){
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(0,ai.decrementAndGet(i));
+ assertEquals(-1,ai.decrementAndGet(i));
+ assertEquals(-2,ai.decrementAndGet(i));
+ assertEquals(-2,ai.get(i));
+ }
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndGet() {
+ AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, 1);
+ assertEquals(2,ai.incrementAndGet(i));
+ assertEquals(2,ai.get(i));
+ ai.set(i, -2);
+ assertEquals(-1,ai.incrementAndGet(i));
+ assertEquals(0,ai.incrementAndGet(i));
+ assertEquals(1,ai.incrementAndGet(i));
+ assertEquals(1,ai.get(i));
+ }
+ }
+
+ static final long COUNTDOWN = 100000;
+
+ class Counter implements Runnable {
+ final AtomicLongArray ai;
+ volatile long counts;
+ Counter(AtomicLongArray a) { ai = a; }
+ public void run() {
+ for (;;) {
+ boolean done = true;
+ for (int i = 0; i < ai.length(); ++i) {
+ long v = ai.get(i);
+ threadAssertTrue(v >= 0);
+ if (v != 0) {
+ done = false;
+ if (ai.compareAndSet(i, v, v-1))
+ ++counts;
+ }
+ }
+ if (done)
+ break;
+ }
+ }
+ }
+
+ /**
+ * Multiple threads using same array of counters successfully
+ * update a number of times equal to total count
+ */
+ public void testCountingInMultipleThreads() {
+ try {
+ final AtomicLongArray ai = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i)
+ ai.set(i, COUNTDOWN);
+ Counter c1 = new Counter(ai);
+ Counter c2 = new Counter(ai);
+ Thread t1 = new Thread(c1);
+ Thread t2 = new Thread(c2);
+ t1.start();
+ t2.start();
+ t1.join();
+ t2.join();
+ assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
+ }
+ catch(InterruptedException ie) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * a deserialized serialized array holds same values
+ */
+ public void testSerialization() {
+ AtomicLongArray l = new AtomicLongArray(SIZE);
+ for (int i = 0; i < SIZE; ++i)
+ l.set(i, -i);
+
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(l);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ AtomicLongArray r = (AtomicLongArray) in.readObject();
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(l.get(i), r.get(i));
+ }
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ long[] a = { 17, 3, -42, 99, -7};
+ AtomicLongArray ai = new AtomicLongArray(a);
+ assertEquals(Arrays.toString(a), ai.toString());
+ }
+
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongArrayTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongFieldUpdaterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongFieldUpdaterTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongFieldUpdaterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongFieldUpdaterTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,275 @@
+/*
+ * 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.
+ */
+
+import java.util.concurrent.atomic.*;
+import junit.framework.*;
+import java.util.*;
+
+public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
+ volatile long x = 0;
+ int z;
+ long w;
+
+ public static void main(String[] args){
+ junit.textui.TestRunner.run(suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicLongFieldUpdaterTest.class);
+ }
+
+ /**
+ * Construction with non-existent field throws RuntimeException
+ */
+ public void testConstructor(){
+ try{
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
+ a = AtomicLongFieldUpdater.newUpdater
+ (AtomicLongFieldUpdaterTest.class, "y");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * construction with field not of given type throws RuntimeException
+ */
+ public void testConstructor2(){
+ try{
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
+ a = AtomicLongFieldUpdater.newUpdater
+ (AtomicLongFieldUpdaterTest.class, "z");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * construction with non-volatile field throws RuntimeException
+ */
+ public void testConstructor3(){
+ try{
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
+ a = AtomicLongFieldUpdater.newUpdater
+ (AtomicLongFieldUpdaterTest.class, "w");
+ shouldThrow();
+ }
+
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * get returns the last value set or assigned
+ */
+ public void testGetSet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.get(this));
+ a.set(this,2);
+ assertEquals(2,a.get(this));
+ a.set(this,-3);
+ assertEquals(-3,a.get(this));
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertTrue(a.compareAndSet(this,1,2));
+ assertTrue(a.compareAndSet(this,2,-4));
+ assertEquals(-4,a.get(this));
+ assertFalse(a.compareAndSet(this,-5,7));
+ assertFalse((7 == a.get(this)));
+ assertTrue(a.compareAndSet(this,-4,7));
+ assertEquals(7,a.get(this));
+ }
+
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ x = 1;
+ final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(a.compareAndSet(this, 1, 2));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(a.get(this), 3);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ while(!a.weakCompareAndSet(this,1,2));
+ while(!a.weakCompareAndSet(this,2,-4));
+ assertEquals(-4,a.get(this));
+ while(!a.weakCompareAndSet(this,-4,7));
+ assertEquals(7,a.get(this));
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndSet(this, 0));
+ assertEquals(0,a.getAndSet(this,-10));
+ assertEquals(-10,a.getAndSet(this,1));
+ }
+
+ /**
+ * getAndAdd returns previous value and adds given value
+ */
+ public void testGetAndAdd(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndAdd(this,2));
+ assertEquals(3,a.get(this));
+ assertEquals(3,a.getAndAdd(this,-4));
+ assertEquals(-1,a.get(this));
+ }
+
+ /**
+ * getAndDecrement returns previous value and decrements
+ */
+ public void testGetAndDecrement(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndDecrement(this));
+ assertEquals(0,a.getAndDecrement(this));
+ assertEquals(-1,a.getAndDecrement(this));
+ }
+
+ /**
+ * getAndIncrement returns previous value and increments
+ */
+ public void testGetAndIncrement(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(1,a.getAndIncrement(this));
+ assertEquals(2,a.get(this));
+ a.set(this,-2);
+ assertEquals(-2,a.getAndIncrement(this));
+ assertEquals(-1,a.getAndIncrement(this));
+ assertEquals(0,a.getAndIncrement(this));
+ assertEquals(1,a.get(this));
+ }
+
+ /**
+ * addAndGet adds given value to current, and returns current value
+ */
+ public void testAddAndGet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(3,a.addAndGet(this,2));
+ assertEquals(3,a.get(this));
+ assertEquals(-1,a.addAndGet(this,-4));
+ assertEquals(-1,a.get(this));
+ }
+
+ /**
+ * decrementAndGet decrements and returns current value
+ */
+ public void testDecrementAndGet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(0,a.decrementAndGet(this));
+ assertEquals(-1,a.decrementAndGet(this));
+ assertEquals(-2,a.decrementAndGet(this));
+ assertEquals(-2,a.get(this));
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndGet(){
+ AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
+ try {
+ a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = 1;
+ assertEquals(2,a.incrementAndGet(this));
+ assertEquals(2,a.get(this));
+ a.set(this,-2);
+ assertEquals(-1,a.incrementAndGet(this));
+ assertEquals(0,a.incrementAndGet(this));
+ assertEquals(1,a.incrementAndGet(this));
+ assertEquals(1,a.get(this));
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongFieldUpdaterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,246 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+
+public class AtomicLongTest extends JSR166TestCase {
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicLongTest.class);
+ }
+
+ /**
+ * constructor initializes to given value
+ */
+ public void testConstructor(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * default constructed initializes to zero
+ */
+ public void testConstructor2(){
+ AtomicLong ai = new AtomicLong();
+ assertEquals(0,ai.get());
+ }
+
+ /**
+ * get returns the last value set
+ */
+ public void testGetSet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.get());
+ ai.set(2);
+ assertEquals(2,ai.get());
+ ai.set(-3);
+ assertEquals(-3,ai.get());
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertTrue(ai.compareAndSet(1,2));
+ assertTrue(ai.compareAndSet(2,-4));
+ assertEquals(-4,ai.get());
+ assertFalse(ai.compareAndSet(-5,7));
+ assertFalse((7 == ai.get()));
+ assertTrue(ai.compareAndSet(-4,7));
+ assertEquals(7,ai.get());
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicLong ai = new AtomicLong(1);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(2, 3)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(1, 2));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.get(), 3);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicLong ai = new AtomicLong(1);
+ while(!ai.weakCompareAndSet(1,2));
+ while(!ai.weakCompareAndSet(2,-4));
+ assertEquals(-4,ai.get());
+ while(!ai.weakCompareAndSet(-4,7));
+ assertEquals(7,ai.get());
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.getAndSet(0));
+ assertEquals(0,ai.getAndSet(-10));
+ assertEquals(-10,ai.getAndSet(1));
+ }
+
+ /**
+ * getAndAdd returns previous value and adds given value
+ */
+ public void testGetAndAdd(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.getAndAdd(2));
+ assertEquals(3,ai.get());
+ assertEquals(3,ai.getAndAdd(-4));
+ assertEquals(-1,ai.get());
+ }
+
+ /**
+ * getAndDecrement returns previous value and decrements
+ */
+ public void testGetAndDecrement(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.getAndDecrement());
+ assertEquals(0,ai.getAndDecrement());
+ assertEquals(-1,ai.getAndDecrement());
+ }
+
+ /**
+ * getAndIncrement returns previous value and increments
+ */
+ public void testGetAndIncrement(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(1,ai.getAndIncrement());
+ assertEquals(2,ai.get());
+ ai.set(-2);
+ assertEquals(-2,ai.getAndIncrement());
+ assertEquals(-1,ai.getAndIncrement());
+ assertEquals(0,ai.getAndIncrement());
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * addAndGet adds given value to current, and returns current value
+ */
+ public void testAddAndGet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(3,ai.addAndGet(2));
+ assertEquals(3,ai.get());
+ assertEquals(-1,ai.addAndGet(-4));
+ assertEquals(-1,ai.get());
+ }
+
+ /**
+ * decrementAndGet decrements and returns current value
+ */
+ public void testDecrementAndGet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(0,ai.decrementAndGet());
+ assertEquals(-1,ai.decrementAndGet());
+ assertEquals(-2,ai.decrementAndGet());
+ assertEquals(-2,ai.get());
+ }
+
+ /**
+ * incrementAndGet increments and returns current value
+ */
+ public void testIncrementAndGet(){
+ AtomicLong ai = new AtomicLong(1);
+ assertEquals(2,ai.incrementAndGet());
+ assertEquals(2,ai.get());
+ ai.set(-2);
+ assertEquals(-1,ai.incrementAndGet());
+ assertEquals(0,ai.incrementAndGet());
+ assertEquals(1,ai.incrementAndGet());
+ assertEquals(1,ai.get());
+ }
+
+ /**
+ * a deserialized serialized atomic holds same value
+ */
+ public void testSerialization() {
+ AtomicLong l = new AtomicLong();
+
+ try {
+ l.set(-22);
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(l);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ AtomicLong r = (AtomicLong) in.readObject();
+ assertEquals(l.get(), r.get());
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ AtomicLong ai = new AtomicLong();
+ for (long i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals(ai.toString(), Long.toString(i));
+ }
+ }
+
+ /**
+ * longValue returns current value.
+ */
+ public void testLongValue() {
+ AtomicLong ai = new AtomicLong();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((long)i, ai.longValue());
+ }
+ }
+
+ /**
+ * floatValue returns current value.
+ */
+ public void testFloatValue() {
+ AtomicLong ai = new AtomicLong();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((float)i, ai.floatValue());
+ }
+ }
+
+ /**
+ * doubleValue returns current value.
+ */
+ public void testDoubleValue() {
+ AtomicLong ai = new AtomicLong();
+ for (int i = -12; i < 6; ++i) {
+ ai.set(i);
+ assertEquals((double)i, ai.doubleValue());
+ }
+ }
+
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicLongTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicMarkableReferenceTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicMarkableReferenceTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicMarkableReferenceTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicMarkableReferenceTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,158 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+
+public class AtomicMarkableReferenceTest extends JSR166TestCase{
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicMarkableReferenceTest.class);
+ }
+
+ /**
+ * constructor initializes to given reference and mark
+ */
+ public void testConstructor(){
+ AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ assertEquals(one,ai.getReference());
+ assertFalse(ai.isMarked());
+ AtomicMarkableReference a2 = new AtomicMarkableReference(null, true);
+ assertNull(a2.getReference());
+ assertTrue(a2.isMarked());
+
+ }
+
+ /**
+ * get returns the last values of reference and mark set
+ */
+ public void testGetSet(){
+ boolean[] mark = new boolean[1];
+ AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ assertEquals(one,ai.getReference());
+ assertFalse(ai.isMarked());
+ assertEquals(one, ai.get(mark));
+ assertFalse(mark[0]);
+ ai.set(two, false);
+ assertEquals(two,ai.getReference());
+ assertFalse(ai.isMarked());
+ assertEquals(two, ai.get(mark));
+ assertFalse(mark[0]);
+ ai.set(one, true);
+ assertEquals(one,ai.getReference());
+ assertTrue(ai.isMarked());
+ assertEquals(one, ai.get(mark));
+ assertTrue(mark[0]);
+ }
+
+ /**
+ * attemptMark succeeds in single thread
+ */
+ public void testAttemptMark(){
+ boolean[] mark = new boolean[1];
+ AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ assertFalse(ai.isMarked());
+ assertTrue(ai.attemptMark(one, true));
+ assertTrue(ai.isMarked());
+ assertEquals(one, ai.get(mark));
+ assertTrue(mark[0]);
+ }
+
+ /**
+ * compareAndSet succeeds in changing values if equal to expected reference
+ * and mark else fails
+ */
+ public void testCompareAndSet(){
+ boolean[] mark = new boolean[1];
+ AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ assertEquals(one, ai.get(mark));
+ assertFalse(ai.isMarked());
+ assertFalse(mark[0]);
+
+ assertTrue(ai.compareAndSet(one, two, false, false));
+ assertEquals(two, ai.get(mark));
+ assertFalse(mark[0]);
+
+ assertTrue(ai.compareAndSet(two, m3, false, true));
+ assertEquals(m3, ai.get(mark));
+ assertTrue(mark[0]);
+
+ assertFalse(ai.compareAndSet(two, m3, true, true));
+ assertEquals(m3, ai.get(mark));
+ assertTrue(mark[0]);
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for reference value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(two, three, false, false)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(one, two, false, false));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.getReference(), three);
+ assertFalse(ai.isMarked());
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for mark value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads2() {
+ final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(one, one, true, false)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(one, one, false, true));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.getReference(), one);
+ assertFalse(ai.isMarked());
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing values when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ boolean[] mark = new boolean[1];
+ AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
+ assertEquals(one, ai.get(mark));
+ assertFalse(ai.isMarked());
+ assertFalse(mark[0]);
+
+ while(!ai.weakCompareAndSet(one, two, false, false));
+ assertEquals(two, ai.get(mark));
+ assertFalse(mark[0]);
+
+ while(!ai.weakCompareAndSet(two, m3, false, true));
+ assertEquals(m3, ai.get(mark));
+ assertTrue(mark[0]);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicMarkableReferenceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceArrayTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceArrayTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceArrayTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceArrayTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,201 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+import java.util.*;
+
+public class AtomicReferenceArrayTest extends JSR166TestCase
+{
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicReferenceArrayTest.class);
+ }
+
+ /**
+ * constructor creates array of given size with all elements null
+ */
+ public void testConstructor(){
+ AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ assertNull(ai.get(i));
+ }
+ }
+
+ /**
+ * constructor with null array throws NPE
+ */
+ public void testConstructor2NPE() {
+ try {
+ Integer[] a = null;
+ AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
+ } catch (NullPointerException success) {
+ } catch (Exception ex) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * constructor with array is of same size and has all elements
+ */
+ public void testConstructor2() {
+ Integer[] a = { two, one, three, four, seven};
+ AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
+ assertEquals(a.length, ai.length());
+ 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(){
+ AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
+ try {
+ ai.get(SIZE);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.get(-1);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.set(SIZE, null);
+ } catch(IndexOutOfBoundsException success){
+ }
+ try {
+ ai.set(-1, null);
+ } catch(IndexOutOfBoundsException success){
+ }
+ }
+
+ /**
+ * get returns the last value set at index
+ */
+ public void testGetSet(){
+ AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, one);
+ assertEquals(one,ai.get(i));
+ ai.set(i, two);
+ assertEquals(two,ai.get(i));
+ ai.set(i, m3);
+ assertEquals(m3,ai.get(i));
+ }
+ }
+
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, one);
+ assertTrue(ai.compareAndSet(i, one,two));
+ assertTrue(ai.compareAndSet(i, two,m4));
+ assertEquals(m4,ai.get(i));
+ assertFalse(ai.compareAndSet(i, m5,seven));
+ assertFalse((seven.equals(ai.get(i))));
+ assertTrue(ai.compareAndSet(i, m4,seven));
+ assertEquals(seven,ai.get(i));
+ }
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicReferenceArray a = new AtomicReferenceArray(1);
+ a.set(0, one);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!a.compareAndSet(0, two, three)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(a.compareAndSet(0, one, two));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(a.get(0), three);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, one);
+ while(!ai.weakCompareAndSet(i, one,two));
+ while(!ai.weakCompareAndSet(i, two,m4));
+ assertEquals(m4,ai.get(i));
+ while(!ai.weakCompareAndSet(i, m4,seven));
+ assertEquals(seven,ai.get(i));
+ }
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value at given index
+ */
+ public void testGetAndSet(){
+ AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ ai.set(i, one);
+ assertEquals(one,ai.getAndSet(i,zero));
+ assertEquals(0,ai.getAndSet(i,m10));
+ assertEquals(m10,ai.getAndSet(i,one));
+ }
+ }
+
+ /**
+ * a deserialized serialized array holds same values
+ */
+ public void testSerialization() {
+ AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
+ for (int i = 0; i < SIZE; ++i) {
+ l.set(i, new Integer(-i));
+ }
+
+ try {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(l);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
+ assertEquals(l.length(), r.length());
+ for (int i = 0; i < SIZE; ++i) {
+ assertEquals(r.get(i), l.get(i));
+ }
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ Integer[] a = { two, one, three, four, seven};
+ AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
+ assertEquals(Arrays.toString(a), ai.toString());
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceArrayTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceFieldUpdaterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceFieldUpdaterTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceFieldUpdaterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceFieldUpdaterTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+/*
+ * 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.
+ */
+
+import java.util.concurrent.atomic.*;
+import junit.framework.*;
+import java.util.*;
+
+public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
+ volatile Integer x = null;
+ Object z;
+ Integer w;
+
+ public static void main(String[] args){
+ junit.textui.TestRunner.run(suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
+ }
+
+ /**
+ * Construction with non-existent field throws RuntimeException
+ */
+ public void testConstructor(){
+ try{
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
+ a = AtomicReferenceFieldUpdater.newUpdater
+ (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+
+ /**
+ * construction with field not of given type throws RuntimeException
+ */
+ public void testConstructor2(){
+ try{
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
+ a = AtomicReferenceFieldUpdater.newUpdater
+ (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * Constructor with non-volatile field throws exception
+ */
+ public void testConstructor3(){
+ try{
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
+ a = AtomicReferenceFieldUpdater.newUpdater
+ (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
+ shouldThrow();
+ }
+ catch (RuntimeException rt) {}
+ }
+
+ /**
+ * get returns the last value set or assigned
+ */
+ public void testGetSet(){
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
+ try {
+ a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = one;
+ assertEquals(one,a.get(this));
+ a.set(this,two);
+ assertEquals(two,a.get(this));
+ a.set(this,-3);
+ assertEquals(-3,a.get(this).intValue());
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
+ try {
+ a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = one;
+ assertTrue(a.compareAndSet(this,one,two));
+ assertTrue(a.compareAndSet(this,two,m4));
+ assertEquals(m4,a.get(this));
+ assertFalse(a.compareAndSet(this,m5,seven));
+ assertFalse((seven == a.get(this)));
+ assertTrue(a.compareAndSet(this,m4,seven));
+ assertEquals(seven,a.get(this));
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ x = one;
+ final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
+ try {
+ a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(a.compareAndSet(this, one, two));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(a.get(this), three);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
+ try {
+ a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = one;
+ while(!a.weakCompareAndSet(this,one,two));
+ while(!a.weakCompareAndSet(this,two,m4));
+ assertEquals(m4,a.get(this));
+ while(!a.weakCompareAndSet(this,m4,seven));
+ assertEquals(seven,a.get(this));
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet(){
+ AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
+ try {
+ a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
+ } catch (RuntimeException ok) {
+ return;
+ }
+ x = one;
+ assertEquals(one,a.getAndSet(this, zero));
+ assertEquals(zero,a.getAndSet(this,m10));
+ assertEquals(m10,a.getAndSet(this,1));
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceFieldUpdaterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,141 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+import java.io.*;
+
+public class AtomicReferenceTest extends JSR166TestCase {
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicReferenceTest.class);
+ }
+
+ /**
+ * constructor initializes to given value
+ */
+ public void testConstructor(){
+ AtomicReference ai = new AtomicReference(one);
+ assertEquals(one,ai.get());
+ }
+
+ /**
+ * default constructed initializes to null
+ */
+ public void testConstructor2(){
+ AtomicReference ai = new AtomicReference();
+ assertNull(ai.get());
+ }
+
+ /**
+ * get returns the last value set
+ */
+ public void testGetSet(){
+ AtomicReference ai = new AtomicReference(one);
+ assertEquals(one,ai.get());
+ ai.set(two);
+ assertEquals(two,ai.get());
+ ai.set(m3);
+ assertEquals(m3,ai.get());
+
+ }
+ /**
+ * compareAndSet succeeds in changing value if equal to expected else fails
+ */
+ public void testCompareAndSet(){
+ AtomicReference ai = new AtomicReference(one);
+ assertTrue(ai.compareAndSet(one,two));
+ assertTrue(ai.compareAndSet(two,m4));
+ assertEquals(m4,ai.get());
+ assertFalse(ai.compareAndSet(m5,seven));
+ assertFalse((seven.equals(ai.get())));
+ assertTrue(ai.compareAndSet(m4,seven));
+ assertEquals(seven,ai.get());
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicReference ai = new AtomicReference(one);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(two, three)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(one, two));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.get(), three);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing value when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ AtomicReference ai = new AtomicReference(one);
+ while(!ai.weakCompareAndSet(one,two));
+ while(!ai.weakCompareAndSet(two,m4));
+ assertEquals(m4,ai.get());
+ while(!ai.weakCompareAndSet(m4,seven));
+ assertEquals(seven,ai.get());
+ }
+
+ /**
+ * getAndSet returns previous value and sets to given value
+ */
+ public void testGetAndSet(){
+ AtomicReference ai = new AtomicReference(one);
+ assertEquals(one,ai.getAndSet(zero));
+ assertEquals(zero,ai.getAndSet(m10));
+ assertEquals(m10,ai.getAndSet(one));
+ }
+
+ /**
+ * a deserialized serialized atomic holds same value
+ */
+ public void testSerialization() {
+ AtomicReference l = new AtomicReference();
+
+ try {
+ l.set(one);
+ ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
+ ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
+ out.writeObject(l);
+ out.close();
+
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
+ AtomicReference r = (AtomicReference) in.readObject();
+ assertEquals(l.get(), r.get());
+ } catch(Exception e){
+ unexpectedException();
+ }
+ }
+
+ /**
+ * toString returns current value.
+ */
+ public void testToString() {
+ AtomicReference<Integer> ai = new AtomicReference<Integer>(one);
+ assertEquals(ai.toString(), one.toString());
+ ai.set(two);
+ assertEquals(ai.toString(), two.toString());
+ }
+
+}
+
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicReferenceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicStampedReferenceTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicStampedReferenceTest.java?rev=434296&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicStampedReferenceTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicStampedReferenceTest.java Wed Aug 23 20:42:25 2006
@@ -0,0 +1,158 @@
+/*
+ * 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.
+ */
+
+import junit.framework.*;
+import java.util.concurrent.atomic.*;
+
+public class AtomicStampedReferenceTest extends JSR166TestCase{
+ public static void main (String[] args) {
+ junit.textui.TestRunner.run (suite());
+ }
+ public static Test suite() {
+ return new TestSuite(AtomicStampedReferenceTest.class);
+ }
+
+ /**
+ * constructor initializes to given reference and stamp
+ */
+ public void testConstructor(){
+ AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ assertEquals(one,ai.getReference());
+ assertEquals(0, ai.getStamp());
+ AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
+ assertNull(a2.getReference());
+ assertEquals(1, a2.getStamp());
+
+ }
+
+ /**
+ * get returns the last values of reference and stamp set
+ */
+ public void testGetSet(){
+ int[] mark = new int[1];
+ AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ assertEquals(one,ai.getReference());
+ assertEquals(0, ai.getStamp());
+ assertEquals(one, ai.get(mark));
+ assertEquals(0, mark[0]);
+ ai.set(two, 0);
+ assertEquals(two,ai.getReference());
+ assertEquals(0, ai.getStamp());
+ assertEquals(two, ai.get(mark));
+ assertEquals(0, mark[0]);
+ ai.set(one, 1);
+ assertEquals(one,ai.getReference());
+ assertEquals(1, ai.getStamp());
+ assertEquals(one, ai.get(mark));
+ assertEquals(1,mark[0]);
+ }
+
+ /**
+ * attemptStamp succeeds in single thread
+ */
+ public void testAttemptStamp(){
+ int[] mark = new int[1];
+ AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ assertEquals(0, ai.getStamp());
+ assertTrue(ai.attemptStamp(one, 1));
+ assertEquals(1, ai.getStamp());
+ assertEquals(one, ai.get(mark));
+ assertEquals(1, mark[0]);
+ }
+
+ /**
+ * compareAndSet succeeds in changing values if equal to expected reference
+ * and stamp else fails
+ */
+ public void testCompareAndSet(){
+ int[] mark = new int[1];
+ AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ assertEquals(one, ai.get(mark));
+ assertEquals(0, ai.getStamp());
+ assertEquals(0, mark[0]);
+
+ assertTrue(ai.compareAndSet(one, two, 0, 0));
+ assertEquals(two, ai.get(mark));
+ assertEquals(0, mark[0]);
+
+ assertTrue(ai.compareAndSet(two, m3, 0, 1));
+ assertEquals(m3, ai.get(mark));
+ assertEquals(1, mark[0]);
+
+ assertFalse(ai.compareAndSet(two, m3, 1, 1));
+ assertEquals(m3, ai.get(mark));
+ assertEquals(1, mark[0]);
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for reference value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads() {
+ final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(two, three, 0, 0)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(one, two, 0, 0));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.getReference(), three);
+ assertEquals(ai.getStamp(), 0);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * compareAndSet in one thread enables another waiting for stamp value
+ * to succeed
+ */
+ public void testCompareAndSetInMultipleThreads2() {
+ final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ while(!ai.compareAndSet(one, one, 1, 2)) Thread.yield();
+ }});
+ try {
+ t.start();
+ assertTrue(ai.compareAndSet(one, one, 0, 1));
+ t.join(LONG_DELAY_MS);
+ assertFalse(t.isAlive());
+ assertEquals(ai.getReference(), one);
+ assertEquals(ai.getStamp(), 2);
+ }
+ catch(Exception e) {
+ unexpectedException();
+ }
+ }
+
+ /**
+ * repeated weakCompareAndSet succeeds in changing values when equal
+ * to expected
+ */
+ public void testWeakCompareAndSet(){
+ int[] mark = new int[1];
+ AtomicStampedReference ai = new AtomicStampedReference(one, 0);
+ assertEquals(one, ai.get(mark));
+ assertEquals(0, ai.getStamp ());
+ assertEquals(0, mark[0]);
+
+ while(!ai.weakCompareAndSet(one, two, 0, 0));
+ assertEquals(two, ai.get(mark));
+ assertEquals(0, mark[0]);
+
+ while(!ai.weakCompareAndSet(two, m3, 0, 1));
+ assertEquals(m3, ai.get(mark));
+ assertEquals(1, mark[0]);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/concurrent/src/test/java/AtomicStampedReferenceTest.java
------------------------------------------------------------------------------
svn:eol-style = native