ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicBooleanTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicBooleanTest.java (file contents):
Revision 1.8 by dl, Fri Jan 9 20:07:36 2004 UTC vs.
Revision 1.20 by jsr166, Fri Jun 10 20:01:21 2011 UTC

# Line 1 | Line 1
1   /*
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.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.concurrent.atomic.*;
11 < import java.io.*;
10 > import java.util.concurrent.atomic.AtomicBoolean;
11  
12   public class AtomicBooleanTest extends JSR166TestCase {
13 <    public static void main (String[] args) {
14 <        junit.textui.TestRunner.run (suite());
13 >    public static void main(String[] args) {
14 >        junit.textui.TestRunner.run(suite());
15      }
16      public static Test suite() {
17          return new TestSuite(AtomicBooleanTest.class);
# Line 22 | Line 21 | public class AtomicBooleanTest extends J
21       * constructor initializes to given value
22       */
23      public void testConstructor() {
24 <        AtomicBoolean ai = new AtomicBoolean(true);
25 <        assertEquals(true,ai.get());
24 >        assertTrue(new AtomicBoolean(true).get());
25 >        assertFalse(new AtomicBoolean(false).get());
26      }
27  
28      /**
# Line 31 | Line 30 | public class AtomicBooleanTest extends J
30       */
31      public void testConstructor2() {
32          AtomicBoolean ai = new AtomicBoolean();
33 <        assertEquals(false,ai.get());
33 >        assertFalse(ai.get());
34      }
35  
36      /**
# Line 39 | Line 38 | public class AtomicBooleanTest extends J
38       */
39      public void testGetSet() {
40          AtomicBoolean ai = new AtomicBoolean(true);
41 <        assertEquals(true,ai.get());
42 <        ai.set(false);
43 <        assertEquals(false,ai.get());
44 <        ai.set(true);
45 <        assertEquals(true,ai.get());
46 <        
41 >        assertTrue(ai.get());
42 >        ai.set(false);
43 >        assertFalse(ai.get());
44 >        ai.set(true);
45 >        assertTrue(ai.get());
46 >    }
47 >
48 >    /**
49 >     * get returns the last value lazySet in same thread
50 >     */
51 >    public void testGetLazySet() {
52 >        AtomicBoolean ai = new AtomicBoolean(true);
53 >        assertTrue(ai.get());
54 >        ai.lazySet(false);
55 >        assertFalse(ai.get());
56 >        ai.lazySet(true);
57 >        assertTrue(ai.get());
58      }
59  
60      /**
# Line 52 | Line 62 | public class AtomicBooleanTest extends J
62       */
63      public void testCompareAndSet() {
64          AtomicBoolean ai = new AtomicBoolean(true);
65 <        assertTrue(ai.compareAndSet(true,false));
66 <        assertEquals(false,ai.get());
67 <        assertTrue(ai.compareAndSet(false,false));
68 <        assertEquals(false,ai.get());
69 <        assertFalse(ai.compareAndSet(true,false));
70 <        assertFalse((ai.get()));
71 <        assertTrue(ai.compareAndSet(false,true));
72 <        assertEquals(true,ai.get());
65 >        assertTrue(ai.compareAndSet(true, false));
66 >        assertFalse(ai.get());
67 >        assertTrue(ai.compareAndSet(false, false));
68 >        assertFalse(ai.get());
69 >        assertFalse(ai.compareAndSet(true, false));
70 >        assertFalse(ai.get());
71 >        assertTrue(ai.compareAndSet(false, true));
72 >        assertTrue(ai.get());
73      }
74  
75      /**
76       * compareAndSet in one thread enables another waiting for value
77       * to succeed
78       */
79 <    public void testCompareAndSetInMultipleThreads() {
79 >    public void testCompareAndSetInMultipleThreads() throws Exception {
80          final AtomicBoolean ai = new AtomicBoolean(true);
81 <        Thread t = new Thread(new Runnable() {
82 <                public void run() {
83 <                    while(!ai.compareAndSet(false, true)) Thread.yield();
84 <                }});
85 <        try {
86 <            t.start();
87 <            assertTrue(ai.compareAndSet(true, false));
88 <            t.join(LONG_DELAY_MS);
89 <            assertFalse(t.isAlive());
80 <        }
81 <        catch(Exception e) {
82 <            unexpectedException();
83 <        }
81 >        Thread t = new Thread(new CheckedRunnable() {
82 >            public void realRun() {
83 >                while (!ai.compareAndSet(false, true)) Thread.yield();
84 >            }});
85 >
86 >        t.start();
87 >        assertTrue(ai.compareAndSet(true, false));
88 >        t.join(LONG_DELAY_MS);
89 >        assertFalse(t.isAlive());
90      }
91  
92      /**
93       * repeated weakCompareAndSet succeeds in changing value when equal
94 <     * to expected
94 >     * to expected
95       */
96      public void testWeakCompareAndSet() {
97          AtomicBoolean ai = new AtomicBoolean(true);
98 <        while(!ai.weakCompareAndSet(true,false));
99 <        assertEquals(false,ai.get());
100 <        while(!ai.weakCompareAndSet(false,false));
101 <        assertEquals(false,ai.get());
102 <        while(!ai.weakCompareAndSet(false,true));
103 <        assertEquals(true,ai.get());
98 >        while (!ai.weakCompareAndSet(true, false));
99 >        assertFalse(ai.get());
100 >        while (!ai.weakCompareAndSet(false, false));
101 >        assertFalse(ai.get());
102 >        while (!ai.weakCompareAndSet(false, true));
103 >        assertTrue(ai.get());
104      }
105  
106      /**
# Line 102 | Line 108 | public class AtomicBooleanTest extends J
108       */
109      public void testGetAndSet() {
110          AtomicBoolean ai = new AtomicBoolean(true);
111 <        assertEquals(true,ai.getAndSet(false));
112 <        assertEquals(false,ai.getAndSet(false));
113 <        assertEquals(false,ai.getAndSet(true));
114 <        assertEquals(true,ai.get());
111 >        assertEquals(true, ai.getAndSet(false));
112 >        assertEquals(false, ai.getAndSet(false));
113 >        assertEquals(false, ai.getAndSet(true));
114 >        assertTrue(ai.get());
115      }
116  
117      /**
118       * a deserialized serialized atomic holds same value
119       */
120 <    public void testSerialization() {
121 <        AtomicBoolean l = new AtomicBoolean();
122 <
123 <        try {
124 <            l.set(true);
125 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
126 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
127 <            out.writeObject(l);
122 <            out.close();
123 <
124 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
125 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
126 <            AtomicBoolean r = (AtomicBoolean) in.readObject();
127 <            assertEquals(l.get(), r.get());
128 <        } catch(Exception e){
129 <            e.printStackTrace();
130 <            unexpectedException();
131 <        }
120 >    public void testSerialization() throws Exception {
121 >        AtomicBoolean x = new AtomicBoolean();
122 >        AtomicBoolean y = serialClone(x);
123 >        x.set(true);
124 >        AtomicBoolean z = serialClone(x);
125 >        assertTrue(x.get());
126 >        assertFalse(y.get());
127 >        assertTrue(z.get());
128      }
129  
130      /**
131       * toString returns current value.
132 <     */
132 >     */
133      public void testToString() {
134 <        AtomicBoolean ai = new AtomicBoolean();
134 >        AtomicBoolean ai = new AtomicBoolean();
135          assertEquals(ai.toString(), Boolean.toString(false));
136          ai.set(true);
137          assertEquals(ai.toString(), Boolean.toString(true));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines