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.16 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.24 by jsr166, Mon Apr 1 20:58:58 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      }
# Line 39 | Line 41 | public class AtomicLongFieldUpdaterTest
41       */
42      public void testConstructor2() {
43          try {
44 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
43 <                a = AtomicLongFieldUpdater.newUpdater
44 <                (AtomicLongFieldUpdaterTest.class, "z");
44 >            updaterFor("z");
45              shouldThrow();
46          } catch (RuntimeException success) {}
47      }
# Line 51 | Line 51 | public class AtomicLongFieldUpdaterTest
51       */
52      public void testConstructor3() {
53          try {
54 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
55 <                a = AtomicLongFieldUpdater.newUpdater
56 <                (AtomicLongFieldUpdaterTest.class, "w");
54 >            updaterFor("w");
55              shouldThrow();
56          } catch (RuntimeException success) {}
57      }
58  
59      /**
60 <     *  get returns the last value set or assigned
60 >     * get returns the last value set or assigned
61       */
62      public void testGetSet() {
63          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
64 <        try {
67 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
68 <        } catch (RuntimeException ok) {
69 <            return;
70 <        }
64 >        a = updaterFor("x");
65          x = 1;
66 <        assertEquals(1,a.get(this));
67 <        a.set(this,2);
68 <        assertEquals(2,a.get(this));
69 <        a.set(this,-3);
70 <        assertEquals(-3,a.get(this));
66 >        assertEquals(1, a.get(this));
67 >        a.set(this, 2);
68 >        assertEquals(2, a.get(this));
69 >        a.set(this, -3);
70 >        assertEquals(-3, a.get(this));
71      }
72  
73      /**
74 <     *  get returns the last value lazySet by same thread
74 >     * get returns the last value lazySet by same thread
75       */
76      public void testGetLazySet() {
77          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
78 <        try {
79 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
80 <        } catch (RuntimeException ok) {
81 <            return;
82 <        }
83 <        x = 1;
84 <        assertEquals(1,a.get(this));
91 <        a.lazySet(this,2);
92 <        assertEquals(2,a.get(this));
93 <        a.lazySet(this,-3);
94 <        assertEquals(-3,a.get(this));
78 >        a = updaterFor("x");
79 >        x = 1;
80 >        assertEquals(1, a.get(this));
81 >        a.lazySet(this, 2);
82 >        assertEquals(2, a.get(this));
83 >        a.lazySet(this, -3);
84 >        assertEquals(-3, a.get(this));
85      }
86  
97
87      /**
88       * compareAndSet succeeds in changing value if equal to expected else fails
89       */
90      public void testCompareAndSet() {
91          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
92 <        try {
93 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
94 <        } catch (RuntimeException ok) {
95 <            return;
96 <        }
97 <        x = 1;
98 <        assertTrue(a.compareAndSet(this,1,2));
99 <        assertTrue(a.compareAndSet(this,2,-4));
100 <        assertEquals(-4,a.get(this));
112 <        assertFalse(a.compareAndSet(this,-5,7));
113 <        assertFalse((7 == a.get(this)));
114 <        assertTrue(a.compareAndSet(this,-4,7));
115 <        assertEquals(7,a.get(this));
92 >        a = updaterFor("x");
93 >        x = 1;
94 >        assertTrue(a.compareAndSet(this, 1, 2));
95 >        assertTrue(a.compareAndSet(this, 2, -4));
96 >        assertEquals(-4, a.get(this));
97 >        assertFalse(a.compareAndSet(this, -5, 7));
98 >        assertEquals(-4, a.get(this));
99 >        assertTrue(a.compareAndSet(this, -4, 7));
100 >        assertEquals(7, a.get(this));
101      }
102  
118
103      /**
104       * compareAndSet in one thread enables another waiting for value
105       * to succeed
106       */
107      public void testCompareAndSetInMultipleThreads() throws Exception {
108          x = 1;
109 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
110 <        try {
127 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
128 <        } catch (RuntimeException ok) {
129 <            return;
130 <        }
109 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
110 >        a = updaterFor("x");
111  
112          Thread t = new Thread(new CheckedRunnable() {
113              public void realRun() {
# Line 139 | Line 119 | public class AtomicLongFieldUpdaterTest
119          assertTrue(a.compareAndSet(this, 1, 2));
120          t.join(LONG_DELAY_MS);
121          assertFalse(t.isAlive());
122 <        assertEquals(a.get(this), 3);
122 >        assertEquals(3, a.get(this));
123      }
124  
125      /**
# Line 148 | Line 128 | public class AtomicLongFieldUpdaterTest
128       */
129      public void testWeakCompareAndSet() {
130          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
131 <        try {
152 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
153 <        } catch (RuntimeException ok) {
154 <            return;
155 <        }
131 >        a = updaterFor("x");
132          x = 1;
133 <        while (!a.weakCompareAndSet(this,1,2));
134 <        while (!a.weakCompareAndSet(this,2,-4));
135 <        assertEquals(-4,a.get(this));
136 <        while (!a.weakCompareAndSet(this,-4,7));
137 <        assertEquals(7,a.get(this));
133 >        while (!a.weakCompareAndSet(this, 1, 2));
134 >        while (!a.weakCompareAndSet(this, 2, -4));
135 >        assertEquals(-4, a.get(this));
136 >        while (!a.weakCompareAndSet(this, -4, 7));
137 >        assertEquals(7, a.get(this));
138      }
139  
140      /**
141 <     *  getAndSet returns previous value and sets to given value
141 >     * getAndSet returns previous value and sets to given value
142       */
143      public void testGetAndSet() {
144          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
145 <        try {
146 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
147 <        } catch (RuntimeException ok) {
148 <            return;
149 <        }
174 <        x = 1;
175 <        assertEquals(1,a.getAndSet(this, 0));
176 <        assertEquals(0,a.getAndSet(this,-10));
177 <        assertEquals(-10,a.getAndSet(this,1));
145 >        a = updaterFor("x");
146 >        x = 1;
147 >        assertEquals(1, a.getAndSet(this, 0));
148 >        assertEquals(0, a.getAndSet(this, -10));
149 >        assertEquals(-10, a.getAndSet(this, 1));
150      }
151  
152      /**
# Line 182 | Line 154 | public class AtomicLongFieldUpdaterTest
154       */
155      public void testGetAndAdd() {
156          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
157 <        try {
158 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
159 <        } catch (RuntimeException ok) {
160 <            return;
161 <        }
162 <        x = 1;
191 <        assertEquals(1,a.getAndAdd(this,2));
192 <        assertEquals(3,a.get(this));
193 <        assertEquals(3,a.getAndAdd(this,-4));
194 <        assertEquals(-1,a.get(this));
157 >        a = updaterFor("x");
158 >        x = 1;
159 >        assertEquals(1, a.getAndAdd(this, 2));
160 >        assertEquals(3, a.get(this));
161 >        assertEquals(3, a.getAndAdd(this, -4));
162 >        assertEquals(-1, a.get(this));
163      }
164  
165      /**
# Line 199 | Line 167 | public class AtomicLongFieldUpdaterTest
167       */
168      public void testGetAndDecrement() {
169          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
170 <        try {
171 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
172 <        } catch (RuntimeException ok) {
173 <            return;
174 <        }
207 <        x = 1;
208 <        assertEquals(1,a.getAndDecrement(this));
209 <        assertEquals(0,a.getAndDecrement(this));
210 <        assertEquals(-1,a.getAndDecrement(this));
170 >        a = updaterFor("x");
171 >        x = 1;
172 >        assertEquals(1, a.getAndDecrement(this));
173 >        assertEquals(0, a.getAndDecrement(this));
174 >        assertEquals(-1, a.getAndDecrement(this));
175      }
176  
177      /**
# Line 215 | Line 179 | public class AtomicLongFieldUpdaterTest
179       */
180      public void testGetAndIncrement() {
181          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
182 <        try {
183 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 <        } catch (RuntimeException ok) {
185 <            return;
186 <        }
187 <        x = 1;
188 <        assertEquals(1,a.getAndIncrement(this));
189 <        assertEquals(2,a.get(this));
190 <        a.set(this,-2);
227 <        assertEquals(-2,a.getAndIncrement(this));
228 <        assertEquals(-1,a.getAndIncrement(this));
229 <        assertEquals(0,a.getAndIncrement(this));
230 <        assertEquals(1,a.get(this));
182 >        a = updaterFor("x");
183 >        x = 1;
184 >        assertEquals(1, a.getAndIncrement(this));
185 >        assertEquals(2, a.get(this));
186 >        a.set(this, -2);
187 >        assertEquals(-2, a.getAndIncrement(this));
188 >        assertEquals(-1, a.getAndIncrement(this));
189 >        assertEquals(0, a.getAndIncrement(this));
190 >        assertEquals(1, a.get(this));
191      }
192  
193      /**
# Line 235 | Line 195 | public class AtomicLongFieldUpdaterTest
195       */
196      public void testAddAndGet() {
197          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
198 <        try {
239 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
240 <        } catch (RuntimeException ok) {
241 <            return;
242 <        }
198 >        a = updaterFor("x");
199          x = 1;
200 <        assertEquals(3,a.addAndGet(this,2));
201 <        assertEquals(3,a.get(this));
202 <        assertEquals(-1,a.addAndGet(this,-4));
203 <        assertEquals(-1,a.get(this));
200 >        assertEquals(3, a.addAndGet(this, 2));
201 >        assertEquals(3, a.get(this));
202 >        assertEquals(-1, a.addAndGet(this, -4));
203 >        assertEquals(-1, a.get(this));
204      }
205  
206      /**
207 <     *  decrementAndGet decrements and returns current value
207 >     * decrementAndGet decrements and returns current value
208       */
209      public void testDecrementAndGet() {
210          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
211 <        try {
212 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
213 <        } catch (RuntimeException ok) {
214 <            return;
215 <        }
216 <        x = 1;
261 <        assertEquals(0,a.decrementAndGet(this));
262 <        assertEquals(-1,a.decrementAndGet(this));
263 <        assertEquals(-2,a.decrementAndGet(this));
264 <        assertEquals(-2,a.get(this));
211 >        a = updaterFor("x");
212 >        x = 1;
213 >        assertEquals(0, a.decrementAndGet(this));
214 >        assertEquals(-1, a.decrementAndGet(this));
215 >        assertEquals(-2, a.decrementAndGet(this));
216 >        assertEquals(-2, a.get(this));
217      }
218  
219      /**
# Line 269 | Line 221 | public class AtomicLongFieldUpdaterTest
221       */
222      public void testIncrementAndGet() {
223          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
224 <        try {
225 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
226 <        } catch (RuntimeException ok) {
227 <            return;
228 <        }
229 <        x = 1;
230 <        assertEquals(2,a.incrementAndGet(this));
231 <        assertEquals(2,a.get(this));
232 <        a.set(this,-2);
281 <        assertEquals(-1,a.incrementAndGet(this));
282 <        assertEquals(0,a.incrementAndGet(this));
283 <        assertEquals(1,a.incrementAndGet(this));
284 <        assertEquals(1,a.get(this));
224 >        a = updaterFor("x");
225 >        x = 1;
226 >        assertEquals(2, a.incrementAndGet(this));
227 >        assertEquals(2, a.get(this));
228 >        a.set(this, -2);
229 >        assertEquals(-1, a.incrementAndGet(this));
230 >        assertEquals(0, a.incrementAndGet(this));
231 >        assertEquals(1, a.incrementAndGet(this));
232 >        assertEquals(1, a.get(this));
233      }
234  
235   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines