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.20 by jsr166, Fri May 27 19:39:07 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.*;
# 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 <        }
58 >        } catch (RuntimeException success) {}
59 >    }
60  
61 <        catch (RuntimeException rt) {}
61 >    /**
62 >     * get returns the last value set or assigned
63 >     */
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 set or assigned
80 >     * get returns the last value lazySet by same thread
81       */
82 <    public void testGetSet(){
83 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
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       * compareAndSet succeeds in changing value if equal to expected else fails
99       */
100 <    public void testCompareAndSet(){
101 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
100 >    public void testCompareAndSet() {
101 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
102 >        try {
103 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
104 >        } catch (RuntimeException ok) {
105 >            return;
106 >        }
107          x = 1;
108 <        assertTrue(a.compareAndSet(this,1,2));
109 <        assertTrue(a.compareAndSet(this,2,-4));
110 <        assertEquals(-4,a.get(this));
111 <        assertFalse(a.compareAndSet(this,-5,7));
112 <        assertFalse((7 == a.get(this)));
113 <        assertTrue(a.compareAndSet(this,-4,7));
114 <        assertEquals(7,a.get(this));
108 >        assertTrue(a.compareAndSet(this,1,2));
109 >        assertTrue(a.compareAndSet(this,2,-4));
110 >        assertEquals(-4,a.get(this));
111 >        assertFalse(a.compareAndSet(this,-5,7));
112 >        assertEquals(-4,a.get(this));
113 >        assertTrue(a.compareAndSet(this,-4,7));
114 >        assertEquals(7,a.get(this));
115      }
116  
93
117      /**
118       * compareAndSet in one thread enables another waiting for value
119       * to succeed
120       */
121 <    public void testCompareAndSetInMultipleThreads() {
121 >    public void testCompareAndSetInMultipleThreads() throws Exception {
122          x = 1;
123 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
124 <
125 <        Thread t = new Thread(new Runnable() {
126 <                public void run() {
127 <                    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();
123 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
124 >        try {
125 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
126 >        } catch (RuntimeException ok) {
127 >            return;
128          }
129 +
130 +        Thread t = new Thread(new CheckedRunnable() {
131 +            public void realRun() {
132 +                while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
133 +                    Thread.yield();
134 +            }});
135 +
136 +        t.start();
137 +        assertTrue(a.compareAndSet(this, 1, 2));
138 +        t.join(LONG_DELAY_MS);
139 +        assertFalse(t.isAlive());
140 +        assertEquals(a.get(this), 3);
141      }
142  
143      /**
144       * repeated weakCompareAndSet succeeds in changing value when equal
145 <     * to expected
145 >     * to expected
146       */
147 <    public void testWeakCompareAndSet(){
148 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
147 >    public void testWeakCompareAndSet() {
148 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
149 >        try {
150 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
151 >        } catch (RuntimeException ok) {
152 >            return;
153 >        }
154          x = 1;
155 <        while(!a.weakCompareAndSet(this,1,2));
156 <        while(!a.weakCompareAndSet(this,2,-4));
157 <        assertEquals(-4,a.get(this));
158 <        while(!a.weakCompareAndSet(this,-4,7));
159 <        assertEquals(7,a.get(this));
155 >        while (!a.weakCompareAndSet(this,1,2));
156 >        while (!a.weakCompareAndSet(this,2,-4));
157 >        assertEquals(-4,a.get(this));
158 >        while (!a.weakCompareAndSet(this,-4,7));
159 >        assertEquals(7,a.get(this));
160      }
161  
162      /**
163 <     *  getAndSet returns previous value and sets to given value
163 >     * getAndSet returns previous value and sets to given value
164       */
165 <    public void testGetAndSet(){
166 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
165 >    public void testGetAndSet() {
166 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
167 >        try {
168 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
169 >        } catch (RuntimeException ok) {
170 >            return;
171 >        }
172          x = 1;
173 <        assertEquals(1,a.getAndSet(this, 0));
174 <        assertEquals(0,a.getAndSet(this,-10));
175 <        assertEquals(-10,a.getAndSet(this,1));
173 >        assertEquals(1,a.getAndSet(this, 0));
174 >        assertEquals(0,a.getAndSet(this,-10));
175 >        assertEquals(-10,a.getAndSet(this,1));
176      }
177  
178      /**
179       * getAndAdd returns previous value and adds given value
180       */
181 <    public void testGetAndAdd(){
182 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
181 >    public void testGetAndAdd() {
182 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
183 >        try {
184 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
185 >        } catch (RuntimeException ok) {
186 >            return;
187 >        }
188          x = 1;
189 <        assertEquals(1,a.getAndAdd(this,2));
190 <        assertEquals(3,a.get(this));
191 <        assertEquals(3,a.getAndAdd(this,-4));
192 <        assertEquals(-1,a.get(this));
189 >        assertEquals(1,a.getAndAdd(this,2));
190 >        assertEquals(3,a.get(this));
191 >        assertEquals(3,a.getAndAdd(this,-4));
192 >        assertEquals(-1,a.get(this));
193      }
194  
195      /**
196       * getAndDecrement returns previous value and decrements
197       */
198 <    public void testGetAndDecrement(){
199 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
198 >    public void testGetAndDecrement() {
199 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
200 >        try {
201 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
202 >        } catch (RuntimeException ok) {
203 >            return;
204 >        }
205          x = 1;
206 <        assertEquals(1,a.getAndDecrement(this));
207 <        assertEquals(0,a.getAndDecrement(this));
208 <        assertEquals(-1,a.getAndDecrement(this));
206 >        assertEquals(1,a.getAndDecrement(this));
207 >        assertEquals(0,a.getAndDecrement(this));
208 >        assertEquals(-1,a.getAndDecrement(this));
209      }
210  
211      /**
212       * getAndIncrement returns previous value and increments
213       */
214 <    public void testGetAndIncrement(){
215 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
214 >    public void testGetAndIncrement() {
215 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
216 >        try {
217 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
218 >        } catch (RuntimeException ok) {
219 >            return;
220 >        }
221          x = 1;
222 <        assertEquals(1,a.getAndIncrement(this));
223 <        assertEquals(2,a.get(this));
224 <        a.set(this,-2);
225 <        assertEquals(-2,a.getAndIncrement(this));
226 <        assertEquals(-1,a.getAndIncrement(this));
227 <        assertEquals(0,a.getAndIncrement(this));
228 <        assertEquals(1,a.get(this));
222 >        assertEquals(1,a.getAndIncrement(this));
223 >        assertEquals(2,a.get(this));
224 >        a.set(this,-2);
225 >        assertEquals(-2,a.getAndIncrement(this));
226 >        assertEquals(-1,a.getAndIncrement(this));
227 >        assertEquals(0,a.getAndIncrement(this));
228 >        assertEquals(1,a.get(this));
229      }
230  
231      /**
232       * addAndGet adds given value to current, and returns current value
233       */
234 <    public void testAddAndGet(){
235 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
234 >    public void testAddAndGet() {
235 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
236 >        try {
237 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
238 >        } catch (RuntimeException ok) {
239 >            return;
240 >        }
241          x = 1;
242 <        assertEquals(3,a.addAndGet(this,2));
243 <        assertEquals(3,a.get(this));
244 <        assertEquals(-1,a.addAndGet(this,-4));
245 <        assertEquals(-1,a.get(this));
242 >        assertEquals(3,a.addAndGet(this,2));
243 >        assertEquals(3,a.get(this));
244 >        assertEquals(-1,a.addAndGet(this,-4));
245 >        assertEquals(-1,a.get(this));
246      }
247  
248      /**
249 <     *  decrementAndGet decrements and returns current value
249 >     * decrementAndGet decrements and returns current value
250       */
251 <    public void testDecrementAndGet(){
252 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
251 >    public void testDecrementAndGet() {
252 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
253 >        try {
254 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
255 >        } catch (RuntimeException ok) {
256 >            return;
257 >        }
258          x = 1;
259 <        assertEquals(0,a.decrementAndGet(this));
260 <        assertEquals(-1,a.decrementAndGet(this));
261 <        assertEquals(-2,a.decrementAndGet(this));
262 <        assertEquals(-2,a.get(this));
259 >        assertEquals(0,a.decrementAndGet(this));
260 >        assertEquals(-1,a.decrementAndGet(this));
261 >        assertEquals(-2,a.decrementAndGet(this));
262 >        assertEquals(-2,a.get(this));
263      }
264  
265      /**
266       * incrementAndGet increments and returns current value
267       */
268 <    public void testIncrementAndGet(){
269 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
268 >    public void testIncrementAndGet() {
269 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
270 >        try {
271 >            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
272 >        } catch (RuntimeException ok) {
273 >            return;
274 >        }
275          x = 1;
276 <        assertEquals(2,a.incrementAndGet(this));
277 <        assertEquals(2,a.get(this));
278 <        a.set(this,-2);
279 <        assertEquals(-1,a.incrementAndGet(this));
280 <        assertEquals(0,a.incrementAndGet(this));
281 <        assertEquals(1,a.incrementAndGet(this));
282 <        assertEquals(1,a.get(this));
276 >        assertEquals(2,a.incrementAndGet(this));
277 >        assertEquals(2,a.get(this));
278 >        a.set(this,-2);
279 >        assertEquals(-1,a.incrementAndGet(this));
280 >        assertEquals(0,a.incrementAndGet(this));
281 >        assertEquals(1,a.incrementAndGet(this));
282 >        assertEquals(1,a.get(this));
283      }
284  
285   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines