ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicBooleanTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +21 -0 lines
Log Message:
Added serialization and lock 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 TestCase {
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 public void testConstructor(){
21 AtomicBoolean ai = new AtomicBoolean(true);
22 assertEquals(true,ai.get());
23 }
24
25 public void testConstructor2(){
26 AtomicBoolean ai = new AtomicBoolean();
27 assertEquals(false,ai.get());
28 }
29
30 public void testGetSet(){
31 AtomicBoolean ai = new AtomicBoolean(true);
32 assertEquals(true,ai.get());
33 ai.set(false);
34 assertEquals(false,ai.get());
35 ai.set(true);
36 assertEquals(true,ai.get());
37
38 }
39 public void testCompareAndSet(){
40 AtomicBoolean ai = new AtomicBoolean(true);
41 assertTrue(ai.compareAndSet(true,false));
42 assertEquals(false,ai.get());
43 assertTrue(ai.compareAndSet(false,false));
44 assertEquals(false,ai.get());
45 assertFalse(ai.compareAndSet(true,false));
46 assertFalse((ai.get()));
47 assertTrue(ai.compareAndSet(false,true));
48 assertEquals(true,ai.get());
49 }
50
51 public void testWeakCompareAndSet(){
52 AtomicBoolean ai = new AtomicBoolean(true);
53 while(!ai.weakCompareAndSet(true,false));
54 assertEquals(false,ai.get());
55 while(!ai.weakCompareAndSet(false,false));
56 assertEquals(false,ai.get());
57 while(!ai.weakCompareAndSet(false,true));
58 assertEquals(true,ai.get());
59 }
60
61 public void testGetAndSet(){
62 AtomicBoolean ai = new AtomicBoolean(true);
63 assertEquals(true,ai.getAndSet(false));
64 assertEquals(false,ai.getAndSet(false));
65 assertEquals(false,ai.getAndSet(true));
66 assertEquals(true,ai.get());
67 }
68
69 public void testSerialization() {
70 AtomicBoolean l = new AtomicBoolean();
71
72 try {
73 l.set(true);
74 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
75 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
76 out.writeObject(l);
77 out.close();
78
79 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
80 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
81 AtomicBoolean r = (AtomicBoolean) in.readObject();
82 assertEquals(l.get(), r.get());
83 } catch(Exception e){
84 e.printStackTrace();
85 fail("unexpected exception");
86 }
87 }
88
89
90 }