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.18 by jsr166, Sat Oct 9 19:30:34 2010 UTC vs.
Revision 1.29 by jsr166, Sat Apr 25 04:55:30 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import java.util.concurrent.atomic.*;
10 < import junit.framework.*;
11 < import java.util.*;
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;
# Line 16 | Line 17 | public class AtomicLongFieldUpdaterTest
17      long w;
18  
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run(suite());
20 >        main(suite(), args);
21      }
22      public static Test suite() {
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>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (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>
43 <                a = AtomicLongFieldUpdater.newUpdater
44 <                (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>
55 <                a = AtomicLongFieldUpdater.newUpdater
56 <                (AtomicLongFieldUpdaterTest.class, "w");
58 >            updaterFor("w");
59              shouldThrow();
60 <        } catch (RuntimeException success) {}
60 >        } catch (IllegalArgumentException success) {}
61      }
62  
63      /**
# Line 63 | Line 65 | public class AtomicLongFieldUpdaterTest
65       */
66      public void testGetSet() {
67          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
68 <        try {
67 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
68 <        } catch (RuntimeException ok) {
69 <            return;
70 <        }
68 >        a = updaterFor("x");
69          x = 1;
70 <        assertEquals(1,a.get(this));
71 <        a.set(this,2);
72 <        assertEquals(2,a.get(this));
73 <        a.set(this,-3);
74 <        assertEquals(-3,a.get(this));
70 >        assertEquals(1, a.get(this));
71 >        a.set(this, 2);
72 >        assertEquals(2, a.get(this));
73 >        a.set(this, -3);
74 >        assertEquals(-3, a.get(this));
75      }
76  
77      /**
# Line 81 | Line 79 | public class AtomicLongFieldUpdaterTest
79       */
80      public void testGetLazySet() {
81          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
82 <        try {
85 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
86 <        } catch (RuntimeException ok) {
87 <            return;
88 <        }
82 >        a = updaterFor("x");
83          x = 1;
84 <        assertEquals(1,a.get(this));
85 <        a.lazySet(this,2);
86 <        assertEquals(2,a.get(this));
87 <        a.lazySet(this,-3);
88 <        assertEquals(-3,a.get(this));
84 >        assertEquals(1, a.get(this));
85 >        a.lazySet(this, 2);
86 >        assertEquals(2, a.get(this));
87 >        a.lazySet(this, -3);
88 >        assertEquals(-3, a.get(this));
89      }
90  
97
91      /**
92       * compareAndSet succeeds in changing value if equal to expected else fails
93       */
94      public void testCompareAndSet() {
95          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
96 <        try {
104 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
105 <        } catch (RuntimeException ok) {
106 <            return;
107 <        }
96 >        a = updaterFor("x");
97          x = 1;
98 <        assertTrue(a.compareAndSet(this,1,2));
99 <        assertTrue(a.compareAndSet(this,2,-4));
100 <        assertEquals(-4,a.get(this));
101 <        assertFalse(a.compareAndSet(this,-5,7));
102 <        assertEquals(-4,a.get(this));
103 <        assertTrue(a.compareAndSet(this,-4,7));
104 <        assertEquals(7,a.get(this));
98 >        assertTrue(a.compareAndSet(this, 1, 2));
99 >        assertTrue(a.compareAndSet(this, 2, -4));
100 >        assertEquals(-4, a.get(this));
101 >        assertFalse(a.compareAndSet(this, -5, 7));
102 >        assertEquals(-4, a.get(this));
103 >        assertTrue(a.compareAndSet(this, -4, 7));
104 >        assertEquals(7, a.get(this));
105      }
106  
118
107      /**
108       * compareAndSet in one thread enables another waiting for value
109       * to succeed
110       */
111      public void testCompareAndSetInMultipleThreads() throws Exception {
112          x = 1;
113 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
114 <        try {
127 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
128 <        } catch (RuntimeException ok) {
129 <            return;
130 <        }
113 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
114 >        a = updaterFor("x");
115  
116          Thread t = new Thread(new CheckedRunnable() {
117              public void realRun() {
# Line 139 | Line 123 | public class AtomicLongFieldUpdaterTest
123          assertTrue(a.compareAndSet(this, 1, 2));
124          t.join(LONG_DELAY_MS);
125          assertFalse(t.isAlive());
126 <        assertEquals(a.get(this), 3);
126 >        assertEquals(3, a.get(this));
127      }
128  
129      /**
# Line 148 | Line 132 | public class AtomicLongFieldUpdaterTest
132       */
133      public void testWeakCompareAndSet() {
134          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
135 <        try {
152 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
153 <        } catch (RuntimeException ok) {
154 <            return;
155 <        }
135 >        a = updaterFor("x");
136          x = 1;
137 <        while (!a.weakCompareAndSet(this,1,2));
138 <        while (!a.weakCompareAndSet(this,2,-4));
139 <        assertEquals(-4,a.get(this));
140 <        while (!a.weakCompareAndSet(this,-4,7));
141 <        assertEquals(7,a.get(this));
137 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
138 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
139 >        assertEquals(-4, a.get(this));
140 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
141 >        assertEquals(7, a.get(this));
142      }
143  
144      /**
# Line 166 | Line 146 | public class AtomicLongFieldUpdaterTest
146       */
147      public void testGetAndSet() {
148          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
149 <        try {
170 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
171 <        } catch (RuntimeException ok) {
172 <            return;
173 <        }
149 >        a = updaterFor("x");
150          x = 1;
151 <        assertEquals(1,a.getAndSet(this, 0));
152 <        assertEquals(0,a.getAndSet(this,-10));
153 <        assertEquals(-10,a.getAndSet(this,1));
151 >        assertEquals(1, a.getAndSet(this, 0));
152 >        assertEquals(0, a.getAndSet(this, -10));
153 >        assertEquals(-10, a.getAndSet(this, 1));
154      }
155  
156      /**
# Line 182 | Line 158 | public class AtomicLongFieldUpdaterTest
158       */
159      public void testGetAndAdd() {
160          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
161 <        try {
186 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
187 <        } catch (RuntimeException ok) {
188 <            return;
189 <        }
161 >        a = updaterFor("x");
162          x = 1;
163 <        assertEquals(1,a.getAndAdd(this,2));
164 <        assertEquals(3,a.get(this));
165 <        assertEquals(3,a.getAndAdd(this,-4));
166 <        assertEquals(-1,a.get(this));
163 >        assertEquals(1, a.getAndAdd(this, 2));
164 >        assertEquals(3, a.get(this));
165 >        assertEquals(3, a.getAndAdd(this, -4));
166 >        assertEquals(-1, a.get(this));
167      }
168  
169      /**
# Line 199 | Line 171 | public class AtomicLongFieldUpdaterTest
171       */
172      public void testGetAndDecrement() {
173          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
174 <        try {
203 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
204 <        } catch (RuntimeException ok) {
205 <            return;
206 <        }
174 >        a = updaterFor("x");
175          x = 1;
176 <        assertEquals(1,a.getAndDecrement(this));
177 <        assertEquals(0,a.getAndDecrement(this));
178 <        assertEquals(-1,a.getAndDecrement(this));
176 >        assertEquals(1, a.getAndDecrement(this));
177 >        assertEquals(0, a.getAndDecrement(this));
178 >        assertEquals(-1, a.getAndDecrement(this));
179      }
180  
181      /**
# Line 215 | Line 183 | public class AtomicLongFieldUpdaterTest
183       */
184      public void testGetAndIncrement() {
185          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
186 <        try {
219 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
220 <        } catch (RuntimeException ok) {
221 <            return;
222 <        }
186 >        a = updaterFor("x");
187          x = 1;
188 <        assertEquals(1,a.getAndIncrement(this));
189 <        assertEquals(2,a.get(this));
190 <        a.set(this,-2);
191 <        assertEquals(-2,a.getAndIncrement(this));
192 <        assertEquals(-1,a.getAndIncrement(this));
193 <        assertEquals(0,a.getAndIncrement(this));
194 <        assertEquals(1,a.get(this));
188 >        assertEquals(1, a.getAndIncrement(this));
189 >        assertEquals(2, a.get(this));
190 >        a.set(this, -2);
191 >        assertEquals(-2, a.getAndIncrement(this));
192 >        assertEquals(-1, a.getAndIncrement(this));
193 >        assertEquals(0, a.getAndIncrement(this));
194 >        assertEquals(1, a.get(this));
195      }
196  
197      /**
# Line 235 | Line 199 | public class AtomicLongFieldUpdaterTest
199       */
200      public void testAddAndGet() {
201          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
202 <        try {
239 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
240 <        } catch (RuntimeException ok) {
241 <            return;
242 <        }
202 >        a = updaterFor("x");
203          x = 1;
204 <        assertEquals(3,a.addAndGet(this,2));
205 <        assertEquals(3,a.get(this));
206 <        assertEquals(-1,a.addAndGet(this,-4));
207 <        assertEquals(-1,a.get(this));
204 >        assertEquals(3, a.addAndGet(this, 2));
205 >        assertEquals(3, a.get(this));
206 >        assertEquals(-1, a.addAndGet(this, -4));
207 >        assertEquals(-1, a.get(this));
208      }
209  
210      /**
# Line 252 | Line 212 | public class AtomicLongFieldUpdaterTest
212       */
213      public void testDecrementAndGet() {
214          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
215 <        try {
256 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
257 <        } catch (RuntimeException ok) {
258 <            return;
259 <        }
215 >        a = updaterFor("x");
216          x = 1;
217 <        assertEquals(0,a.decrementAndGet(this));
218 <        assertEquals(-1,a.decrementAndGet(this));
219 <        assertEquals(-2,a.decrementAndGet(this));
220 <        assertEquals(-2,a.get(this));
217 >        assertEquals(0, a.decrementAndGet(this));
218 >        assertEquals(-1, a.decrementAndGet(this));
219 >        assertEquals(-2, a.decrementAndGet(this));
220 >        assertEquals(-2, a.get(this));
221      }
222  
223      /**
# Line 269 | Line 225 | public class AtomicLongFieldUpdaterTest
225       */
226      public void testIncrementAndGet() {
227          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
228 <        try {
273 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
274 <        } catch (RuntimeException ok) {
275 <            return;
276 <        }
228 >        a = updaterFor("x");
229          x = 1;
230 <        assertEquals(2,a.incrementAndGet(this));
231 <        assertEquals(2,a.get(this));
232 <        a.set(this,-2);
233 <        assertEquals(-1,a.incrementAndGet(this));
234 <        assertEquals(0,a.incrementAndGet(this));
235 <        assertEquals(1,a.incrementAndGet(this));
236 <        assertEquals(1,a.get(this));
230 >        assertEquals(2, a.incrementAndGet(this));
231 >        assertEquals(2, a.get(this));
232 >        a.set(this, -2);
233 >        assertEquals(-1, a.incrementAndGet(this));
234 >        assertEquals(0, a.incrementAndGet(this));
235 >        assertEquals(1, a.incrementAndGet(this));
236 >        assertEquals(1, a.get(this));
237      }
238  
239   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines