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.30 by dl, Sun Nov 8 15:34:01 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 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>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (AtomicLongFieldUpdaterTest.class, "y");
78 >            updaterFor("y");
79              shouldThrow();
80 <        } catch (RuntimeException success) {}
80 >        } catch (RuntimeException success) {
81 >            assertNotNull(success.getCause());
82 >        }
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>
42 <                a = AtomicLongFieldUpdater.newUpdater
43 <                (AtomicLongFieldUpdaterTest.class, "z");
90 >            updaterFor("z");
91              shouldThrow();
92 <        } catch (RuntimeException success) {}
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>
54 <                a = AtomicLongFieldUpdater.newUpdater
55 <                (AtomicLongFieldUpdaterTest.class, "w");
100 >            updaterFor("w");
101              shouldThrow();
102 <        } catch (RuntimeException success) {}
102 >        } catch (IllegalArgumentException success) {}
103 >    }
104 >
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 >     * construction from unrelated class throws RuntimeException
116 >     */
117 >    public void testUnrelatedClassAccess() {
118 >        UnrelatedClass s = new UnrelatedClass();
119 >        s.checkPrivateAccess();
120      }
121  
122      /**
# Line 62 | Line 124 | public class AtomicLongFieldUpdaterTest
124       */
125      public void testGetSet() {
126          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
127 <        try {
66 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
127 >        a = updaterFor("x");
128          x = 1;
129          assertEquals(1, a.get(this));
130          a.set(this, 2);
# Line 80 | Line 138 | public class AtomicLongFieldUpdaterTest
138       */
139      public void testGetLazySet() {
140          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
141 <        try {
84 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
141 >        a = updaterFor("x");
142          x = 1;
143          assertEquals(1, a.get(this));
144          a.lazySet(this, 2);
# Line 98 | Line 152 | public class AtomicLongFieldUpdaterTest
152       */
153      public void testCompareAndSet() {
154          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
155 <        try {
102 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
155 >        a = updaterFor("x");
156          x = 1;
157          assertTrue(a.compareAndSet(this, 1, 2));
158          assertTrue(a.compareAndSet(this, 2, -4));
# Line 114 | Line 164 | public class AtomicLongFieldUpdaterTest
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
195       * to succeed
196       */
197      public void testCompareAndSetInMultipleThreads() throws Exception {
198          x = 1;
199 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
200 <        try {
124 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
199 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 >        a = updaterFor("x");
201  
202          Thread t = new Thread(new CheckedRunnable() {
203              public void realRun() {
# Line 145 | Line 218 | public class AtomicLongFieldUpdaterTest
218       */
219      public void testWeakCompareAndSet() {
220          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
221 <        try {
149 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
221 >        a = updaterFor("x");
222          x = 1;
223 <        while (!a.weakCompareAndSet(this, 1, 2));
224 <        while (!a.weakCompareAndSet(this, 2, -4));
223 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
224 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
225          assertEquals(-4, a.get(this));
226 <        while (!a.weakCompareAndSet(this, -4, 7));
226 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
227          assertEquals(7, a.get(this));
228      }
229  
# Line 163 | Line 232 | public class AtomicLongFieldUpdaterTest
232       */
233      public void testGetAndSet() {
234          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
235 <        try {
167 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
235 >        a = updaterFor("x");
236          x = 1;
237          assertEquals(1, a.getAndSet(this, 0));
238          assertEquals(0, a.getAndSet(this, -10));
# Line 179 | Line 244 | public class AtomicLongFieldUpdaterTest
244       */
245      public void testGetAndAdd() {
246          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
247 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
247 >        a = updaterFor("x");
248          x = 1;
249          assertEquals(1, a.getAndAdd(this, 2));
250          assertEquals(3, a.get(this));
# Line 196 | Line 257 | public class AtomicLongFieldUpdaterTest
257       */
258      public void testGetAndDecrement() {
259          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
260 <        try {
200 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 <        } catch (RuntimeException ok) {
202 <            return;
203 <        }
260 >        a = updaterFor("x");
261          x = 1;
262          assertEquals(1, a.getAndDecrement(this));
263          assertEquals(0, a.getAndDecrement(this));
# Line 212 | Line 269 | public class AtomicLongFieldUpdaterTest
269       */
270      public void testGetAndIncrement() {
271          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
272 <        try {
216 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 <        } catch (RuntimeException ok) {
218 <            return;
219 <        }
272 >        a = updaterFor("x");
273          x = 1;
274          assertEquals(1, a.getAndIncrement(this));
275          assertEquals(2, a.get(this));
# Line 232 | Line 285 | public class AtomicLongFieldUpdaterTest
285       */
286      public void testAddAndGet() {
287          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
288 <        try {
236 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 <        } catch (RuntimeException ok) {
238 <            return;
239 <        }
288 >        a = updaterFor("x");
289          x = 1;
290          assertEquals(3, a.addAndGet(this, 2));
291          assertEquals(3, a.get(this));
# Line 249 | Line 298 | public class AtomicLongFieldUpdaterTest
298       */
299      public void testDecrementAndGet() {
300          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
301 <        try {
253 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 <        } catch (RuntimeException ok) {
255 <            return;
256 <        }
301 >        a = updaterFor("x");
302          x = 1;
303          assertEquals(0, a.decrementAndGet(this));
304          assertEquals(-1, a.decrementAndGet(this));
# Line 266 | Line 311 | public class AtomicLongFieldUpdaterTest
311       */
312      public void testIncrementAndGet() {
313          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
314 <        try {
270 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 <        } catch (RuntimeException ok) {
272 <            return;
273 <        }
314 >        a = updaterFor("x");
315          x = 1;
316          assertEquals(2, a.incrementAndGet(this));
317          assertEquals(2, a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines