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.27 by jsr166, Wed Dec 31 19:05:42 2014 UTC vs.
Revision 1.31 by jsr166, Mon Nov 9 07:54:28 2015 UTC

# Line 13 | Line 13 | 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);
# Line 61 | Line 112 | public class AtomicLongFieldUpdaterTest
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 +    /**
133       * get returns the last value set or assigned
134       */
135      public void testGetSet() {
# Line 105 | 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       */
# Line 134 | Line 230 | public class AtomicLongFieldUpdaterTest
230          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines