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