ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java (file contents):
Revision 1.35 by jsr166, Sat Mar 18 20:42:20 2017 UTC vs.
Revision 1.38 by dl, Tue Jan 26 13:33:05 2021 UTC

# Line 12 | Line 12 | import junit.framework.Test;
12   import junit.framework.TestSuite;
13  
14   public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
15 <    volatile Integer x = null;
16 <    protected volatile Integer protectedField;
17 <    private volatile Integer privateField;
15 >    volatile Item x = null;
16 >    protected volatile Item protectedField;
17 >    private volatile Item privateField;
18      Object z;
19 <    Integer w;
19 >    Item w;
20      volatile int i;
21  
22      public static void main(String[] args) {
# Line 26 | Line 26 | public class AtomicReferenceFieldUpdater
26          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
27      }
28  
29 <    // for testing subclass access
30 <    static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest {
31 <        public void checkPrivateAccess() {
32 <            try {
33 <                AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
34 <                    AtomicReferenceFieldUpdater.newUpdater
35 <                    (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
36 <                shouldThrow();
37 <            } catch (RuntimeException success) {
38 <                assertNotNull(success.getCause());
39 <            }
40 <        }
41 <
42 <        public void checkCompareAndSetProtectedSub() {
43 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
44 <                AtomicReferenceFieldUpdater.newUpdater
45 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
46 <            this.protectedField = one;
47 <            assertTrue(a.compareAndSet(this, one, two));
48 <            assertTrue(a.compareAndSet(this, two, m4));
49 <            assertSame(m4, a.get(this));
50 <            assertFalse(a.compareAndSet(this, m5, seven));
51 <            assertNotSame(seven, a.get(this));
52 <            assertTrue(a.compareAndSet(this, m4, seven));
53 <            assertSame(seven, a.get(this));
54 <        }
55 <    }
56 <
57 <    static class UnrelatedClass {
58 <        public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
59 <            obj.x = one;
60 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
61 <                AtomicReferenceFieldUpdater.newUpdater
62 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
63 <            assertSame(one, a.get(obj));
64 <            assertTrue(a.compareAndSet(obj, one, two));
65 <            assertSame(two, a.get(obj));
66 <        }
67 <
68 <        public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
69 <            try {
70 <                AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
71 <                    AtomicReferenceFieldUpdater.newUpdater
72 <                    (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
73 <                throw new AssertionError("should throw");
74 <            } catch (RuntimeException success) {
75 <                assertNotNull(success.getCause());
76 <            }
77 <        }
78 <    }
79 <
80 <    static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
29 >    static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> updaterFor(String fieldName) {
30          return AtomicReferenceFieldUpdater.newUpdater
31 <            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
31 >            (AtomicReferenceFieldUpdaterTest.class, Item.class, fieldName);
32      }
33  
34      /**
# Line 128 | Line 77 | public class AtomicReferenceFieldUpdater
77       * construction using private field from subclass throws RuntimeException
78       */
79      public void testPrivateFieldInSubclass() {
80 <        AtomicReferenceFieldUpdaterTestSubclass s =
81 <            new AtomicReferenceFieldUpdaterTestSubclass();
133 <        s.checkPrivateAccess();
80 >        new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass()
81 >            .checkPrivateAccess();
82      }
83  
84      /**
# Line 138 | Line 86 | public class AtomicReferenceFieldUpdater
86       * private access is not
87       */
88      public void testUnrelatedClassAccess() {
89 <        new UnrelatedClass().checkPackageAccess(this);
90 <        new UnrelatedClass().checkPrivateAccess(this);
89 >        new NonNestmates().checkPackageAccess(this);
90 >        new NonNestmates().checkPrivateAccess(this);
91      }
92  
93      /**
94       * get returns the last value set or assigned
95       */
96      public void testGetSet() {
97 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
97 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
98          a = updaterFor("x");
99          x = one;
100          assertSame(one, a.get(this));
101          a.set(this, two);
102          assertSame(two, a.get(this));
103 <        a.set(this, m3);
104 <        assertSame(m3, a.get(this));
103 >        a.set(this, minusThree);
104 >        assertSame(minusThree, a.get(this));
105      }
106  
107      /**
108       * get returns the last value lazySet by same thread
109       */
110      public void testGetLazySet() {
111 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
111 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
112          a = updaterFor("x");
113          x = one;
114          assertSame(one, a.get(this));
115          a.lazySet(this, two);
116          assertSame(two, a.get(this));
117 <        a.lazySet(this, m3);
118 <        assertSame(m3, a.get(this));
117 >        a.lazySet(this, minusThree);
118 >        assertSame(minusThree, a.get(this));
119      }
120  
121      /**
122 <     * compareAndSet succeeds in changing value if equal to expected else fails
122 >     * compareAndSet succeeds in changing value if same as expected else fails
123       */
124      public void testCompareAndSet() {
125 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
125 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
126          a = updaterFor("x");
127          x = one;
128          assertTrue(a.compareAndSet(this, one, two));
129 <        assertTrue(a.compareAndSet(this, two, m4));
130 <        assertSame(m4, a.get(this));
131 <        assertFalse(a.compareAndSet(this, m5, seven));
129 >        assertTrue(a.compareAndSet(this, two, minusFour));
130 >        assertSame(minusFour, a.get(this));
131 >        assertFalse(a.compareAndSet(this, minusFive, seven));
132          assertNotSame(seven, a.get(this));
133 <        assertTrue(a.compareAndSet(this, m4, seven));
133 >        assertTrue(a.compareAndSet(this, minusFour, seven));
134          assertSame(seven, a.get(this));
135      }
136  
137      /**
138 +     * compareAndSet succeeds in changing protected field value if
139 +     * same as expected else fails
140 +     */
141 +    public void testCompareAndSetProtectedInSubclass() {
142 +        new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass()
143 +            .checkCompareAndSetProtectedSub();
144 +    }
145 +
146 +    /**
147       * compareAndSet in one thread enables another waiting for value
148       * to succeed
149       */
150      public void testCompareAndSetInMultipleThreads() throws Exception {
151          x = one;
152 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
152 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
153          a = updaterFor("x");
154  
155          Thread t = new Thread(new CheckedRunnable() {
# Line 209 | Line 166 | public class AtomicReferenceFieldUpdater
166      }
167  
168      /**
169 <     * repeated weakCompareAndSet succeeds in changing value when equal
213 <     * to expected
169 >     * repeated weakCompareAndSet succeeds in changing value when same as expected
170       */
171      public void testWeakCompareAndSet() {
172 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
172 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
173          a = updaterFor("x");
174          x = one;
175          do {} while (!a.weakCompareAndSet(this, one, two));
176 <        do {} while (!a.weakCompareAndSet(this, two, m4));
177 <        assertSame(m4, a.get(this));
178 <        do {} while (!a.weakCompareAndSet(this, m4, seven));
176 >        do {} while (!a.weakCompareAndSet(this, two, minusFour));
177 >        assertSame(minusFour, a.get(this));
178 >        do {} while (!a.weakCompareAndSet(this, minusFour, seven));
179          assertSame(seven, a.get(this));
180      }
181  
# Line 227 | Line 183 | public class AtomicReferenceFieldUpdater
183       * getAndSet returns previous value and sets to given value
184       */
185      public void testGetAndSet() {
186 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
186 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Item> a;
187          a = updaterFor("x");
188          x = one;
189          assertSame(one, a.getAndSet(this, zero));
190 <        assertSame(zero, a.getAndSet(this, m10));
191 <        assertSame(m10, a.getAndSet(this, 1));
190 >        assertSame(zero, a.getAndSet(this, minusTen));
191 >        assertSame(minusTen, a.getAndSet(this, one));
192      }
193  
194   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines