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.22 by jsr166, Fri Jun 10 20:01:21 2011 UTC vs.
Revision 1.34 by jsr166, Wed Sep 20 00:59:17 2017 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 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
28 +        return AtomicLongFieldUpdater.newUpdater
29 +            (AtomicLongFieldUpdaterTest.class, fieldName);
30 +    }
31 +
32      /**
33       * Construction with non-existent field throws RuntimeException
34       */
35      public void testConstructor() {
36          try {
37 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (AtomicLongFieldUpdaterTest.class, "y");
37 >            updaterFor("y");
38              shouldThrow();
39 <        } catch (RuntimeException success) {}
39 >        } catch (RuntimeException success) {
40 >            assertNotNull(success.getCause());
41 >        }
42      }
43  
44      /**
45 <     * construction with field not of given type throws RuntimeException
45 >     * construction with field not of given type throws IllegalArgumentException
46       */
47      public void testConstructor2() {
48          try {
49 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
42 <                a = AtomicLongFieldUpdater.newUpdater
43 <                (AtomicLongFieldUpdaterTest.class, "z");
49 >            updaterFor("z");
50              shouldThrow();
51 <        } catch (RuntimeException success) {}
51 >        } catch (IllegalArgumentException success) {}
52      }
53  
54      /**
55 <     * construction with non-volatile field throws RuntimeException
55 >     * construction with non-volatile field throws IllegalArgumentException
56       */
57      public void testConstructor3() {
58          try {
59 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
54 <                a = AtomicLongFieldUpdater.newUpdater
55 <                (AtomicLongFieldUpdaterTest.class, "w");
59 >            updaterFor("w");
60              shouldThrow();
61 <        } catch (RuntimeException success) {}
61 >        } catch (IllegalArgumentException success) {}
62 >    }
63 >
64 >    /**
65 >     * construction using private field from subclass throws RuntimeException
66 >     */
67 >    public void testPrivateFieldInSubclass() {
68 >        new NonNestmates.AtomicLongFieldUpdaterTestSubclass()
69 >            .checkPrivateAccess();
70 >    }
71 >
72 >    /**
73 >     * construction from unrelated class; package access is allowed,
74 >     * private access is not
75 >     */
76 >    public void testUnrelatedClassAccess() {
77 >        new NonNestmates().checkPackageAccess(this);
78 >        new NonNestmates().checkPrivateAccess(this);
79      }
80  
81      /**
# Line 62 | Line 83 | public class AtomicLongFieldUpdaterTest
83       */
84      public void testGetSet() {
85          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
86 <        try {
66 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
86 >        a = updaterFor("x");
87          x = 1;
88          assertEquals(1, a.get(this));
89          a.set(this, 2);
# Line 80 | Line 97 | public class AtomicLongFieldUpdaterTest
97       */
98      public void testGetLazySet() {
99          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
100 <        try {
84 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
100 >        a = updaterFor("x");
101          x = 1;
102          assertEquals(1, a.get(this));
103          a.lazySet(this, 2);
# Line 98 | Line 111 | public class AtomicLongFieldUpdaterTest
111       */
112      public void testCompareAndSet() {
113          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
114 <        try {
102 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
114 >        a = updaterFor("x");
115          x = 1;
116          assertTrue(a.compareAndSet(this, 1, 2));
117          assertTrue(a.compareAndSet(this, 2, -4));
# Line 114 | Line 123 | public class AtomicLongFieldUpdaterTest
123      }
124  
125      /**
126 +     * compareAndSet succeeds in changing protected field value if
127 +     * equal to expected else fails
128 +     */
129 +    public void testCompareAndSetProtected() {
130 +        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
131 +        a = updaterFor("protectedField");
132 +        protectedField = 1;
133 +        assertTrue(a.compareAndSet(this, 1, 2));
134 +        assertTrue(a.compareAndSet(this, 2, -4));
135 +        assertEquals(-4, a.get(this));
136 +        assertFalse(a.compareAndSet(this, -5, 7));
137 +        assertEquals(-4, a.get(this));
138 +        assertTrue(a.compareAndSet(this, -4, 7));
139 +        assertEquals(7, a.get(this));
140 +    }
141 +
142 +    /**
143 +     * compareAndSet succeeds in changing protected field value if
144 +     * equal to expected else fails
145 +     */
146 +    public void testCompareAndSetProtectedInSubclass() {
147 +        new NonNestmates.AtomicLongFieldUpdaterTestSubclass()
148 +            .checkCompareAndSetProtectedSub();
149 +    }
150 +
151 +    /**
152       * compareAndSet in one thread enables another waiting for value
153       * to succeed
154       */
155      public void testCompareAndSetInMultipleThreads() throws Exception {
156          x = 1;
157 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
158 <        try {
124 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
157 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
158 >        a = updaterFor("x");
159  
160          Thread t = new Thread(new CheckedRunnable() {
161              public void realRun() {
# Line 136 | Line 167 | public class AtomicLongFieldUpdaterTest
167          assertTrue(a.compareAndSet(this, 1, 2));
168          t.join(LONG_DELAY_MS);
169          assertFalse(t.isAlive());
170 <        assertEquals(a.get(this), 3);
170 >        assertEquals(3, a.get(this));
171      }
172  
173      /**
# Line 145 | Line 176 | public class AtomicLongFieldUpdaterTest
176       */
177      public void testWeakCompareAndSet() {
178          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
179 <        try {
149 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
179 >        a = updaterFor("x");
180          x = 1;
181 <        while (!a.weakCompareAndSet(this, 1, 2));
182 <        while (!a.weakCompareAndSet(this, 2, -4));
181 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
182 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
183          assertEquals(-4, a.get(this));
184 <        while (!a.weakCompareAndSet(this, -4, 7));
184 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
185          assertEquals(7, a.get(this));
186      }
187  
# Line 163 | Line 190 | public class AtomicLongFieldUpdaterTest
190       */
191      public void testGetAndSet() {
192          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
193 <        try {
167 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
193 >        a = updaterFor("x");
194          x = 1;
195          assertEquals(1, a.getAndSet(this, 0));
196          assertEquals(0, a.getAndSet(this, -10));
# Line 179 | Line 202 | public class AtomicLongFieldUpdaterTest
202       */
203      public void testGetAndAdd() {
204          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
205 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
205 >        a = updaterFor("x");
206          x = 1;
207          assertEquals(1, a.getAndAdd(this, 2));
208          assertEquals(3, a.get(this));
# Line 196 | Line 215 | public class AtomicLongFieldUpdaterTest
215       */
216      public void testGetAndDecrement() {
217          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
218 <        try {
200 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 <        } catch (RuntimeException ok) {
202 <            return;
203 <        }
218 >        a = updaterFor("x");
219          x = 1;
220          assertEquals(1, a.getAndDecrement(this));
221          assertEquals(0, a.getAndDecrement(this));
# Line 212 | Line 227 | public class AtomicLongFieldUpdaterTest
227       */
228      public void testGetAndIncrement() {
229          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
230 <        try {
216 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 <        } catch (RuntimeException ok) {
218 <            return;
219 <        }
230 >        a = updaterFor("x");
231          x = 1;
232          assertEquals(1, a.getAndIncrement(this));
233          assertEquals(2, a.get(this));
# Line 232 | Line 243 | public class AtomicLongFieldUpdaterTest
243       */
244      public void testAddAndGet() {
245          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
246 <        try {
236 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 <        } catch (RuntimeException ok) {
238 <            return;
239 <        }
246 >        a = updaterFor("x");
247          x = 1;
248          assertEquals(3, a.addAndGet(this, 2));
249          assertEquals(3, a.get(this));
# Line 249 | Line 256 | public class AtomicLongFieldUpdaterTest
256       */
257      public void testDecrementAndGet() {
258          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
259 <        try {
253 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 <        } catch (RuntimeException ok) {
255 <            return;
256 <        }
259 >        a = updaterFor("x");
260          x = 1;
261          assertEquals(0, a.decrementAndGet(this));
262          assertEquals(-1, a.decrementAndGet(this));
# Line 266 | Line 269 | public class AtomicLongFieldUpdaterTest
269       */
270      public void testIncrementAndGet() {
271          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
272 <        try {
270 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 <        } catch (RuntimeException ok) {
272 <            return;
273 <        }
272 >        a = updaterFor("x");
273          x = 1;
274          assertEquals(2, a.incrementAndGet(this));
275          assertEquals(2, a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines