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.15 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.27 by jsr166, Wed Dec 31 19:05:42 2014 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 22 | 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>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (AtomicLongFieldUpdaterTest.class, "y");
36 >            updaterFor("y");
37              shouldThrow();
38 +        } catch (RuntimeException success) {
39 +            assertNotNull(success.getCause());
40          }
35        catch (RuntimeException rt) {}
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>
44 <                a = AtomicLongFieldUpdater.newUpdater
45 <                (AtomicLongFieldUpdaterTest.class, "z");
48 >            updaterFor("z");
49              shouldThrow();
50 <        }
48 <        catch (RuntimeException rt) {}
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>
57 <                a = AtomicLongFieldUpdater.newUpdater
58 <                (AtomicLongFieldUpdaterTest.class, "w");
58 >            updaterFor("w");
59              shouldThrow();
60 <        }
61 <
62 <        catch (RuntimeException rt) {}
60 >        } catch (IllegalArgumentException success) {}
61      }
62  
63      /**
64 <     *  get returns the last value set or assigned
64 >     * get returns the last value set or assigned
65       */
66      public void testGetSet() {
67          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
68 <        try {
71 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
72 <        } catch (RuntimeException ok) {
73 <            return;
74 <        }
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      /**
78 <     *  get returns the last value lazySet by same thread
78 >     * get returns the last value lazySet by same thread
79       */
80      public void testGetLazySet() {
81          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
82 <        try {
89 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
90 <        } catch (RuntimeException ok) {
91 <            return;
92 <        }
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  
101
91      /**
92       * compareAndSet succeeds in changing value if equal to expected else fails
93       */
94      public void testCompareAndSet() {
95          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
96 <        try {
108 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
109 <        } catch (RuntimeException ok) {
110 <            return;
111 <        }
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 <        assertFalse((7 == 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  
122
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 {
131 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
132 <        } catch (RuntimeException ok) {
133 <            return;
134 <        }
113 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
114 >        a = updaterFor("x");
115  
116          Thread t = new Thread(new CheckedRunnable() {
117              public void realRun() {
# Line 143 | 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 152 | Line 132 | public class AtomicLongFieldUpdaterTest
132       */
133      public void testWeakCompareAndSet() {
134          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
135 <        try {
156 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
157 <        } catch (RuntimeException ok) {
158 <            return;
159 <        }
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 >        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));
142      }
143  
144      /**
145 <     *  getAndSet returns previous value and sets to given value
145 >     * getAndSet returns previous value and sets to given value
146       */
147      public void testGetAndSet() {
148          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
149 <        try {
174 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
175 <        } catch (RuntimeException ok) {
176 <            return;
177 <        }
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 186 | Line 158 | public class AtomicLongFieldUpdaterTest
158       */
159      public void testGetAndAdd() {
160          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
161 <        try {
190 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
191 <        } catch (RuntimeException ok) {
192 <            return;
193 <        }
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 203 | Line 171 | public class AtomicLongFieldUpdaterTest
171       */
172      public void testGetAndDecrement() {
173          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
174 <        try {
207 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
208 <        } catch (RuntimeException ok) {
209 <            return;
210 <        }
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 219 | Line 183 | public class AtomicLongFieldUpdaterTest
183       */
184      public void testGetAndIncrement() {
185          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
186 <        try {
223 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
224 <        } catch (RuntimeException ok) {
225 <            return;
226 <        }
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 239 | Line 199 | public class AtomicLongFieldUpdaterTest
199       */
200      public void testAddAndGet() {
201          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
202 <        try {
243 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
244 <        } catch (RuntimeException ok) {
245 <            return;
246 <        }
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      /**
211 <     *  decrementAndGet decrements and returns current value
211 >     * decrementAndGet decrements and returns current value
212       */
213      public void testDecrementAndGet() {
214          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
215 <        try {
260 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
261 <        } catch (RuntimeException ok) {
262 <            return;
263 <        }
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 273 | Line 225 | public class AtomicLongFieldUpdaterTest
225       */
226      public void testIncrementAndGet() {
227          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
228 <        try {
277 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
278 <        } catch (RuntimeException ok) {
279 <            return;
280 <        }
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