11 |
|
|
12 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile int x = 0; |
14 |
+ |
int w; |
15 |
|
long z; |
15 |
– |
|
16 |
|
public static void main(String[] args){ |
17 |
|
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
– |
|
20 |
– |
|
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* |
24 |
> |
* Contruction with non-existent field throws RuntimeException |
25 |
|
*/ |
26 |
|
public void testConstructor() { |
27 |
|
try{ |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
< |
* |
37 |
> |
* construction with field not of given type throws RuntimeException |
38 |
|
*/ |
39 |
|
public void testConstructor2() { |
40 |
|
try{ |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* |
50 |
> |
* construction with non-volatile field throws RuntimeException |
51 |
> |
*/ |
52 |
> |
public void testConstructor3() { |
53 |
> |
try{ |
54 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
55 |
> |
a = AtomicIntegerFieldUpdater.newUpdater |
56 |
> |
(getClass(), "w"); |
57 |
> |
shouldThrow(); |
58 |
> |
} |
59 |
> |
catch (RuntimeException rt) {} |
60 |
> |
} |
61 |
> |
|
62 |
> |
/** |
63 |
> |
* get returns the last value set or assigned |
64 |
|
*/ |
65 |
|
public void testGetSet() { |
66 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
73 |
|
|
74 |
|
} |
75 |
|
/** |
76 |
< |
* |
76 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
77 |
|
*/ |
78 |
|
public void testCompareAndSet() { |
79 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
87 |
|
assertEquals(7,a.get(this)); |
88 |
|
} |
89 |
|
|
90 |
+ |
|
91 |
+ |
/** |
92 |
+ |
* compareAndSet in one thread enables another waiting for value |
93 |
+ |
* to succeed |
94 |
+ |
*/ |
95 |
+ |
public void testCompareAndSetInMultipleThreads() { |
96 |
+ |
x = 1; |
97 |
+ |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
98 |
+ |
|
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 |
|
/** |
116 |
< |
* |
116 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
117 |
> |
* to expected |
118 |
|
*/ |
119 |
|
public void testWeakCompareAndSet() { |
120 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
< |
* |
130 |
> |
* getAndSet returns previous value and sets to given value |
131 |
|
*/ |
132 |
|
public void testGetAndSet() { |
133 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* |
141 |
> |
* getAndAdd returns previous value and adds given value |
142 |
|
*/ |
143 |
|
public void testGetAndAdd() { |
144 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
< |
* |
153 |
> |
* getAndDecrement returns previous value and decrements |
154 |
|
*/ |
155 |
|
public void testGetAndDecrement() { |
156 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* |
164 |
> |
* getAndIncrement returns previous value and increments |
165 |
|
*/ |
166 |
|
public void testGetAndIncrement() { |
167 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* |
179 |
> |
* addAndGet adds given value to current, and returns current value |
180 |
|
*/ |
181 |
|
public void testAddAndGet() { |
182 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* |
191 |
> |
* decrementAndGet decrements and returns current value |
192 |
|
*/ |
193 |
|
public void testDecrementAndGet() { |
194 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
< |
* |
203 |
> |
* incrementAndGet increments and returns current value |
204 |
|
*/ |
205 |
|
public void testIncrementAndGet() { |
206 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |