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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines