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.10 by dl, Wed May 25 14:27:37 2005 UTC vs.
Revision 1.14 by jsr166, Tue Nov 17 03:12:51 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      }
# Line 29 | Line 29 | public class AtomicLongTest extends JSR1
29      /**
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(){
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 <        
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 78 | 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();
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 <        }
94 <        catch(Exception e) {
95 <            unexpectedException();
96 <        }
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
97 >     * to expected
98       */
99 <    public void testWeakCompareAndSet(){
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       * 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 122 | Line 118 | public class AtomicLongTest extends JSR1
118      /**
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 133 | Line 129 | public class AtomicLongTest extends JSR1
129      /**
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 143 | Line 139 | public class AtomicLongTest extends JSR1
139      /**
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 157 | Line 153 | public class AtomicLongTest extends JSR1
153      /**
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 168 | Line 164 | public class AtomicLongTest extends JSR1
164      /**
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 179 | Line 175 | public class AtomicLongTest extends JSR1
175      /**
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 193 | Line 189 | public class AtomicLongTest extends JSR1
189      /**
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();
209 <            assertEquals(l.get(), r.get());
210 <        } catch(Exception e){
211 <            unexpectedException();
212 <        }
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 <     */
209 >     */
210      public void testToString() {
211          AtomicLong ai = new AtomicLong();
212          for (long i = -12; i < 6; ++i) {
# Line 225 | Line 217 | public class AtomicLongTest extends JSR1
217  
218      /**
219       * longValue returns current value.
220 <     */
220 >     */
221      public void testLongValue() {
222          AtomicLong ai = new AtomicLong();
223          for (int i = -12; i < 6; ++i) {
# Line 236 | Line 228 | public class AtomicLongTest extends JSR1
228  
229      /**
230       * floatValue returns current value.
231 <     */
231 >     */
232      public void testFloatValue() {
233          AtomicLong ai = new AtomicLong();
234          for (int i = -12; i < 6; ++i) {
# Line 247 | Line 239 | public class AtomicLongTest extends JSR1
239  
240      /**
241       * doubleValue returns current value.
242 <     */
242 >     */
243      public void testDoubleValue() {
244          AtomicLong ai = new AtomicLong();
245          for (int i = -12; i < 6; ++i) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines