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.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.17 by jsr166, Tue Dec 1 09:56:28 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines