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

Comparing jsr166/src/test/tck/AtomicLongTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.14 by jsr166, Tue Nov 17 03:12:51 2009 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/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.concurrent.atomic.*;
11 + import java.io.*;
12  
13 < public class AtomicLongTest extends TestCase {
13 > public class AtomicLongTest extends JSR166TestCase {
14      public static void main (String[] args) {
15          junit.textui.TestRunner.run (suite());
16      }
# Line 16 | Line 18 | public class AtomicLongTest extends Test
18          return new TestSuite(AtomicLongTest.class);
19      }
20  
21 <    public void testConstructor(){
21 >    /**
22 >     * constructor initializes to given value
23 >     */
24 >    public void testConstructor() {
25          AtomicLong ai = new AtomicLong(1);
26          assertEquals(1,ai.get());
27      }
28  
29 <    public void testConstructor2(){
29 >    /**
30 >     * default constructed initializes to zero
31 >     */
32 >    public void testConstructor2() {
33          AtomicLong ai = new AtomicLong();
34          assertEquals(0,ai.get());
35      }
36  
37 <    public void testGetSet(){
37 >    /**
38 >     * get returns the last value set
39 >     */
40 >    public void testGetSet() {
41          AtomicLong ai = new AtomicLong(1);
42          assertEquals(1,ai.get());
43          ai.set(2);
44          assertEquals(2,ai.get());
45          ai.set(-3);
46          assertEquals(-3,ai.get());
47 <        
47 >
48      }
49 <    public void testCompareAndSet(){
49 >
50 >    /**
51 >     * get returns the last value lazySet in same thread
52 >     */
53 >    public void testGetLazySet() {
54 >        AtomicLong ai = new AtomicLong(1);
55 >        assertEquals(1,ai.get());
56 >        ai.lazySet(2);
57 >        assertEquals(2,ai.get());
58 >        ai.lazySet(-3);
59 >        assertEquals(-3,ai.get());
60 >
61 >    }
62 >
63 >    /**
64 >     * compareAndSet succeeds in changing value if equal to expected else fails
65 >     */
66 >    public void testCompareAndSet() {
67          AtomicLong ai = new AtomicLong(1);
68          assertTrue(ai.compareAndSet(1,2));
69          assertTrue(ai.compareAndSet(2,-4));
# Line 46 | Line 74 | public class AtomicLongTest extends Test
74          assertEquals(7,ai.get());
75      }
76  
77 <    public void testWeakCompareAndSet(){
77 >    /**
78 >     * compareAndSet in one thread enables another waiting for value
79 >     * to succeed
80 >     */
81 >    public void testCompareAndSetInMultipleThreads() throws Exception {
82 >        final AtomicLong ai = new AtomicLong(1);
83 >        Thread t = new Thread(new Runnable() {
84 >                public void run() {
85 >                    while (!ai.compareAndSet(2, 3)) Thread.yield();
86 >                }});
87 >
88 >        t.start();
89 >        assertTrue(ai.compareAndSet(1, 2));
90 >        t.join(LONG_DELAY_MS);
91 >        assertFalse(t.isAlive());
92 >        assertEquals(ai.get(), 3);
93 >    }
94 >
95 >    /**
96 >     * repeated weakCompareAndSet succeeds in changing value when equal
97 >     * to expected
98 >     */
99 >    public void testWeakCompareAndSet() {
100          AtomicLong ai = new AtomicLong(1);
101 <        while(!ai.weakCompareAndSet(1,2));
102 <        while(!ai.weakCompareAndSet(2,-4));
101 >        while (!ai.weakCompareAndSet(1,2));
102 >        while (!ai.weakCompareAndSet(2,-4));
103          assertEquals(-4,ai.get());
104 <        while(!ai.weakCompareAndSet(-4,7));
104 >        while (!ai.weakCompareAndSet(-4,7));
105          assertEquals(7,ai.get());
106      }
107  
108 <    public void testGetAndSet(){
108 >    /**
109 >     * getAndSet returns previous value and sets to given value
110 >     */
111 >    public void testGetAndSet() {
112          AtomicLong ai = new AtomicLong(1);
113          assertEquals(1,ai.getAndSet(0));
114          assertEquals(0,ai.getAndSet(-10));
115          assertEquals(-10,ai.getAndSet(1));
116      }
117  
118 <    public void testGetAndAdd(){
118 >    /**
119 >     * getAndAdd returns previous value and adds given value
120 >     */
121 >    public void testGetAndAdd() {
122          AtomicLong ai = new AtomicLong(1);
123          assertEquals(1,ai.getAndAdd(2));
124          assertEquals(3,ai.get());
# Line 70 | Line 126 | public class AtomicLongTest extends Test
126          assertEquals(-1,ai.get());
127      }
128  
129 <    public void testGetAndDecrement(){
129 >    /**
130 >     * getAndDecrement returns previous value and decrements
131 >     */
132 >    public void testGetAndDecrement() {
133          AtomicLong ai = new AtomicLong(1);
134          assertEquals(1,ai.getAndDecrement());
135          assertEquals(0,ai.getAndDecrement());
136          assertEquals(-1,ai.getAndDecrement());
137      }
138  
139 <    public void testGetAndIncrement(){
139 >    /**
140 >     * getAndIncrement returns previous value and increments
141 >     */
142 >    public void testGetAndIncrement() {
143          AtomicLong ai = new AtomicLong(1);
144          assertEquals(1,ai.getAndIncrement());
145          assertEquals(2,ai.get());
# Line 88 | Line 150 | public class AtomicLongTest extends Test
150          assertEquals(1,ai.get());
151      }
152  
153 <    public void testAddAndGet(){
153 >    /**
154 >     * addAndGet adds given value to current, and returns current value
155 >     */
156 >    public void testAddAndGet() {
157          AtomicLong ai = new AtomicLong(1);
158          assertEquals(3,ai.addAndGet(2));
159          assertEquals(3,ai.get());
# Line 96 | Line 161 | public class AtomicLongTest extends Test
161          assertEquals(-1,ai.get());
162      }
163  
164 <    public void testDecrementAndGet(){
164 >    /**
165 >     * decrementAndGet decrements and returns current value
166 >     */
167 >    public void testDecrementAndGet() {
168          AtomicLong ai = new AtomicLong(1);
169          assertEquals(0,ai.decrementAndGet());
170          assertEquals(-1,ai.decrementAndGet());
# Line 104 | Line 172 | public class AtomicLongTest extends Test
172          assertEquals(-2,ai.get());
173      }
174  
175 <    public void testIncrementAndGet(){
175 >    /**
176 >     * incrementAndGet increments and returns current value
177 >     */
178 >    public void testIncrementAndGet() {
179          AtomicLong ai = new AtomicLong(1);
180          assertEquals(2,ai.incrementAndGet());
181          assertEquals(2,ai.get());
# Line 115 | Line 186 | public class AtomicLongTest extends Test
186          assertEquals(1,ai.get());
187      }
188  
189 +    /**
190 +     * a deserialized serialized atomic holds same value
191 +     */
192 +    public void testSerialization() throws Exception {
193 +        AtomicLong l = new AtomicLong();
194 +
195 +        l.set(-22);
196 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
197 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
198 +        out.writeObject(l);
199 +        out.close();
200 +
201 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
202 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
203 +        AtomicLong r = (AtomicLong) in.readObject();
204 +        assertEquals(l.get(), r.get());
205 +    }
206 +
207 +    /**
208 +     * toString returns current value.
209 +     */
210 +    public void testToString() {
211 +        AtomicLong ai = new AtomicLong();
212 +        for (long i = -12; i < 6; ++i) {
213 +            ai.set(i);
214 +            assertEquals(ai.toString(), Long.toString(i));
215 +        }
216 +    }
217 +
218 +    /**
219 +     * longValue returns current value.
220 +     */
221 +    public void testLongValue() {
222 +        AtomicLong ai = new AtomicLong();
223 +        for (int i = -12; i < 6; ++i) {
224 +            ai.set(i);
225 +            assertEquals((long)i, ai.longValue());
226 +        }
227 +    }
228 +
229 +    /**
230 +     * floatValue returns current value.
231 +     */
232 +    public void testFloatValue() {
233 +        AtomicLong ai = new AtomicLong();
234 +        for (int i = -12; i < 6; ++i) {
235 +            ai.set(i);
236 +            assertEquals((float)i, ai.floatValue());
237 +        }
238 +    }
239 +
240 +    /**
241 +     * doubleValue returns current value.
242 +     */
243 +    public void testDoubleValue() {
244 +        AtomicLong ai = new AtomicLong();
245 +        for (int i = -12; i < 6; ++i) {
246 +            ai.set(i);
247 +            assertEquals((double)i, ai.doubleValue());
248 +        }
249 +    }
250 +
251  
252   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines