1 /*
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 package jsr166;
10 
11 import junit.framework.*;
12 import java.util.concurrent.atomic.AtomicInteger;
13 
14 public class AtomicIntegerTest extends JSR166TestCase {
15 
16     final int[] VALUES = {
17         Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE,
18     };
19 
20     /**
21      * constructor initializes to given value
22      */
testConstructor()23     public void testConstructor() {
24         AtomicInteger ai = new AtomicInteger(1);
25         assertEquals(1, ai.get());
26     }
27 
28     /**
29      * default constructed initializes to zero
30      */
testConstructor2()31     public void testConstructor2() {
32         AtomicInteger ai = new AtomicInteger();
33         assertEquals(0, ai.get());
34     }
35 
36     /**
37      * get returns the last value set
38      */
testGetSet()39     public void testGetSet() {
40         AtomicInteger ai = new AtomicInteger(1);
41         assertEquals(1, ai.get());
42         ai.set(2);
43         assertEquals(2, ai.get());
44         ai.set(-3);
45         assertEquals(-3, ai.get());
46     }
47 
48     /**
49      * get returns the last value lazySet in same thread
50      */
testGetLazySet()51     public void testGetLazySet() {
52         AtomicInteger ai = new AtomicInteger(1);
53         assertEquals(1, ai.get());
54         ai.lazySet(2);
55         assertEquals(2, ai.get());
56         ai.lazySet(-3);
57         assertEquals(-3, ai.get());
58     }
59 
60     /**
61      * compareAndSet succeeds in changing value if equal to expected else fails
62      */
testCompareAndSet()63     public void testCompareAndSet() {
64         AtomicInteger ai = new AtomicInteger(1);
65         assertTrue(ai.compareAndSet(1, 2));
66         assertTrue(ai.compareAndSet(2, -4));
67         assertEquals(-4, ai.get());
68         assertFalse(ai.compareAndSet(-5, 7));
69         assertEquals(-4, ai.get());
70         assertTrue(ai.compareAndSet(-4, 7));
71         assertEquals(7, ai.get());
72     }
73 
74     /**
75      * compareAndSet in one thread enables another waiting for value
76      * to succeed
77      */
testCompareAndSetInMultipleThreads()78     public void testCompareAndSetInMultipleThreads() throws Exception {
79         final AtomicInteger ai = new AtomicInteger(1);
80         Thread t = new Thread(new CheckedRunnable() {
81             public void realRun() {
82                 while (!ai.compareAndSet(2, 3))
83                     Thread.yield();
84             }});
85 
86         t.start();
87         assertTrue(ai.compareAndSet(1, 2));
88         t.join(LONG_DELAY_MS);
89         assertFalse(t.isAlive());
90         assertEquals(3, ai.get());
91     }
92 
93     /**
94      * repeated weakCompareAndSet succeeds in changing value when equal
95      * to expected
96      */
testWeakCompareAndSet()97     public void testWeakCompareAndSet() {
98         AtomicInteger ai = new AtomicInteger(1);
99         while (!ai.weakCompareAndSet(1, 2));
100         while (!ai.weakCompareAndSet(2, -4));
101         assertEquals(-4, ai.get());
102         while (!ai.weakCompareAndSet(-4, 7));
103         assertEquals(7, ai.get());
104     }
105 
106     /**
107      * getAndSet returns previous value and sets to given value
108      */
testGetAndSet()109     public void testGetAndSet() {
110         AtomicInteger ai = new AtomicInteger(1);
111         assertEquals(1, ai.getAndSet(0));
112         assertEquals(0, ai.getAndSet(-10));
113         assertEquals(-10, ai.getAndSet(1));
114     }
115 
116     /**
117      * getAndAdd returns previous value and adds given value
118      */
testGetAndAdd()119     public void testGetAndAdd() {
120         AtomicInteger ai = new AtomicInteger(1);
121         assertEquals(1, ai.getAndAdd(2));
122         assertEquals(3, ai.get());
123         assertEquals(3, ai.getAndAdd(-4));
124         assertEquals(-1, ai.get());
125     }
126 
127     /**
128      * getAndDecrement returns previous value and decrements
129      */
testGetAndDecrement()130     public void testGetAndDecrement() {
131         AtomicInteger ai = new AtomicInteger(1);
132         assertEquals(1, ai.getAndDecrement());
133         assertEquals(0, ai.getAndDecrement());
134         assertEquals(-1, ai.getAndDecrement());
135     }
136 
137     /**
138      * getAndIncrement returns previous value and increments
139      */
testGetAndIncrement()140     public void testGetAndIncrement() {
141         AtomicInteger ai = new AtomicInteger(1);
142         assertEquals(1, ai.getAndIncrement());
143         assertEquals(2, ai.get());
144         ai.set(-2);
145         assertEquals(-2, ai.getAndIncrement());
146         assertEquals(-1, ai.getAndIncrement());
147         assertEquals(0, ai.getAndIncrement());
148         assertEquals(1, ai.get());
149     }
150 
151     /**
152      * addAndGet adds given value to current, and returns current value
153      */
testAddAndGet()154     public void testAddAndGet() {
155         AtomicInteger ai = new AtomicInteger(1);
156         assertEquals(3, ai.addAndGet(2));
157         assertEquals(3, ai.get());
158         assertEquals(-1, ai.addAndGet(-4));
159         assertEquals(-1, ai.get());
160     }
161 
162     /**
163      * decrementAndGet decrements and returns current value
164      */
testDecrementAndGet()165     public void testDecrementAndGet() {
166         AtomicInteger ai = new AtomicInteger(1);
167         assertEquals(0, ai.decrementAndGet());
168         assertEquals(-1, ai.decrementAndGet());
169         assertEquals(-2, ai.decrementAndGet());
170         assertEquals(-2, ai.get());
171     }
172 
173     /**
174      * incrementAndGet increments and returns current value
175      */
testIncrementAndGet()176     public void testIncrementAndGet() {
177         AtomicInteger ai = new AtomicInteger(1);
178         assertEquals(2, ai.incrementAndGet());
179         assertEquals(2, ai.get());
180         ai.set(-2);
181         assertEquals(-1, ai.incrementAndGet());
182         assertEquals(0, ai.incrementAndGet());
183         assertEquals(1, ai.incrementAndGet());
184         assertEquals(1, ai.get());
185     }
186 
187     /**
188      * a deserialized serialized atomic holds same value
189      */
testSerialization()190     public void testSerialization() throws Exception {
191         AtomicInteger x = new AtomicInteger();
192         AtomicInteger y = serialClone(x);
193         assertNotSame(x, y);
194         x.set(22);
195         AtomicInteger z = serialClone(x);
196         assertEquals(22, x.get());
197         assertEquals(0, y.get());
198         assertEquals(22, z.get());
199     }
200 
201     /**
202      * toString returns current value.
203      */
testToString()204     public void testToString() {
205         AtomicInteger ai = new AtomicInteger();
206         assertEquals("0", ai.toString());
207         for (int x : VALUES) {
208             ai.set(x);
209             assertEquals(Integer.toString(x), ai.toString());
210         }
211     }
212 
213     /**
214      * intValue returns current value.
215      */
testIntValue()216     public void testIntValue() {
217         AtomicInteger ai = new AtomicInteger();
218         assertEquals(0, ai.intValue());
219         for (int x : VALUES) {
220             ai.set(x);
221             assertEquals(x, ai.intValue());
222         }
223     }
224 
225     /**
226      * longValue returns current value.
227      */
testLongValue()228     public void testLongValue() {
229         AtomicInteger ai = new AtomicInteger();
230         assertEquals(0L, ai.longValue());
231         for (int x : VALUES) {
232             ai.set(x);
233             assertEquals((long)x, ai.longValue());
234         }
235     }
236 
237     /**
238      * floatValue returns current value.
239      */
testFloatValue()240     public void testFloatValue() {
241         AtomicInteger ai = new AtomicInteger();
242         assertEquals(0.0f, ai.floatValue());
243         for (int x : VALUES) {
244             ai.set(x);
245             assertEquals((float)x, ai.floatValue());
246         }
247     }
248 
249     /**
250      * doubleValue returns current value.
251      */
testDoubleValue()252     public void testDoubleValue() {
253         AtomicInteger ai = new AtomicInteger();
254         assertEquals(0.0d, ai.doubleValue());
255         for (int x : VALUES) {
256             ai.set(x);
257             assertEquals((double)x, ai.doubleValue());
258         }
259     }
260 
261 }
262