7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicIntegerTest extends TestCase { |
12 |
> |
public class AtomicIntegerTest extends JSR166TestCase { |
13 |
|
public static void main (String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
|
return new TestSuite(AtomicIntegerTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
/** |
21 |
+ |
* |
22 |
+ |
*/ |
23 |
|
public void testConstructor(){ |
24 |
|
AtomicInteger ai = new AtomicInteger(1); |
25 |
|
assertEquals(1,ai.get()); |
26 |
|
} |
27 |
|
|
28 |
+ |
/** |
29 |
+ |
* |
30 |
+ |
*/ |
31 |
|
public void testConstructor2(){ |
32 |
|
AtomicInteger ai = new AtomicInteger(); |
33 |
|
assertEquals(0,ai.get()); |
34 |
|
} |
35 |
|
|
36 |
+ |
/** |
37 |
+ |
* |
38 |
+ |
*/ |
39 |
|
public void testGetSet(){ |
40 |
|
AtomicInteger ai = new AtomicInteger(1); |
41 |
|
assertEquals(1,ai.get()); |
45 |
|
assertEquals(-3,ai.get()); |
46 |
|
|
47 |
|
} |
48 |
+ |
/** |
49 |
+ |
* |
50 |
+ |
*/ |
51 |
|
public void testCompareAndSet(){ |
52 |
|
AtomicInteger ai = new AtomicInteger(1); |
53 |
|
assertTrue(ai.compareAndSet(1,2)); |
59 |
|
assertEquals(7,ai.get()); |
60 |
|
} |
61 |
|
|
62 |
+ |
/** |
63 |
+ |
* |
64 |
+ |
*/ |
65 |
|
public void testWeakCompareAndSet(){ |
66 |
|
AtomicInteger ai = new AtomicInteger(1); |
67 |
|
while(!ai.weakCompareAndSet(1,2)); |
71 |
|
assertEquals(7,ai.get()); |
72 |
|
} |
73 |
|
|
74 |
+ |
/** |
75 |
+ |
* |
76 |
+ |
*/ |
77 |
|
public void testGetAndSet(){ |
78 |
|
AtomicInteger ai = new AtomicInteger(1); |
79 |
|
assertEquals(1,ai.getAndSet(0)); |
81 |
|
assertEquals(-10,ai.getAndSet(1)); |
82 |
|
} |
83 |
|
|
84 |
+ |
/** |
85 |
+ |
* |
86 |
+ |
*/ |
87 |
|
public void testGetAndAdd(){ |
88 |
|
AtomicInteger ai = new AtomicInteger(1); |
89 |
|
assertEquals(1,ai.getAndAdd(2)); |
92 |
|
assertEquals(-1,ai.get()); |
93 |
|
} |
94 |
|
|
95 |
+ |
/** |
96 |
+ |
* |
97 |
+ |
*/ |
98 |
|
public void testGetAndDecrement(){ |
99 |
|
AtomicInteger ai = new AtomicInteger(1); |
100 |
|
assertEquals(1,ai.getAndDecrement()); |
102 |
|
assertEquals(-1,ai.getAndDecrement()); |
103 |
|
} |
104 |
|
|
105 |
+ |
/** |
106 |
+ |
* |
107 |
+ |
*/ |
108 |
|
public void testGetAndIncrement(){ |
109 |
|
AtomicInteger ai = new AtomicInteger(1); |
110 |
|
assertEquals(1,ai.getAndIncrement()); |
116 |
|
assertEquals(1,ai.get()); |
117 |
|
} |
118 |
|
|
119 |
+ |
/** |
120 |
+ |
* |
121 |
+ |
*/ |
122 |
|
public void testAddAndGet(){ |
123 |
|
AtomicInteger ai = new AtomicInteger(1); |
124 |
|
assertEquals(3,ai.addAndGet(2)); |
127 |
|
assertEquals(-1,ai.get()); |
128 |
|
} |
129 |
|
|
130 |
+ |
/** |
131 |
+ |
* |
132 |
+ |
*/ |
133 |
|
public void testDecrementAndGet(){ |
134 |
|
AtomicInteger ai = new AtomicInteger(1); |
135 |
|
assertEquals(0,ai.decrementAndGet()); |
138 |
|
assertEquals(-2,ai.get()); |
139 |
|
} |
140 |
|
|
141 |
+ |
/** |
142 |
+ |
* |
143 |
+ |
*/ |
144 |
|
public void testIncrementAndGet(){ |
145 |
|
AtomicInteger ai = new AtomicInteger(1); |
146 |
|
assertEquals(2,ai.incrementAndGet()); |
152 |
|
assertEquals(1,ai.get()); |
153 |
|
} |
154 |
|
|
155 |
+ |
/** |
156 |
+ |
* |
157 |
+ |
*/ |
158 |
+ |
public void testSerialization() { |
159 |
+ |
AtomicInteger l = new AtomicInteger(); |
160 |
+ |
|
161 |
+ |
try { |
162 |
+ |
l.set(22); |
163 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
164 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
165 |
+ |
out.writeObject(l); |
166 |
+ |
out.close(); |
167 |
+ |
|
168 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
169 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
170 |
+ |
AtomicInteger r = (AtomicInteger) in.readObject(); |
171 |
+ |
assertEquals(l.get(), r.get()); |
172 |
+ |
} catch(Exception e){ |
173 |
+ |
unexpectedException(); |
174 |
+ |
} |
175 |
+ |
} |
176 |
+ |
|
177 |
|
|
178 |
|
} |