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.5 by dl, Sat Dec 27 19:26:43 2003 UTC

# Line 1 | Line 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.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7 > */
8   */
9  
10   import junit.framework.*;
# Line 16 | Line 18 | public class AtomicStampedReferenceTest
18          return new TestSuite(AtomicStampedReferenceTest.class);
19      }
20      
21 <    static final Integer zero = new Integer(0);
22 <    static final Integer one = new Integer(1);
23 <    static final Integer two = new Integer(2);
22 <    static final Integer m3  = new Integer(-3);
23 <
21 >    /**
22 >     * constructeor initializes to given reference and stamp
23 >     */
24      public void testConstructor(){
25          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
26          assertEquals(one,ai.getReference());
# Line 31 | Line 31 | public class AtomicStampedReferenceTest
31  
32      }
33  
34 +    /**
35 +     *  get returns the last values of reference and stamp set
36 +     */
37      public void testGetSet(){
38          int[] mark = new int[1];
39          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 50 | Line 53 | public class AtomicStampedReferenceTest
53          assertEquals(1,mark[0]);
54      }
55  
56 +    /**
57 +     *  attemptStamp succeeds in single thread
58 +     */
59      public void testAttemptStamp(){
60          int[] mark = new int[1];
61          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 60 | Line 66 | public class AtomicStampedReferenceTest
66          assertEquals(1, mark[0]);
67      }
68  
69 +    /**
70 +     * compareAndSet succeeds in changing values if equal to expected reference
71 +     * and stamp else fails
72 +     */
73      public void testCompareAndSet(){
74          int[] mark = new int[1];
75          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
# Line 80 | Line 90 | public class AtomicStampedReferenceTest
90          assertEquals(1, mark[0]);
91      }
92  
93 +    /**
94 +     * compareAndSet in one thread enables another waiting for reference value
95 +     * to succeed
96 +     */
97 +    public void testCompareAndSetInMultipleThreads() {
98 +        final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
99 +        Thread t = new Thread(new Runnable() {
100 +                public void run() {
101 +                    while(!ai.compareAndSet(two, three, 0, 0)) Thread.yield();
102 +                }});
103 +        try {
104 +            t.start();
105 +            assertTrue(ai.compareAndSet(one, two, 0, 0));
106 +            t.join(LONG_DELAY_MS);
107 +            assertFalse(t.isAlive());
108 +            assertEquals(ai.getReference(), three);
109 +            assertEquals(ai.getStamp(), 0);
110 +        }
111 +        catch(Exception e) {
112 +            unexpectedException();
113 +        }
114 +    }
115 +
116 +    /**
117 +     * compareAndSet in one thread enables another waiting for stamp value
118 +     * to succeed
119 +     */
120 +    public void testCompareAndSetInMultipleThreads2() {
121 +        final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
122 +        Thread t = new Thread(new Runnable() {
123 +                public void run() {
124 +                    while(!ai.compareAndSet(one, one, 1, 2)) Thread.yield();
125 +                }});
126 +        try {
127 +            t.start();
128 +            assertTrue(ai.compareAndSet(one, one, 0, 1));
129 +            t.join(LONG_DELAY_MS);
130 +            assertFalse(t.isAlive());
131 +            assertEquals(ai.getReference(), one);
132 +            assertEquals(ai.getStamp(), 2);
133 +        }
134 +        catch(Exception e) {
135 +            unexpectedException();
136 +        }
137 +    }
138 +
139 +    /**
140 +     * repeated weakCompareAndSet succeeds in changing values when equal
141 +     * to expected
142 +     */
143      public void testWeakCompareAndSet(){
144          int[] mark = new int[1];
145          AtomicStampedReference ai = new AtomicStampedReference(one, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines