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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.13 by jsr166, Tue Nov 17 03:12:51 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines