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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines