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 AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
dl |
1.1 |
volatile int x = 0; |
14 |
dl |
1.4 |
int w; |
15 |
dl |
1.2 |
long z; |
16 |
dl |
1.1 |
public static void main(String[] args){ |
17 |
|
|
junit.textui.TestRunner.run(suite()); |
18 |
|
|
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
dl |
1.3 |
/** |
24 |
dl |
1.4 |
* Contruction with non-existent field throws RuntimeException |
25 |
dl |
1.3 |
*/ |
26 |
|
|
public void testConstructor() { |
27 |
dl |
1.1 |
try{ |
28 |
|
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
29 |
|
|
a = AtomicIntegerFieldUpdater.newUpdater |
30 |
dl |
1.5 |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
31 |
dl |
1.3 |
shouldThrow(); |
32 |
dl |
1.2 |
} |
33 |
|
|
catch (RuntimeException rt) {} |
34 |
|
|
} |
35 |
|
|
|
36 |
dl |
1.3 |
/** |
37 |
dl |
1.4 |
* construction with field not of given type throws RuntimeException |
38 |
dl |
1.3 |
*/ |
39 |
|
|
public void testConstructor2() { |
40 |
dl |
1.2 |
try{ |
41 |
|
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
42 |
|
|
a = AtomicIntegerFieldUpdater.newUpdater |
43 |
dl |
1.5 |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
44 |
dl |
1.3 |
shouldThrow(); |
45 |
dl |
1.1 |
} |
46 |
|
|
catch (RuntimeException rt) {} |
47 |
|
|
} |
48 |
|
|
|
49 |
dl |
1.3 |
/** |
50 |
dl |
1.4 |
* construction with non-volatile field throws RuntimeException |
51 |
|
|
*/ |
52 |
|
|
public void testConstructor3() { |
53 |
|
|
try{ |
54 |
|
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
55 |
|
|
a = AtomicIntegerFieldUpdater.newUpdater |
56 |
dl |
1.5 |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
57 |
dl |
1.4 |
shouldThrow(); |
58 |
|
|
} |
59 |
|
|
catch (RuntimeException rt) {} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* get returns the last value set or assigned |
64 |
dl |
1.3 |
*/ |
65 |
|
|
public void testGetSet() { |
66 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
67 |
dl |
1.1 |
x = 1; |
68 |
|
|
assertEquals(1,a.get(this)); |
69 |
|
|
a.set(this,2); |
70 |
|
|
assertEquals(2,a.get(this)); |
71 |
|
|
a.set(this,-3); |
72 |
|
|
assertEquals(-3,a.get(this)); |
73 |
|
|
|
74 |
|
|
} |
75 |
dl |
1.3 |
/** |
76 |
dl |
1.4 |
* compareAndSet succeeds in changing value if equal to expected else fails |
77 |
dl |
1.3 |
*/ |
78 |
|
|
public void testCompareAndSet() { |
79 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
80 |
dl |
1.1 |
x = 1; |
81 |
|
|
assertTrue(a.compareAndSet(this,1,2)); |
82 |
|
|
assertTrue(a.compareAndSet(this,2,-4)); |
83 |
|
|
assertEquals(-4,a.get(this)); |
84 |
|
|
assertFalse(a.compareAndSet(this,-5,7)); |
85 |
|
|
assertFalse((7 == a.get(this))); |
86 |
|
|
assertTrue(a.compareAndSet(this,-4,7)); |
87 |
|
|
assertEquals(7,a.get(this)); |
88 |
|
|
} |
89 |
|
|
|
90 |
dl |
1.4 |
|
91 |
|
|
/** |
92 |
|
|
* compareAndSet in one thread enables another waiting for value |
93 |
|
|
* to succeed |
94 |
|
|
*/ |
95 |
|
|
public void testCompareAndSetInMultipleThreads() { |
96 |
|
|
x = 1; |
97 |
dl |
1.5 |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
98 |
dl |
1.4 |
|
99 |
|
|
Thread t = new Thread(new Runnable() { |
100 |
|
|
public void run() { |
101 |
|
|
while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
102 |
|
|
}}); |
103 |
|
|
try { |
104 |
|
|
t.start(); |
105 |
|
|
assertTrue(a.compareAndSet(this, 1, 2)); |
106 |
|
|
t.join(LONG_DELAY_MS); |
107 |
|
|
assertFalse(t.isAlive()); |
108 |
|
|
assertEquals(a.get(this), 3); |
109 |
|
|
} |
110 |
|
|
catch(Exception e) { |
111 |
|
|
unexpectedException(); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
dl |
1.3 |
/** |
116 |
dl |
1.4 |
* repeated weakCompareAndSet succeeds in changing value when equal |
117 |
|
|
* to expected |
118 |
dl |
1.3 |
*/ |
119 |
|
|
public void testWeakCompareAndSet() { |
120 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
121 |
dl |
1.1 |
x = 1; |
122 |
|
|
while(!a.weakCompareAndSet(this,1,2)); |
123 |
|
|
while(!a.weakCompareAndSet(this,2,-4)); |
124 |
|
|
assertEquals(-4,a.get(this)); |
125 |
|
|
while(!a.weakCompareAndSet(this,-4,7)); |
126 |
|
|
assertEquals(7,a.get(this)); |
127 |
|
|
} |
128 |
|
|
|
129 |
dl |
1.3 |
/** |
130 |
dl |
1.4 |
* getAndSet returns previous value and sets to given value |
131 |
dl |
1.3 |
*/ |
132 |
|
|
public void testGetAndSet() { |
133 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
134 |
dl |
1.1 |
x = 1; |
135 |
|
|
assertEquals(1,a.getAndSet(this, 0)); |
136 |
|
|
assertEquals(0,a.getAndSet(this,-10)); |
137 |
|
|
assertEquals(-10,a.getAndSet(this,1)); |
138 |
|
|
} |
139 |
|
|
|
140 |
dl |
1.3 |
/** |
141 |
dl |
1.4 |
* getAndAdd returns previous value and adds given value |
142 |
dl |
1.3 |
*/ |
143 |
|
|
public void testGetAndAdd() { |
144 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
145 |
dl |
1.1 |
x = 1; |
146 |
|
|
assertEquals(1,a.getAndAdd(this,2)); |
147 |
|
|
assertEquals(3,a.get(this)); |
148 |
|
|
assertEquals(3,a.getAndAdd(this,-4)); |
149 |
|
|
assertEquals(-1,a.get(this)); |
150 |
|
|
} |
151 |
|
|
|
152 |
dl |
1.3 |
/** |
153 |
dl |
1.4 |
* getAndDecrement returns previous value and decrements |
154 |
dl |
1.3 |
*/ |
155 |
|
|
public void testGetAndDecrement() { |
156 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
157 |
dl |
1.1 |
x = 1; |
158 |
|
|
assertEquals(1,a.getAndDecrement(this)); |
159 |
|
|
assertEquals(0,a.getAndDecrement(this)); |
160 |
|
|
assertEquals(-1,a.getAndDecrement(this)); |
161 |
|
|
} |
162 |
|
|
|
163 |
dl |
1.3 |
/** |
164 |
dl |
1.4 |
* getAndIncrement returns previous value and increments |
165 |
dl |
1.3 |
*/ |
166 |
|
|
public void testGetAndIncrement() { |
167 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
168 |
dl |
1.1 |
x = 1; |
169 |
|
|
assertEquals(1,a.getAndIncrement(this)); |
170 |
|
|
assertEquals(2,a.get(this)); |
171 |
|
|
a.set(this,-2); |
172 |
|
|
assertEquals(-2,a.getAndIncrement(this)); |
173 |
|
|
assertEquals(-1,a.getAndIncrement(this)); |
174 |
|
|
assertEquals(0,a.getAndIncrement(this)); |
175 |
|
|
assertEquals(1,a.get(this)); |
176 |
|
|
} |
177 |
|
|
|
178 |
dl |
1.3 |
/** |
179 |
dl |
1.4 |
* addAndGet adds given value to current, and returns current value |
180 |
dl |
1.3 |
*/ |
181 |
|
|
public void testAddAndGet() { |
182 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
183 |
dl |
1.1 |
x = 1; |
184 |
|
|
assertEquals(3,a.addAndGet(this,2)); |
185 |
|
|
assertEquals(3,a.get(this)); |
186 |
|
|
assertEquals(-1,a.addAndGet(this,-4)); |
187 |
|
|
assertEquals(-1,a.get(this)); |
188 |
|
|
} |
189 |
|
|
|
190 |
dl |
1.3 |
/** |
191 |
dl |
1.4 |
* decrementAndGet decrements and returns current value |
192 |
dl |
1.3 |
*/ |
193 |
|
|
public void testDecrementAndGet() { |
194 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
195 |
dl |
1.1 |
x = 1; |
196 |
|
|
assertEquals(0,a.decrementAndGet(this)); |
197 |
|
|
assertEquals(-1,a.decrementAndGet(this)); |
198 |
|
|
assertEquals(-2,a.decrementAndGet(this)); |
199 |
|
|
assertEquals(-2,a.get(this)); |
200 |
|
|
} |
201 |
|
|
|
202 |
dl |
1.3 |
/** |
203 |
dl |
1.4 |
* incrementAndGet increments and returns current value |
204 |
dl |
1.3 |
*/ |
205 |
|
|
public void testIncrementAndGet() { |
206 |
dl |
1.5 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
207 |
dl |
1.1 |
x = 1; |
208 |
|
|
assertEquals(2,a.incrementAndGet(this)); |
209 |
|
|
assertEquals(2,a.get(this)); |
210 |
|
|
a.set(this,-2); |
211 |
|
|
assertEquals(-1,a.incrementAndGet(this)); |
212 |
|
|
assertEquals(0,a.incrementAndGet(this)); |
213 |
|
|
assertEquals(1,a.incrementAndGet(this)); |
214 |
|
|
assertEquals(1,a.get(this)); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
} |