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.23 by jsr166, Fri Jun 10 20:17:11 2011 UTC vs.
Revision 1.31 by jsr166, Mon Nov 9 07:54:28 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
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>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (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>
42 <                a = AtomicLongFieldUpdater.newUpdater
43 <                (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>
54 <                a = AtomicLongFieldUpdater.newUpdater
55 <                (AtomicLongFieldUpdaterTest.class, "w");
109 >            updaterFor("w");
110              shouldThrow();
111 <        } catch (RuntimeException success) {}
111 >        } catch (IllegalArgumentException success) {}
112 >    }
113 >
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 >     * 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      /**
# Line 62 | Line 134 | public class AtomicLongFieldUpdaterTest
134       */
135      public void testGetSet() {
136          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
137 <        try {
66 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
137 >        a = updaterFor("x");
138          x = 1;
139          assertEquals(1, a.get(this));
140          a.set(this, 2);
# Line 80 | Line 148 | public class AtomicLongFieldUpdaterTest
148       */
149      public void testGetLazySet() {
150          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
151 <        try {
84 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
151 >        a = updaterFor("x");
152          x = 1;
153          assertEquals(1, a.get(this));
154          a.lazySet(this, 2);
# Line 98 | Line 162 | public class AtomicLongFieldUpdaterTest
162       */
163      public void testCompareAndSet() {
164          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
165 <        try {
102 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
165 >        a = updaterFor("x");
166          x = 1;
167          assertTrue(a.compareAndSet(this, 1, 2));
168          assertTrue(a.compareAndSet(this, 2, -4));
# Line 114 | Line 174 | public class AtomicLongFieldUpdaterTest
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
205       * to succeed
206       */
207      public void testCompareAndSetInMultipleThreads() throws Exception {
208          x = 1;
209 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
210 <        try {
124 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
209 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
210 >        a = updaterFor("x");
211  
212          Thread t = new Thread(new CheckedRunnable() {
213              public void realRun() {
# Line 145 | Line 228 | public class AtomicLongFieldUpdaterTest
228       */
229      public void testWeakCompareAndSet() {
230          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
231 <        try {
149 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
231 >        a = updaterFor("x");
232          x = 1;
233 <        while (!a.weakCompareAndSet(this, 1, 2));
234 <        while (!a.weakCompareAndSet(this, 2, -4));
233 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
234 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
235          assertEquals(-4, a.get(this));
236 <        while (!a.weakCompareAndSet(this, -4, 7));
236 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
237          assertEquals(7, a.get(this));
238      }
239  
# Line 163 | Line 242 | public class AtomicLongFieldUpdaterTest
242       */
243      public void testGetAndSet() {
244          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
245 <        try {
167 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
245 >        a = updaterFor("x");
246          x = 1;
247          assertEquals(1, a.getAndSet(this, 0));
248          assertEquals(0, a.getAndSet(this, -10));
# Line 179 | Line 254 | public class AtomicLongFieldUpdaterTest
254       */
255      public void testGetAndAdd() {
256          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
257 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
257 >        a = updaterFor("x");
258          x = 1;
259          assertEquals(1, a.getAndAdd(this, 2));
260          assertEquals(3, a.get(this));
# Line 196 | Line 267 | public class AtomicLongFieldUpdaterTest
267       */
268      public void testGetAndDecrement() {
269          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
270 <        try {
200 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 <        } catch (RuntimeException ok) {
202 <            return;
203 <        }
270 >        a = updaterFor("x");
271          x = 1;
272          assertEquals(1, a.getAndDecrement(this));
273          assertEquals(0, a.getAndDecrement(this));
# Line 212 | Line 279 | public class AtomicLongFieldUpdaterTest
279       */
280      public void testGetAndIncrement() {
281          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
282 <        try {
216 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 <        } catch (RuntimeException ok) {
218 <            return;
219 <        }
282 >        a = updaterFor("x");
283          x = 1;
284          assertEquals(1, a.getAndIncrement(this));
285          assertEquals(2, a.get(this));
# Line 232 | Line 295 | public class AtomicLongFieldUpdaterTest
295       */
296      public void testAddAndGet() {
297          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
298 <        try {
236 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 <        } catch (RuntimeException ok) {
238 <            return;
239 <        }
298 >        a = updaterFor("x");
299          x = 1;
300          assertEquals(3, a.addAndGet(this, 2));
301          assertEquals(3, a.get(this));
# Line 249 | Line 308 | public class AtomicLongFieldUpdaterTest
308       */
309      public void testDecrementAndGet() {
310          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
311 <        try {
253 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 <        } catch (RuntimeException ok) {
255 <            return;
256 <        }
311 >        a = updaterFor("x");
312          x = 1;
313          assertEquals(0, a.decrementAndGet(this));
314          assertEquals(-1, a.decrementAndGet(this));
# Line 266 | Line 321 | public class AtomicLongFieldUpdaterTest
321       */
322      public void testIncrementAndGet() {
323          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
324 <        try {
270 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 <        } catch (RuntimeException ok) {
272 <            return;
273 <        }
324 >        a = updaterFor("x");
325          x = 1;
326          assertEquals(2, a.incrementAndGet(this));
327          assertEquals(2, a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines