ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 17 | Line 17 | public class AtomicReferenceTest extends
17          return new TestSuite(AtomicReferenceTest.class);
18      }
19  
20 +    /**
21 +     * constructor initializes to given value
22 +     */
23      public void testConstructor(){
24          AtomicReference ai = new AtomicReference(one);
25          assertEquals(one,ai.get());
26      }
27  
28 +    /**
29 +     * default constructed initializes to null
30 +     */
31      public void testConstructor2(){
32          AtomicReference ai = new AtomicReference();
33          assertNull(ai.get());
34      }
35  
36 +    /**
37 +     * get returns the last value set
38 +     */
39      public void testGetSet(){
40          AtomicReference ai = new AtomicReference(one);
41          assertEquals(one,ai.get());
# Line 36 | Line 45 | public class AtomicReferenceTest extends
45          assertEquals(m3,ai.get());
46          
47      }
48 +    /**
49 +     * compareAndSet succeeds in changing value if equal to expected else fails
50 +     */
51      public void testCompareAndSet(){
52          AtomicReference ai = new AtomicReference(one);
53          assertTrue(ai.compareAndSet(one,two));
# Line 47 | Line 59 | public class AtomicReferenceTest extends
59          assertEquals(seven,ai.get());
60      }
61  
62 +    /**
63 +     * compareAndSet in one thread enables another waiting for value
64 +     * to succeed
65 +     */
66 +    public void testCompareAndSetInMultipleThreads() {
67 +        final AtomicReference ai = new AtomicReference(one);
68 +        Thread t = new Thread(new Runnable() {
69 +                public void run() {
70 +                    while(!ai.compareAndSet(two, three)) Thread.yield();
71 +                }});
72 +        try {
73 +            t.start();
74 +            assertTrue(ai.compareAndSet(one, two));
75 +            t.join(LONG_DELAY_MS);
76 +            assertFalse(t.isAlive());
77 +            assertEquals(ai.get(), three);
78 +        }
79 +        catch(Exception e) {
80 +            unexpectedException();
81 +        }
82 +    }
83 +
84 +    /**
85 +     * repeated weakCompareAndSet succeeds in changing value when equal
86 +     * to expected
87 +     */
88      public void testWeakCompareAndSet(){
89          AtomicReference ai = new AtomicReference(one);
90          while(!ai.weakCompareAndSet(one,two));
# Line 56 | Line 94 | public class AtomicReferenceTest extends
94          assertEquals(seven,ai.get());
95      }
96  
97 +    /**
98 +     * getAndSet returns previous value and sets to given value
99 +     */
100      public void testGetAndSet(){
101          AtomicReference ai = new AtomicReference(one);
102          assertEquals(one,ai.getAndSet(zero));
# Line 63 | Line 104 | public class AtomicReferenceTest extends
104          assertEquals(m10,ai.getAndSet(one));
105      }
106  
107 +    /**
108 +     * a deserialized serialized atomic holds same value
109 +     */
110      public void testSerialization() {
111          AtomicReference l = new AtomicReference();
112  
# Line 78 | Line 122 | public class AtomicReferenceTest extends
122              AtomicReference r = (AtomicReference) in.readObject();
123              assertEquals(l.get(), r.get());
124          } catch(Exception e){
125 <            e.printStackTrace();
82 <            fail("unexpected exception");
125 >            unexpectedException();
126          }
127      }
128  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines