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.8 by dl, Tue Jan 20 20:20:56 2004 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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;
14      int w;
15      long z;
16 <    public static void main(String[] args){
16 >    public static void main(String[] args) {
17          junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
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");
32 >        try {
33 >            updaterFor("y");
34              shouldThrow();
35 <        }
34 <        catch (RuntimeException rt) {}
35 >        } catch (RuntimeException success) {}
36      }
37  
38      /**
39       * construction with field not of given type throws RuntimeException
40       */
41      public void testConstructor2() {
42 <        try{
43 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43 <                a = AtomicIntegerFieldUpdater.newUpdater
44 <                (AtomicIntegerFieldUpdaterTest.class, "z");
42 >        try {
43 >            updaterFor("z");
44              shouldThrow();
45 <        }
47 <        catch (RuntimeException rt) {}
45 >        } catch (RuntimeException success) {}
46      }
47  
48      /**
49       * construction with non-volatile field throws RuntimeException
50       */
51      public void testConstructor3() {
52 <        try{
53 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56 <                a = AtomicIntegerFieldUpdater.newUpdater
57 <                (AtomicIntegerFieldUpdaterTest.class, "w");
52 >        try {
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 {
64 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
65 <        } catch (RuntimeException ok) {
66 <            return;
67 <        }
68 <        x = 1;
69 <        assertEquals(1,a.get(this));
70 <        a.set(this,2);
71 <        assertEquals(2,a.get(this));
72 <        a.set(this,-3);
73 <        assertEquals(-3,a.get(this));
74 <        
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));
70 >    }
71 >
72 >    /**
73 >     * get returns the last value lazySet by same thread
74 >     */
75 >    public void testGetLazySet() {
76 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
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      /**
87       * compareAndSet succeeds in changing value if equal to expected else fails
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));
95 <        assertFalse(a.compareAndSet(this,-5,7));
96 <        assertFalse((7 == a.get(this)));
97 <        assertTrue(a.compareAndSet(this,-4,7));
98 <        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  
101
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));
122 <            t.join(LONG_DELAY_MS);
123 <            assertFalse(t.isAlive());
124 <            assertEquals(a.get(this), 3);
125 <        }
126 <        catch(Exception e) {
127 <            unexpectedException();
128 <        }
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      /**
125       * repeated weakCompareAndSet succeeds in changing value when equal
126 <     * to expected
126 >     * to expected
127       */
128      public void testWeakCompareAndSet() {
129          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
130 <        try {
138 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
139 <        } catch (RuntimeException ok) {
140 <            return;
141 <        }
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 <        }
160 <        x = 1;
161 <        assertEquals(1,a.getAndSet(this, 0));
162 <        assertEquals(0,a.getAndSet(this,-10));
163 <        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 168 | 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;
177 <        assertEquals(1,a.getAndAdd(this,2));
178 <        assertEquals(3,a.get(this));
179 <        assertEquals(3,a.getAndAdd(this,-4));
180 <        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 185 | 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 <        }
193 <        x = 1;
194 <        assertEquals(1,a.getAndDecrement(this));
195 <        assertEquals(0,a.getAndDecrement(this));
196 <        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 201 | 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);
213 <        assertEquals(-2,a.getAndIncrement(this));
214 <        assertEquals(-1,a.getAndIncrement(this));
215 <        assertEquals(0,a.getAndIncrement(this));
216 <        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 221 | 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;
230 <        assertEquals(3,a.addAndGet(this,2));
231 <        assertEquals(3,a.get(this));
232 <        assertEquals(-1,a.addAndGet(this,-4));
233 <        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 238 | 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;
247 <        assertEquals(0,a.decrementAndGet(this));
248 <        assertEquals(-1,a.decrementAndGet(this));
249 <        assertEquals(-2,a.decrementAndGet(this));
250 <        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 255 | 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);
267 <        assertEquals(-1,a.incrementAndGet(this));
268 <        assertEquals(0,a.incrementAndGet(this));
269 <        assertEquals(1,a.incrementAndGet(this));
270 <        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