1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import java.util.concurrent.atomic.*; |
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
|
12 |
dl |
1.2 |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ |
13 |
dl |
1.1 |
volatile Integer x = null; |
14 |
|
|
Object z; |
15 |
|
|
Integer w; |
16 |
|
|
|
17 |
|
|
public static void main(String[] args){ |
18 |
|
|
junit.textui.TestRunner.run(suite()); |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
public static Test suite() { |
23 |
|
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
public void testConstructor(){ |
27 |
|
|
try{ |
28 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
29 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater |
30 |
|
|
(getClass(), Integer.class, "y"); |
31 |
|
|
fail("Exception not thrown"); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
catch (RuntimeException rt) {} |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
public void testConstructor2(){ |
39 |
|
|
try{ |
40 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
41 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater |
42 |
|
|
(getClass(), Integer.class, "z"); |
43 |
|
|
fail("Exception not thrown"); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
catch (RuntimeException rt) {} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
public void testConstructor3(){ |
50 |
|
|
try{ |
51 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
52 |
|
|
a = AtomicReferenceFieldUpdater.newUpdater |
53 |
|
|
(getClass(), Integer.class, "w"); |
54 |
|
|
fail("Exception not thrown"); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
catch (RuntimeException rt) {} |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
public void testGetSet(){ |
61 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
62 |
|
|
x = one; |
63 |
|
|
assertEquals(one,a.get(this)); |
64 |
|
|
a.set(this,two); |
65 |
|
|
assertEquals(two,a.get(this)); |
66 |
|
|
a.set(this,-3); |
67 |
|
|
assertEquals(-3,a.get(this)); |
68 |
|
|
|
69 |
|
|
} |
70 |
|
|
public void testCompareAndSet(){ |
71 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
72 |
|
|
x = one; |
73 |
|
|
assertTrue(a.compareAndSet(this,one,two)); |
74 |
|
|
assertTrue(a.compareAndSet(this,two,m4)); |
75 |
|
|
assertEquals(m4,a.get(this)); |
76 |
|
|
assertFalse(a.compareAndSet(this,m5,seven)); |
77 |
|
|
assertFalse((seven == a.get(this))); |
78 |
|
|
assertTrue(a.compareAndSet(this,m4,seven)); |
79 |
|
|
assertEquals(seven,a.get(this)); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
public void testWeakCompareAndSet(){ |
83 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
84 |
|
|
x = one; |
85 |
|
|
while(!a.weakCompareAndSet(this,one,two)); |
86 |
|
|
while(!a.weakCompareAndSet(this,two,m4)); |
87 |
|
|
assertEquals(m4,a.get(this)); |
88 |
|
|
while(!a.weakCompareAndSet(this,m4,seven)); |
89 |
|
|
assertEquals(seven,a.get(this)); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
public void testGetAndSet(){ |
93 |
|
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
94 |
|
|
x = one; |
95 |
|
|
assertEquals(one,a.getAndSet(this, zero)); |
96 |
|
|
assertEquals(zero,a.getAndSet(this,m10)); |
97 |
|
|
assertEquals(m10,a.getAndSet(this,1)); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
} |