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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.15 by jsr166, Tue Nov 17 06:58:50 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.*;
# Line 17 | Line 18 | public class AtomicLongTest extends JSR1
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 >
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 <    public void testCompareAndSet(){
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 47 | Line 74 | public class AtomicLongTest extends JSR1
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 CheckedRunnable() {
84 >            public void realRun() {
85 >                while (!ai.compareAndSet(2, 3))
86 >                    Thread.yield();
87 >            }});
88 >
89 >        t.start();
90 >        assertTrue(ai.compareAndSet(1, 2));
91 >        t.join(LONG_DELAY_MS);
92 >        assertFalse(t.isAlive());
93 >        assertEquals(ai.get(), 3);
94 >    }
95 >
96 >    /**
97 >     * repeated weakCompareAndSet succeeds in changing value when equal
98 >     * to expected
99 >     */
100 >    public void testWeakCompareAndSet() {
101          AtomicLong ai = new AtomicLong(1);
102 <        while(!ai.weakCompareAndSet(1,2));
103 <        while(!ai.weakCompareAndSet(2,-4));
102 >        while (!ai.weakCompareAndSet(1,2));
103 >        while (!ai.weakCompareAndSet(2,-4));
104          assertEquals(-4,ai.get());
105 <        while(!ai.weakCompareAndSet(-4,7));
105 >        while (!ai.weakCompareAndSet(-4,7));
106          assertEquals(7,ai.get());
107      }
108  
109 <    public void testGetAndSet(){
109 >    /**
110 >     * getAndSet returns previous value and sets to given value
111 >     */
112 >    public void testGetAndSet() {
113          AtomicLong ai = new AtomicLong(1);
114          assertEquals(1,ai.getAndSet(0));
115          assertEquals(0,ai.getAndSet(-10));
116          assertEquals(-10,ai.getAndSet(1));
117      }
118  
119 <    public void testGetAndAdd(){
119 >    /**
120 >     * getAndAdd returns previous value and adds given value
121 >     */
122 >    public void testGetAndAdd() {
123          AtomicLong ai = new AtomicLong(1);
124          assertEquals(1,ai.getAndAdd(2));
125          assertEquals(3,ai.get());
# Line 71 | Line 127 | public class AtomicLongTest extends JSR1
127          assertEquals(-1,ai.get());
128      }
129  
130 <    public void testGetAndDecrement(){
130 >    /**
131 >     * getAndDecrement returns previous value and decrements
132 >     */
133 >    public void testGetAndDecrement() {
134          AtomicLong ai = new AtomicLong(1);
135          assertEquals(1,ai.getAndDecrement());
136          assertEquals(0,ai.getAndDecrement());
137          assertEquals(-1,ai.getAndDecrement());
138      }
139  
140 <    public void testGetAndIncrement(){
140 >    /**
141 >     * getAndIncrement returns previous value and increments
142 >     */
143 >    public void testGetAndIncrement() {
144          AtomicLong ai = new AtomicLong(1);
145          assertEquals(1,ai.getAndIncrement());
146          assertEquals(2,ai.get());
# Line 89 | Line 151 | public class AtomicLongTest extends JSR1
151          assertEquals(1,ai.get());
152      }
153  
154 <    public void testAddAndGet(){
154 >    /**
155 >     * addAndGet adds given value to current, and returns current value
156 >     */
157 >    public void testAddAndGet() {
158          AtomicLong ai = new AtomicLong(1);
159          assertEquals(3,ai.addAndGet(2));
160          assertEquals(3,ai.get());
# Line 97 | Line 162 | public class AtomicLongTest extends JSR1
162          assertEquals(-1,ai.get());
163      }
164  
165 <    public void testDecrementAndGet(){
165 >    /**
166 >     * decrementAndGet decrements and returns current value
167 >     */
168 >    public void testDecrementAndGet() {
169          AtomicLong ai = new AtomicLong(1);
170          assertEquals(0,ai.decrementAndGet());
171          assertEquals(-1,ai.decrementAndGet());
# Line 105 | Line 173 | public class AtomicLongTest extends JSR1
173          assertEquals(-2,ai.get());
174      }
175  
176 <    public void testIncrementAndGet(){
176 >    /**
177 >     * incrementAndGet increments and returns current value
178 >     */
179 >    public void testIncrementAndGet() {
180          AtomicLong ai = new AtomicLong(1);
181          assertEquals(2,ai.incrementAndGet());
182          assertEquals(2,ai.get());
# Line 116 | Line 187 | public class AtomicLongTest extends JSR1
187          assertEquals(1,ai.get());
188      }
189  
190 <    public void testSerialization() {
190 >    /**
191 >     * a deserialized serialized atomic holds same value
192 >     */
193 >    public void testSerialization() throws Exception {
194          AtomicLong l = new AtomicLong();
195  
196 <        try {
197 <            l.set(-22);
198 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
199 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
200 <            out.writeObject(l);
201 <            out.close();
202 <
203 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
204 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
205 <            AtomicLong r = (AtomicLong) in.readObject();
206 <            assertEquals(l.get(), r.get());
207 <        } catch(Exception e){
208 <            e.printStackTrace();
209 <            fail("unexpected exception");
196 >        l.set(-22);
197 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
198 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
199 >        out.writeObject(l);
200 >        out.close();
201 >
202 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
203 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
204 >        AtomicLong r = (AtomicLong) in.readObject();
205 >        assertEquals(l.get(), r.get());
206 >    }
207 >
208 >    /**
209 >     * toString returns current value.
210 >     */
211 >    public void testToString() {
212 >        AtomicLong ai = new AtomicLong();
213 >        for (long i = -12; i < 6; ++i) {
214 >            ai.set(i);
215 >            assertEquals(ai.toString(), Long.toString(i));
216          }
217      }
218  
219 +    /**
220 +     * longValue returns current value.
221 +     */
222 +    public void testLongValue() {
223 +        AtomicLong ai = new AtomicLong();
224 +        for (int i = -12; i < 6; ++i) {
225 +            ai.set(i);
226 +            assertEquals((long)i, ai.longValue());
227 +        }
228 +    }
229 +
230 +    /**
231 +     * floatValue returns current value.
232 +     */
233 +    public void testFloatValue() {
234 +        AtomicLong ai = new AtomicLong();
235 +        for (int i = -12; i < 6; ++i) {
236 +            ai.set(i);
237 +            assertEquals((float)i, ai.floatValue());
238 +        }
239 +    }
240 +
241 +    /**
242 +     * doubleValue returns current value.
243 +     */
244 +    public void testDoubleValue() {
245 +        AtomicLong ai = new AtomicLong();
246 +        for (int i = -12; i < 6; ++i) {
247 +            ai.set(i);
248 +            assertEquals((double)i, ai.doubleValue());
249 +        }
250 +    }
251 +
252 +
253   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines