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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines