ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicBooleanTest.java
Revision: 1.7
Committed: Mon Dec 29 19:05:40 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
spellcheck

File Contents

# Content
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.
7 */
8
9 import junit.framework.*;
10 import java.util.concurrent.atomic.*;
11 import java.io.*;
12
13 public class AtomicBooleanTest extends JSR166TestCase {
14 public static void main (String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17 public static Test suite() {
18 return new TestSuite(AtomicBooleanTest.class);
19 }
20
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 /**
30 * default constructed initializes to false
31 */
32 public void testConstructor2() {
33 AtomicBoolean ai = new AtomicBoolean();
34 assertEquals(false,ai.get());
35 }
36
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);
44 assertEquals(false,ai.get());
45 ai.set(true);
46 assertEquals(true,ai.get());
47
48 }
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());
57 assertTrue(ai.compareAndSet(false,false));
58 assertEquals(false,ai.get());
59 assertFalse(ai.compareAndSet(true,false));
60 assertFalse((ai.get()));
61 assertTrue(ai.compareAndSet(false,true));
62 assertEquals(true,ai.get());
63 }
64
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());
94 while(!ai.weakCompareAndSet(false,false));
95 assertEquals(false,ai.get());
96 while(!ai.weakCompareAndSet(false,true));
97 assertEquals(true,ai.get());
98 }
99
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));
107 assertEquals(false,ai.getAndSet(true));
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 }