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.13 by jsr166, Tue Nov 17 03:12:51 2009 UTC vs.
Revision 1.26 by jsr166, Thu May 30 03:28:55 2013 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.*;
9   import junit.framework.*;
10 < import java.util.*;
10 > import java.util.concurrent.atomic.AtomicLongFieldUpdater;
11  
12   public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
13      volatile long x = 0;
# Line 22 | 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>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (AtomicLongFieldUpdaterTest.class, "y");
34 >            updaterFor("y");
35              shouldThrow();
36 +        } catch (RuntimeException success) {
37 +            assertNotNull(success.getCause());
38          }
35        catch (RuntimeException rt) {}
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>
44 <                a = AtomicLongFieldUpdater.newUpdater
45 <                (AtomicLongFieldUpdaterTest.class, "z");
46 >            updaterFor("z");
47              shouldThrow();
48 <        }
48 <        catch (RuntimeException rt) {}
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>
57 <                a = AtomicLongFieldUpdater.newUpdater
58 <                (AtomicLongFieldUpdaterTest.class, "w");
56 >            updaterFor("w");
57              shouldThrow();
58 <        }
61 <
62 <        catch (RuntimeException rt) {}
58 >        } catch (IllegalArgumentException success) {}
59      }
60  
61      /**
62 <     *  get returns the last value set or assigned
62 >     * get returns the last value set or assigned
63       */
64      public void testGetSet() {
65          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
66 <        try {
71 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
72 <        } catch (RuntimeException ok) {
73 <            return;
74 <        }
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      /**
76 <     *  get returns the last value lazySet by same thread
76 >     * get returns the last value lazySet by same thread
77       */
78      public void testGetLazySet() {
79          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
80 <        try {
89 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
90 <        } catch (RuntimeException ok) {
91 <            return;
92 <        }
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  
101
89      /**
90       * compareAndSet succeeds in changing value if equal to expected else fails
91       */
92      public void testCompareAndSet() {
93          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
94 <        try {
108 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
109 <        } catch (RuntimeException ok) {
110 <            return;
111 <        }
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 <        assertFalse((7 == 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  
122
105      /**
106       * compareAndSet in one thread enables another waiting for value
107       * to succeed
108       */
109      public void testCompareAndSetInMultipleThreads() throws Exception {
110          x = 1;
111 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
112 <        try {
131 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
132 <        } catch (RuntimeException ok) {
133 <            return;
134 <        }
111 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
112 >        a = updaterFor("x");
113  
114 <        Thread t = new Thread(new Runnable() {
115 <                public void run() {
116 <                    while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield();
117 <                }});
114 >        Thread t = new Thread(new CheckedRunnable() {
115 >            public void realRun() {
116 >                while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
117 >                    Thread.yield();
118 >            }});
119  
120          t.start();
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 151 | Line 130 | public class AtomicLongFieldUpdaterTest
130       */
131      public void testWeakCompareAndSet() {
132          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
133 <        try {
155 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
156 <        } catch (RuntimeException ok) {
157 <            return;
158 <        }
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      /**
143 <     *  getAndSet returns previous value and sets to given value
143 >     * getAndSet returns previous value and sets to given value
144       */
145      public void testGetAndSet() {
146          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
147 <        try {
173 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
174 <        } catch (RuntimeException ok) {
175 <            return;
176 <        }
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 185 | Line 156 | public class AtomicLongFieldUpdaterTest
156       */
157      public void testGetAndAdd() {
158          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
159 <        try {
189 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
190 <        } catch (RuntimeException ok) {
191 <            return;
192 <        }
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 202 | Line 169 | public class AtomicLongFieldUpdaterTest
169       */
170      public void testGetAndDecrement() {
171          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
172 <        try {
206 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
207 <        } catch (RuntimeException ok) {
208 <            return;
209 <        }
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 218 | Line 181 | public class AtomicLongFieldUpdaterTest
181       */
182      public void testGetAndIncrement() {
183          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
184 <        try {
222 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
223 <        } catch (RuntimeException ok) {
224 <            return;
225 <        }
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 238 | Line 197 | public class AtomicLongFieldUpdaterTest
197       */
198      public void testAddAndGet() {
199          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 <        try {
242 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
243 <        } catch (RuntimeException ok) {
244 <            return;
245 <        }
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      /**
209 <     *  decrementAndGet decrements and returns current value
209 >     * decrementAndGet decrements and returns current value
210       */
211      public void testDecrementAndGet() {
212          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
213 <        try {
259 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
260 <        } catch (RuntimeException ok) {
261 <            return;
262 <        }
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 272 | Line 223 | public class AtomicLongFieldUpdaterTest
223       */
224      public void testIncrementAndGet() {
225          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
226 <        try {
276 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
277 <        } catch (RuntimeException ok) {
278 <            return;
279 <        }
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