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.27 by jsr166, Wed Dec 31 19:05:42 2014 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;
# Line 21 | Line 23 | public class AtomicLongFieldUpdaterTest
23          return new TestSuite(AtomicLongFieldUpdaterTest.class);
24      }
25  
26 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
27 +        return AtomicLongFieldUpdater.newUpdater
28 +            (AtomicLongFieldUpdaterTest.class, fieldName);
29 +    }
30 +
31      /**
32       * Construction with non-existent field throws RuntimeException
33       */
34      public void testConstructor() {
35          try {
36 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (AtomicLongFieldUpdaterTest.class, "y");
36 >            updaterFor("y");
37              shouldThrow();
38 <        } catch (RuntimeException success) {}
38 >        } catch (RuntimeException success) {
39 >            assertNotNull(success.getCause());
40 >        }
41      }
42  
43      /**
44 <     * construction with field not of given type throws RuntimeException
44 >     * construction with field not of given type throws IllegalArgumentException
45       */
46      public void testConstructor2() {
47          try {
48 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
42 <                a = AtomicLongFieldUpdater.newUpdater
43 <                (AtomicLongFieldUpdaterTest.class, "z");
48 >            updaterFor("z");
49              shouldThrow();
50 <        } catch (RuntimeException success) {}
50 >        } catch (IllegalArgumentException success) {}
51      }
52  
53      /**
54 <     * construction with non-volatile field throws RuntimeException
54 >     * construction with non-volatile field throws IllegalArgumentException
55       */
56      public void testConstructor3() {
57          try {
58 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
54 <                a = AtomicLongFieldUpdater.newUpdater
55 <                (AtomicLongFieldUpdaterTest.class, "w");
58 >            updaterFor("w");
59              shouldThrow();
60 <        } catch (RuntimeException success) {}
60 >        } catch (IllegalArgumentException success) {}
61      }
62  
63      /**
# Line 62 | Line 65 | public class AtomicLongFieldUpdaterTest
65       */
66      public void testGetSet() {
67          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
68 <        try {
66 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
68 >        a = updaterFor("x");
69          x = 1;
70          assertEquals(1, a.get(this));
71          a.set(this, 2);
# Line 80 | Line 79 | public class AtomicLongFieldUpdaterTest
79       */
80      public void testGetLazySet() {
81          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
82 <        try {
84 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
82 >        a = updaterFor("x");
83          x = 1;
84          assertEquals(1, a.get(this));
85          a.lazySet(this, 2);
# Line 98 | Line 93 | public class AtomicLongFieldUpdaterTest
93       */
94      public void testCompareAndSet() {
95          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
96 <        try {
102 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
96 >        a = updaterFor("x");
97          x = 1;
98          assertTrue(a.compareAndSet(this, 1, 2));
99          assertTrue(a.compareAndSet(this, 2, -4));
# Line 119 | Line 110 | public class AtomicLongFieldUpdaterTest
110       */
111      public void testCompareAndSetInMultipleThreads() throws Exception {
112          x = 1;
113 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
114 <        try {
124 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
113 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
114 >        a = updaterFor("x");
115  
116          Thread t = new Thread(new CheckedRunnable() {
117              public void realRun() {
# Line 145 | Line 132 | public class AtomicLongFieldUpdaterTest
132       */
133      public void testWeakCompareAndSet() {
134          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
135 <        try {
149 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
135 >        a = updaterFor("x");
136          x = 1;
137          while (!a.weakCompareAndSet(this, 1, 2));
138          while (!a.weakCompareAndSet(this, 2, -4));
# Line 163 | Line 146 | public class AtomicLongFieldUpdaterTest
146       */
147      public void testGetAndSet() {
148          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
149 <        try {
167 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
149 >        a = updaterFor("x");
150          x = 1;
151          assertEquals(1, a.getAndSet(this, 0));
152          assertEquals(0, a.getAndSet(this, -10));
# Line 179 | Line 158 | public class AtomicLongFieldUpdaterTest
158       */
159      public void testGetAndAdd() {
160          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
161 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
161 >        a = updaterFor("x");
162          x = 1;
163          assertEquals(1, a.getAndAdd(this, 2));
164          assertEquals(3, a.get(this));
# Line 196 | Line 171 | public class AtomicLongFieldUpdaterTest
171       */
172      public void testGetAndDecrement() {
173          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
174 <        try {
200 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 <        } catch (RuntimeException ok) {
202 <            return;
203 <        }
174 >        a = updaterFor("x");
175          x = 1;
176          assertEquals(1, a.getAndDecrement(this));
177          assertEquals(0, a.getAndDecrement(this));
# Line 212 | Line 183 | public class AtomicLongFieldUpdaterTest
183       */
184      public void testGetAndIncrement() {
185          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
186 <        try {
216 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 <        } catch (RuntimeException ok) {
218 <            return;
219 <        }
186 >        a = updaterFor("x");
187          x = 1;
188          assertEquals(1, a.getAndIncrement(this));
189          assertEquals(2, a.get(this));
# Line 232 | Line 199 | public class AtomicLongFieldUpdaterTest
199       */
200      public void testAddAndGet() {
201          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
202 <        try {
236 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 <        } catch (RuntimeException ok) {
238 <            return;
239 <        }
202 >        a = updaterFor("x");
203          x = 1;
204          assertEquals(3, a.addAndGet(this, 2));
205          assertEquals(3, a.get(this));
# Line 249 | Line 212 | public class AtomicLongFieldUpdaterTest
212       */
213      public void testDecrementAndGet() {
214          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
215 <        try {
253 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 <        } catch (RuntimeException ok) {
255 <            return;
256 <        }
215 >        a = updaterFor("x");
216          x = 1;
217          assertEquals(0, a.decrementAndGet(this));
218          assertEquals(-1, a.decrementAndGet(this));
# Line 266 | Line 225 | public class AtomicLongFieldUpdaterTest
225       */
226      public void testIncrementAndGet() {
227          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
228 <        try {
270 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 <        } catch (RuntimeException ok) {
272 <            return;
273 <        }
228 >        a = updaterFor("x");
229          x = 1;
230          assertEquals(2, a.incrementAndGet(this));
231          assertEquals(2, a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines