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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.29 by jsr166, Fri Aug 4 03:30:21 2017 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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/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.*;
12 <
13 < public class AtomicBooleanTest extends TestCase {
14 <    public static void main (String[] args) {
15 <        junit.textui.TestRunner.run (suite());
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 >        main(suite(), args);
17      }
18      public static Test suite() {
19          return new TestSuite(AtomicBooleanTest.class);
20      }
21  
22 <    public void testConstructor(){
23 <        AtomicBoolean ai = new AtomicBoolean(true);
24 <        assertEquals(true,ai.get());
22 >    /**
23 >     * constructor initializes to given value
24 >     */
25 >    public void testConstructor() {
26 >        assertTrue(new AtomicBoolean(true).get());
27 >        assertFalse(new AtomicBoolean(false).get());
28      }
29  
30 <    public void testConstructor2(){
30 >    /**
31 >     * default constructed initializes to false
32 >     */
33 >    public void testConstructor2() {
34          AtomicBoolean ai = new AtomicBoolean();
35 <        assertEquals(false,ai.get());
35 >        assertFalse(ai.get());
36      }
37  
38 <    public void testGetSet(){
39 <        AtomicBoolean ai = new AtomicBoolean(true);
40 <        assertEquals(true,ai.get());
41 <        ai.set(false);
34 <        assertEquals(false,ai.get());
35 <        ai.set(true);
36 <        assertEquals(true,ai.get());
37 <        
38 <    }
39 <    public void testCompareAndSet(){
38 >    /**
39 >     * get returns the last value set
40 >     */
41 >    public void testGetSet() {
42          AtomicBoolean ai = new AtomicBoolean(true);
43 <        assertTrue(ai.compareAndSet(true,false));
44 <        assertEquals(false,ai.get());
45 <        assertTrue(ai.compareAndSet(false,false));
46 <        assertEquals(false,ai.get());
47 <        assertFalse(ai.compareAndSet(true,false));
46 <        assertFalse((ai.get()));
47 <        assertTrue(ai.compareAndSet(false,true));
48 <        assertEquals(true,ai.get());
43 >        assertTrue(ai.get());
44 >        ai.set(false);
45 >        assertFalse(ai.get());
46 >        ai.set(true);
47 >        assertTrue(ai.get());
48      }
49  
50 <    public void testWeakCompareAndSet(){
50 >    /**
51 >     * get returns the last value lazySet in same thread
52 >     */
53 >    public void testGetLazySet() {
54          AtomicBoolean ai = new AtomicBoolean(true);
55 <        while(!ai.weakCompareAndSet(true,false));
56 <        assertEquals(false,ai.get());
57 <        while(!ai.weakCompareAndSet(false,false));
58 <        assertEquals(false,ai.get());
59 <        while(!ai.weakCompareAndSet(false,true));
58 <        assertEquals(true,ai.get());
55 >        assertTrue(ai.get());
56 >        ai.lazySet(false);
57 >        assertFalse(ai.get());
58 >        ai.lazySet(true);
59 >        assertTrue(ai.get());
60      }
61  
62 <    public void testGetAndSet(){
62 >    /**
63 >     * compareAndSet succeeds in changing value if equal to expected else fails
64 >     */
65 >    public void testCompareAndSet() {
66          AtomicBoolean ai = new AtomicBoolean(true);
67 <        assertEquals(true,ai.getAndSet(false));
68 <        assertEquals(false,ai.getAndSet(false));
69 <        assertEquals(false,ai.getAndSet(true));
70 <        assertEquals(true,ai.get());
71 <    }
72 <
73 <    public void testSerialization() {
74 <        AtomicBoolean l = new AtomicBoolean();
75 <
76 <        try {
77 <            l.set(true);
78 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
79 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
80 <            out.writeObject(l);
81 <            out.close();
82 <
83 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
84 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
85 <            AtomicBoolean r = (AtomicBoolean) in.readObject();
86 <            assertEquals(l.get(), r.get());
87 <        } catch(Exception e){
88 <            e.printStackTrace();
89 <            fail("unexpected exception");
90 <        }
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() throws Exception {
82 >        final AtomicBoolean ai = new AtomicBoolean(true);
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
97 >     */
98 >    public void testWeakCompareAndSet() {
99 >        AtomicBoolean ai = new AtomicBoolean(true);
100 >        do {} while (!ai.weakCompareAndSet(true, false));
101 >        assertFalse(ai.get());
102 >        do {} while (!ai.weakCompareAndSet(false, false));
103 >        assertFalse(ai.get());
104 >        do {} while (!ai.weakCompareAndSet(false, true));
105 >        assertTrue(ai.get());
106      }
107  
108 +    /**
109 +     * getAndSet returns previous value and sets to given value
110 +     */
111 +    public void testGetAndSet() {
112 +        AtomicBoolean ai = new AtomicBoolean();
113 +        boolean[] booleans = { false, true };
114 +        for (boolean before : booleans)
115 +            for (boolean after : booleans) {
116 +                ai.set(before);
117 +                assertEquals(before, ai.getAndSet(after));
118 +                assertEquals(after, ai.get());
119 +            }
120 +    }
121 +
122 +    /**
123 +     * a deserialized/reserialized atomic holds same value
124 +     */
125 +    public void testSerialization() throws Exception {
126 +        AtomicBoolean x = new AtomicBoolean();
127 +        AtomicBoolean y = serialClone(x);
128 +        x.set(true);
129 +        AtomicBoolean z = serialClone(x);
130 +        assertTrue(x.get());
131 +        assertFalse(y.get());
132 +        assertTrue(z.get());
133 +    }
134 +
135 +    /**
136 +     * toString returns current value.
137 +     */
138 +    public void testToString() {
139 +        AtomicBoolean ai = new AtomicBoolean();
140 +        assertEquals(Boolean.toString(false), ai.toString());
141 +        ai.set(true);
142 +        assertEquals(Boolean.toString(true), ai.toString());
143 +    }
144  
145   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines