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.16 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.31 by jsr166, Mon Nov 9 07:54:28 2015 UTC

# Line 1 | Line 1
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
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 <    int z;
16 >    protected volatile long protectedField;
17 >    private volatile long privateField;
18      long w;
19 <
19 >    float z;
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run(suite());
21 >        main(suite(), args);
22      }
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       * Construction with non-existent field throws RuntimeException
84       */
85      public void testConstructor() {
86          try {
87 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (AtomicLongFieldUpdaterTest.class, "y");
87 >            updaterFor("y");
88              shouldThrow();
89 <        } catch (RuntimeException success) {}
89 >        } catch (RuntimeException success) {
90 >            assertNotNull(success.getCause());
91 >        }
92      }
93  
94      /**
95 <     * construction with field not of given type throws RuntimeException
95 >     * construction with field not of given type throws IllegalArgumentException
96       */
97      public void testConstructor2() {
98          try {
99 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
43 <                a = AtomicLongFieldUpdater.newUpdater
44 <                (AtomicLongFieldUpdaterTest.class, "z");
99 >            updaterFor("z");
100              shouldThrow();
101 <        } catch (RuntimeException success) {}
101 >        } catch (IllegalArgumentException success) {}
102      }
103  
104      /**
105 <     * construction with non-volatile field throws RuntimeException
105 >     * construction with non-volatile field throws IllegalArgumentException
106       */
107      public void testConstructor3() {
108          try {
109 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
55 <                a = AtomicLongFieldUpdater.newUpdater
56 <                (AtomicLongFieldUpdaterTest.class, "w");
109 >            updaterFor("w");
110              shouldThrow();
111 <        } catch (RuntimeException success) {}
111 >        } catch (IllegalArgumentException success) {}
112      }
113  
114      /**
115 <     *  get returns the last value set or assigned
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 >     * construction from unrelated class; package access is allowed,
125 >     * private access is not
126 >     */
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 <        try {
67 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
68 <        } catch (RuntimeException ok) {
69 <            return;
70 <        }
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));
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
147 >     * get returns the last value lazySet by same thread
148       */
149      public void testGetLazySet() {
150          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
151 <        try {
85 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
86 <        } catch (RuntimeException ok) {
87 <            return;
88 <        }
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));
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  
97
160      /**
161       * compareAndSet succeeds in changing value if equal to expected else fails
162       */
163      public void testCompareAndSet() {
164          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
165 <        try {
104 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
105 <        } catch (RuntimeException ok) {
106 <            return;
107 <        }
165 >        a = updaterFor("x");
166          x = 1;
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 <        assertFalse((7 == a.get(this)));
172 <        assertTrue(a.compareAndSet(this,-4,7));
173 <        assertEquals(7,a.get(this));
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 +     * compareAndSet succeeds in changing protected field value if
195 +     * equal to expected else fails
196 +     */
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
# Line 122 | Line 206 | public class AtomicLongFieldUpdaterTest
206       */
207      public void testCompareAndSetInMultipleThreads() throws Exception {
208          x = 1;
209 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
210 <        try {
127 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
128 <        } catch (RuntimeException ok) {
129 <            return;
130 <        }
209 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
210 >        a = updaterFor("x");
211  
212          Thread t = new Thread(new CheckedRunnable() {
213              public void realRun() {
# Line 139 | Line 219 | public class AtomicLongFieldUpdaterTest
219          assertTrue(a.compareAndSet(this, 1, 2));
220          t.join(LONG_DELAY_MS);
221          assertFalse(t.isAlive());
222 <        assertEquals(a.get(this), 3);
222 >        assertEquals(3, a.get(this));
223      }
224  
225      /**
# Line 148 | Line 228 | public class AtomicLongFieldUpdaterTest
228       */
229      public void testWeakCompareAndSet() {
230          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
231 <        try {
152 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
153 <        } catch (RuntimeException ok) {
154 <            return;
155 <        }
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 <     *  getAndSet returns previous value and sets to given value
241 >     * getAndSet returns previous value and sets to given value
242       */
243      public void testGetAndSet() {
244          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
245 <        try {
170 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
171 <        } catch (RuntimeException ok) {
172 <            return;
173 <        }
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      /**
# Line 182 | Line 254 | public class AtomicLongFieldUpdaterTest
254       */
255      public void testGetAndAdd() {
256          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
257 <        try {
186 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
187 <        } catch (RuntimeException ok) {
188 <            return;
189 <        }
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      /**
# Line 199 | Line 267 | public class AtomicLongFieldUpdaterTest
267       */
268      public void testGetAndDecrement() {
269          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
270 <        try {
203 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
204 <        } catch (RuntimeException ok) {
205 <            return;
206 <        }
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      /**
# Line 215 | Line 279 | public class AtomicLongFieldUpdaterTest
279       */
280      public void testGetAndIncrement() {
281          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
282 <        try {
219 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
220 <        } catch (RuntimeException ok) {
221 <            return;
222 <        }
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      /**
# Line 235 | Line 295 | public class AtomicLongFieldUpdaterTest
295       */
296      public void testAddAndGet() {
297          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
298 <        try {
239 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
240 <        } catch (RuntimeException ok) {
241 <            return;
242 <        }
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 <     *  decrementAndGet decrements and returns current value
307 >     * decrementAndGet decrements and returns current value
308       */
309      public void testDecrementAndGet() {
310          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
311 <        try {
256 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
257 <        } catch (RuntimeException ok) {
258 <            return;
259 <        }
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      /**
# Line 269 | Line 321 | public class AtomicLongFieldUpdaterTest
321       */
322      public void testIncrementAndGet() {
323          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
324 <        try {
273 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
274 <        } catch (RuntimeException ok) {
275 <            return;
276 <        }
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