ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicStampedReferenceTest.java
Revision: 1.5
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.4: +6 -4 lines
Log Message:
Headers reference Creative Commons

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.5 * 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 dl 1.1 */
9    
10     import junit.framework.*;
11     import java.util.concurrent.atomic.*;
12    
13 dl 1.2 public class AtomicStampedReferenceTest extends JSR166TestCase{
14 dl 1.1 public static void main (String[] args) {
15     junit.textui.TestRunner.run (suite());
16     }
17     public static Test suite() {
18     return new TestSuite(AtomicStampedReferenceTest.class);
19     }
20    
21 dl 1.3 /**
22 dl 1.4 * constructeor initializes to given reference and stamp
23 dl 1.3 */
24 dl 1.1 public void testConstructor(){
25     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
26     assertEquals(one,ai.getReference());
27     assertEquals(0, ai.getStamp());
28     AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
29     assertNull(a2.getReference());
30     assertEquals(1, a2.getStamp());
31    
32     }
33    
34 dl 1.3 /**
35 dl 1.4 * get returns the last values of reference and stamp set
36 dl 1.3 */
37 dl 1.1 public void testGetSet(){
38     int[] mark = new int[1];
39     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
40     assertEquals(one,ai.getReference());
41     assertEquals(0, ai.getStamp());
42     assertEquals(one, ai.get(mark));
43     assertEquals(0, mark[0]);
44     ai.set(two, 0);
45     assertEquals(two,ai.getReference());
46     assertEquals(0, ai.getStamp());
47     assertEquals(two, ai.get(mark));
48     assertEquals(0, mark[0]);
49     ai.set(one, 1);
50     assertEquals(one,ai.getReference());
51     assertEquals(1, ai.getStamp());
52     assertEquals(one, ai.get(mark));
53     assertEquals(1,mark[0]);
54     }
55    
56 dl 1.3 /**
57 dl 1.4 * attemptStamp succeeds in single thread
58 dl 1.3 */
59 dl 1.1 public void testAttemptStamp(){
60     int[] mark = new int[1];
61     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
62     assertEquals(0, ai.getStamp());
63     assertTrue(ai.attemptStamp(one, 1));
64     assertEquals(1, ai.getStamp());
65     assertEquals(one, ai.get(mark));
66     assertEquals(1, mark[0]);
67     }
68    
69 dl 1.3 /**
70 dl 1.4 * compareAndSet succeeds in changing values if equal to expected reference
71     * and stamp else fails
72 dl 1.3 */
73 dl 1.1 public void testCompareAndSet(){
74     int[] mark = new int[1];
75     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
76     assertEquals(one, ai.get(mark));
77     assertEquals(0, ai.getStamp());
78     assertEquals(0, mark[0]);
79    
80     assertTrue(ai.compareAndSet(one, two, 0, 0));
81     assertEquals(two, ai.get(mark));
82     assertEquals(0, mark[0]);
83    
84     assertTrue(ai.compareAndSet(two, m3, 0, 1));
85     assertEquals(m3, ai.get(mark));
86     assertEquals(1, mark[0]);
87    
88     assertFalse(ai.compareAndSet(two, m3, 1, 1));
89     assertEquals(m3, ai.get(mark));
90     assertEquals(1, mark[0]);
91     }
92    
93 dl 1.3 /**
94 dl 1.4 * 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 dl 1.3 */
143 dl 1.1 public void testWeakCompareAndSet(){
144     int[] mark = new int[1];
145     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
146     assertEquals(one, ai.get(mark));
147     assertEquals(0, ai.getStamp ());
148     assertEquals(0, mark[0]);
149    
150     while(!ai.weakCompareAndSet(one, two, 0, 0));
151     assertEquals(two, ai.get(mark));
152     assertEquals(0, mark[0]);
153    
154     while(!ai.weakCompareAndSet(two, m3, 0, 1));
155     assertEquals(m3, ai.get(mark));
156     assertEquals(1, mark[0]);
157     }
158    
159     }