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.11 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.15 by jsr166, Tue Nov 17 06:58:50 2009 UTC

# 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);
# Line 50 | Line 50 | public class AtomicLongTest extends JSR1
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);
# Line 63 | Line 63 | public class AtomicLongTest extends JSR1
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();
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 <        }
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(){
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 122 | 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 133 | 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 143 | 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 157 | 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 168 | 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 179 | 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 193 | 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();
209 <            assertEquals(l.get(), r.get());
210 <        } catch(Exception e){
211 <            unexpectedException();
212 <        }
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      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines