[cvs] / jsr166 / src / test / tck / AtomicBooleanTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AtomicBooleanTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (view) (download)

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8