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.14 by jsr166, Tue Nov 17 06:58:50 2009 UTC vs.
Revision 1.30 by dl, Sun Nov 8 15:34:01 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 checkPrivateAccess() {
56 +            Exception ex = null;
57 +            try {
58 +                AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
59 +                    AtomicLongFieldUpdater.newUpdater
60 +                    (AtomicLongFieldUpdaterTest.class, "x");
61 +            } catch (RuntimeException rex) {
62 +                ex = rex;
63 +            }
64 +            if (ex != null) throw new Error();
65 +        }
66 +    }
67 +
68 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
69 +        return AtomicLongFieldUpdater.newUpdater
70 +            (AtomicLongFieldUpdaterTest.class, fieldName);
71 +    }
72 +
73      /**
74       * Construction with non-existent field throws RuntimeException
75       */
76      public void testConstructor() {
77          try {
78 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (AtomicLongFieldUpdaterTest.class, "y");
78 >            updaterFor("y");
79              shouldThrow();
80 +        } catch (RuntimeException success) {
81 +            assertNotNull(success.getCause());
82          }
35        catch (RuntimeException rt) {}
83      }
84  
85      /**
86 <     * construction with field not of given type throws RuntimeException
86 >     * construction with field not of given type throws IllegalArgumentException
87       */
88      public void testConstructor2() {
89          try {
90 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
44 <                a = AtomicLongFieldUpdater.newUpdater
45 <                (AtomicLongFieldUpdaterTest.class, "z");
90 >            updaterFor("z");
91              shouldThrow();
92 <        }
48 <        catch (RuntimeException rt) {}
92 >        } catch (IllegalArgumentException success) {}
93      }
94  
95      /**
96 <     * construction with non-volatile field throws RuntimeException
96 >     * construction with non-volatile field throws IllegalArgumentException
97       */
98      public void testConstructor3() {
99          try {
100 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
57 <                a = AtomicLongFieldUpdater.newUpdater
58 <                (AtomicLongFieldUpdaterTest.class, "w");
100 >            updaterFor("w");
101              shouldThrow();
102 <        }
102 >        } catch (IllegalArgumentException success) {}
103 >    }
104  
105 <        catch (RuntimeException rt) {}
105 >    /**
106 >     * construction using private field from subclass throws RuntimeException
107 >     */
108 >    public void testPrivateFieldInSubclass() {
109 >        AtomicLongFieldUpdaterTestSubclass s =
110 >            new AtomicLongFieldUpdaterTestSubclass();
111 >        s.checkPrivateAccess();
112      }
113  
114      /**
115 <     *  get returns the last value set or assigned
115 >     * construction from unrelated class throws RuntimeException
116 >     */
117 >    public void testUnrelatedClassAccess() {
118 >        UnrelatedClass s = new UnrelatedClass();
119 >        s.checkPrivateAccess();
120 >    }
121 >
122 >    /**
123 >     * get returns the last value set or assigned
124       */
125      public void testGetSet() {
126          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
127 <        try {
71 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
72 <        } catch (RuntimeException ok) {
73 <            return;
74 <        }
127 >        a = updaterFor("x");
128          x = 1;
129 <        assertEquals(1,a.get(this));
130 <        a.set(this,2);
131 <        assertEquals(2,a.get(this));
132 <        a.set(this,-3);
133 <        assertEquals(-3,a.get(this));
129 >        assertEquals(1, a.get(this));
130 >        a.set(this, 2);
131 >        assertEquals(2, a.get(this));
132 >        a.set(this, -3);
133 >        assertEquals(-3, a.get(this));
134      }
135  
136      /**
137 <     *  get returns the last value lazySet by same thread
137 >     * get returns the last value lazySet by same thread
138       */
139      public void testGetLazySet() {
140          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
141 <        try {
89 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
90 <        } catch (RuntimeException ok) {
91 <            return;
92 <        }
141 >        a = updaterFor("x");
142          x = 1;
143 <        assertEquals(1,a.get(this));
144 <        a.lazySet(this,2);
145 <        assertEquals(2,a.get(this));
146 <        a.lazySet(this,-3);
147 <        assertEquals(-3,a.get(this));
143 >        assertEquals(1, a.get(this));
144 >        a.lazySet(this, 2);
145 >        assertEquals(2, a.get(this));
146 >        a.lazySet(this, -3);
147 >        assertEquals(-3, a.get(this));
148      }
149  
101
150      /**
151       * compareAndSet succeeds in changing value if equal to expected else fails
152       */
153      public void testCompareAndSet() {
154          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
155 <        try {
108 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
109 <        } catch (RuntimeException ok) {
110 <            return;
111 <        }
155 >        a = updaterFor("x");
156          x = 1;
157 <        assertTrue(a.compareAndSet(this,1,2));
158 <        assertTrue(a.compareAndSet(this,2,-4));
159 <        assertEquals(-4,a.get(this));
160 <        assertFalse(a.compareAndSet(this,-5,7));
161 <        assertFalse((7 == a.get(this)));
162 <        assertTrue(a.compareAndSet(this,-4,7));
163 <        assertEquals(7,a.get(this));
157 >        assertTrue(a.compareAndSet(this, 1, 2));
158 >        assertTrue(a.compareAndSet(this, 2, -4));
159 >        assertEquals(-4, a.get(this));
160 >        assertFalse(a.compareAndSet(this, -5, 7));
161 >        assertEquals(-4, a.get(this));
162 >        assertTrue(a.compareAndSet(this, -4, 7));
163 >        assertEquals(7, a.get(this));
164      }
165  
166 +    /**
167 +     * compareAndSet succeeds in changing protected field value if
168 +     * equal to expected else fails
169 +     */
170 +    public void testCompareAndSetProtected() {
171 +        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
172 +        a = updaterFor("protectedField");
173 +        protectedField = 1;
174 +        assertTrue(a.compareAndSet(this, 1, 2));
175 +        assertTrue(a.compareAndSet(this, 2, -4));
176 +        assertEquals(-4, a.get(this));
177 +        assertFalse(a.compareAndSet(this, -5, 7));
178 +        assertEquals(-4, a.get(this));
179 +        assertTrue(a.compareAndSet(this, -4, 7));
180 +        assertEquals(7, a.get(this));
181 +    }
182 +
183 +    /**
184 +     * compareAndSet succeeds in changing protected field value if
185 +     * equal to expected else fails
186 +     */
187 +    public void testCompareAndSetProtectedInSubclass() {
188 +        AtomicLongFieldUpdaterTestSubclass s =
189 +            new AtomicLongFieldUpdaterTestSubclass();
190 +        s.checkCompareAndSetProtectedSub();
191 +    }
192  
193      /**
194       * compareAndSet in one thread enables another waiting for value
# Line 126 | Line 196 | public class AtomicLongFieldUpdaterTest
196       */
197      public void testCompareAndSetInMultipleThreads() throws Exception {
198          x = 1;
199 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
200 <        try {
131 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
132 <        } catch (RuntimeException ok) {
133 <            return;
134 <        }
199 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 >        a = updaterFor("x");
201  
202          Thread t = new Thread(new CheckedRunnable() {
203              public void realRun() {
# Line 143 | Line 209 | public class AtomicLongFieldUpdaterTest
209          assertTrue(a.compareAndSet(this, 1, 2));
210          t.join(LONG_DELAY_MS);
211          assertFalse(t.isAlive());
212 <        assertEquals(a.get(this), 3);
212 >        assertEquals(3, a.get(this));
213      }
214  
215      /**
# Line 152 | Line 218 | public class AtomicLongFieldUpdaterTest
218       */
219      public void testWeakCompareAndSet() {
220          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
221 <        try {
156 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
157 <        } catch (RuntimeException ok) {
158 <            return;
159 <        }
221 >        a = updaterFor("x");
222          x = 1;
223 <        while (!a.weakCompareAndSet(this,1,2));
224 <        while (!a.weakCompareAndSet(this,2,-4));
225 <        assertEquals(-4,a.get(this));
226 <        while (!a.weakCompareAndSet(this,-4,7));
227 <        assertEquals(7,a.get(this));
223 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
224 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
225 >        assertEquals(-4, a.get(this));
226 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
227 >        assertEquals(7, a.get(this));
228      }
229  
230      /**
231 <     *  getAndSet returns previous value and sets to given value
231 >     * getAndSet returns previous value and sets to given value
232       */
233      public void testGetAndSet() {
234          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
235 <        try {
174 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
175 <        } catch (RuntimeException ok) {
176 <            return;
177 <        }
235 >        a = updaterFor("x");
236          x = 1;
237 <        assertEquals(1,a.getAndSet(this, 0));
238 <        assertEquals(0,a.getAndSet(this,-10));
239 <        assertEquals(-10,a.getAndSet(this,1));
237 >        assertEquals(1, a.getAndSet(this, 0));
238 >        assertEquals(0, a.getAndSet(this, -10));
239 >        assertEquals(-10, a.getAndSet(this, 1));
240      }
241  
242      /**
# Line 186 | Line 244 | public class AtomicLongFieldUpdaterTest
244       */
245      public void testGetAndAdd() {
246          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
247 <        try {
190 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
191 <        } catch (RuntimeException ok) {
192 <            return;
193 <        }
247 >        a = updaterFor("x");
248          x = 1;
249 <        assertEquals(1,a.getAndAdd(this,2));
250 <        assertEquals(3,a.get(this));
251 <        assertEquals(3,a.getAndAdd(this,-4));
252 <        assertEquals(-1,a.get(this));
249 >        assertEquals(1, a.getAndAdd(this, 2));
250 >        assertEquals(3, a.get(this));
251 >        assertEquals(3, a.getAndAdd(this, -4));
252 >        assertEquals(-1, a.get(this));
253      }
254  
255      /**
# Line 203 | Line 257 | public class AtomicLongFieldUpdaterTest
257       */
258      public void testGetAndDecrement() {
259          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
260 <        try {
207 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
208 <        } catch (RuntimeException ok) {
209 <            return;
210 <        }
260 >        a = updaterFor("x");
261          x = 1;
262 <        assertEquals(1,a.getAndDecrement(this));
263 <        assertEquals(0,a.getAndDecrement(this));
264 <        assertEquals(-1,a.getAndDecrement(this));
262 >        assertEquals(1, a.getAndDecrement(this));
263 >        assertEquals(0, a.getAndDecrement(this));
264 >        assertEquals(-1, a.getAndDecrement(this));
265      }
266  
267      /**
# Line 219 | Line 269 | public class AtomicLongFieldUpdaterTest
269       */
270      public void testGetAndIncrement() {
271          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
272 <        try {
223 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
224 <        } catch (RuntimeException ok) {
225 <            return;
226 <        }
272 >        a = updaterFor("x");
273          x = 1;
274 <        assertEquals(1,a.getAndIncrement(this));
275 <        assertEquals(2,a.get(this));
276 <        a.set(this,-2);
277 <        assertEquals(-2,a.getAndIncrement(this));
278 <        assertEquals(-1,a.getAndIncrement(this));
279 <        assertEquals(0,a.getAndIncrement(this));
280 <        assertEquals(1,a.get(this));
274 >        assertEquals(1, a.getAndIncrement(this));
275 >        assertEquals(2, a.get(this));
276 >        a.set(this, -2);
277 >        assertEquals(-2, a.getAndIncrement(this));
278 >        assertEquals(-1, a.getAndIncrement(this));
279 >        assertEquals(0, a.getAndIncrement(this));
280 >        assertEquals(1, a.get(this));
281      }
282  
283      /**
# Line 239 | Line 285 | public class AtomicLongFieldUpdaterTest
285       */
286      public void testAddAndGet() {
287          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
288 <        try {
243 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
244 <        } catch (RuntimeException ok) {
245 <            return;
246 <        }
288 >        a = updaterFor("x");
289          x = 1;
290 <        assertEquals(3,a.addAndGet(this,2));
291 <        assertEquals(3,a.get(this));
292 <        assertEquals(-1,a.addAndGet(this,-4));
293 <        assertEquals(-1,a.get(this));
290 >        assertEquals(3, a.addAndGet(this, 2));
291 >        assertEquals(3, a.get(this));
292 >        assertEquals(-1, a.addAndGet(this, -4));
293 >        assertEquals(-1, a.get(this));
294      }
295  
296      /**
297 <     *  decrementAndGet decrements and returns current value
297 >     * decrementAndGet decrements and returns current value
298       */
299      public void testDecrementAndGet() {
300          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
301 <        try {
260 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
261 <        } catch (RuntimeException ok) {
262 <            return;
263 <        }
301 >        a = updaterFor("x");
302          x = 1;
303 <        assertEquals(0,a.decrementAndGet(this));
304 <        assertEquals(-1,a.decrementAndGet(this));
305 <        assertEquals(-2,a.decrementAndGet(this));
306 <        assertEquals(-2,a.get(this));
303 >        assertEquals(0, a.decrementAndGet(this));
304 >        assertEquals(-1, a.decrementAndGet(this));
305 >        assertEquals(-2, a.decrementAndGet(this));
306 >        assertEquals(-2, a.get(this));
307      }
308  
309      /**
# Line 273 | Line 311 | public class AtomicLongFieldUpdaterTest
311       */
312      public void testIncrementAndGet() {
313          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
314 <        try {
277 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
278 <        } catch (RuntimeException ok) {
279 <            return;
280 <        }
314 >        a = updaterFor("x");
315          x = 1;
316 <        assertEquals(2,a.incrementAndGet(this));
317 <        assertEquals(2,a.get(this));
318 <        a.set(this,-2);
319 <        assertEquals(-1,a.incrementAndGet(this));
320 <        assertEquals(0,a.incrementAndGet(this));
321 <        assertEquals(1,a.incrementAndGet(this));
322 <        assertEquals(1,a.get(this));
316 >        assertEquals(2, a.incrementAndGet(this));
317 >        assertEquals(2, a.get(this));
318 >        a.set(this, -2);
319 >        assertEquals(-1, a.incrementAndGet(this));
320 >        assertEquals(0, a.incrementAndGet(this));
321 >        assertEquals(1, a.incrementAndGet(this));
322 >        assertEquals(1, a.get(this));
323      }
324  
325   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines