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.21 by jsr166, Tue May 31 16:16:23 2011 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() {
# Line 25 | Line 24 | public class AtomicIntegerFieldUpdaterTe
24       * Construction with non-existent field throws RuntimeException
25       */
26      public void testConstructor() {
27 <        try{
28 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
27 >        try {
28 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
29                  a = AtomicIntegerFieldUpdater.newUpdater
30                  (AtomicIntegerFieldUpdaterTest.class, "y");
31              shouldThrow();
32 <        }
34 <        catch (RuntimeException rt) {}
32 >        } catch (RuntimeException success) {}
33      }
34  
35      /**
36       * construction with field not of given type throws RuntimeException
37       */
38      public void testConstructor2() {
39 <        try{
40 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
39 >        try {
40 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
41                  a = AtomicIntegerFieldUpdater.newUpdater
42                  (AtomicIntegerFieldUpdaterTest.class, "z");
43              shouldThrow();
44 <        }
47 <        catch (RuntimeException rt) {}
44 >        } catch (RuntimeException success) {}
45      }
46  
47      /**
48       * construction with non-volatile field throws RuntimeException
49       */
50      public void testConstructor3() {
51 <        try{
52 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
51 >        try {
52 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
53                  a = AtomicIntegerFieldUpdater.newUpdater
54                  (AtomicIntegerFieldUpdaterTest.class, "w");
55              shouldThrow();
56 <        }
60 <        catch (RuntimeException rt) {}
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          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
# Line 71 | Line 67 | public class AtomicIntegerFieldUpdaterTe
67              return;
68          }
69          x = 1;
70 <        assertEquals(1,a.get(this));
71 <        a.set(this,2);
72 <        assertEquals(2,a.get(this));
73 <        a.set(this,-3);
74 <        assertEquals(-3,a.get(this));
79 <        
70 >        assertEquals(1,a.get(this));
71 >        a.set(this,2);
72 >        assertEquals(2,a.get(this));
73 >        a.set(this,-3);
74 >        assertEquals(-3,a.get(this));
75      }
76 +
77 +    /**
78 +     * get returns the last value lazySet by same thread
79 +     */
80 +    public void testGetLazySet() {
81 +        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
82 +        try {
83 +            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
84 +        } catch (RuntimeException ok) {
85 +            return;
86 +        }
87 +        x = 1;
88 +        assertEquals(1,a.get(this));
89 +        a.lazySet(this,2);
90 +        assertEquals(2,a.get(this));
91 +        a.lazySet(this,-3);
92 +        assertEquals(-3,a.get(this));
93 +    }
94 +
95      /**
96       * compareAndSet succeeds in changing value if equal to expected else fails
97       */
# Line 89 | Line 103 | public class AtomicIntegerFieldUpdaterTe
103              return;
104          }
105          x = 1;
106 <        assertTrue(a.compareAndSet(this,1,2));
107 <        assertTrue(a.compareAndSet(this,2,-4));
108 <        assertEquals(-4,a.get(this));
109 <        assertFalse(a.compareAndSet(this,-5,7));
110 <        assertFalse((7 == a.get(this)));
111 <        assertTrue(a.compareAndSet(this,-4,7));
112 <        assertEquals(7,a.get(this));
106 >        assertTrue(a.compareAndSet(this,1,2));
107 >        assertTrue(a.compareAndSet(this,2,-4));
108 >        assertEquals(-4,a.get(this));
109 >        assertFalse(a.compareAndSet(this,-5,7));
110 >        assertEquals(-4,a.get(this));
111 >        assertTrue(a.compareAndSet(this,-4,7));
112 >        assertEquals(7,a.get(this));
113      }
114  
101
115      /**
116       * compareAndSet in one thread enables another waiting for value
117       * to succeed
118       */
119 <    public void testCompareAndSetInMultipleThreads() {
119 >    public void testCompareAndSetInMultipleThreads() throws Exception {
120          x = 1;
121          final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
122          try {
# Line 112 | Line 125 | public class AtomicIntegerFieldUpdaterTe
125              return;
126          }
127  
128 <        Thread t = new Thread(new Runnable() {
129 <                public void run() {
130 <                    while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
131 <                }});
132 <        try {
133 <            t.start();
134 <            assertTrue(a.compareAndSet(this, 1, 2));
135 <            t.join(LONG_DELAY_MS);
136 <            assertFalse(t.isAlive());
137 <            assertEquals(a.get(this), 3);
138 <        }
126 <        catch(Exception e) {
127 <            unexpectedException();
128 <        }
128 >        Thread t = new Thread(new CheckedRunnable() {
129 >            public void realRun() {
130 >                while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
131 >                    Thread.yield();
132 >            }});
133 >
134 >        t.start();
135 >        assertTrue(a.compareAndSet(this, 1, 2));
136 >        t.join(LONG_DELAY_MS);
137 >        assertFalse(t.isAlive());
138 >        assertEquals(a.get(this), 3);
139      }
140  
141      /**
142       * repeated weakCompareAndSet succeeds in changing value when equal
143 <     * to expected
143 >     * to expected
144       */
145      public void testWeakCompareAndSet() {
146          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
# Line 140 | Line 150 | public class AtomicIntegerFieldUpdaterTe
150              return;
151          }
152          x = 1;
153 <        while(!a.weakCompareAndSet(this,1,2));
154 <        while(!a.weakCompareAndSet(this,2,-4));
155 <        assertEquals(-4,a.get(this));
156 <        while(!a.weakCompareAndSet(this,-4,7));
157 <        assertEquals(7,a.get(this));
153 >        while (!a.weakCompareAndSet(this,1,2));
154 >        while (!a.weakCompareAndSet(this,2,-4));
155 >        assertEquals(-4,a.get(this));
156 >        while (!a.weakCompareAndSet(this,-4,7));
157 >        assertEquals(7,a.get(this));
158      }
159  
160      /**
161 <     *  getAndSet returns previous value and sets to given value
161 >     * getAndSet returns previous value and sets to given value
162       */
163      public void testGetAndSet() {
164          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
# Line 158 | Line 168 | public class AtomicIntegerFieldUpdaterTe
168              return;
169          }
170          x = 1;
171 <        assertEquals(1,a.getAndSet(this, 0));
172 <        assertEquals(0,a.getAndSet(this,-10));
173 <        assertEquals(-10,a.getAndSet(this,1));
171 >        assertEquals(1,a.getAndSet(this, 0));
172 >        assertEquals(0,a.getAndSet(this,-10));
173 >        assertEquals(-10,a.getAndSet(this,1));
174      }
175  
176      /**
# Line 174 | Line 184 | public class AtomicIntegerFieldUpdaterTe
184              return;
185          }
186          x = 1;
187 <        assertEquals(1,a.getAndAdd(this,2));
188 <        assertEquals(3,a.get(this));
189 <        assertEquals(3,a.getAndAdd(this,-4));
190 <        assertEquals(-1,a.get(this));
187 >        assertEquals(1,a.getAndAdd(this,2));
188 >        assertEquals(3,a.get(this));
189 >        assertEquals(3,a.getAndAdd(this,-4));
190 >        assertEquals(-1,a.get(this));
191      }
192  
193      /**
# Line 191 | Line 201 | public class AtomicIntegerFieldUpdaterTe
201              return;
202          }
203          x = 1;
204 <        assertEquals(1,a.getAndDecrement(this));
205 <        assertEquals(0,a.getAndDecrement(this));
206 <        assertEquals(-1,a.getAndDecrement(this));
204 >        assertEquals(1,a.getAndDecrement(this));
205 >        assertEquals(0,a.getAndDecrement(this));
206 >        assertEquals(-1,a.getAndDecrement(this));
207      }
208  
209      /**
# Line 207 | Line 217 | public class AtomicIntegerFieldUpdaterTe
217              return;
218          }
219          x = 1;
220 <        assertEquals(1,a.getAndIncrement(this));
221 <        assertEquals(2,a.get(this));
222 <        a.set(this,-2);
223 <        assertEquals(-2,a.getAndIncrement(this));
224 <        assertEquals(-1,a.getAndIncrement(this));
225 <        assertEquals(0,a.getAndIncrement(this));
226 <        assertEquals(1,a.get(this));
220 >        assertEquals(1,a.getAndIncrement(this));
221 >        assertEquals(2,a.get(this));
222 >        a.set(this,-2);
223 >        assertEquals(-2,a.getAndIncrement(this));
224 >        assertEquals(-1,a.getAndIncrement(this));
225 >        assertEquals(0,a.getAndIncrement(this));
226 >        assertEquals(1,a.get(this));
227      }
228  
229      /**
# Line 227 | Line 237 | public class AtomicIntegerFieldUpdaterTe
237              return;
238          }
239          x = 1;
240 <        assertEquals(3,a.addAndGet(this,2));
241 <        assertEquals(3,a.get(this));
242 <        assertEquals(-1,a.addAndGet(this,-4));
243 <        assertEquals(-1,a.get(this));
240 >        assertEquals(3,a.addAndGet(this,2));
241 >        assertEquals(3,a.get(this));
242 >        assertEquals(-1,a.addAndGet(this,-4));
243 >        assertEquals(-1,a.get(this));
244      }
245  
246      /**
# Line 244 | Line 254 | public class AtomicIntegerFieldUpdaterTe
254              return;
255          }
256          x = 1;
257 <        assertEquals(0,a.decrementAndGet(this));
258 <        assertEquals(-1,a.decrementAndGet(this));
259 <        assertEquals(-2,a.decrementAndGet(this));
260 <        assertEquals(-2,a.get(this));
257 >        assertEquals(0,a.decrementAndGet(this));
258 >        assertEquals(-1,a.decrementAndGet(this));
259 >        assertEquals(-2,a.decrementAndGet(this));
260 >        assertEquals(-2,a.get(this));
261      }
262  
263      /**
# Line 261 | Line 271 | public class AtomicIntegerFieldUpdaterTe
271              return;
272          }
273          x = 1;
274 <        assertEquals(2,a.incrementAndGet(this));
275 <        assertEquals(2,a.get(this));
276 <        a.set(this,-2);
277 <        assertEquals(-1,a.incrementAndGet(this));
278 <        assertEquals(0,a.incrementAndGet(this));
279 <        assertEquals(1,a.incrementAndGet(this));
280 <        assertEquals(1,a.get(this));
274 >        assertEquals(2,a.incrementAndGet(this));
275 >        assertEquals(2,a.get(this));
276 >        a.set(this,-2);
277 >        assertEquals(-1,a.incrementAndGet(this));
278 >        assertEquals(0,a.incrementAndGet(this));
279 >        assertEquals(1,a.incrementAndGet(this));
280 >        assertEquals(1,a.get(this));
281      }
282  
283   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines