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.28 by jsr166, Wed Dec 31 19:21:20 2014 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  
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){
19 >    public static void main(String[] args) {
20          junit.textui.TestRunner.run(suite());
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 <     * Contruction with non-existent field throws RuntimeException
32 >     * Construction with non-existent field throws RuntimeException
33       */
34 <    public void testConstructor(){
35 <        try{
36 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
30 <                a = AtomicLongFieldUpdater.newUpdater
31 <                (getClass(), "y");
34 >    public void testConstructor() {
35 >        try {
36 >            updaterFor("y");
37              shouldThrow();
38 +        } catch (RuntimeException success) {
39 +            assertNotNull(success.getCause());
40          }
34        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(){
47 <        try{
48 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
43 <                a = AtomicLongFieldUpdater.newUpdater
44 <                (getClass(), "z");
46 >    public void testConstructor2() {
47 >        try {
48 >            updaterFor("z");
49              shouldThrow();
50 <        }
47 <        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(){
57 <        try{
58 <            AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
56 <                a = AtomicLongFieldUpdater.newUpdater
57 <                (getClass(), "w");
56 >    public void testConstructor3() {
57 >        try {
58 >            updaterFor("w");
59              shouldThrow();
60 <        }
60 <
61 <        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(){
67 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
66 >    public void testGetSet() {
67 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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));
75 <        
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 <     * compareAndSet succeeds in changing value if equal to expected else fails
78 >     * get returns the last value lazySet by same thread
79       */
80 <    public void testCompareAndSet(){
81 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
80 >    public void testGetLazySet() {
81 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
82 >        a = updaterFor("x");
83          x = 1;
84 <        assertTrue(a.compareAndSet(this,1,2));
85 <        assertTrue(a.compareAndSet(this,2,-4));
86 <        assertEquals(-4,a.get(this));
87 <        assertFalse(a.compareAndSet(this,-5,7));
88 <        assertFalse((7 == a.get(this)));
88 <        assertTrue(a.compareAndSet(this,-4,7));
89 <        assertEquals(7,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  
91 +    /**
92 +     * compareAndSet succeeds in changing value if equal to expected else fails
93 +     */
94 +    public void testCompareAndSet() {
95 +        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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 +        assertEquals(-4, a.get(this));
103 +        assertTrue(a.compareAndSet(this, -4, 7));
104 +        assertEquals(7, a.get(this));
105 +    }
106  
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 = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
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 <        }
112 <        catch(Exception e) {
113 <            unexpectedException();
114 <        }
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
131 >     * to expected
132       */
133 <    public void testWeakCompareAndSet(){
134 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
133 >    public void testWeakCompareAndSet() {
134 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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(){
148 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
147 >    public void testGetAndSet() {
148 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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(){
160 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
161 <        x = 1;
162 <        assertEquals(1,a.getAndAdd(this,2));
163 <        assertEquals(3,a.get(this));
164 <        assertEquals(3,a.getAndAdd(this,-4));
165 <        assertEquals(-1,a.get(this));
159 >    public void testGetAndAdd() {
160 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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));
167      }
168  
169      /**
170       * getAndDecrement returns previous value and decrements
171       */
172 <    public void testGetAndDecrement(){
173 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
174 <        x = 1;
175 <        assertEquals(1,a.getAndDecrement(this));
176 <        assertEquals(0,a.getAndDecrement(this));
177 <        assertEquals(-1,a.getAndDecrement(this));
172 >    public void testGetAndDecrement() {
173 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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));
179      }
180  
181      /**
182       * getAndIncrement returns previous value and increments
183       */
184 <    public void testGetAndIncrement(){
185 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
186 <        x = 1;
187 <        assertEquals(1,a.getAndIncrement(this));
188 <        assertEquals(2,a.get(this));
189 <        a.set(this,-2);
190 <        assertEquals(-2,a.getAndIncrement(this));
191 <        assertEquals(-1,a.getAndIncrement(this));
192 <        assertEquals(0,a.getAndIncrement(this));
193 <        assertEquals(1,a.get(this));
184 >    public void testGetAndIncrement() {
185 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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));
195      }
196  
197      /**
198       * addAndGet adds given value to current, and returns current value
199       */
200 <    public void testAddAndGet(){
201 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
200 >    public void testAddAndGet() {
201 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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(){
214 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
213 >    public void testDecrementAndGet() {
214 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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(){
227 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
228 <        x = 1;
229 <        assertEquals(2,a.incrementAndGet(this));
230 <        assertEquals(2,a.get(this));
231 <        a.set(this,-2);
232 <        assertEquals(-1,a.incrementAndGet(this));
233 <        assertEquals(0,a.incrementAndGet(this));
234 <        assertEquals(1,a.incrementAndGet(this));
235 <        assertEquals(1,a.get(this));
226 >    public void testIncrementAndGet() {
227 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
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));
237      }
238  
239   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines