ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicBooleanTest.java
Revision: 1.5
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +30 -7 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

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