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

Comparing jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java (file contents):
Revision 1.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.31 by jsr166, Mon Nov 9 07:54:28 2015 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/publicdomain/zero/1.0/
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.*;
9 > import java.util.concurrent.atomic.AtomicLongFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
15      volatile long x = 0;
16 <    long z;
17 <    int w;
18 <
19 <    public static void main(String[] args){
20 <        junit.textui.TestRunner.run(suite());
16 >    protected volatile long protectedField;
17 >    private volatile long privateField;
18 >    long w;
19 >    float z;
20 >    public static void main(String[] args) {
21 >        main(suite(), args);
22      }
20
21  
23      public static Test suite() {
24          return new TestSuite(AtomicLongFieldUpdaterTest.class);
25      }
26  
27 +    // for testing subclass access
28 +    static class AtomicLongFieldUpdaterTestSubclass extends AtomicLongFieldUpdaterTest {
29 +        public void checkPrivateAccess() {
30 +            try {
31 +                AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
32 +                    AtomicLongFieldUpdater.newUpdater
33 +                    (AtomicLongFieldUpdaterTest.class, "privateField");
34 +                shouldThrow();
35 +            } catch (RuntimeException success) {
36 +                assertNotNull(success.getCause());
37 +            }
38 +        }
39 +
40 +        public void checkCompareAndSetProtectedSub() {
41 +            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
42 +            a = updaterFor("protectedField");
43 +            protectedField = 1;
44 +            assertTrue(a.compareAndSet(this, 1, 2));
45 +            assertTrue(a.compareAndSet(this, 2, -4));
46 +            assertEquals(-4, a.get(this));
47 +            assertFalse(a.compareAndSet(this, -5, 7));
48 +            assertEquals(-4, a.get(this));
49 +            assertTrue(a.compareAndSet(this, -4, 7));
50 +            assertEquals(7, a.get(this));
51 +        }
52 +    }
53 +
54 +    static class UnrelatedClass {
55 +        public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) {
56 +            obj.x = 72L;
57 +            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
58 +                AtomicLongFieldUpdater.newUpdater
59 +                (AtomicLongFieldUpdaterTest.class, "x");
60 +            assertEquals(72L, a.get(obj));
61 +            assertTrue(a.compareAndSet(obj, 72L, 73L));
62 +            assertEquals(73L, a.get(obj));
63 +        }
64 +
65 +        public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) {
66 +            try {
67 +                AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
68 +                    AtomicLongFieldUpdater.newUpdater
69 +                    (AtomicLongFieldUpdaterTest.class, "privateField");
70 +                throw new AssertionError("should throw");
71 +            } catch (RuntimeException success) {
72 +                assertNotNull(success.getCause());
73 +            }
74 +        }
75 +    }
76 +
77 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
78 +        return AtomicLongFieldUpdater.newUpdater
79 +            (AtomicLongFieldUpdaterTest.class, fieldName);
80 +    }
81 +
82      /**
83 <     *
83 >     * Construction with non-existent field throws RuntimeException
84       */
85 <    public void testConstructor(){
86 <        try{
87 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
32 <                a = AtomicLongFieldUpdater.newUpdater
33 <                (getClass(), "y");
85 >    public void testConstructor() {
86 >        try {
87 >            updaterFor("y");
88              shouldThrow();
89 +        } catch (RuntimeException success) {
90 +            assertNotNull(success.getCause());
91          }
36        catch (RuntimeException rt) {}
92      }
93  
94      /**
95 <     *
95 >     * construction with field not of given type throws IllegalArgumentException
96       */
97 <    public void testConstructor2(){
98 <        try{
99 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
45 <                a = AtomicLongFieldUpdater.newUpdater
46 <                (getClass(), "z");
97 >    public void testConstructor2() {
98 >        try {
99 >            updaterFor("z");
100              shouldThrow();
101 <        }
49 <        catch (RuntimeException rt) {}
101 >        } catch (IllegalArgumentException success) {}
102      }
103  
104      /**
105 <     *
105 >     * construction with non-volatile field throws IllegalArgumentException
106       */
107 <    public void testConstructor3(){
108 <        try{
109 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
58 <                a = AtomicLongFieldUpdater.newUpdater
59 <                (getClass(), "w");
107 >    public void testConstructor3() {
108 >        try {
109 >            updaterFor("w");
110              shouldThrow();
111 <        }
111 >        } catch (IllegalArgumentException success) {}
112 >    }
113  
114 <        catch (RuntimeException rt) {}
114 >    /**
115 >     * construction using private field from subclass throws RuntimeException
116 >     */
117 >    public void testPrivateFieldInSubclass() {
118 >        AtomicLongFieldUpdaterTestSubclass s =
119 >            new AtomicLongFieldUpdaterTestSubclass();
120 >        s.checkPrivateAccess();
121      }
122  
123      /**
124 <     *
124 >     * construction from unrelated class; package access is allowed,
125 >     * private access is not
126       */
127 <    public void testGetSet(){
128 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
127 >    public void testUnrelatedClassAccess() {
128 >        new UnrelatedClass().checkPackageAccess(this);
129 >        new UnrelatedClass().checkPrivateAccess(this);
130 >    }
131 >
132 >    /**
133 >     * get returns the last value set or assigned
134 >     */
135 >    public void testGetSet() {
136 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
137 >        a = updaterFor("x");
138 >        x = 1;
139 >        assertEquals(1, a.get(this));
140 >        a.set(this, 2);
141 >        assertEquals(2, a.get(this));
142 >        a.set(this, -3);
143 >        assertEquals(-3, a.get(this));
144 >    }
145 >
146 >    /**
147 >     * get returns the last value lazySet by same thread
148 >     */
149 >    public void testGetLazySet() {
150 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
151 >        a = updaterFor("x");
152 >        x = 1;
153 >        assertEquals(1, a.get(this));
154 >        a.lazySet(this, 2);
155 >        assertEquals(2, a.get(this));
156 >        a.lazySet(this, -3);
157 >        assertEquals(-3, a.get(this));
158 >    }
159 >
160 >    /**
161 >     * compareAndSet succeeds in changing value if equal to expected else fails
162 >     */
163 >    public void testCompareAndSet() {
164 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
165 >        a = updaterFor("x");
166          x = 1;
167 <        assertEquals(1,a.get(this));
168 <        a.set(this,2);
169 <        assertEquals(2,a.get(this));
170 <        a.set(this,-3);
171 <        assertEquals(-3,a.get(this));
172 <        
167 >        assertTrue(a.compareAndSet(this, 1, 2));
168 >        assertTrue(a.compareAndSet(this, 2, -4));
169 >        assertEquals(-4, a.get(this));
170 >        assertFalse(a.compareAndSet(this, -5, 7));
171 >        assertEquals(-4, a.get(this));
172 >        assertTrue(a.compareAndSet(this, -4, 7));
173 >        assertEquals(7, a.get(this));
174      }
175 +
176 +    /**
177 +     * compareAndSet succeeds in changing protected field value if
178 +     * equal to expected else fails
179 +     */
180 +    public void testCompareAndSetProtected() {
181 +        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
182 +        a = updaterFor("protectedField");
183 +        protectedField = 1;
184 +        assertTrue(a.compareAndSet(this, 1, 2));
185 +        assertTrue(a.compareAndSet(this, 2, -4));
186 +        assertEquals(-4, a.get(this));
187 +        assertFalse(a.compareAndSet(this, -5, 7));
188 +        assertEquals(-4, a.get(this));
189 +        assertTrue(a.compareAndSet(this, -4, 7));
190 +        assertEquals(7, a.get(this));
191 +    }
192 +
193      /**
194 <     *
194 >     * compareAndSet succeeds in changing protected field value if
195 >     * equal to expected else fails
196       */
197 <    public void testCompareAndSet(){
198 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
197 >    public void testCompareAndSetProtectedInSubclass() {
198 >        AtomicLongFieldUpdaterTestSubclass s =
199 >            new AtomicLongFieldUpdaterTestSubclass();
200 >        s.checkCompareAndSetProtectedSub();
201 >    }
202 >
203 >    /**
204 >     * compareAndSet in one thread enables another waiting for value
205 >     * to succeed
206 >     */
207 >    public void testCompareAndSetInMultipleThreads() throws Exception {
208          x = 1;
209 <        assertTrue(a.compareAndSet(this,1,2));
210 <        assertTrue(a.compareAndSet(this,2,-4));
211 <        assertEquals(-4,a.get(this));
212 <        assertFalse(a.compareAndSet(this,-5,7));
213 <        assertFalse((7 == a.get(this)));
214 <        assertTrue(a.compareAndSet(this,-4,7));
215 <        assertEquals(7,a.get(this));
209 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
210 >        a = updaterFor("x");
211 >
212 >        Thread t = new Thread(new CheckedRunnable() {
213 >            public void realRun() {
214 >                while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
215 >                    Thread.yield();
216 >            }});
217 >
218 >        t.start();
219 >        assertTrue(a.compareAndSet(this, 1, 2));
220 >        t.join(LONG_DELAY_MS);
221 >        assertFalse(t.isAlive());
222 >        assertEquals(3, a.get(this));
223      }
224  
225      /**
226 <     *
226 >     * repeated weakCompareAndSet succeeds in changing value when equal
227 >     * to expected
228       */
229 <    public void testWeakCompareAndSet(){
230 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
229 >    public void testWeakCompareAndSet() {
230 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
231 >        a = updaterFor("x");
232          x = 1;
233 <        while(!a.weakCompareAndSet(this,1,2));
234 <        while(!a.weakCompareAndSet(this,2,-4));
235 <        assertEquals(-4,a.get(this));
236 <        while(!a.weakCompareAndSet(this,-4,7));
237 <        assertEquals(7,a.get(this));
233 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
234 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
235 >        assertEquals(-4, a.get(this));
236 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
237 >        assertEquals(7, a.get(this));
238      }
239  
240      /**
241 <     *
241 >     * getAndSet returns previous value and sets to given value
242       */
243 <    public void testGetAndSet(){
244 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
243 >    public void testGetAndSet() {
244 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
245 >        a = updaterFor("x");
246          x = 1;
247 <        assertEquals(1,a.getAndSet(this, 0));
248 <        assertEquals(0,a.getAndSet(this,-10));
249 <        assertEquals(-10,a.getAndSet(this,1));
247 >        assertEquals(1, a.getAndSet(this, 0));
248 >        assertEquals(0, a.getAndSet(this, -10));
249 >        assertEquals(-10, a.getAndSet(this, 1));
250      }
251  
252      /**
253 <     *
253 >     * getAndAdd returns previous value and adds given value
254       */
255 <    public void testGetAndAdd(){
256 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
255 >    public void testGetAndAdd() {
256 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
257 >        a = updaterFor("x");
258          x = 1;
259 <        assertEquals(1,a.getAndAdd(this,2));
260 <        assertEquals(3,a.get(this));
261 <        assertEquals(3,a.getAndAdd(this,-4));
262 <        assertEquals(-1,a.get(this));
259 >        assertEquals(1, a.getAndAdd(this, 2));
260 >        assertEquals(3, a.get(this));
261 >        assertEquals(3, a.getAndAdd(this, -4));
262 >        assertEquals(-1, a.get(this));
263      }
264  
265      /**
266 <     *
266 >     * getAndDecrement returns previous value and decrements
267       */
268 <    public void testGetAndDecrement(){
269 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
268 >    public void testGetAndDecrement() {
269 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
270 >        a = updaterFor("x");
271          x = 1;
272 <        assertEquals(1,a.getAndDecrement(this));
273 <        assertEquals(0,a.getAndDecrement(this));
274 <        assertEquals(-1,a.getAndDecrement(this));
272 >        assertEquals(1, a.getAndDecrement(this));
273 >        assertEquals(0, a.getAndDecrement(this));
274 >        assertEquals(-1, a.getAndDecrement(this));
275      }
276  
277      /**
278 <     *
278 >     * getAndIncrement returns previous value and increments
279       */
280 <    public void testGetAndIncrement(){
281 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
280 >    public void testGetAndIncrement() {
281 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
282 >        a = updaterFor("x");
283          x = 1;
284 <        assertEquals(1,a.getAndIncrement(this));
285 <        assertEquals(2,a.get(this));
286 <        a.set(this,-2);
287 <        assertEquals(-2,a.getAndIncrement(this));
288 <        assertEquals(-1,a.getAndIncrement(this));
289 <        assertEquals(0,a.getAndIncrement(this));
290 <        assertEquals(1,a.get(this));
284 >        assertEquals(1, a.getAndIncrement(this));
285 >        assertEquals(2, a.get(this));
286 >        a.set(this, -2);
287 >        assertEquals(-2, a.getAndIncrement(this));
288 >        assertEquals(-1, a.getAndIncrement(this));
289 >        assertEquals(0, a.getAndIncrement(this));
290 >        assertEquals(1, a.get(this));
291      }
292  
293      /**
294 <     *
294 >     * addAndGet adds given value to current, and returns current value
295       */
296 <    public void testAddAndGet(){
297 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
296 >    public void testAddAndGet() {
297 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
298 >        a = updaterFor("x");
299          x = 1;
300 <        assertEquals(3,a.addAndGet(this,2));
301 <        assertEquals(3,a.get(this));
302 <        assertEquals(-1,a.addAndGet(this,-4));
303 <        assertEquals(-1,a.get(this));
300 >        assertEquals(3, a.addAndGet(this, 2));
301 >        assertEquals(3, a.get(this));
302 >        assertEquals(-1, a.addAndGet(this, -4));
303 >        assertEquals(-1, a.get(this));
304      }
305  
306      /**
307 <     *
307 >     * decrementAndGet decrements and returns current value
308       */
309 <    public void testDecrementAndGet(){
310 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
309 >    public void testDecrementAndGet() {
310 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
311 >        a = updaterFor("x");
312          x = 1;
313 <        assertEquals(0,a.decrementAndGet(this));
314 <        assertEquals(-1,a.decrementAndGet(this));
315 <        assertEquals(-2,a.decrementAndGet(this));
316 <        assertEquals(-2,a.get(this));
313 >        assertEquals(0, a.decrementAndGet(this));
314 >        assertEquals(-1, a.decrementAndGet(this));
315 >        assertEquals(-2, a.decrementAndGet(this));
316 >        assertEquals(-2, a.get(this));
317      }
318  
319      /**
320 <     *
320 >     * incrementAndGet increments and returns current value
321       */
322 <    public void testIncrementAndGet(){
323 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
322 >    public void testIncrementAndGet() {
323 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
324 >        a = updaterFor("x");
325          x = 1;
326 <        assertEquals(2,a.incrementAndGet(this));
327 <        assertEquals(2,a.get(this));
328 <        a.set(this,-2);
329 <        assertEquals(-1,a.incrementAndGet(this));
330 <        assertEquals(0,a.incrementAndGet(this));
331 <        assertEquals(1,a.incrementAndGet(this));
332 <        assertEquals(1,a.get(this));
326 >        assertEquals(2, a.incrementAndGet(this));
327 >        assertEquals(2, a.get(this));
328 >        a.set(this, -2);
329 >        assertEquals(-1, a.incrementAndGet(this));
330 >        assertEquals(0, a.incrementAndGet(this));
331 >        assertEquals(1, a.incrementAndGet(this));
332 >        assertEquals(1, a.get(this));
333      }
334  
335   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines