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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines