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.13 by jsr166, Tue Nov 17 03:12:51 2009 UTC vs.
Revision 1.30 by dl, Sun Nov 8 15:34:00 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.AtomicIntegerFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
15      volatile int x = 0;
16 +    protected volatile int protectedField;
17 +    private volatile int privateField;
18      int w;
19 <    long z;
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(AtomicIntegerFieldUpdaterTest.class);
25      }
26  
27 +    // for testing subclass access
28 +    static class AtomicIntegerFieldUpdaterTestSubclass extends AtomicIntegerFieldUpdaterTest {
29 +        public void checkPrivateAccess() {
30 +            try {
31 +                AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
32 +                    AtomicIntegerFieldUpdater.newUpdater
33 +                    (AtomicIntegerFieldUpdaterTest.class, "privateField");
34 +                shouldThrow();
35 +            } catch (RuntimeException success) {
36 +                assertNotNull(success.getCause());
37 +            }
38 +        }
39 +
40 +        public void checkCompareAndSetProtectedSub() {
41 +            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> 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 +                AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
59 +                    AtomicIntegerFieldUpdater.newUpdater
60 +                    (AtomicIntegerFieldUpdaterTest.class, "x");
61 +            } catch (RuntimeException rex) {
62 +                ex = rex;
63 +            }
64 +            if (ex != null) throw new Error();
65 +        }
66 +    }
67 +
68 +    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
69 +        return AtomicIntegerFieldUpdater.newUpdater
70 +            (AtomicIntegerFieldUpdaterTest.class, fieldName);
71 +    }
72 +
73      /**
74       * Construction with non-existent field throws RuntimeException
75       */
76      public void testConstructor() {
77          try {
78 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30 <                a = AtomicIntegerFieldUpdater.newUpdater
31 <                (AtomicIntegerFieldUpdaterTest.class, "y");
78 >            updaterFor("y");
79              shouldThrow();
80 +        } catch (RuntimeException success) {
81 +            assertNotNull(success.getCause());
82          }
34        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 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43 <                a = AtomicIntegerFieldUpdater.newUpdater
44 <                (AtomicIntegerFieldUpdaterTest.class, "z");
90 >            updaterFor("z");
91              shouldThrow();
92 <        }
47 <        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 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56 <                a = AtomicIntegerFieldUpdater.newUpdater
57 <                (AtomicIntegerFieldUpdaterTest.class, "w");
100 >            updaterFor("w");
101              shouldThrow();
102 <        }
60 <        catch (RuntimeException rt) {}
102 >        } catch (IllegalArgumentException success) {}
103      }
104  
105      /**
106 <     *  get returns the last value set or assigned
106 >     * construction using private field from subclass throws RuntimeException
107 >     */
108 >    public void testPrivateFieldInSubclass() {
109 >        AtomicIntegerFieldUpdaterTestSubclass s =
110 >            new AtomicIntegerFieldUpdaterTestSubclass();
111 >        s.checkPrivateAccess();
112 >    }
113 >
114 >    /**
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          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
127 <        try {
69 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70 <        } catch (RuntimeException ok) {
71 <            return;
72 <        }
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));
79 <
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          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
141 <        try {
88 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
89 <        } catch (RuntimeException ok) {
90 <            return;
91 <        }
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));
98 <
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  
150      /**
# Line 103 | Line 152 | public class AtomicIntegerFieldUpdaterTe
152       */
153      public void testCompareAndSet() {
154          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
155 <        try {
107 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
108 <        } catch (RuntimeException ok) {
109 <            return;
110 <        }
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 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> 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 +        AtomicIntegerFieldUpdaterTestSubclass s =
189 +            new AtomicIntegerFieldUpdaterTestSubclass();
190 +        s.checkCompareAndSetProtectedSub();
191 +    }
192  
193      /**
194       * compareAndSet in one thread enables another waiting for value
# Line 125 | Line 196 | public class AtomicIntegerFieldUpdaterTe
196       */
197      public void testCompareAndSetInMultipleThreads() throws Exception {
198          x = 1;
199 <        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
200 <        try {
130 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
131 <        } catch (RuntimeException ok) {
132 <            return;
133 <        }
199 >        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
200 >        a = updaterFor("x");
201  
202 <        Thread t = new Thread(new Runnable() {
203 <                public void run() {
204 <                    while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
205 <                }});
202 >        Thread t = new Thread(new CheckedRunnable() {
203 >            public void realRun() {
204 >                while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
205 >                    Thread.yield();
206 >            }});
207  
208          t.start();
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 150 | Line 218 | public class AtomicIntegerFieldUpdaterTe
218       */
219      public void testWeakCompareAndSet() {
220          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
221 <        try {
154 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
155 <        } catch (RuntimeException ok) {
156 <            return;
157 <        }
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          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
235 <        try {
172 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
173 <        } catch (RuntimeException ok) {
174 <            return;
175 <        }
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 184 | Line 244 | public class AtomicIntegerFieldUpdaterTe
244       */
245      public void testGetAndAdd() {
246          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
247 <        try {
188 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
189 <        } catch (RuntimeException ok) {
190 <            return;
191 <        }
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 201 | Line 257 | public class AtomicIntegerFieldUpdaterTe
257       */
258      public void testGetAndDecrement() {
259          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
260 <        try {
205 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
206 <        } catch (RuntimeException ok) {
207 <            return;
208 <        }
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 217 | Line 269 | public class AtomicIntegerFieldUpdaterTe
269       */
270      public void testGetAndIncrement() {
271          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
272 <        try {
221 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
222 <        } catch (RuntimeException ok) {
223 <            return;
224 <        }
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 237 | Line 285 | public class AtomicIntegerFieldUpdaterTe
285       */
286      public void testAddAndGet() {
287          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
288 <        try {
241 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
242 <        } catch (RuntimeException ok) {
243 <            return;
244 <        }
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      /**
# Line 254 | Line 298 | public class AtomicIntegerFieldUpdaterTe
298       */
299      public void testDecrementAndGet() {
300          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
301 <        try {
258 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
259 <        } catch (RuntimeException ok) {
260 <            return;
261 <        }
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 271 | Line 311 | public class AtomicIntegerFieldUpdaterTe
311       */
312      public void testIncrementAndGet() {
313          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
314 <        try {
275 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
276 <        } catch (RuntimeException ok) {
277 <            return;
278 <        }
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