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.16 by jsr166, Sat Nov 21 17:38:05 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 14 | Line 14 | public class AtomicIntegerFieldUpdaterTe
14      volatile int x = 0;
15      int w;
16      long z;
17 <    public static void main(String[] args){
17 >    public static void main(String[] args) {
18          junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
# Line 25 | Line 25 | public class AtomicIntegerFieldUpdaterTe
25       * Construction with non-existent field throws RuntimeException
26       */
27      public void testConstructor() {
28 <        try{
29 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
28 >        try {
29 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30                  a = AtomicIntegerFieldUpdater.newUpdater
31                  (AtomicIntegerFieldUpdaterTest.class, "y");
32              shouldThrow();
33 <        }
34 <        catch (RuntimeException rt) {}
33 >        } catch (RuntimeException success) {}
34      }
35  
36      /**
37       * construction with field not of given type throws RuntimeException
38       */
39      public void testConstructor2() {
40 <        try{
41 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
40 >        try {
41 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
42                  a = AtomicIntegerFieldUpdater.newUpdater
43                  (AtomicIntegerFieldUpdaterTest.class, "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>
52 >        try {
53 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
54                  a = AtomicIntegerFieldUpdater.newUpdater
55                  (AtomicIntegerFieldUpdaterTest.class, "w");
56              shouldThrow();
57 <        }
60 <        catch (RuntimeException rt) {}
57 >        } catch (RuntimeException success) {}
58      }
59  
60      /**
# Line 71 | Line 68 | public class AtomicIntegerFieldUpdaterTe
68              return;
69          }
70          x = 1;
71 <        assertEquals(1,a.get(this));
72 <        a.set(this,2);
73 <        assertEquals(2,a.get(this));
74 <        a.set(this,-3);
75 <        assertEquals(-3,a.get(this));
76 <        
71 >        assertEquals(1,a.get(this));
72 >        a.set(this,2);
73 >        assertEquals(2,a.get(this));
74 >        a.set(this,-3);
75 >        assertEquals(-3,a.get(this));
76 >
77 >    }
78 >
79 >    /**
80 >     *  get returns the last value lazySet by same thread
81 >     */
82 >    public void testGetLazySet() {
83 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
84 >        try {
85 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
86 >        } catch (RuntimeException ok) {
87 >            return;
88 >        }
89 >        x = 1;
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       */
# Line 89 | Line 106 | public class AtomicIntegerFieldUpdaterTe
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 >        assertFalse((7 == a.get(this)));
114 >        assertTrue(a.compareAndSet(this,-4,7));
115 >        assertEquals(7,a.get(this));
116      }
117  
118  
# Line 103 | Line 120 | public class AtomicIntegerFieldUpdaterTe
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 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
126          try {
# Line 112 | Line 129 | public class AtomicIntegerFieldUpdaterTe
129              return;
130          }
131  
132 <        Thread t = new Thread(new Runnable() {
133 <                public void run() {
134 <                    while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
135 <                }});
136 <        try {
137 <            t.start();
138 <            assertTrue(a.compareAndSet(this, 1, 2));
139 <            t.join(LONG_DELAY_MS);
140 <            assertFalse(t.isAlive());
141 <            assertEquals(a.get(this), 3);
142 <        }
126 <        catch(Exception e) {
127 <            unexpectedException();
128 <        }
132 >        Thread t = new Thread(new CheckedRunnable() {
133 >            public void realRun() {
134 >                while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.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          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
# Line 140 | Line 154 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 158 | Line 172 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 174 | Line 188 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 191 | Line 205 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 207 | Line 221 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 227 | Line 241 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 244 | Line 258 | public class AtomicIntegerFieldUpdaterTe
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      /**
# Line 261 | Line 275 | public class AtomicIntegerFieldUpdaterTe
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