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.1 by dl, Sun Aug 31 19:24:52 2003 UTC vs.
Revision 1.8 by dl, Fri Jan 9 20:07:36 2004 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/licenses/publicdomain
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 {
13 > public class AtomicBooleanTest extends JSR166TestCase {
14      public static void main (String[] args) {
15          junit.textui.TestRunner.run (suite());
16      }
# Line 16 | Line 18 | public class AtomicBooleanTest extends T
18          return new TestSuite(AtomicBooleanTest.class);
19      }
20  
21 <    public void testConstructor(){
21 >    /**
22 >     * constructor initializes to given value
23 >     */
24 >    public void testConstructor() {
25          AtomicBoolean ai = new AtomicBoolean(true);
26          assertEquals(true,ai.get());
27      }
28  
29 <    public void testConstructor2(){
29 >    /**
30 >     * default constructed initializes to false
31 >     */
32 >    public void testConstructor2() {
33          AtomicBoolean ai = new AtomicBoolean();
34          assertEquals(false,ai.get());
35      }
36  
37 <    public void testGetSet(){
37 >    /**
38 >     * get returns the last value set
39 >     */
40 >    public void testGetSet() {
41          AtomicBoolean ai = new AtomicBoolean(true);
42          assertEquals(true,ai.get());
43          ai.set(false);
# Line 35 | Line 46 | public class AtomicBooleanTest extends T
46          assertEquals(true,ai.get());
47          
48      }
49 <    public void testCompareAndSet(){
49 >
50 >    /**
51 >     * compareAndSet succeeds in changing value if equal to expected else fails
52 >     */
53 >    public void testCompareAndSet() {
54          AtomicBoolean ai = new AtomicBoolean(true);
55          assertTrue(ai.compareAndSet(true,false));
56          assertEquals(false,ai.get());
# Line 47 | Line 62 | public class AtomicBooleanTest extends T
62          assertEquals(true,ai.get());
63      }
64  
65 <    public void testWeakCompareAndSet(){
65 >    /**
66 >     * compareAndSet in one thread enables another waiting for value
67 >     * to succeed
68 >     */
69 >    public void testCompareAndSetInMultipleThreads() {
70 >        final AtomicBoolean ai = new AtomicBoolean(true);
71 >        Thread t = new Thread(new Runnable() {
72 >                public void run() {
73 >                    while(!ai.compareAndSet(false, true)) Thread.yield();
74 >                }});
75 >        try {
76 >            t.start();
77 >            assertTrue(ai.compareAndSet(true, false));
78 >            t.join(LONG_DELAY_MS);
79 >            assertFalse(t.isAlive());
80 >        }
81 >        catch(Exception e) {
82 >            unexpectedException();
83 >        }
84 >    }
85 >
86 >    /**
87 >     * repeated weakCompareAndSet succeeds in changing value when equal
88 >     * to expected
89 >     */
90 >    public void testWeakCompareAndSet() {
91          AtomicBoolean ai = new AtomicBoolean(true);
92          while(!ai.weakCompareAndSet(true,false));
93          assertEquals(false,ai.get());
# Line 57 | Line 97 | public class AtomicBooleanTest extends T
97          assertEquals(true,ai.get());
98      }
99  
100 <    public void testGetAndSet(){
100 >    /**
101 >     * getAndSet returns previous value and sets to given value
102 >     */
103 >    public void testGetAndSet() {
104          AtomicBoolean ai = new AtomicBoolean(true);
105          assertEquals(true,ai.getAndSet(false));
106          assertEquals(false,ai.getAndSet(false));
# Line 65 | Line 108 | public class AtomicBooleanTest extends T
108          assertEquals(true,ai.get());
109      }
110  
111 +    /**
112 +     * a deserialized serialized atomic holds same value
113 +     */
114 +    public void testSerialization() {
115 +        AtomicBoolean l = new AtomicBoolean();
116 +
117 +        try {
118 +            l.set(true);
119 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
120 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
121 +            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 +        }
132 +    }
133 +
134 +    /**
135 +     * toString returns current value.
136 +     */
137 +    public void testToString() {
138 +        AtomicBoolean ai = new AtomicBoolean();
139 +        assertEquals(ai.toString(), Boolean.toString(false));
140 +        ai.set(true);
141 +        assertEquals(ai.toString(), Boolean.toString(true));
142 +    }
143  
144   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines