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