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.9 by dl, Wed May 25 14:27:37 2005 UTC vs.
Revision 1.30 by dl, Tue Jan 26 13:33:05 2021 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.*;
9 > import java.util.concurrent.atomic.AtomicBoolean;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicBooleanTest extends JSR166TestCase {
15 <    public static void main (String[] args) {
16 <        junit.textui.TestRunner.run (suite());
15 >    public static void main(String[] args) {
16 >        main(suite(), args);
17      }
18      public static Test suite() {
19          return new TestSuite(AtomicBooleanTest.class);
# Line 22 | Line 23 | public class AtomicBooleanTest extends J
23       * constructor initializes to given value
24       */
25      public void testConstructor() {
26 <        AtomicBoolean ai = new AtomicBoolean(true);
27 <        assertEquals(true,ai.get());
26 >        assertTrue(new AtomicBoolean(true).get());
27 >        assertFalse(new AtomicBoolean(false).get());
28      }
29  
30      /**
# Line 31 | Line 32 | public class AtomicBooleanTest extends J
32       */
33      public void testConstructor2() {
34          AtomicBoolean ai = new AtomicBoolean();
35 <        assertEquals(false,ai.get());
35 >        assertFalse(ai.get());
36      }
37  
38      /**
# Line 39 | Line 40 | public class AtomicBooleanTest extends J
40       */
41      public void testGetSet() {
42          AtomicBoolean ai = new AtomicBoolean(true);
43 <        assertEquals(true,ai.get());
44 <        ai.set(false);
45 <        assertEquals(false,ai.get());
46 <        ai.set(true);
47 <        assertEquals(true,ai.get());
47 <        
43 >        assertTrue(ai.get());
44 >        ai.set(false);
45 >        assertFalse(ai.get());
46 >        ai.set(true);
47 >        assertTrue(ai.get());
48      }
49  
50      /**
# Line 52 | Line 52 | public class AtomicBooleanTest extends J
52       */
53      public void testGetLazySet() {
54          AtomicBoolean ai = new AtomicBoolean(true);
55 <        assertEquals(true,ai.get());
56 <        ai.lazySet(false);
57 <        assertEquals(false,ai.get());
58 <        ai.lazySet(true);
59 <        assertEquals(true,ai.get());
60 <        
55 >        assertTrue(ai.get());
56 >        ai.lazySet(false);
57 >        assertFalse(ai.get());
58 >        ai.lazySet(true);
59 >        assertTrue(ai.get());
60      }
61  
62      /**
# Line 65 | Line 64 | public class AtomicBooleanTest extends J
64       */
65      public void testCompareAndSet() {
66          AtomicBoolean ai = new AtomicBoolean(true);
67 <        assertTrue(ai.compareAndSet(true,false));
68 <        assertEquals(false,ai.get());
69 <        assertTrue(ai.compareAndSet(false,false));
70 <        assertEquals(false,ai.get());
71 <        assertFalse(ai.compareAndSet(true,false));
72 <        assertFalse((ai.get()));
73 <        assertTrue(ai.compareAndSet(false,true));
74 <        assertEquals(true,ai.get());
67 >        assertTrue(ai.compareAndSet(true, false));
68 >        assertFalse(ai.get());
69 >        assertTrue(ai.compareAndSet(false, false));
70 >        assertFalse(ai.get());
71 >        assertFalse(ai.compareAndSet(true, false));
72 >        assertFalse(ai.get());
73 >        assertTrue(ai.compareAndSet(false, true));
74 >        assertTrue(ai.get());
75      }
76  
77      /**
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 AtomicBoolean ai = new AtomicBoolean(true);
83 <        Thread t = new Thread(new Runnable() {
84 <                public void run() {
85 <                    while(!ai.compareAndSet(false, true)) Thread.yield();
86 <                }});
87 <        try {
88 <            t.start();
89 <            assertTrue(ai.compareAndSet(true, false));
90 <            t.join(LONG_DELAY_MS);
91 <            assertFalse(t.isAlive());
93 <        }
94 <        catch(Exception e) {
95 <            unexpectedException();
96 <        }
83 >        Thread t = new Thread(new CheckedRunnable() {
84 >            public void realRun() {
85 >                while (!ai.compareAndSet(false, true)) Thread.yield();
86 >            }});
87 >
88 >        t.start();
89 >        assertTrue(ai.compareAndSet(true, false));
90 >        t.join(LONG_DELAY_MS);
91 >        assertFalse(t.isAlive());
92      }
93  
94      /**
95       * repeated weakCompareAndSet succeeds in changing value when equal
96 <     * to expected
96 >     * to expected
97       */
98 +    @SuppressWarnings("deprecation")
99      public void testWeakCompareAndSet() {
100          AtomicBoolean ai = new AtomicBoolean(true);
101 <        while(!ai.weakCompareAndSet(true,false));
102 <        assertEquals(false,ai.get());
103 <        while(!ai.weakCompareAndSet(false,false));
104 <        assertEquals(false,ai.get());
105 <        while(!ai.weakCompareAndSet(false,true));
106 <        assertEquals(true,ai.get());
101 >        do {} while (!ai.weakCompareAndSet(true, false));
102 >        assertFalse(ai.get());
103 >        do {} while (!ai.weakCompareAndSet(false, false));
104 >        assertFalse(ai.get());
105 >        do {} while (!ai.weakCompareAndSet(false, true));
106 >        assertTrue(ai.get());
107      }
108  
109      /**
110       * getAndSet returns previous value and sets to given value
111       */
112      public void testGetAndSet() {
113 <        AtomicBoolean ai = new AtomicBoolean(true);
114 <        assertEquals(true,ai.getAndSet(false));
115 <        assertEquals(false,ai.getAndSet(false));
116 <        assertEquals(false,ai.getAndSet(true));
117 <        assertEquals(true,ai.get());
113 >        AtomicBoolean ai = new AtomicBoolean();
114 >        boolean[] booleans = { false, true };
115 >        for (boolean before : booleans)
116 >            for (boolean after : booleans) {
117 >                ai.set(before);
118 >                assertEquals(before, ai.getAndSet(after));
119 >                assertEquals(after, ai.get());
120 >            }
121      }
122  
123      /**
124 <     * a deserialized serialized atomic holds same value
125 <     */
126 <    public void testSerialization() {
127 <        AtomicBoolean l = new AtomicBoolean();
128 <
129 <        try {
130 <            l.set(true);
131 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
132 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
133 <            out.writeObject(l);
135 <            out.close();
136 <
137 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
138 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
139 <            AtomicBoolean r = (AtomicBoolean) in.readObject();
140 <            assertEquals(l.get(), r.get());
141 <        } catch(Exception e){
142 <            e.printStackTrace();
143 <            unexpectedException();
144 <        }
124 >     * a deserialized/reserialized atomic holds same value
125 >     */
126 >    public void testSerialization() throws Exception {
127 >        AtomicBoolean x = new AtomicBoolean();
128 >        AtomicBoolean y = serialClone(x);
129 >        x.set(true);
130 >        AtomicBoolean z = serialClone(x);
131 >        assertTrue(x.get());
132 >        assertFalse(y.get());
133 >        assertTrue(z.get());
134      }
135  
136      /**
137       * toString returns current value.
138 <     */
138 >     */
139      public void testToString() {
140 <        AtomicBoolean ai = new AtomicBoolean();
141 <        assertEquals(ai.toString(), Boolean.toString(false));
140 >        AtomicBoolean ai = new AtomicBoolean();
141 >        assertEquals(Boolean.toString(false), ai.toString());
142          ai.set(true);
143 <        assertEquals(ai.toString(), Boolean.toString(true));
143 >        assertEquals(Boolean.toString(true), ai.toString());
144      }
145  
146   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines