ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.12 by jsr166, Mon Nov 16 05:30:07 2009 UTC

# Line 1 | Line 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.
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 TestCase{
13 > public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
14      volatile int x = 0;
15 <
16 <    public static void main(String[] args){
15 >    int w;
16 >    long z;
17 >    public static void main(String[] args) {
18          junit.textui.TestRunner.run(suite());
19      }
18
19  
20      public static Test suite() {
21          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
22      }
23  
24 <    public void testConstructor(){
25 <        try{
26 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
24 >    /**
25 >     * Construction 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 <                (getClass(), "y");
58 <            fail("Exception not thrown");
57 >                (AtomicIntegerFieldUpdaterTest.class, "w");
58 >            shouldThrow();
59          }
60          catch (RuntimeException rt) {}
61      }
62  
63 <    public void testGetSet(){
64 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
63 >    /**
64 >     *  get returns the last value set or assigned
65 >     */
66 >    public void testGetSet() {
67 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
68 >        try {
69 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70 >        } catch (RuntimeException ok) {
71 >            return;
72 >        }
73          x = 1;
74          assertEquals(1,a.get(this));
75          a.set(this,2);
76          assertEquals(2,a.get(this));
77          a.set(this,-3);
78          assertEquals(-3,a.get(this));
79 <        
79 >
80 >    }
81 >
82 >    /**
83 >     *  get returns the last value lazySet by same thread
84 >     */
85 >    public void testGetLazySet() {
86 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
87 >        try {
88 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
89 >        } catch (RuntimeException ok) {
90 >            return;
91 >        }
92 >        x = 1;
93 >        assertEquals(1,a.get(this));
94 >        a.lazySet(this,2);
95 >        assertEquals(2,a.get(this));
96 >        a.lazySet(this,-3);
97 >        assertEquals(-3,a.get(this));
98 >
99      }
100 <    public void testCompareAndSet(){
101 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
100 >
101 >    /**
102 >     * compareAndSet succeeds in changing value if equal to expected else fails
103 >     */
104 >    public void testCompareAndSet() {
105 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
106 >        try {
107 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
108 >        } catch (RuntimeException ok) {
109 >            return;
110 >        }
111          x = 1;
112          assertTrue(a.compareAndSet(this,1,2));
113          assertTrue(a.compareAndSet(this,2,-4));
# Line 53 | Line 118 | public class AtomicIntegerFieldUpdaterTe
118          assertEquals(7,a.get(this));
119      }
120  
121 <    public void testWeakCompareAndSet(){
122 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
121 >
122 >    /**
123 >     * compareAndSet in one thread enables another waiting for value
124 >     * to succeed
125 >     */
126 >    public void testCompareAndSetInMultipleThreads() {
127 >        x = 1;
128 >        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
129 >        try {
130 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
131 >        } catch (RuntimeException ok) {
132 >            return;
133 >        }
134 >
135 >        Thread t = new Thread(new Runnable() {
136 >                public void run() {
137 >                    while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
138 >                }});
139 >        try {
140 >            t.start();
141 >            assertTrue(a.compareAndSet(this, 1, 2));
142 >            t.join(LONG_DELAY_MS);
143 >            assertFalse(t.isAlive());
144 >            assertEquals(a.get(this), 3);
145 >        }
146 >        catch (Exception e) {
147 >            unexpectedException();
148 >        }
149 >    }
150 >
151 >    /**
152 >     * repeated weakCompareAndSet succeeds in changing value when equal
153 >     * to expected
154 >     */
155 >    public void testWeakCompareAndSet() {
156 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
157 >        try {
158 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
159 >        } catch (RuntimeException ok) {
160 >            return;
161 >        }
162          x = 1;
163 <        while(!a.weakCompareAndSet(this,1,2));
164 <        while(!a.weakCompareAndSet(this,2,-4));
163 >        while (!a.weakCompareAndSet(this,1,2));
164 >        while (!a.weakCompareAndSet(this,2,-4));
165          assertEquals(-4,a.get(this));
166 <        while(!a.weakCompareAndSet(this,-4,7));
166 >        while (!a.weakCompareAndSet(this,-4,7));
167          assertEquals(7,a.get(this));
168      }
169  
170 <    public void testGetAndSet(){
171 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
170 >    /**
171 >     *  getAndSet returns previous value and sets to given value
172 >     */
173 >    public void testGetAndSet() {
174 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
175 >        try {
176 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
177 >        } catch (RuntimeException ok) {
178 >            return;
179 >        }
180          x = 1;
181          assertEquals(1,a.getAndSet(this, 0));
182          assertEquals(0,a.getAndSet(this,-10));
183          assertEquals(-10,a.getAndSet(this,1));
184      }
185  
186 <    public void testGetAndAdd(){
187 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
186 >    /**
187 >     * getAndAdd returns previous value and adds given value
188 >     */
189 >    public void testGetAndAdd() {
190 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
191 >        try {
192 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
193 >        } catch (RuntimeException ok) {
194 >            return;
195 >        }
196          x = 1;
197          assertEquals(1,a.getAndAdd(this,2));
198          assertEquals(3,a.get(this));
# Line 80 | Line 200 | public class AtomicIntegerFieldUpdaterTe
200          assertEquals(-1,a.get(this));
201      }
202  
203 <    public void testGetAndDecrement(){
204 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
203 >    /**
204 >     * getAndDecrement returns previous value and decrements
205 >     */
206 >    public void testGetAndDecrement() {
207 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
208 >        try {
209 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
210 >        } catch (RuntimeException ok) {
211 >            return;
212 >        }
213          x = 1;
214          assertEquals(1,a.getAndDecrement(this));
215          assertEquals(0,a.getAndDecrement(this));
216          assertEquals(-1,a.getAndDecrement(this));
217      }
218  
219 <    public void testGetAndIncrement(){
220 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
219 >    /**
220 >     * getAndIncrement returns previous value and increments
221 >     */
222 >    public void testGetAndIncrement() {
223 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
224 >        try {
225 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
226 >        } catch (RuntimeException ok) {
227 >            return;
228 >        }
229          x = 1;
230          assertEquals(1,a.getAndIncrement(this));
231          assertEquals(2,a.get(this));
# Line 100 | Line 236 | public class AtomicIntegerFieldUpdaterTe
236          assertEquals(1,a.get(this));
237      }
238  
239 <    public void testAddAndGet(){
240 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
239 >    /**
240 >     * addAndGet adds given value to current, and returns current value
241 >     */
242 >    public void testAddAndGet() {
243 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
244 >        try {
245 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
246 >        } catch (RuntimeException ok) {
247 >            return;
248 >        }
249          x = 1;
250          assertEquals(3,a.addAndGet(this,2));
251          assertEquals(3,a.get(this));
# Line 109 | Line 253 | public class AtomicIntegerFieldUpdaterTe
253          assertEquals(-1,a.get(this));
254      }
255  
256 <    public void testDecrementAndGet(){
257 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
256 >    /**
257 >     * decrementAndGet decrements and returns current value
258 >     */
259 >    public void testDecrementAndGet() {
260 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
261 >        try {
262 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
263 >        } catch (RuntimeException ok) {
264 >            return;
265 >        }
266          x = 1;
267          assertEquals(0,a.decrementAndGet(this));
268          assertEquals(-1,a.decrementAndGet(this));
# Line 118 | Line 270 | public class AtomicIntegerFieldUpdaterTe
270          assertEquals(-2,a.get(this));
271      }
272  
273 <    public void testIncrementAndGet(){
274 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
273 >    /**
274 >     * incrementAndGet increments and returns current value
275 >     */
276 >    public void testIncrementAndGet() {
277 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
278 >        try {
279 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
280 >        } catch (RuntimeException ok) {
281 >            return;
282 >        }
283          x = 1;
284          assertEquals(2,a.incrementAndGet(this));
285          assertEquals(2,a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines