ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicIntegerArrayTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:52 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC

# Line 7 | Line 7
7  
8   import junit.framework.*;
9   import java.util.concurrent.atomic.*;
10 + import java.io.*;
11  
12 < public class AtomicIntegerArrayTest extends TestCase
12 < {
13 <    static final int N = 10;
12 > public class AtomicIntegerArrayTest extends JSR166TestCase {
13  
14      public static void main (String[] args) {
15          junit.textui.TestRunner.run (suite());
# Line 19 | Line 18 | public class AtomicIntegerArrayTest exte
18          return new TestSuite(AtomicIntegerArrayTest.class);
19      }
20  
21 <    public void testConstructor(){
22 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
23 <        for (int i = 0; i < N; ++i)
21 >
22 >    /**
23 >     *
24 >     */
25 >    public void testConstructor() {
26 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
27 >        for (int i = 0; i < SIZE; ++i)
28              assertEquals(0,ai.get(i));
29      }
30  
31 <    public void testGetSet(){
32 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
33 <        for (int i = 0; i < N; ++i) {
31 >    /**
32 >     *
33 >     */
34 >    public void testGetSet() {
35 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
36 >        for (int i = 0; i < SIZE; ++i) {
37              ai.set(i, 1);
38              assertEquals(1,ai.get(i));
39              ai.set(i, 2);
# Line 37 | Line 43 | public class AtomicIntegerArrayTest exte
43          }
44      }
45  
46 <    public void testCompareAndSet(){
47 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
48 <        for (int i = 0; i < N; ++i) {
46 >    /**
47 >     *
48 >     */
49 >    public void testCompareAndSet() {
50 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
51 >        for (int i = 0; i < SIZE; ++i) {
52              ai.set(i, 1);
53              assertTrue(ai.compareAndSet(i, 1,2));
54              assertTrue(ai.compareAndSet(i, 2,-4));
# Line 51 | Line 60 | public class AtomicIntegerArrayTest exte
60          }
61      }
62  
63 <    public void testWeakCompareAndSet(){
64 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
65 <        for (int i = 0; i < N; ++i) {
63 >    /**
64 >     *
65 >     */
66 >    public void testWeakCompareAndSet() {
67 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
68 >        for (int i = 0; i < SIZE; ++i) {
69              ai.set(i, 1);
70              while(!ai.weakCompareAndSet(i, 1,2));
71              while(!ai.weakCompareAndSet(i, 2,-4));
# Line 63 | Line 75 | public class AtomicIntegerArrayTest exte
75          }
76      }
77  
78 <    public void testGetAndSet(){
79 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
80 <        for (int i = 0; i < N; ++i) {
78 >    /**
79 >     *
80 >     */
81 >    public void testGetAndSet() {
82 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
83 >        for (int i = 0; i < SIZE; ++i) {
84              ai.set(i, 1);
85              assertEquals(1,ai.getAndSet(i,0));
86              assertEquals(0,ai.getAndSet(i,-10));
# Line 73 | Line 88 | public class AtomicIntegerArrayTest exte
88          }
89      }
90  
91 <    public void testGetAndAdd(){
92 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
93 <        for (int i = 0; i < N; ++i) {
91 >    /**
92 >     *
93 >     */
94 >    public void testGetAndAdd() {
95 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
96 >        for (int i = 0; i < SIZE; ++i) {
97              ai.set(i, 1);
98              assertEquals(1,ai.getAndAdd(i,2));
99              assertEquals(3,ai.get(i));
# Line 84 | Line 102 | public class AtomicIntegerArrayTest exte
102          }
103      }
104  
105 <    public void testGetAndDecrement(){
106 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
107 <        for (int i = 0; i < N; ++i) {
105 >    /**
106 >     *
107 >     */
108 >    public void testGetAndDecrement() {
109 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
110 >        for (int i = 0; i < SIZE; ++i) {
111              ai.set(i, 1);
112              assertEquals(1,ai.getAndDecrement(i));
113              assertEquals(0,ai.getAndDecrement(i));
# Line 94 | Line 115 | public class AtomicIntegerArrayTest exte
115          }
116      }
117  
118 <    public void testGetAndIncrement(){
119 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
120 <        for (int i = 0; i < N; ++i) {
118 >    /**
119 >     *
120 >     */
121 >    public void testGetAndIncrement() {
122 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
123 >        for (int i = 0; i < SIZE; ++i) {
124              ai.set(i, 1);
125              assertEquals(1,ai.getAndIncrement(i));
126              assertEquals(2,ai.get(i));
# Line 108 | Line 132 | public class AtomicIntegerArrayTest exte
132          }
133      }
134  
135 +    /**
136 +     *
137 +     */
138      public void testAddAndGet() {
139 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
140 <        for (int i = 0; i < N; ++i) {
139 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
140 >        for (int i = 0; i < SIZE; ++i) {
141              ai.set(i, 1);
142              assertEquals(3,ai.addAndGet(i,2));
143              assertEquals(3,ai.get(i));
# Line 119 | Line 146 | public class AtomicIntegerArrayTest exte
146          }
147      }
148  
149 <    public void testDecrementAndGet(){
150 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
151 <        for (int i = 0; i < N; ++i) {
149 >    /**
150 >     *
151 >     */
152 >    public void testDecrementAndGet() {
153 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
154 >        for (int i = 0; i < SIZE; ++i) {
155              ai.set(i, 1);
156              assertEquals(0,ai.decrementAndGet(i));
157              assertEquals(-1,ai.decrementAndGet(i));
# Line 130 | Line 160 | public class AtomicIntegerArrayTest exte
160          }
161      }
162  
163 +    /**
164 +     *
165 +     */
166      public void testIncrementAndGet() {
167 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
168 <        for (int i = 0; i < N; ++i) {
167 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
168 >        for (int i = 0; i < SIZE; ++i) {
169              ai.set(i, 1);
170              assertEquals(2,ai.incrementAndGet(i));
171              assertEquals(2,ai.get(i));
# Line 144 | Line 177 | public class AtomicIntegerArrayTest exte
177          }
178      }
179  
180 +    static final int COUNTDOWN = 100000;
181 +    
182 +    class Counter implements Runnable {
183 +        final AtomicIntegerArray ai;
184 +        volatile int counts;
185 +        Counter(AtomicIntegerArray a) { ai = a; }
186 +        public void run() {
187 +            for (;;) {
188 +                boolean done = true;
189 +                for (int i = 0; i < ai.length(); ++i) {
190 +                    int v = ai.get(i);
191 +                    threadAssertTrue(v >= 0);
192 +                    if (v != 0) {
193 +                        done = false;
194 +                        if (ai.compareAndSet(i, v, v-1))
195 +                            ++counts;
196 +                    }
197 +                }
198 +                if (done)
199 +                    break;
200 +            }
201 +        }
202 +    }
203 +
204 +    /**
205 +     *
206 +     */
207 +    public void testCountingInMultipleThreads() {
208 +        try {
209 +            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
210 +            for (int i = 0; i < SIZE; ++i)
211 +                ai.set(i, COUNTDOWN);
212 +            Counter c1 = new Counter(ai);
213 +            Counter c2 = new Counter(ai);
214 +            Thread t1 = new Thread(c1);
215 +            Thread t2 = new Thread(c2);
216 +            t1.start();
217 +            t2.start();
218 +            t1.join();
219 +            t2.join();
220 +            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
221 +        }
222 +        catch(InterruptedException ie) {
223 +            unexpectedException();
224 +        }
225 +    }
226 +
227 +
228 +    /**
229 +     *
230 +     */
231 +    public void testSerialization() {
232 +        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
233 +        for (int i = 0; i < SIZE; ++i)
234 +            l.set(i, -i);
235 +
236 +        try {
237 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
238 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
239 +            out.writeObject(l);
240 +            out.close();
241 +
242 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
243 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
244 +            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
245 +            for (int i = 0; i < SIZE; ++i) {
246 +                assertEquals(l.get(i), r.get(i));
247 +            }
248 +        } catch(Exception e){
249 +            e.printStackTrace();
250 +            unexpectedException();
251 +        }
252 +    }
253 +
254   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines