ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceTest.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

# 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     public class AtomicReferenceTest 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(AtomicReferenceTest.class);
18     }
19    
20     static final Integer zero = new Integer(0);
21     static final Integer one = new Integer(1);
22     static final Integer two = new Integer(2);
23     static final Integer m3 = new Integer(-3);
24     static final Integer m4 = new Integer(-4);
25     static final Integer m5 = new Integer(-5);
26     static final Integer seven = new Integer(7);
27     static final Integer m10 = new Integer(-10);
28    
29     public void testConstructor(){
30     AtomicReference ai = new AtomicReference(one);
31     assertEquals(one,ai.get());
32     }
33    
34     public void testConstructor2(){
35     AtomicReference ai = new AtomicReference();
36     assertNull(ai.get());
37     }
38    
39     public void testGetSet(){
40     AtomicReference ai = new AtomicReference(one);
41     assertEquals(one,ai.get());
42     ai.set(two);
43     assertEquals(two,ai.get());
44     ai.set(m3);
45     assertEquals(m3,ai.get());
46    
47     }
48     public void testCompareAndSet(){
49     AtomicReference ai = new AtomicReference(one);
50     assertTrue(ai.compareAndSet(one,two));
51     assertTrue(ai.compareAndSet(two,m4));
52     assertEquals(m4,ai.get());
53     assertFalse(ai.compareAndSet(m5,seven));
54     assertFalse((seven.equals(ai.get())));
55     assertTrue(ai.compareAndSet(m4,seven));
56     assertEquals(seven,ai.get());
57     }
58    
59     public void testWeakCompareAndSet(){
60     AtomicReference ai = new AtomicReference(one);
61     while(!ai.weakCompareAndSet(one,two));
62     while(!ai.weakCompareAndSet(two,m4));
63     assertEquals(m4,ai.get());
64     while(!ai.weakCompareAndSet(m4,seven));
65     assertEquals(seven,ai.get());
66     }
67    
68     public void testGetAndSet(){
69     AtomicReference ai = new AtomicReference(one);
70     assertEquals(one,ai.getAndSet(zero));
71     assertEquals(zero,ai.getAndSet(m10));
72     assertEquals(m10,ai.getAndSet(one));
73 dl 1.2 }
74    
75     public void testSerialization() {
76     AtomicReference l = new AtomicReference();
77    
78     try {
79     l.set(one);
80     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
81     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
82     out.writeObject(l);
83     out.close();
84    
85     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
86     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
87     AtomicReference r = (AtomicReference) in.readObject();
88     assertEquals(l.get(), r.get());
89     } catch(Exception e){
90     e.printStackTrace();
91     fail("unexpected exception");
92     }
93 dl 1.1 }
94    
95     }
96