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.4 by dl, Sat Sep 20 18:20:07 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.*;
# Line 18 | Line 19 | public class AtomicLongTest extends JSR1
19      }
20  
21      /**
22 <     *
22 >     * constructor initializes to given value
23       */
24 <    public void testConstructor(){
24 >    public void testConstructor() {
25          AtomicLong ai = new AtomicLong(1);
26          assertEquals(1,ai.get());
27      }
28  
29      /**
30 <     *
30 >     * default constructed initializes to zero
31       */
32 <    public void testConstructor2(){
32 >    public void testConstructor2() {
33          AtomicLong ai = new AtomicLong();
34          assertEquals(0,ai.get());
35      }
36  
37      /**
38 <     *
38 >     * get returns the last value set
39       */
40 <    public void testGetSet(){
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 +
63      /**
64 <     *
64 >     * compareAndSet succeeds in changing value if equal to expected else fails
65       */
66 <    public void testCompareAndSet(){
66 >    public void testCompareAndSet() {
67          AtomicLong ai = new AtomicLong(1);
68          assertTrue(ai.compareAndSet(1,2));
69          assertTrue(ai.compareAndSet(2,-4));
# Line 60 | Line 75 | public class AtomicLongTest extends JSR1
75      }
76  
77      /**
78 <     *
78 >     * compareAndSet in one thread enables another waiting for value
79 >     * to succeed
80       */
81 <    public void testWeakCompareAndSet(){
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      /**
109 <     *
109 >     * getAndSet returns previous value and sets to given value
110       */
111 <    public void testGetAndSet(){
111 >    public void testGetAndSet() {
112          AtomicLong ai = new AtomicLong(1);
113          assertEquals(1,ai.getAndSet(0));
114          assertEquals(0,ai.getAndSet(-10));
# Line 82 | Line 116 | public class AtomicLongTest extends JSR1
116      }
117  
118      /**
119 <     *
119 >     * getAndAdd returns previous value and adds given value
120       */
121 <    public void testGetAndAdd(){
121 >    public void testGetAndAdd() {
122          AtomicLong ai = new AtomicLong(1);
123          assertEquals(1,ai.getAndAdd(2));
124          assertEquals(3,ai.get());
# Line 93 | Line 127 | public class AtomicLongTest extends JSR1
127      }
128  
129      /**
130 <     *
130 >     * getAndDecrement returns previous value and decrements
131       */
132 <    public void testGetAndDecrement(){
132 >    public void testGetAndDecrement() {
133          AtomicLong ai = new AtomicLong(1);
134          assertEquals(1,ai.getAndDecrement());
135          assertEquals(0,ai.getAndDecrement());
# Line 103 | Line 137 | public class AtomicLongTest extends JSR1
137      }
138  
139      /**
140 <     *
140 >     * getAndIncrement returns previous value and increments
141       */
142 <    public void testGetAndIncrement(){
142 >    public void testGetAndIncrement() {
143          AtomicLong ai = new AtomicLong(1);
144          assertEquals(1,ai.getAndIncrement());
145          assertEquals(2,ai.get());
# Line 117 | Line 151 | public class AtomicLongTest extends JSR1
151      }
152  
153      /**
154 <     *
154 >     * addAndGet adds given value to current, and returns current value
155       */
156 <    public void testAddAndGet(){
156 >    public void testAddAndGet() {
157          AtomicLong ai = new AtomicLong(1);
158          assertEquals(3,ai.addAndGet(2));
159          assertEquals(3,ai.get());
# Line 128 | Line 162 | public class AtomicLongTest extends JSR1
162      }
163  
164      /**
165 <     *
165 >     * decrementAndGet decrements and returns current value
166       */
167 <    public void testDecrementAndGet(){
167 >    public void testDecrementAndGet() {
168          AtomicLong ai = new AtomicLong(1);
169          assertEquals(0,ai.decrementAndGet());
170          assertEquals(-1,ai.decrementAndGet());
# Line 139 | Line 173 | public class AtomicLongTest extends JSR1
173      }
174  
175      /**
176 <     *
176 >     * incrementAndGet increments and returns current value
177       */
178 <    public void testIncrementAndGet(){
178 >    public void testIncrementAndGet() {
179          AtomicLong ai = new AtomicLong(1);
180          assertEquals(2,ai.incrementAndGet());
181          assertEquals(2,ai.get());
# Line 153 | Line 187 | public class AtomicLongTest extends JSR1
187      }
188  
189      /**
190 <     *
190 >     * a deserialized serialized atomic holds same value
191       */
192 <    public void testSerialization() {
192 >    public void testSerialization() throws Exception {
193          AtomicLong l = new AtomicLong();
194  
195 <        try {
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 <        } catch(Exception e){
207 <            unexpectedException();
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