ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java (file contents):
Revision 1.12 by jsr166, Mon Nov 16 05:30:07 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.AtomicIntegerFieldUpdater;
11  
12   public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
13      volatile int x = 0;
# Line 21 | Line 20 | public class AtomicIntegerFieldUpdaterTe
20          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
21      }
22  
23 +    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
24 +        return AtomicIntegerFieldUpdater.newUpdater
25 +            (AtomicIntegerFieldUpdaterTest.class, fieldName);
26 +    }
27 +
28      /**
29       * Construction with non-existent field throws RuntimeException
30       */
31      public void testConstructor() {
32          try {
33 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30 <                a = AtomicIntegerFieldUpdater.newUpdater
31 <                (AtomicIntegerFieldUpdaterTest.class, "y");
33 >            updaterFor("y");
34              shouldThrow();
35 <        }
34 <        catch (RuntimeException rt) {}
35 >        } catch (RuntimeException success) {}
36      }
37  
38      /**
# Line 39 | Line 40 | public class AtomicIntegerFieldUpdaterTe
40       */
41      public void testConstructor2() {
42          try {
43 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43 <                a = AtomicIntegerFieldUpdater.newUpdater
44 <                (AtomicIntegerFieldUpdaterTest.class, "z");
43 >            updaterFor("z");
44              shouldThrow();
45 <        }
47 <        catch (RuntimeException rt) {}
45 >        } catch (RuntimeException success) {}
46      }
47  
48      /**
# Line 52 | Line 50 | public class AtomicIntegerFieldUpdaterTe
50       */
51      public void testConstructor3() {
52          try {
53 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56 <                a = AtomicIntegerFieldUpdater.newUpdater
57 <                (AtomicIntegerFieldUpdaterTest.class, "w");
53 >            updaterFor("w");
54              shouldThrow();
55 <        }
60 <        catch (RuntimeException rt) {}
55 >        } catch (RuntimeException success) {}
56      }
57  
58      /**
59 <     *  get returns the last value set or assigned
59 >     * get returns the last value set or assigned
60       */
61      public void testGetSet() {
62          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
63 <        try {
69 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70 <        } catch (RuntimeException ok) {
71 <            return;
72 <        }
63 >        a = updaterFor("x");
64          x = 1;
65 <        assertEquals(1,a.get(this));
66 <        a.set(this,2);
67 <        assertEquals(2,a.get(this));
68 <        a.set(this,-3);
69 <        assertEquals(-3,a.get(this));
79 <
65 >        assertEquals(1, a.get(this));
66 >        a.set(this, 2);
67 >        assertEquals(2, a.get(this));
68 >        a.set(this, -3);
69 >        assertEquals(-3, a.get(this));
70      }
71  
72      /**
73 <     *  get returns the last value lazySet by same thread
73 >     * get returns the last value lazySet by same thread
74       */
75      public void testGetLazySet() {
76          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
77 <        try {
78 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
79 <        } catch (RuntimeException ok) {
80 <            return;
81 <        }
82 <        x = 1;
83 <        assertEquals(1,a.get(this));
94 <        a.lazySet(this,2);
95 <        assertEquals(2,a.get(this));
96 <        a.lazySet(this,-3);
97 <        assertEquals(-3,a.get(this));
98 <
77 >        a = updaterFor("x");
78 >        x = 1;
79 >        assertEquals(1, a.get(this));
80 >        a.lazySet(this, 2);
81 >        assertEquals(2, a.get(this));
82 >        a.lazySet(this, -3);
83 >        assertEquals(-3, a.get(this));
84      }
85  
86      /**
# Line 103 | Line 88 | public class AtomicIntegerFieldUpdaterTe
88       */
89      public void testCompareAndSet() {
90          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
91 <        try {
92 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
93 <        } catch (RuntimeException ok) {
94 <            return;
95 <        }
96 <        x = 1;
97 <        assertTrue(a.compareAndSet(this,1,2));
98 <        assertTrue(a.compareAndSet(this,2,-4));
99 <        assertEquals(-4,a.get(this));
115 <        assertFalse(a.compareAndSet(this,-5,7));
116 <        assertFalse((7 == a.get(this)));
117 <        assertTrue(a.compareAndSet(this,-4,7));
118 <        assertEquals(7,a.get(this));
91 >        a = updaterFor("x");
92 >        x = 1;
93 >        assertTrue(a.compareAndSet(this, 1, 2));
94 >        assertTrue(a.compareAndSet(this, 2, -4));
95 >        assertEquals(-4, a.get(this));
96 >        assertFalse(a.compareAndSet(this, -5, 7));
97 >        assertEquals(-4, a.get(this));
98 >        assertTrue(a.compareAndSet(this, -4, 7));
99 >        assertEquals(7, a.get(this));
100      }
101  
121
102      /**
103       * compareAndSet in one thread enables another waiting for value
104       * to succeed
105       */
106 <    public void testCompareAndSetInMultipleThreads() {
106 >    public void testCompareAndSetInMultipleThreads() throws Exception {
107          x = 1;
108 <        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
109 <        try {
110 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
111 <        } catch (RuntimeException ok) {
112 <            return;
113 <        }
114 <
115 <        Thread t = new Thread(new Runnable() {
116 <                public void run() {
117 <                    while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
118 <                }});
119 <        try {
120 <            t.start();
121 <            assertTrue(a.compareAndSet(this, 1, 2));
142 <            t.join(LONG_DELAY_MS);
143 <            assertFalse(t.isAlive());
144 <            assertEquals(a.get(this), 3);
145 <        }
146 <        catch (Exception e) {
147 <            unexpectedException();
148 <        }
108 >        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
109 >        a = updaterFor("x");
110 >
111 >        Thread t = new Thread(new CheckedRunnable() {
112 >            public void realRun() {
113 >                while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
114 >                    Thread.yield();
115 >            }});
116 >
117 >        t.start();
118 >        assertTrue(a.compareAndSet(this, 1, 2));
119 >        t.join(LONG_DELAY_MS);
120 >        assertFalse(t.isAlive());
121 >        assertEquals(3, a.get(this));
122      }
123  
124      /**
# Line 154 | Line 127 | public class AtomicIntegerFieldUpdaterTe
127       */
128      public void testWeakCompareAndSet() {
129          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
130 <        try {
158 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
159 <        } catch (RuntimeException ok) {
160 <            return;
161 <        }
130 >        a = updaterFor("x");
131          x = 1;
132 <        while (!a.weakCompareAndSet(this,1,2));
133 <        while (!a.weakCompareAndSet(this,2,-4));
134 <        assertEquals(-4,a.get(this));
135 <        while (!a.weakCompareAndSet(this,-4,7));
136 <        assertEquals(7,a.get(this));
132 >        while (!a.weakCompareAndSet(this, 1, 2));
133 >        while (!a.weakCompareAndSet(this, 2, -4));
134 >        assertEquals(-4, a.get(this));
135 >        while (!a.weakCompareAndSet(this, -4, 7));
136 >        assertEquals(7, a.get(this));
137      }
138  
139      /**
140 <     *  getAndSet returns previous value and sets to given value
140 >     * getAndSet returns previous value and sets to given value
141       */
142      public void testGetAndSet() {
143          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
144 <        try {
145 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
146 <        } catch (RuntimeException ok) {
147 <            return;
148 <        }
180 <        x = 1;
181 <        assertEquals(1,a.getAndSet(this, 0));
182 <        assertEquals(0,a.getAndSet(this,-10));
183 <        assertEquals(-10,a.getAndSet(this,1));
144 >        a = updaterFor("x");
145 >        x = 1;
146 >        assertEquals(1, a.getAndSet(this, 0));
147 >        assertEquals(0, a.getAndSet(this, -10));
148 >        assertEquals(-10, a.getAndSet(this, 1));
149      }
150  
151      /**
# Line 188 | Line 153 | public class AtomicIntegerFieldUpdaterTe
153       */
154      public void testGetAndAdd() {
155          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
156 <        try {
157 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
158 <        } catch (RuntimeException ok) {
159 <            return;
160 <        }
161 <        x = 1;
197 <        assertEquals(1,a.getAndAdd(this,2));
198 <        assertEquals(3,a.get(this));
199 <        assertEquals(3,a.getAndAdd(this,-4));
200 <        assertEquals(-1,a.get(this));
156 >        a = updaterFor("x");
157 >        x = 1;
158 >        assertEquals(1, a.getAndAdd(this, 2));
159 >        assertEquals(3, a.get(this));
160 >        assertEquals(3, a.getAndAdd(this, -4));
161 >        assertEquals(-1, a.get(this));
162      }
163  
164      /**
# Line 205 | Line 166 | public class AtomicIntegerFieldUpdaterTe
166       */
167      public void testGetAndDecrement() {
168          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
169 <        try {
170 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
171 <        } catch (RuntimeException ok) {
172 <            return;
173 <        }
213 <        x = 1;
214 <        assertEquals(1,a.getAndDecrement(this));
215 <        assertEquals(0,a.getAndDecrement(this));
216 <        assertEquals(-1,a.getAndDecrement(this));
169 >        a = updaterFor("x");
170 >        x = 1;
171 >        assertEquals(1, a.getAndDecrement(this));
172 >        assertEquals(0, a.getAndDecrement(this));
173 >        assertEquals(-1, a.getAndDecrement(this));
174      }
175  
176      /**
# Line 221 | Line 178 | public class AtomicIntegerFieldUpdaterTe
178       */
179      public void testGetAndIncrement() {
180          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
181 <        try {
182 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
183 <        } catch (RuntimeException ok) {
184 <            return;
185 <        }
186 <        x = 1;
187 <        assertEquals(1,a.getAndIncrement(this));
188 <        assertEquals(2,a.get(this));
189 <        a.set(this,-2);
233 <        assertEquals(-2,a.getAndIncrement(this));
234 <        assertEquals(-1,a.getAndIncrement(this));
235 <        assertEquals(0,a.getAndIncrement(this));
236 <        assertEquals(1,a.get(this));
181 >        a = updaterFor("x");
182 >        x = 1;
183 >        assertEquals(1, a.getAndIncrement(this));
184 >        assertEquals(2, a.get(this));
185 >        a.set(this, -2);
186 >        assertEquals(-2, a.getAndIncrement(this));
187 >        assertEquals(-1, a.getAndIncrement(this));
188 >        assertEquals(0, a.getAndIncrement(this));
189 >        assertEquals(1, a.get(this));
190      }
191  
192      /**
# Line 241 | Line 194 | public class AtomicIntegerFieldUpdaterTe
194       */
195      public void testAddAndGet() {
196          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
197 <        try {
198 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
199 <        } catch (RuntimeException ok) {
200 <            return;
201 <        }
202 <        x = 1;
250 <        assertEquals(3,a.addAndGet(this,2));
251 <        assertEquals(3,a.get(this));
252 <        assertEquals(-1,a.addAndGet(this,-4));
253 <        assertEquals(-1,a.get(this));
197 >        a = updaterFor("x");
198 >        x = 1;
199 >        assertEquals(3, a.addAndGet(this, 2));
200 >        assertEquals(3, a.get(this));
201 >        assertEquals(-1, a.addAndGet(this, -4));
202 >        assertEquals(-1, a.get(this));
203      }
204  
205      /**
# Line 258 | Line 207 | public class AtomicIntegerFieldUpdaterTe
207       */
208      public void testDecrementAndGet() {
209          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
210 <        try {
211 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
212 <        } catch (RuntimeException ok) {
213 <            return;
214 <        }
215 <        x = 1;
267 <        assertEquals(0,a.decrementAndGet(this));
268 <        assertEquals(-1,a.decrementAndGet(this));
269 <        assertEquals(-2,a.decrementAndGet(this));
270 <        assertEquals(-2,a.get(this));
210 >        a = updaterFor("x");
211 >        x = 1;
212 >        assertEquals(0, a.decrementAndGet(this));
213 >        assertEquals(-1, a.decrementAndGet(this));
214 >        assertEquals(-2, a.decrementAndGet(this));
215 >        assertEquals(-2, a.get(this));
216      }
217  
218      /**
# Line 275 | Line 220 | public class AtomicIntegerFieldUpdaterTe
220       */
221      public void testIncrementAndGet() {
222          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
223 <        try {
224 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
225 <        } catch (RuntimeException ok) {
226 <            return;
227 <        }
228 <        x = 1;
229 <        assertEquals(2,a.incrementAndGet(this));
230 <        assertEquals(2,a.get(this));
231 <        a.set(this,-2);
287 <        assertEquals(-1,a.incrementAndGet(this));
288 <        assertEquals(0,a.incrementAndGet(this));
289 <        assertEquals(1,a.incrementAndGet(this));
290 <        assertEquals(1,a.get(this));
223 >        a = updaterFor("x");
224 >        x = 1;
225 >        assertEquals(2, a.incrementAndGet(this));
226 >        assertEquals(2, a.get(this));
227 >        a.set(this, -2);
228 >        assertEquals(-1, a.incrementAndGet(this));
229 >        assertEquals(0, a.incrementAndGet(this));
230 >        assertEquals(1, a.incrementAndGet(this));
231 >        assertEquals(1, a.get(this));
232      }
233  
234   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines