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.21 by jsr166, Tue May 31 16:16:23 2011 UTC vs.
Revision 1.26 by jsr166, Thu May 30 03:28:55 2013 UTC

# Line 21 | Line 21 | public class AtomicLongFieldUpdaterTest
21          return new TestSuite(AtomicLongFieldUpdaterTest.class);
22      }
23  
24 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
25 +        return AtomicLongFieldUpdater.newUpdater
26 +            (AtomicLongFieldUpdaterTest.class, fieldName);
27 +    }
28 +
29      /**
30       * Construction with non-existent field throws RuntimeException
31       */
32      public void testConstructor() {
33          try {
34 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (AtomicLongFieldUpdaterTest.class, "y");
34 >            updaterFor("y");
35              shouldThrow();
36 <        } catch (RuntimeException success) {}
36 >        } catch (RuntimeException success) {
37 >            assertNotNull(success.getCause());
38 >        }
39      }
40  
41      /**
42 <     * construction with field not of given type throws RuntimeException
42 >     * construction with field not of given type throws IllegalArgumentException
43       */
44      public void testConstructor2() {
45          try {
46 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
42 <                a = AtomicLongFieldUpdater.newUpdater
43 <                (AtomicLongFieldUpdaterTest.class, "z");
46 >            updaterFor("z");
47              shouldThrow();
48 <        } catch (RuntimeException success) {}
48 >        } catch (IllegalArgumentException success) {}
49      }
50  
51      /**
52 <     * construction with non-volatile field throws RuntimeException
52 >     * construction with non-volatile field throws IllegalArgumentException
53       */
54      public void testConstructor3() {
55          try {
56 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
54 <                a = AtomicLongFieldUpdater.newUpdater
55 <                (AtomicLongFieldUpdaterTest.class, "w");
56 >            updaterFor("w");
57              shouldThrow();
58 <        } catch (RuntimeException success) {}
58 >        } catch (IllegalArgumentException success) {}
59      }
60  
61      /**
# Line 62 | Line 63 | public class AtomicLongFieldUpdaterTest
63       */
64      public void testGetSet() {
65          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
66 <        try {
66 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
66 >        a = updaterFor("x");
67          x = 1;
68 <        assertEquals(1,a.get(this));
69 <        a.set(this,2);
70 <        assertEquals(2,a.get(this));
71 <        a.set(this,-3);
72 <        assertEquals(-3,a.get(this));
68 >        assertEquals(1, a.get(this));
69 >        a.set(this, 2);
70 >        assertEquals(2, a.get(this));
71 >        a.set(this, -3);
72 >        assertEquals(-3, a.get(this));
73      }
74  
75      /**
# Line 80 | Line 77 | public class AtomicLongFieldUpdaterTest
77       */
78      public void testGetLazySet() {
79          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
80 <        try {
84 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
80 >        a = updaterFor("x");
81          x = 1;
82 <        assertEquals(1,a.get(this));
83 <        a.lazySet(this,2);
84 <        assertEquals(2,a.get(this));
85 <        a.lazySet(this,-3);
86 <        assertEquals(-3,a.get(this));
82 >        assertEquals(1, a.get(this));
83 >        a.lazySet(this, 2);
84 >        assertEquals(2, a.get(this));
85 >        a.lazySet(this, -3);
86 >        assertEquals(-3, a.get(this));
87      }
88  
89      /**
# Line 98 | Line 91 | public class AtomicLongFieldUpdaterTest
91       */
92      public void testCompareAndSet() {
93          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
94 <        try {
102 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
94 >        a = updaterFor("x");
95          x = 1;
96 <        assertTrue(a.compareAndSet(this,1,2));
97 <        assertTrue(a.compareAndSet(this,2,-4));
98 <        assertEquals(-4,a.get(this));
99 <        assertFalse(a.compareAndSet(this,-5,7));
100 <        assertEquals(-4,a.get(this));
101 <        assertTrue(a.compareAndSet(this,-4,7));
102 <        assertEquals(7,a.get(this));
96 >        assertTrue(a.compareAndSet(this, 1, 2));
97 >        assertTrue(a.compareAndSet(this, 2, -4));
98 >        assertEquals(-4, a.get(this));
99 >        assertFalse(a.compareAndSet(this, -5, 7));
100 >        assertEquals(-4, a.get(this));
101 >        assertTrue(a.compareAndSet(this, -4, 7));
102 >        assertEquals(7, a.get(this));
103      }
104  
105      /**
# Line 119 | Line 108 | public class AtomicLongFieldUpdaterTest
108       */
109      public void testCompareAndSetInMultipleThreads() throws Exception {
110          x = 1;
111 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
112 <        try {
124 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
111 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
112 >        a = updaterFor("x");
113  
114          Thread t = new Thread(new CheckedRunnable() {
115              public void realRun() {
# Line 136 | Line 121 | public class AtomicLongFieldUpdaterTest
121          assertTrue(a.compareAndSet(this, 1, 2));
122          t.join(LONG_DELAY_MS);
123          assertFalse(t.isAlive());
124 <        assertEquals(a.get(this), 3);
124 >        assertEquals(3, a.get(this));
125      }
126  
127      /**
# Line 145 | Line 130 | public class AtomicLongFieldUpdaterTest
130       */
131      public void testWeakCompareAndSet() {
132          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
133 <        try {
149 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
133 >        a = updaterFor("x");
134          x = 1;
135 <        while (!a.weakCompareAndSet(this,1,2));
136 <        while (!a.weakCompareAndSet(this,2,-4));
137 <        assertEquals(-4,a.get(this));
138 <        while (!a.weakCompareAndSet(this,-4,7));
139 <        assertEquals(7,a.get(this));
135 >        while (!a.weakCompareAndSet(this, 1, 2));
136 >        while (!a.weakCompareAndSet(this, 2, -4));
137 >        assertEquals(-4, a.get(this));
138 >        while (!a.weakCompareAndSet(this, -4, 7));
139 >        assertEquals(7, a.get(this));
140      }
141  
142      /**
# Line 163 | Line 144 | public class AtomicLongFieldUpdaterTest
144       */
145      public void testGetAndSet() {
146          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
147 <        try {
167 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
147 >        a = updaterFor("x");
148          x = 1;
149 <        assertEquals(1,a.getAndSet(this, 0));
150 <        assertEquals(0,a.getAndSet(this,-10));
151 <        assertEquals(-10,a.getAndSet(this,1));
149 >        assertEquals(1, a.getAndSet(this, 0));
150 >        assertEquals(0, a.getAndSet(this, -10));
151 >        assertEquals(-10, a.getAndSet(this, 1));
152      }
153  
154      /**
# Line 179 | Line 156 | public class AtomicLongFieldUpdaterTest
156       */
157      public void testGetAndAdd() {
158          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
159 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
159 >        a = updaterFor("x");
160          x = 1;
161 <        assertEquals(1,a.getAndAdd(this,2));
162 <        assertEquals(3,a.get(this));
163 <        assertEquals(3,a.getAndAdd(this,-4));
164 <        assertEquals(-1,a.get(this));
161 >        assertEquals(1, a.getAndAdd(this, 2));
162 >        assertEquals(3, a.get(this));
163 >        assertEquals(3, a.getAndAdd(this, -4));
164 >        assertEquals(-1, a.get(this));
165      }
166  
167      /**
# Line 196 | Line 169 | public class AtomicLongFieldUpdaterTest
169       */
170      public void testGetAndDecrement() {
171          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
172 <        try {
200 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 <        } catch (RuntimeException ok) {
202 <            return;
203 <        }
172 >        a = updaterFor("x");
173          x = 1;
174 <        assertEquals(1,a.getAndDecrement(this));
175 <        assertEquals(0,a.getAndDecrement(this));
176 <        assertEquals(-1,a.getAndDecrement(this));
174 >        assertEquals(1, a.getAndDecrement(this));
175 >        assertEquals(0, a.getAndDecrement(this));
176 >        assertEquals(-1, a.getAndDecrement(this));
177      }
178  
179      /**
# Line 212 | Line 181 | public class AtomicLongFieldUpdaterTest
181       */
182      public void testGetAndIncrement() {
183          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
184 <        try {
216 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 <        } catch (RuntimeException ok) {
218 <            return;
219 <        }
184 >        a = updaterFor("x");
185          x = 1;
186 <        assertEquals(1,a.getAndIncrement(this));
187 <        assertEquals(2,a.get(this));
188 <        a.set(this,-2);
189 <        assertEquals(-2,a.getAndIncrement(this));
190 <        assertEquals(-1,a.getAndIncrement(this));
191 <        assertEquals(0,a.getAndIncrement(this));
192 <        assertEquals(1,a.get(this));
186 >        assertEquals(1, a.getAndIncrement(this));
187 >        assertEquals(2, a.get(this));
188 >        a.set(this, -2);
189 >        assertEquals(-2, a.getAndIncrement(this));
190 >        assertEquals(-1, a.getAndIncrement(this));
191 >        assertEquals(0, a.getAndIncrement(this));
192 >        assertEquals(1, a.get(this));
193      }
194  
195      /**
# Line 232 | Line 197 | public class AtomicLongFieldUpdaterTest
197       */
198      public void testAddAndGet() {
199          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 <        try {
236 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 <        } catch (RuntimeException ok) {
238 <            return;
239 <        }
200 >        a = updaterFor("x");
201          x = 1;
202 <        assertEquals(3,a.addAndGet(this,2));
203 <        assertEquals(3,a.get(this));
204 <        assertEquals(-1,a.addAndGet(this,-4));
205 <        assertEquals(-1,a.get(this));
202 >        assertEquals(3, a.addAndGet(this, 2));
203 >        assertEquals(3, a.get(this));
204 >        assertEquals(-1, a.addAndGet(this, -4));
205 >        assertEquals(-1, a.get(this));
206      }
207  
208      /**
# Line 249 | Line 210 | public class AtomicLongFieldUpdaterTest
210       */
211      public void testDecrementAndGet() {
212          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
213 <        try {
253 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 <        } catch (RuntimeException ok) {
255 <            return;
256 <        }
213 >        a = updaterFor("x");
214          x = 1;
215 <        assertEquals(0,a.decrementAndGet(this));
216 <        assertEquals(-1,a.decrementAndGet(this));
217 <        assertEquals(-2,a.decrementAndGet(this));
218 <        assertEquals(-2,a.get(this));
215 >        assertEquals(0, a.decrementAndGet(this));
216 >        assertEquals(-1, a.decrementAndGet(this));
217 >        assertEquals(-2, a.decrementAndGet(this));
218 >        assertEquals(-2, a.get(this));
219      }
220  
221      /**
# Line 266 | Line 223 | public class AtomicLongFieldUpdaterTest
223       */
224      public void testIncrementAndGet() {
225          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
226 <        try {
270 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 <        } catch (RuntimeException ok) {
272 <            return;
273 <        }
226 >        a = updaterFor("x");
227          x = 1;
228 <        assertEquals(2,a.incrementAndGet(this));
229 <        assertEquals(2,a.get(this));
230 <        a.set(this,-2);
231 <        assertEquals(-1,a.incrementAndGet(this));
232 <        assertEquals(0,a.incrementAndGet(this));
233 <        assertEquals(1,a.incrementAndGet(this));
234 <        assertEquals(1,a.get(this));
228 >        assertEquals(2, a.incrementAndGet(this));
229 >        assertEquals(2, a.get(this));
230 >        a.set(this, -2);
231 >        assertEquals(-1, a.incrementAndGet(this));
232 >        assertEquals(0, a.incrementAndGet(this));
233 >        assertEquals(1, a.incrementAndGet(this));
234 >        assertEquals(1, a.get(this));
235      }
236  
237   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines