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

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

# Line 16 | Line 16 | public class AtomicStampedReferenceTest
16          return new TestSuite(AtomicStampedReferenceTest.class);
17      }
18      
19 <    static final Integer zero = new Integer(0);
20 <    static final Integer one = new Integer(1);
21 <    static final Integer two = new Integer(2);
22 <    static final Integer m3  = new Integer(-3);
23 <
19 >    /**
20 >     * constructeor initializes to given reference and stamp
21 >     */
22      public void testConstructor(){
23          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
24          assertEquals(one,ai.getReference());
# Line 31 | Line 29 | public class AtomicStampedReferenceTest
29  
30      }
31  
32 +    /**
33 +     *  get returns the last values of reference and stamp set
34 +     */
35      public void testGetSet(){
36          int[] mark = new int[1];
37          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 50 | Line 51 | public class AtomicStampedReferenceTest
51          assertEquals(1,mark[0]);
52      }
53  
54 +    /**
55 +     *  attemptStamp succeeds in single thread
56 +     */
57      public void testAttemptStamp(){
58          int[] mark = new int[1];
59          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 60 | Line 64 | public class AtomicStampedReferenceTest
64          assertEquals(1, mark[0]);
65      }
66  
67 +    /**
68 +     * compareAndSet succeeds in changing values if equal to expected reference
69 +     * and stamp else fails
70 +     */
71      public void testCompareAndSet(){
72          int[] mark = new int[1];
73          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 80 | Line 88 | public class AtomicStampedReferenceTest
88          assertEquals(1, mark[0]);
89      }
90  
91 +    /**
92 +     * compareAndSet in one thread enables another waiting for reference value
93 +     * to succeed
94 +     */
95 +    public void testCompareAndSetInMultipleThreads() {
96 +        final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
97 +        Thread t = new Thread(new Runnable() {
98 +                public void run() {
99 +                    while(!ai.compareAndSet(two, three, 0, 0)) Thread.yield();
100 +                }});
101 +        try {
102 +            t.start();
103 +            assertTrue(ai.compareAndSet(one, two, 0, 0));
104 +            t.join(LONG_DELAY_MS);
105 +            assertFalse(t.isAlive());
106 +            assertEquals(ai.getReference(), three);
107 +            assertEquals(ai.getStamp(), 0);
108 +        }
109 +        catch(Exception e) {
110 +            unexpectedException();
111 +        }
112 +    }
113 +
114 +    /**
115 +     * compareAndSet in one thread enables another waiting for stamp value
116 +     * to succeed
117 +     */
118 +    public void testCompareAndSetInMultipleThreads2() {
119 +        final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
120 +        Thread t = new Thread(new Runnable() {
121 +                public void run() {
122 +                    while(!ai.compareAndSet(one, one, 1, 2)) Thread.yield();
123 +                }});
124 +        try {
125 +            t.start();
126 +            assertTrue(ai.compareAndSet(one, one, 0, 1));
127 +            t.join(LONG_DELAY_MS);
128 +            assertFalse(t.isAlive());
129 +            assertEquals(ai.getReference(), one);
130 +            assertEquals(ai.getStamp(), 2);
131 +        }
132 +        catch(Exception e) {
133 +            unexpectedException();
134 +        }
135 +    }
136 +
137 +    /**
138 +     * repeated weakCompareAndSet succeeds in changing values when equal
139 +     * to expected
140 +     */
141      public void testWeakCompareAndSet(){
142          int[] mark = new int[1];
143          AtomicStampedReference ai = new AtomicStampedReference(one, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines