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.5 by dl, Sat Oct 25 16:02:13 2003 UTC vs.
Revision 1.15 by jsr166, Sat Nov 21 02:07:26 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 13 | 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 21 | Line 22 | public class AtomicIntegerFieldUpdaterTe
22      }
23  
24      /**
25 <     * Contruction with non-existent field throws RuntimeException
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();
# Line 37 | Line 38 | public class AtomicIntegerFieldUpdaterTe
38       * construction with field not of given type throws RuntimeException
39       */
40      public void testConstructor2() {
41 <        try{
42 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
41 >        try {
42 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43                  a = AtomicIntegerFieldUpdater.newUpdater
44                  (AtomicIntegerFieldUpdaterTest.class, "z");
45              shouldThrow();
# Line 50 | Line 51 | public class AtomicIntegerFieldUpdaterTe
51       * construction with non-volatile field throws RuntimeException
52       */
53      public void testConstructor3() {
54 <        try{
55 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
54 >        try {
55 >            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56                  a = AtomicIntegerFieldUpdater.newUpdater
57                  (AtomicIntegerFieldUpdaterTest.class, "w");
58              shouldThrow();
# Line 63 | Line 64 | public class AtomicIntegerFieldUpdaterTe
64       *  get returns the last value set or assigned
65       */
66      public void testGetSet() {
67 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
67 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
68 >        try {
69 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70 >        } catch (RuntimeException ok) {
71 >            return;
72 >        }
73          x = 1;
74 <        assertEquals(1,a.get(this));
75 <        a.set(this,2);
76 <        assertEquals(2,a.get(this));
77 <        a.set(this,-3);
78 <        assertEquals(-3,a.get(this));
79 <        
74 >        assertEquals(1,a.get(this));
75 >        a.set(this,2);
76 >        assertEquals(2,a.get(this));
77 >        a.set(this,-3);
78 >        assertEquals(-3,a.get(this));
79 >
80      }
81 +
82 +    /**
83 +     *  get returns the last value lazySet by same thread
84 +     */
85 +    public void testGetLazySet() {
86 +        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
87 +        try {
88 +            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
89 +        } catch (RuntimeException ok) {
90 +            return;
91 +        }
92 +        x = 1;
93 +        assertEquals(1,a.get(this));
94 +        a.lazySet(this,2);
95 +        assertEquals(2,a.get(this));
96 +        a.lazySet(this,-3);
97 +        assertEquals(-3,a.get(this));
98 +
99 +    }
100 +
101      /**
102       * compareAndSet succeeds in changing value if equal to expected else fails
103       */
104      public void testCompareAndSet() {
105 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
105 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
106 >        try {
107 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
108 >        } catch (RuntimeException ok) {
109 >            return;
110 >        }
111          x = 1;
112 <        assertTrue(a.compareAndSet(this,1,2));
113 <        assertTrue(a.compareAndSet(this,2,-4));
114 <        assertEquals(-4,a.get(this));
115 <        assertFalse(a.compareAndSet(this,-5,7));
116 <        assertFalse((7 == a.get(this)));
117 <        assertTrue(a.compareAndSet(this,-4,7));
118 <        assertEquals(7,a.get(this));
112 >        assertTrue(a.compareAndSet(this,1,2));
113 >        assertTrue(a.compareAndSet(this,2,-4));
114 >        assertEquals(-4,a.get(this));
115 >        assertFalse(a.compareAndSet(this,-5,7));
116 >        assertFalse((7 == a.get(this)));
117 >        assertTrue(a.compareAndSet(this,-4,7));
118 >        assertEquals(7,a.get(this));
119      }
120  
121  
# Line 92 | Line 123 | public class AtomicIntegerFieldUpdaterTe
123       * compareAndSet in one thread enables another waiting for value
124       * to succeed
125       */
126 <    public void testCompareAndSetInMultipleThreads() {
126 >    public void testCompareAndSetInMultipleThreads() throws Exception {
127          x = 1;
128 <        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
129 <
130 <        Thread t = new Thread(new Runnable() {
131 <                public void run() {
132 <                    while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
102 <                }});
103 <        try {
104 <            t.start();
105 <            assertTrue(a.compareAndSet(this, 1, 2));
106 <            t.join(LONG_DELAY_MS);
107 <            assertFalse(t.isAlive());
108 <            assertEquals(a.get(this), 3);
109 <        }
110 <        catch(Exception e) {
111 <            unexpectedException();
128 >        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
129 >        try {
130 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
131 >        } catch (RuntimeException ok) {
132 >            return;
133          }
134 +
135 +        Thread t = new Thread(new CheckedRunnable() {
136 +            public void realRun() {
137 +                while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
138 +                    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
150 >     * to expected
151       */
152      public void testWeakCompareAndSet() {
153 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
153 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
154 >        try {
155 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.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));
164 <        assertEquals(7,a.get(this));
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));
164 >        assertEquals(7,a.get(this));
165      }
166  
167      /**
168       *  getAndSet returns previous value and sets to given value
169       */
170      public void testGetAndSet() {
171 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
171 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
172 >        try {
173 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.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));
180 <        assertEquals(-10,a.getAndSet(this,1));
178 >        assertEquals(1,a.getAndSet(this, 0));
179 >        assertEquals(0,a.getAndSet(this,-10));
180 >        assertEquals(-10,a.getAndSet(this,1));
181      }
182  
183      /**
184       * getAndAdd returns previous value and adds given value
185       */
186      public void testGetAndAdd() {
187 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
187 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
188 >        try {
189 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.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));
196 <        assertEquals(3,a.getAndAdd(this,-4));
197 <        assertEquals(-1,a.get(this));
194 >        assertEquals(1,a.getAndAdd(this,2));
195 >        assertEquals(3,a.get(this));
196 >        assertEquals(3,a.getAndAdd(this,-4));
197 >        assertEquals(-1,a.get(this));
198      }
199  
200      /**
201       * getAndDecrement returns previous value and decrements
202       */
203      public void testGetAndDecrement() {
204 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
204 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
205 >        try {
206 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
207 >        } catch (RuntimeException ok) {
208 >            return;
209 >        }
210          x = 1;
211 <        assertEquals(1,a.getAndDecrement(this));
212 <        assertEquals(0,a.getAndDecrement(this));
213 <        assertEquals(-1,a.getAndDecrement(this));
211 >        assertEquals(1,a.getAndDecrement(this));
212 >        assertEquals(0,a.getAndDecrement(this));
213 >        assertEquals(-1,a.getAndDecrement(this));
214      }
215  
216      /**
217       * getAndIncrement returns previous value and increments
218       */
219      public void testGetAndIncrement() {
220 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
220 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
221 >        try {
222 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
223 >        } catch (RuntimeException ok) {
224 >            return;
225 >        }
226          x = 1;
227 <        assertEquals(1,a.getAndIncrement(this));
228 <        assertEquals(2,a.get(this));
229 <        a.set(this,-2);
230 <        assertEquals(-2,a.getAndIncrement(this));
231 <        assertEquals(-1,a.getAndIncrement(this));
232 <        assertEquals(0,a.getAndIncrement(this));
233 <        assertEquals(1,a.get(this));
227 >        assertEquals(1,a.getAndIncrement(this));
228 >        assertEquals(2,a.get(this));
229 >        a.set(this,-2);
230 >        assertEquals(-2,a.getAndIncrement(this));
231 >        assertEquals(-1,a.getAndIncrement(this));
232 >        assertEquals(0,a.getAndIncrement(this));
233 >        assertEquals(1,a.get(this));
234      }
235  
236      /**
237       * addAndGet adds given value to current, and returns current value
238       */
239      public void testAddAndGet() {
240 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
240 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
241 >        try {
242 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.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));
249 <        assertEquals(-1,a.addAndGet(this,-4));
250 <        assertEquals(-1,a.get(this));
247 >        assertEquals(3,a.addAndGet(this,2));
248 >        assertEquals(3,a.get(this));
249 >        assertEquals(-1,a.addAndGet(this,-4));
250 >        assertEquals(-1,a.get(this));
251      }
252  
253      /**
254       * decrementAndGet decrements and returns current value
255       */
256      public void testDecrementAndGet() {
257 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
257 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
258 >        try {
259 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
260 >        } catch (RuntimeException ok) {
261 >            return;
262 >        }
263          x = 1;
264 <        assertEquals(0,a.decrementAndGet(this));
265 <        assertEquals(-1,a.decrementAndGet(this));
266 <        assertEquals(-2,a.decrementAndGet(this));
267 <        assertEquals(-2,a.get(this));
264 >        assertEquals(0,a.decrementAndGet(this));
265 >        assertEquals(-1,a.decrementAndGet(this));
266 >        assertEquals(-2,a.decrementAndGet(this));
267 >        assertEquals(-2,a.get(this));
268      }
269  
270      /**
271       * incrementAndGet increments and returns current value
272       */
273      public void testIncrementAndGet() {
274 <        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
274 >        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
275 >        try {
276 >            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
277 >        } catch (RuntimeException ok) {
278 >            return;
279 >        }
280          x = 1;
281 <        assertEquals(2,a.incrementAndGet(this));
282 <        assertEquals(2,a.get(this));
283 <        a.set(this,-2);
284 <        assertEquals(-1,a.incrementAndGet(this));
285 <        assertEquals(0,a.incrementAndGet(this));
286 <        assertEquals(1,a.incrementAndGet(this));
287 <        assertEquals(1,a.get(this));
281 >        assertEquals(2,a.incrementAndGet(this));
282 >        assertEquals(2,a.get(this));
283 >        a.set(this,-2);
284 >        assertEquals(-1,a.incrementAndGet(this));
285 >        assertEquals(0,a.incrementAndGet(this));
286 >        assertEquals(1,a.incrementAndGet(this));
287 >        assertEquals(1,a.get(this));
288      }
289  
290   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines