7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicReferenceTest extends TestCase { |
12 |
> |
public class AtomicReferenceTest extends JSR166TestCase { |
13 |
|
public static void main (String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
|
return new TestSuite(AtomicReferenceTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
static final Integer zero = new Integer(0); |
21 |
< |
static final Integer one = new Integer(1); |
22 |
< |
static final Integer two = new Integer(2); |
22 |
< |
static final Integer m3 = new Integer(-3); |
23 |
< |
static final Integer m4 = new Integer(-4); |
24 |
< |
static final Integer m5 = new Integer(-5); |
25 |
< |
static final Integer seven = new Integer(7); |
26 |
< |
static final Integer m10 = new Integer(-10); |
27 |
< |
|
20 |
> |
/** |
21 |
> |
* constructor initializes to given value |
22 |
> |
*/ |
23 |
|
public void testConstructor(){ |
24 |
|
AtomicReference ai = new AtomicReference(one); |
25 |
|
assertEquals(one,ai.get()); |
26 |
|
} |
27 |
|
|
28 |
+ |
/** |
29 |
+ |
* default constructed initializes to null |
30 |
+ |
*/ |
31 |
|
public void testConstructor2(){ |
32 |
|
AtomicReference ai = new AtomicReference(); |
33 |
|
assertNull(ai.get()); |
34 |
|
} |
35 |
|
|
36 |
+ |
/** |
37 |
+ |
* get returns the last value set |
38 |
+ |
*/ |
39 |
|
public void testGetSet(){ |
40 |
|
AtomicReference ai = new AtomicReference(one); |
41 |
|
assertEquals(one,ai.get()); |
45 |
|
assertEquals(m3,ai.get()); |
46 |
|
|
47 |
|
} |
48 |
+ |
/** |
49 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
50 |
+ |
*/ |
51 |
|
public void testCompareAndSet(){ |
52 |
|
AtomicReference ai = new AtomicReference(one); |
53 |
|
assertTrue(ai.compareAndSet(one,two)); |
59 |
|
assertEquals(seven,ai.get()); |
60 |
|
} |
61 |
|
|
62 |
+ |
/** |
63 |
+ |
* compareAndSet in one thread enables another waiting for value |
64 |
+ |
* to succeed |
65 |
+ |
*/ |
66 |
+ |
public void testCompareAndSetInMultipleThreads() { |
67 |
+ |
final AtomicReference ai = new AtomicReference(one); |
68 |
+ |
Thread t = new Thread(new Runnable() { |
69 |
+ |
public void run() { |
70 |
+ |
while(!ai.compareAndSet(two, three)) Thread.yield(); |
71 |
+ |
}}); |
72 |
+ |
try { |
73 |
+ |
t.start(); |
74 |
+ |
assertTrue(ai.compareAndSet(one, two)); |
75 |
+ |
t.join(LONG_DELAY_MS); |
76 |
+ |
assertFalse(t.isAlive()); |
77 |
+ |
assertEquals(ai.get(), three); |
78 |
+ |
} |
79 |
+ |
catch(Exception e) { |
80 |
+ |
unexpectedException(); |
81 |
+ |
} |
82 |
+ |
} |
83 |
+ |
|
84 |
+ |
/** |
85 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
86 |
+ |
* to expected |
87 |
+ |
*/ |
88 |
|
public void testWeakCompareAndSet(){ |
89 |
|
AtomicReference ai = new AtomicReference(one); |
90 |
|
while(!ai.weakCompareAndSet(one,two)); |
94 |
|
assertEquals(seven,ai.get()); |
95 |
|
} |
96 |
|
|
97 |
+ |
/** |
98 |
+ |
* getAndSet returns previous value and sets to given value |
99 |
+ |
*/ |
100 |
|
public void testGetAndSet(){ |
101 |
|
AtomicReference ai = new AtomicReference(one); |
102 |
|
assertEquals(one,ai.getAndSet(zero)); |
104 |
|
assertEquals(m10,ai.getAndSet(one)); |
105 |
|
} |
106 |
|
|
107 |
+ |
/** |
108 |
+ |
* a deserialized serialized atomic holds same value |
109 |
+ |
*/ |
110 |
+ |
public void testSerialization() { |
111 |
+ |
AtomicReference l = new AtomicReference(); |
112 |
+ |
|
113 |
+ |
try { |
114 |
+ |
l.set(one); |
115 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
116 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
117 |
+ |
out.writeObject(l); |
118 |
+ |
out.close(); |
119 |
+ |
|
120 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
121 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
122 |
+ |
AtomicReference r = (AtomicReference) in.readObject(); |
123 |
+ |
assertEquals(l.get(), r.get()); |
124 |
+ |
} catch(Exception e){ |
125 |
+ |
unexpectedException(); |
126 |
+ |
} |
127 |
+ |
} |
128 |
+ |
|
129 |
|
} |
130 |
|
|