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.11 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.29 by jsr166, Sat Apr 25 04:55:30 2015 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
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.*;
10 < import junit.framework.*;
11 < import java.util.*;
9 > import java.util.concurrent.atomic.AtomicLongFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
15      volatile long x = 0;
16      int z;
17      long w;
18  
19 <    public static void main(String[] args){
20 <        junit.textui.TestRunner.run(suite());
19 >    public static void main(String[] args) {
20 >        main(suite(), args);
21      }
22      public static Test suite() {
23          return new TestSuite(AtomicLongFieldUpdaterTest.class);
24      }
25  
26 +    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
27 +        return AtomicLongFieldUpdater.newUpdater
28 +            (AtomicLongFieldUpdaterTest.class, fieldName);
29 +    }
30 +
31      /**
32       * Construction with non-existent field throws RuntimeException
33       */
34 <    public void testConstructor(){
34 >    public void testConstructor() {
35          try {
36 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
31 <                a = AtomicLongFieldUpdater.newUpdater
32 <                (AtomicLongFieldUpdaterTest.class, "y");
36 >            updaterFor("y");
37              shouldThrow();
38 +        } catch (RuntimeException success) {
39 +            assertNotNull(success.getCause());
40          }
35        catch (RuntimeException rt) {}
41      }
42  
43      /**
44 <     * construction with field not of given type throws RuntimeException
44 >     * construction with field not of given type throws IllegalArgumentException
45       */
46 <    public void testConstructor2(){
46 >    public void testConstructor2() {
47          try {
48 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
44 <                a = AtomicLongFieldUpdater.newUpdater
45 <                (AtomicLongFieldUpdaterTest.class, "z");
48 >            updaterFor("z");
49              shouldThrow();
50 <        }
48 <        catch (RuntimeException rt) {}
50 >        } catch (IllegalArgumentException success) {}
51      }
52  
53      /**
54 <     * construction with non-volatile field throws RuntimeException
54 >     * construction with non-volatile field throws IllegalArgumentException
55       */
56 <    public void testConstructor3(){
56 >    public void testConstructor3() {
57          try {
58 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
57 <                a = AtomicLongFieldUpdater.newUpdater
58 <                (AtomicLongFieldUpdaterTest.class, "w");
58 >            updaterFor("w");
59              shouldThrow();
60 <        }
61 <
62 <        catch (RuntimeException rt) {}
60 >        } catch (IllegalArgumentException success) {}
61      }
62  
63      /**
64 <     *  get returns the last value set or assigned
64 >     * get returns the last value set or assigned
65       */
66 <    public void testGetSet(){
66 >    public void testGetSet() {
67          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
68 <        try {
71 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
72 <        } catch (RuntimeException ok) {
73 <            return;
74 <        }
68 >        a = updaterFor("x");
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));
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
78 >     * get returns the last value lazySet by same thread
79       */
80 <    public void testGetLazySet(){
80 >    public void testGetLazySet() {
81          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
82 <        try {
89 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
90 <        } catch (RuntimeException ok) {
91 <            return;
92 <        }
82 >        a = updaterFor("x");
83          x = 1;
84 <        assertEquals(1,a.get(this));
85 <        a.lazySet(this,2);
86 <        assertEquals(2,a.get(this));
87 <        a.lazySet(this,-3);
88 <        assertEquals(-3,a.get(this));
84 >        assertEquals(1, a.get(this));
85 >        a.lazySet(this, 2);
86 >        assertEquals(2, a.get(this));
87 >        a.lazySet(this, -3);
88 >        assertEquals(-3, a.get(this));
89      }
90  
101
91      /**
92       * compareAndSet succeeds in changing value if equal to expected else fails
93       */
94 <    public void testCompareAndSet(){
94 >    public void testCompareAndSet() {
95          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
96 <        try {
108 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
109 <        } catch (RuntimeException ok) {
110 <            return;
111 <        }
96 >        a = updaterFor("x");
97          x = 1;
98 <        assertTrue(a.compareAndSet(this,1,2));
99 <        assertTrue(a.compareAndSet(this,2,-4));
100 <        assertEquals(-4,a.get(this));
101 <        assertFalse(a.compareAndSet(this,-5,7));
102 <        assertFalse((7 == a.get(this)));
103 <        assertTrue(a.compareAndSet(this,-4,7));
104 <        assertEquals(7,a.get(this));
98 >        assertTrue(a.compareAndSet(this, 1, 2));
99 >        assertTrue(a.compareAndSet(this, 2, -4));
100 >        assertEquals(-4, a.get(this));
101 >        assertFalse(a.compareAndSet(this, -5, 7));
102 >        assertEquals(-4, a.get(this));
103 >        assertTrue(a.compareAndSet(this, -4, 7));
104 >        assertEquals(7, a.get(this));
105      }
106  
122
107      /**
108       * compareAndSet in one thread enables another waiting for value
109       * to succeed
110       */
111 <    public void testCompareAndSetInMultipleThreads() {
111 >    public void testCompareAndSetInMultipleThreads() throws Exception {
112          x = 1;
113 <        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
114 <        try {
131 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
132 <        } catch (RuntimeException ok) {
133 <            return;
134 <        }
113 >        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
114 >        a = updaterFor("x");
115  
116 <        Thread t = new Thread(new Runnable() {
117 <                public void run() {
118 <                    while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield();
119 <                }});
120 <        try {
121 <            t.start();
122 <            assertTrue(a.compareAndSet(this, 1, 2));
123 <            t.join(LONG_DELAY_MS);
124 <            assertFalse(t.isAlive());
125 <            assertEquals(a.get(this), 3);
126 <        }
147 <        catch (Exception e) {
148 <            unexpectedException();
149 <        }
116 >        Thread t = new Thread(new CheckedRunnable() {
117 >            public void realRun() {
118 >                while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
119 >                    Thread.yield();
120 >            }});
121 >
122 >        t.start();
123 >        assertTrue(a.compareAndSet(this, 1, 2));
124 >        t.join(LONG_DELAY_MS);
125 >        assertFalse(t.isAlive());
126 >        assertEquals(3, a.get(this));
127      }
128  
129      /**
130       * repeated weakCompareAndSet succeeds in changing value when equal
131       * to expected
132       */
133 <    public void testWeakCompareAndSet(){
133 >    public void testWeakCompareAndSet() {
134          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
135 <        try {
159 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
160 <        } catch (RuntimeException ok) {
161 <            return;
162 <        }
135 >        a = updaterFor("x");
136          x = 1;
137 <        while (!a.weakCompareAndSet(this,1,2));
138 <        while (!a.weakCompareAndSet(this,2,-4));
139 <        assertEquals(-4,a.get(this));
140 <        while (!a.weakCompareAndSet(this,-4,7));
141 <        assertEquals(7,a.get(this));
137 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
138 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
139 >        assertEquals(-4, a.get(this));
140 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
141 >        assertEquals(7, a.get(this));
142      }
143  
144      /**
145 <     *  getAndSet returns previous value and sets to given value
145 >     * getAndSet returns previous value and sets to given value
146       */
147 <    public void testGetAndSet(){
147 >    public void testGetAndSet() {
148          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
149 <        try {
177 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
178 <        } catch (RuntimeException ok) {
179 <            return;
180 <        }
149 >        a = updaterFor("x");
150          x = 1;
151 <        assertEquals(1,a.getAndSet(this, 0));
152 <        assertEquals(0,a.getAndSet(this,-10));
153 <        assertEquals(-10,a.getAndSet(this,1));
151 >        assertEquals(1, a.getAndSet(this, 0));
152 >        assertEquals(0, a.getAndSet(this, -10));
153 >        assertEquals(-10, a.getAndSet(this, 1));
154      }
155  
156      /**
157       * getAndAdd returns previous value and adds given value
158       */
159 <    public void testGetAndAdd(){
159 >    public void testGetAndAdd() {
160          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
161 <        try {
193 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
194 <        } catch (RuntimeException ok) {
195 <            return;
196 <        }
161 >        a = updaterFor("x");
162          x = 1;
163 <        assertEquals(1,a.getAndAdd(this,2));
164 <        assertEquals(3,a.get(this));
165 <        assertEquals(3,a.getAndAdd(this,-4));
166 <        assertEquals(-1,a.get(this));
163 >        assertEquals(1, a.getAndAdd(this, 2));
164 >        assertEquals(3, a.get(this));
165 >        assertEquals(3, a.getAndAdd(this, -4));
166 >        assertEquals(-1, a.get(this));
167      }
168  
169      /**
170       * getAndDecrement returns previous value and decrements
171       */
172 <    public void testGetAndDecrement(){
172 >    public void testGetAndDecrement() {
173          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
174 <        try {
210 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
211 <        } catch (RuntimeException ok) {
212 <            return;
213 <        }
174 >        a = updaterFor("x");
175          x = 1;
176 <        assertEquals(1,a.getAndDecrement(this));
177 <        assertEquals(0,a.getAndDecrement(this));
178 <        assertEquals(-1,a.getAndDecrement(this));
176 >        assertEquals(1, a.getAndDecrement(this));
177 >        assertEquals(0, a.getAndDecrement(this));
178 >        assertEquals(-1, a.getAndDecrement(this));
179      }
180  
181      /**
182       * getAndIncrement returns previous value and increments
183       */
184 <    public void testGetAndIncrement(){
184 >    public void testGetAndIncrement() {
185          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
186 <        try {
226 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
227 <        } catch (RuntimeException ok) {
228 <            return;
229 <        }
186 >        a = updaterFor("x");
187          x = 1;
188 <        assertEquals(1,a.getAndIncrement(this));
189 <        assertEquals(2,a.get(this));
190 <        a.set(this,-2);
191 <        assertEquals(-2,a.getAndIncrement(this));
192 <        assertEquals(-1,a.getAndIncrement(this));
193 <        assertEquals(0,a.getAndIncrement(this));
194 <        assertEquals(1,a.get(this));
188 >        assertEquals(1, a.getAndIncrement(this));
189 >        assertEquals(2, a.get(this));
190 >        a.set(this, -2);
191 >        assertEquals(-2, a.getAndIncrement(this));
192 >        assertEquals(-1, a.getAndIncrement(this));
193 >        assertEquals(0, a.getAndIncrement(this));
194 >        assertEquals(1, a.get(this));
195      }
196  
197      /**
198       * addAndGet adds given value to current, and returns current value
199       */
200 <    public void testAddAndGet(){
200 >    public void testAddAndGet() {
201          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
202 <        try {
246 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
247 <        } catch (RuntimeException ok) {
248 <            return;
249 <        }
202 >        a = updaterFor("x");
203          x = 1;
204 <        assertEquals(3,a.addAndGet(this,2));
205 <        assertEquals(3,a.get(this));
206 <        assertEquals(-1,a.addAndGet(this,-4));
207 <        assertEquals(-1,a.get(this));
204 >        assertEquals(3, a.addAndGet(this, 2));
205 >        assertEquals(3, a.get(this));
206 >        assertEquals(-1, a.addAndGet(this, -4));
207 >        assertEquals(-1, a.get(this));
208      }
209  
210      /**
211 <     *  decrementAndGet decrements and returns current value
211 >     * decrementAndGet decrements and returns current value
212       */
213 <    public void testDecrementAndGet(){
213 >    public void testDecrementAndGet() {
214          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
215 <        try {
263 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
264 <        } catch (RuntimeException ok) {
265 <            return;
266 <        }
215 >        a = updaterFor("x");
216          x = 1;
217 <        assertEquals(0,a.decrementAndGet(this));
218 <        assertEquals(-1,a.decrementAndGet(this));
219 <        assertEquals(-2,a.decrementAndGet(this));
220 <        assertEquals(-2,a.get(this));
217 >        assertEquals(0, a.decrementAndGet(this));
218 >        assertEquals(-1, a.decrementAndGet(this));
219 >        assertEquals(-2, a.decrementAndGet(this));
220 >        assertEquals(-2, a.get(this));
221      }
222  
223      /**
224       * incrementAndGet increments and returns current value
225       */
226 <    public void testIncrementAndGet(){
226 >    public void testIncrementAndGet() {
227          AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
228 <        try {
280 <            a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
281 <        } catch (RuntimeException ok) {
282 <            return;
283 <        }
228 >        a = updaterFor("x");
229          x = 1;
230 <        assertEquals(2,a.incrementAndGet(this));
231 <        assertEquals(2,a.get(this));
232 <        a.set(this,-2);
233 <        assertEquals(-1,a.incrementAndGet(this));
234 <        assertEquals(0,a.incrementAndGet(this));
235 <        assertEquals(1,a.incrementAndGet(this));
236 <        assertEquals(1,a.get(this));
230 >        assertEquals(2, a.incrementAndGet(this));
231 >        assertEquals(2, a.get(this));
232 >        a.set(this, -2);
233 >        assertEquals(-1, a.incrementAndGet(this));
234 >        assertEquals(0, a.incrementAndGet(this));
235 >        assertEquals(1, a.incrementAndGet(this));
236 >        assertEquals(1, a.get(this));
237      }
238  
239   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines