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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class AtomicLongTest extends JSR166TestCase { |
13 |
public static void main (String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(AtomicLongTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* constructor initializes to given value |
22 |
*/ |
23 |
public void testConstructor(){ |
24 |
AtomicLong ai = new AtomicLong(1); |
25 |
assertEquals(1,ai.get()); |
26 |
} |
27 |
|
28 |
/** |
29 |
* default constructed intializes to zero |
30 |
*/ |
31 |
public void testConstructor2(){ |
32 |
AtomicLong ai = new AtomicLong(); |
33 |
assertEquals(0,ai.get()); |
34 |
} |
35 |
|
36 |
/** |
37 |
* get returns the last value set |
38 |
*/ |
39 |
public void testGetSet(){ |
40 |
AtomicLong ai = new AtomicLong(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 |
* compareAndSet succeeds in changing value if equal to expected else fails |
50 |
*/ |
51 |
public void testCompareAndSet(){ |
52 |
AtomicLong ai = new AtomicLong(1); |
53 |
assertTrue(ai.compareAndSet(1,2)); |
54 |
assertTrue(ai.compareAndSet(2,-4)); |
55 |
assertEquals(-4,ai.get()); |
56 |
assertFalse(ai.compareAndSet(-5,7)); |
57 |
assertFalse((7 == ai.get())); |
58 |
assertTrue(ai.compareAndSet(-4,7)); |
59 |
assertEquals(7,ai.get()); |
60 |
} |
61 |
|
62 |
/** |
63 |
* compareAndSet in one thread enables another waiting for value |
64 |
* to succeed |
65 |
*/ |
66 |
public void testCompareAndSetInMultipleThreads() { |
67 |
final AtomicLong ai = new AtomicLong(1); |
68 |
Thread t = new Thread(new Runnable() { |
69 |
public void run() { |
70 |
while(!ai.compareAndSet(2, 3)) Thread.yield(); |
71 |
}}); |
72 |
try { |
73 |
t.start(); |
74 |
assertTrue(ai.compareAndSet(1, 2)); |
75 |
t.join(LONG_DELAY_MS); |
76 |
assertFalse(t.isAlive()); |
77 |
assertEquals(ai.get(), 3); |
78 |
} |
79 |
catch(Exception e) { |
80 |
unexpectedException(); |
81 |
} |
82 |
} |
83 |
|
84 |
/** |
85 |
* repeated weakCompareAndSet succeeds in changing value when equal |
86 |
* to expected |
87 |
*/ |
88 |
public void testWeakCompareAndSet(){ |
89 |
AtomicLong ai = new AtomicLong(1); |
90 |
while(!ai.weakCompareAndSet(1,2)); |
91 |
while(!ai.weakCompareAndSet(2,-4)); |
92 |
assertEquals(-4,ai.get()); |
93 |
while(!ai.weakCompareAndSet(-4,7)); |
94 |
assertEquals(7,ai.get()); |
95 |
} |
96 |
|
97 |
/** |
98 |
* getAndSet returns previous value and sets to given value |
99 |
*/ |
100 |
public void testGetAndSet(){ |
101 |
AtomicLong ai = new AtomicLong(1); |
102 |
assertEquals(1,ai.getAndSet(0)); |
103 |
assertEquals(0,ai.getAndSet(-10)); |
104 |
assertEquals(-10,ai.getAndSet(1)); |
105 |
} |
106 |
|
107 |
/** |
108 |
* getAndAdd returns previous value and adds given value |
109 |
*/ |
110 |
public void testGetAndAdd(){ |
111 |
AtomicLong ai = new AtomicLong(1); |
112 |
assertEquals(1,ai.getAndAdd(2)); |
113 |
assertEquals(3,ai.get()); |
114 |
assertEquals(3,ai.getAndAdd(-4)); |
115 |
assertEquals(-1,ai.get()); |
116 |
} |
117 |
|
118 |
/** |
119 |
* getAndDecrement returns previous value and decrements |
120 |
*/ |
121 |
public void testGetAndDecrement(){ |
122 |
AtomicLong ai = new AtomicLong(1); |
123 |
assertEquals(1,ai.getAndDecrement()); |
124 |
assertEquals(0,ai.getAndDecrement()); |
125 |
assertEquals(-1,ai.getAndDecrement()); |
126 |
} |
127 |
|
128 |
/** |
129 |
* getAndIncrement returns previous value and increments |
130 |
*/ |
131 |
public void testGetAndIncrement(){ |
132 |
AtomicLong ai = new AtomicLong(1); |
133 |
assertEquals(1,ai.getAndIncrement()); |
134 |
assertEquals(2,ai.get()); |
135 |
ai.set(-2); |
136 |
assertEquals(-2,ai.getAndIncrement()); |
137 |
assertEquals(-1,ai.getAndIncrement()); |
138 |
assertEquals(0,ai.getAndIncrement()); |
139 |
assertEquals(1,ai.get()); |
140 |
} |
141 |
|
142 |
/** |
143 |
* addAndGet adds given value to current, and returns current value |
144 |
*/ |
145 |
public void testAddAndGet(){ |
146 |
AtomicLong ai = new AtomicLong(1); |
147 |
assertEquals(3,ai.addAndGet(2)); |
148 |
assertEquals(3,ai.get()); |
149 |
assertEquals(-1,ai.addAndGet(-4)); |
150 |
assertEquals(-1,ai.get()); |
151 |
} |
152 |
|
153 |
/** |
154 |
* decrementAndGet decrements and returns current value |
155 |
*/ |
156 |
public void testDecrementAndGet(){ |
157 |
AtomicLong ai = new AtomicLong(1); |
158 |
assertEquals(0,ai.decrementAndGet()); |
159 |
assertEquals(-1,ai.decrementAndGet()); |
160 |
assertEquals(-2,ai.decrementAndGet()); |
161 |
assertEquals(-2,ai.get()); |
162 |
} |
163 |
|
164 |
/** |
165 |
* incrementAndGet increments and returns current value |
166 |
*/ |
167 |
public void testIncrementAndGet(){ |
168 |
AtomicLong ai = new AtomicLong(1); |
169 |
assertEquals(2,ai.incrementAndGet()); |
170 |
assertEquals(2,ai.get()); |
171 |
ai.set(-2); |
172 |
assertEquals(-1,ai.incrementAndGet()); |
173 |
assertEquals(0,ai.incrementAndGet()); |
174 |
assertEquals(1,ai.incrementAndGet()); |
175 |
assertEquals(1,ai.get()); |
176 |
} |
177 |
|
178 |
/** |
179 |
* a deserialized serialized atomic holds same value |
180 |
*/ |
181 |
public void testSerialization() { |
182 |
AtomicLong l = new AtomicLong(); |
183 |
|
184 |
try { |
185 |
l.set(-22); |
186 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
187 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
188 |
out.writeObject(l); |
189 |
out.close(); |
190 |
|
191 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
192 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
193 |
AtomicLong r = (AtomicLong) in.readObject(); |
194 |
assertEquals(l.get(), r.get()); |
195 |
} catch(Exception e){ |
196 |
unexpectedException(); |
197 |
} |
198 |
} |
199 |
|
200 |
} |