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.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.15 by jsr166, Tue Nov 17 06:58:50 2009 UTC

# Line 2 | Line 2
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.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 21 | Line 21 | public class AtomicLongTest extends JSR1
21      /**
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 <     * default constructed intializes to zero
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      }
# Line 37 | Line 37 | public class AtomicLongTest extends JSR1
37      /**
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       * 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 64 | Line 78 | public class AtomicLongTest extends JSR1
78       * compareAndSet in one thread enables another waiting for value
79       * to succeed
80       */
81 <    public void testCompareAndSetInMultipleThreads() {
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 <        try {
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 <        }
80 <        catch(Exception e) {
81 <            unexpectedException();
82 <        }
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
98 >     * to expected
99       */
100 <    public void testWeakCompareAndSet(){
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      /**
110       * getAndSet returns previous value and sets to given value
111       */
112 <    public void testGetAndSet(){
112 >    public void testGetAndSet() {
113          AtomicLong ai = new AtomicLong(1);
114          assertEquals(1,ai.getAndSet(0));
115          assertEquals(0,ai.getAndSet(-10));
# Line 108 | Line 119 | public class AtomicLongTest extends JSR1
119      /**
120       * getAndAdd returns previous value and adds given value
121       */
122 <    public void testGetAndAdd(){
122 >    public void testGetAndAdd() {
123          AtomicLong ai = new AtomicLong(1);
124          assertEquals(1,ai.getAndAdd(2));
125          assertEquals(3,ai.get());
# Line 119 | Line 130 | public class AtomicLongTest extends JSR1
130      /**
131       * getAndDecrement returns previous value and decrements
132       */
133 <    public void testGetAndDecrement(){
133 >    public void testGetAndDecrement() {
134          AtomicLong ai = new AtomicLong(1);
135          assertEquals(1,ai.getAndDecrement());
136          assertEquals(0,ai.getAndDecrement());
# Line 129 | Line 140 | public class AtomicLongTest extends JSR1
140      /**
141       * getAndIncrement returns previous value and increments
142       */
143 <    public void testGetAndIncrement(){
143 >    public void testGetAndIncrement() {
144          AtomicLong ai = new AtomicLong(1);
145          assertEquals(1,ai.getAndIncrement());
146          assertEquals(2,ai.get());
# Line 143 | Line 154 | public class AtomicLongTest extends JSR1
154      /**
155       * addAndGet adds given value to current, and returns current value
156       */
157 <    public void testAddAndGet(){
157 >    public void testAddAndGet() {
158          AtomicLong ai = new AtomicLong(1);
159          assertEquals(3,ai.addAndGet(2));
160          assertEquals(3,ai.get());
# Line 154 | Line 165 | public class AtomicLongTest extends JSR1
165      /**
166       * decrementAndGet decrements and returns current value
167       */
168 <    public void testDecrementAndGet(){
168 >    public void testDecrementAndGet() {
169          AtomicLong ai = new AtomicLong(1);
170          assertEquals(0,ai.decrementAndGet());
171          assertEquals(-1,ai.decrementAndGet());
# Line 165 | Line 176 | public class AtomicLongTest extends JSR1
176      /**
177       * incrementAndGet increments and returns current value
178       */
179 <    public void testIncrementAndGet(){
179 >    public void testIncrementAndGet() {
180          AtomicLong ai = new AtomicLong(1);
181          assertEquals(2,ai.incrementAndGet());
182          assertEquals(2,ai.get());
# Line 179 | Line 190 | public class AtomicLongTest extends JSR1
190      /**
191       * a deserialized serialized atomic holds same value
192       */
193 <    public void testSerialization() {
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 <            unexpectedException();
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