ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicBooleanTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +28 -7 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 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.
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.atomic.*;
10 dl 1.2 import java.io.*;
11 dl 1.1
12 dl 1.3 public class AtomicBooleanTest extends JSR166TestCase {
13 dl 1.1 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 dl 1.4 /**
21     *
22     */
23     public void testConstructor() {
24 dl 1.1 AtomicBoolean ai = new AtomicBoolean(true);
25     assertEquals(true,ai.get());
26     }
27    
28 dl 1.4 /**
29     *
30     */
31     public void testConstructor2() {
32 dl 1.1 AtomicBoolean ai = new AtomicBoolean();
33     assertEquals(false,ai.get());
34     }
35    
36 dl 1.4 /**
37     *
38     */
39     public void testGetSet() {
40 dl 1.1 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 dl 1.4 /**
49     *
50     */
51     public void testCompareAndSet() {
52 dl 1.1 AtomicBoolean ai = new AtomicBoolean(true);
53     assertTrue(ai.compareAndSet(true,false));
54     assertEquals(false,ai.get());
55     assertTrue(ai.compareAndSet(false,false));
56     assertEquals(false,ai.get());
57     assertFalse(ai.compareAndSet(true,false));
58     assertFalse((ai.get()));
59     assertTrue(ai.compareAndSet(false,true));
60     assertEquals(true,ai.get());
61     }
62    
63 dl 1.4 /**
64     *
65     */
66     public void testWeakCompareAndSet() {
67 dl 1.1 AtomicBoolean ai = new AtomicBoolean(true);
68     while(!ai.weakCompareAndSet(true,false));
69     assertEquals(false,ai.get());
70     while(!ai.weakCompareAndSet(false,false));
71     assertEquals(false,ai.get());
72     while(!ai.weakCompareAndSet(false,true));
73     assertEquals(true,ai.get());
74     }
75    
76 dl 1.4 /**
77     *
78     */
79     public void testGetAndSet() {
80 dl 1.1 AtomicBoolean ai = new AtomicBoolean(true);
81     assertEquals(true,ai.getAndSet(false));
82     assertEquals(false,ai.getAndSet(false));
83     assertEquals(false,ai.getAndSet(true));
84     assertEquals(true,ai.get());
85 dl 1.2 }
86    
87 dl 1.4 /**
88     *
89     */
90 dl 1.2 public void testSerialization() {
91     AtomicBoolean l = new AtomicBoolean();
92    
93     try {
94     l.set(true);
95     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
96     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
97     out.writeObject(l);
98     out.close();
99    
100     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
101     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
102     AtomicBoolean r = (AtomicBoolean) in.readObject();
103     assertEquals(l.get(), r.get());
104     } catch(Exception e){
105     e.printStackTrace();
106 dl 1.4 unexpectedException();
107 dl 1.2 }
108 dl 1.1 }
109    
110    
111     }