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.4 by dl, Sat Sep 20 18:20:07 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.*;
# Line 18 | Line 19 | public class AtomicBooleanTest extends J
19      }
20  
21      /**
22 <     *
22 >     * constructor initializes to given value
23       */
24      public void testConstructor() {
25          AtomicBoolean ai = new AtomicBoolean(true);
# Line 26 | Line 27 | public class AtomicBooleanTest extends J
27      }
28  
29      /**
30 <     *
30 >     * default constructed initializes to false
31       */
32      public void testConstructor2() {
33          AtomicBoolean ai = new AtomicBoolean();
# Line 34 | Line 35 | public class AtomicBooleanTest extends J
35      }
36  
37      /**
38 <     *
38 >     * get returns the last value set
39       */
40      public void testGetSet() {
41          AtomicBoolean ai = new AtomicBoolean(true);
# Line 45 | Line 46 | public class AtomicBooleanTest extends J
46          assertEquals(true,ai.get());
47          
48      }
49 +
50      /**
51 <     *
51 >     * compareAndSet succeeds in changing value if equal to expected else fails
52       */
53      public void testCompareAndSet() {
54          AtomicBoolean ai = new AtomicBoolean(true);
# Line 61 | Line 63 | public class AtomicBooleanTest extends J
63      }
64  
65      /**
66 <     *
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);
# Line 74 | Line 98 | public class AtomicBooleanTest extends J
98      }
99  
100      /**
101 <     *
101 >     * getAndSet returns previous value and sets to given value
102       */
103      public void testGetAndSet() {
104          AtomicBoolean ai = new AtomicBoolean(true);
# Line 85 | Line 109 | public class AtomicBooleanTest extends J
109      }
110  
111      /**
112 <     *
112 >     * a deserialized serialized atomic holds same value
113       */
114      public void testSerialization() {
115          AtomicBoolean l = new AtomicBoolean();
# Line 107 | Line 131 | public class AtomicBooleanTest extends J
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