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 |
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{ |
13 |
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 |
/** |
27 |
* |
28 |
*/ |
29 |
public void testConstructor(){ |
30 |
try{ |
31 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
32 |
a = AtomicReferenceFieldUpdater.newUpdater |
33 |
(getClass(), Integer.class, "y"); |
34 |
shouldThrow(); |
35 |
} |
36 |
catch (RuntimeException rt) {} |
37 |
} |
38 |
|
39 |
|
40 |
/** |
41 |
* |
42 |
*/ |
43 |
public void testConstructor2(){ |
44 |
try{ |
45 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
46 |
a = AtomicReferenceFieldUpdater.newUpdater |
47 |
(getClass(), Integer.class, "z"); |
48 |
shouldThrow(); |
49 |
} |
50 |
catch (RuntimeException rt) {} |
51 |
} |
52 |
|
53 |
/** |
54 |
* |
55 |
*/ |
56 |
public void testConstructor3(){ |
57 |
try{ |
58 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
59 |
a = AtomicReferenceFieldUpdater.newUpdater |
60 |
(getClass(), Integer.class, "w"); |
61 |
shouldThrow(); |
62 |
} |
63 |
catch (RuntimeException rt) {} |
64 |
} |
65 |
|
66 |
/** |
67 |
* |
68 |
*/ |
69 |
public void testGetSet(){ |
70 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
71 |
x = one; |
72 |
assertEquals(one,a.get(this)); |
73 |
a.set(this,two); |
74 |
assertEquals(two,a.get(this)); |
75 |
a.set(this,-3); |
76 |
assertEquals(-3,a.get(this)); |
77 |
|
78 |
} |
79 |
/** |
80 |
* |
81 |
*/ |
82 |
public void testCompareAndSet(){ |
83 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
84 |
x = one; |
85 |
assertTrue(a.compareAndSet(this,one,two)); |
86 |
assertTrue(a.compareAndSet(this,two,m4)); |
87 |
assertEquals(m4,a.get(this)); |
88 |
assertFalse(a.compareAndSet(this,m5,seven)); |
89 |
assertFalse((seven == a.get(this))); |
90 |
assertTrue(a.compareAndSet(this,m4,seven)); |
91 |
assertEquals(seven,a.get(this)); |
92 |
} |
93 |
|
94 |
/** |
95 |
* |
96 |
*/ |
97 |
public void testWeakCompareAndSet(){ |
98 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
99 |
x = one; |
100 |
while(!a.weakCompareAndSet(this,one,two)); |
101 |
while(!a.weakCompareAndSet(this,two,m4)); |
102 |
assertEquals(m4,a.get(this)); |
103 |
while(!a.weakCompareAndSet(this,m4,seven)); |
104 |
assertEquals(seven,a.get(this)); |
105 |
} |
106 |
|
107 |
/** |
108 |
* |
109 |
*/ |
110 |
public void testGetAndSet(){ |
111 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
112 |
x = one; |
113 |
assertEquals(one,a.getAndSet(this, zero)); |
114 |
assertEquals(zero,a.getAndSet(this,m10)); |
115 |
assertEquals(m10,a.getAndSet(this,1)); |
116 |
} |
117 |
|
118 |
} |