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.4 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.15 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import java.util.concurrent.atomic.*;
# Line 14 | Line 15 | public class AtomicLongFieldUpdaterTest
15      int z;
16      long w;
17  
18 <    public static void main(String[] args){
18 >    public static void main(String[] args) {
19          junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
# Line 22 | Line 23 | public class AtomicLongFieldUpdaterTest
23      }
24  
25      /**
26 <     * Contruction with non-existent field throws RuntimeException
26 >     * Construction with non-existent field throws RuntimeException
27       */
28 <    public void testConstructor(){
29 <        try{
30 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
28 >    public void testConstructor() {
29 >        try {
30 >            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
31                  a = AtomicLongFieldUpdater.newUpdater
32 <                (getClass(), "y");
32 >                (AtomicLongFieldUpdaterTest.class, "y");
33              shouldThrow();
34          }
35          catch (RuntimeException rt) {}
# Line 37 | Line 38 | public class AtomicLongFieldUpdaterTest
38      /**
39       * construction with field not of given type throws RuntimeException
40       */
41 <    public void testConstructor2(){
42 <        try{
43 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
41 >    public void testConstructor2() {
42 >        try {
43 >            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
44                  a = AtomicLongFieldUpdater.newUpdater
45 <                (getClass(), "z");
45 >                (AtomicLongFieldUpdaterTest.class, "z");
46              shouldThrow();
47          }
48          catch (RuntimeException rt) {}
# Line 50 | Line 51 | public class AtomicLongFieldUpdaterTest
51      /**
52       * construction with non-volatile field throws RuntimeException
53       */
54 <    public void testConstructor3(){
55 <        try{
56 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
54 >    public void testConstructor3() {
55 >        try {
56 >            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
57                  a = AtomicLongFieldUpdater.newUpdater
58 <                (getClass(), "w");
58 >                (AtomicLongFieldUpdaterTest.class, "w");
59              shouldThrow();
60          }
61  
# Line 64 | Line 65 | public class AtomicLongFieldUpdaterTest
65      /**
66       *  get returns the last value set or assigned
67       */
68 <    public void testGetSet(){
69 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
68 >    public void testGetSet() {
69 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
70 >        try {
71 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
72 >        } catch (RuntimeException ok) {
73 >            return;
74 >        }
75 >        x = 1;
76 >        assertEquals(1,a.get(this));
77 >        a.set(this,2);
78 >        assertEquals(2,a.get(this));
79 >        a.set(this,-3);
80 >        assertEquals(-3,a.get(this));
81 >    }
82 >
83 >    /**
84 >     *  get returns the last value lazySet by same thread
85 >     */
86 >    public void testGetLazySet() {
87 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
88 >        try {
89 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
90 >        } catch (RuntimeException ok) {
91 >            return;
92 >        }
93          x = 1;
94 <        assertEquals(1,a.get(this));
95 <        a.set(this,2);
96 <        assertEquals(2,a.get(this));
97 <        a.set(this,-3);
98 <        assertEquals(-3,a.get(this));
75 <        
94 >        assertEquals(1,a.get(this));
95 >        a.lazySet(this,2);
96 >        assertEquals(2,a.get(this));
97 >        a.lazySet(this,-3);
98 >        assertEquals(-3,a.get(this));
99      }
100 +
101 +
102      /**
103       * compareAndSet succeeds in changing value if equal to expected else fails
104       */
105 <    public void testCompareAndSet(){
106 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
105 >    public void testCompareAndSet() {
106 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
107 >        try {
108 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
109 >        } catch (RuntimeException ok) {
110 >            return;
111 >        }
112          x = 1;
113 <        assertTrue(a.compareAndSet(this,1,2));
114 <        assertTrue(a.compareAndSet(this,2,-4));
115 <        assertEquals(-4,a.get(this));
116 <        assertFalse(a.compareAndSet(this,-5,7));
117 <        assertFalse((7 == a.get(this)));
118 <        assertTrue(a.compareAndSet(this,-4,7));
119 <        assertEquals(7,a.get(this));
113 >        assertTrue(a.compareAndSet(this,1,2));
114 >        assertTrue(a.compareAndSet(this,2,-4));
115 >        assertEquals(-4,a.get(this));
116 >        assertFalse(a.compareAndSet(this,-5,7));
117 >        assertFalse((7 == a.get(this)));
118 >        assertTrue(a.compareAndSet(this,-4,7));
119 >        assertEquals(7,a.get(this));
120      }
121  
122  
# Line 94 | Line 124 | public class AtomicLongFieldUpdaterTest
124       * compareAndSet in one thread enables another waiting for value
125       * to succeed
126       */
127 <    public void testCompareAndSetInMultipleThreads() {
127 >    public void testCompareAndSetInMultipleThreads() throws Exception {
128          x = 1;
129 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
130 <
131 <        Thread t = new Thread(new Runnable() {
132 <                public void run() {
133 <                    while(!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield();
104 <                }});
105 <        try {
106 <            t.start();
107 <            assertTrue(a.compareAndSet(this, 1, 2));
108 <            t.join(LONG_DELAY_MS);
109 <            assertFalse(t.isAlive());
110 <            assertEquals(a.get(this), 3);
111 <        }
112 <        catch(Exception e) {
113 <            unexpectedException();
129 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
130 >        try {
131 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
132 >        } catch (RuntimeException ok) {
133 >            return;
134          }
135 +
136 +        Thread t = new Thread(new CheckedRunnable() {
137 +            public void realRun() {
138 +                while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
139 +                    Thread.yield();
140 +            }});
141 +
142 +        t.start();
143 +        assertTrue(a.compareAndSet(this, 1, 2));
144 +        t.join(LONG_DELAY_MS);
145 +        assertFalse(t.isAlive());
146 +        assertEquals(a.get(this), 3);
147      }
148  
149      /**
150       * repeated weakCompareAndSet succeeds in changing value when equal
151 <     * to expected
151 >     * to expected
152       */
153 <    public void testWeakCompareAndSet(){
154 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
153 >    public void testWeakCompareAndSet() {
154 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
155 >        try {
156 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
157 >        } catch (RuntimeException ok) {
158 >            return;
159 >        }
160          x = 1;
161 <        while(!a.weakCompareAndSet(this,1,2));
162 <        while(!a.weakCompareAndSet(this,2,-4));
163 <        assertEquals(-4,a.get(this));
164 <        while(!a.weakCompareAndSet(this,-4,7));
165 <        assertEquals(7,a.get(this));
161 >        while (!a.weakCompareAndSet(this,1,2));
162 >        while (!a.weakCompareAndSet(this,2,-4));
163 >        assertEquals(-4,a.get(this));
164 >        while (!a.weakCompareAndSet(this,-4,7));
165 >        assertEquals(7,a.get(this));
166      }
167  
168      /**
169       *  getAndSet returns previous value and sets to given value
170       */
171 <    public void testGetAndSet(){
172 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
171 >    public void testGetAndSet() {
172 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
173 >        try {
174 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
175 >        } catch (RuntimeException ok) {
176 >            return;
177 >        }
178          x = 1;
179 <        assertEquals(1,a.getAndSet(this, 0));
180 <        assertEquals(0,a.getAndSet(this,-10));
181 <        assertEquals(-10,a.getAndSet(this,1));
179 >        assertEquals(1,a.getAndSet(this, 0));
180 >        assertEquals(0,a.getAndSet(this,-10));
181 >        assertEquals(-10,a.getAndSet(this,1));
182      }
183  
184      /**
185       * getAndAdd returns previous value and adds given value
186       */
187 <    public void testGetAndAdd(){
188 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
187 >    public void testGetAndAdd() {
188 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
189 >        try {
190 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
191 >        } catch (RuntimeException ok) {
192 >            return;
193 >        }
194          x = 1;
195 <        assertEquals(1,a.getAndAdd(this,2));
196 <        assertEquals(3,a.get(this));
197 <        assertEquals(3,a.getAndAdd(this,-4));
198 <        assertEquals(-1,a.get(this));
195 >        assertEquals(1,a.getAndAdd(this,2));
196 >        assertEquals(3,a.get(this));
197 >        assertEquals(3,a.getAndAdd(this,-4));
198 >        assertEquals(-1,a.get(this));
199      }
200  
201      /**
202       * getAndDecrement returns previous value and decrements
203       */
204 <    public void testGetAndDecrement(){
205 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
204 >    public void testGetAndDecrement() {
205 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
206 >        try {
207 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
208 >        } catch (RuntimeException ok) {
209 >            return;
210 >        }
211          x = 1;
212 <        assertEquals(1,a.getAndDecrement(this));
213 <        assertEquals(0,a.getAndDecrement(this));
214 <        assertEquals(-1,a.getAndDecrement(this));
212 >        assertEquals(1,a.getAndDecrement(this));
213 >        assertEquals(0,a.getAndDecrement(this));
214 >        assertEquals(-1,a.getAndDecrement(this));
215      }
216  
217      /**
218       * getAndIncrement returns previous value and increments
219       */
220 <    public void testGetAndIncrement(){
221 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
220 >    public void testGetAndIncrement() {
221 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
222 >        try {
223 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
224 >        } catch (RuntimeException ok) {
225 >            return;
226 >        }
227          x = 1;
228 <        assertEquals(1,a.getAndIncrement(this));
229 <        assertEquals(2,a.get(this));
230 <        a.set(this,-2);
231 <        assertEquals(-2,a.getAndIncrement(this));
232 <        assertEquals(-1,a.getAndIncrement(this));
233 <        assertEquals(0,a.getAndIncrement(this));
234 <        assertEquals(1,a.get(this));
228 >        assertEquals(1,a.getAndIncrement(this));
229 >        assertEquals(2,a.get(this));
230 >        a.set(this,-2);
231 >        assertEquals(-2,a.getAndIncrement(this));
232 >        assertEquals(-1,a.getAndIncrement(this));
233 >        assertEquals(0,a.getAndIncrement(this));
234 >        assertEquals(1,a.get(this));
235      }
236  
237      /**
238       * addAndGet adds given value to current, and returns current value
239       */
240 <    public void testAddAndGet(){
241 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
240 >    public void testAddAndGet() {
241 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
242 >        try {
243 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
244 >        } catch (RuntimeException ok) {
245 >            return;
246 >        }
247          x = 1;
248 <        assertEquals(3,a.addAndGet(this,2));
249 <        assertEquals(3,a.get(this));
250 <        assertEquals(-1,a.addAndGet(this,-4));
251 <        assertEquals(-1,a.get(this));
248 >        assertEquals(3,a.addAndGet(this,2));
249 >        assertEquals(3,a.get(this));
250 >        assertEquals(-1,a.addAndGet(this,-4));
251 >        assertEquals(-1,a.get(this));
252      }
253  
254      /**
255       *  decrementAndGet decrements and returns current value
256       */
257 <    public void testDecrementAndGet(){
258 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
257 >    public void testDecrementAndGet() {
258 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
259 >        try {
260 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
261 >        } catch (RuntimeException ok) {
262 >            return;
263 >        }
264          x = 1;
265 <        assertEquals(0,a.decrementAndGet(this));
266 <        assertEquals(-1,a.decrementAndGet(this));
267 <        assertEquals(-2,a.decrementAndGet(this));
268 <        assertEquals(-2,a.get(this));
265 >        assertEquals(0,a.decrementAndGet(this));
266 >        assertEquals(-1,a.decrementAndGet(this));
267 >        assertEquals(-2,a.decrementAndGet(this));
268 >        assertEquals(-2,a.get(this));
269      }
270  
271      /**
272       * incrementAndGet increments and returns current value
273       */
274 <    public void testIncrementAndGet(){
275 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
274 >    public void testIncrementAndGet() {
275 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
276 >        try {
277 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
278 >        } catch (RuntimeException ok) {
279 >            return;
280 >        }
281          x = 1;
282 <        assertEquals(2,a.incrementAndGet(this));
283 <        assertEquals(2,a.get(this));
284 <        a.set(this,-2);
285 <        assertEquals(-1,a.incrementAndGet(this));
286 <        assertEquals(0,a.incrementAndGet(this));
287 <        assertEquals(1,a.incrementAndGet(this));
288 <        assertEquals(1,a.get(this));
282 >        assertEquals(2,a.incrementAndGet(this));
283 >        assertEquals(2,a.get(this));
284 >        a.set(this,-2);
285 >        assertEquals(-1,a.incrementAndGet(this));
286 >        assertEquals(0,a.incrementAndGet(this));
287 >        assertEquals(1,a.incrementAndGet(this));
288 >        assertEquals(1,a.get(this));
289      }
290  
291   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines