ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicStampedReferenceTest.java
Revision: 1.4
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.3: +53 -5 lines
Log Message:
improve tck javadocs; rename and add a few 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    
11 dl 1.2 public class AtomicStampedReferenceTest extends JSR166TestCase{
12 dl 1.1 public static void main (String[] args) {
13     junit.textui.TestRunner.run (suite());
14     }
15     public static Test suite() {
16     return new TestSuite(AtomicStampedReferenceTest.class);
17     }
18    
19 dl 1.3 /**
20 dl 1.4 * constructeor initializes to given reference and stamp
21 dl 1.3 */
22 dl 1.1 public void testConstructor(){
23     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
24     assertEquals(one,ai.getReference());
25     assertEquals(0, ai.getStamp());
26     AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
27     assertNull(a2.getReference());
28     assertEquals(1, a2.getStamp());
29    
30     }
31    
32 dl 1.3 /**
33 dl 1.4 * get returns the last values of reference and stamp set
34 dl 1.3 */
35 dl 1.1 public void testGetSet(){
36     int[] mark = new int[1];
37     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
38     assertEquals(one,ai.getReference());
39     assertEquals(0, ai.getStamp());
40     assertEquals(one, ai.get(mark));
41     assertEquals(0, mark[0]);
42     ai.set(two, 0);
43     assertEquals(two,ai.getReference());
44     assertEquals(0, ai.getStamp());
45     assertEquals(two, ai.get(mark));
46     assertEquals(0, mark[0]);
47     ai.set(one, 1);
48     assertEquals(one,ai.getReference());
49     assertEquals(1, ai.getStamp());
50     assertEquals(one, ai.get(mark));
51     assertEquals(1,mark[0]);
52     }
53    
54 dl 1.3 /**
55 dl 1.4 * attemptStamp succeeds in single thread
56 dl 1.3 */
57 dl 1.1 public void testAttemptStamp(){
58     int[] mark = new int[1];
59     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
60     assertEquals(0, ai.getStamp());
61     assertTrue(ai.attemptStamp(one, 1));
62     assertEquals(1, ai.getStamp());
63     assertEquals(one, ai.get(mark));
64     assertEquals(1, mark[0]);
65     }
66    
67 dl 1.3 /**
68 dl 1.4 * compareAndSet succeeds in changing values if equal to expected reference
69     * and stamp else fails
70 dl 1.3 */
71 dl 1.1 public void testCompareAndSet(){
72     int[] mark = new int[1];
73     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
74     assertEquals(one, ai.get(mark));
75     assertEquals(0, ai.getStamp());
76     assertEquals(0, mark[0]);
77    
78     assertTrue(ai.compareAndSet(one, two, 0, 0));
79     assertEquals(two, ai.get(mark));
80     assertEquals(0, mark[0]);
81    
82     assertTrue(ai.compareAndSet(two, m3, 0, 1));
83     assertEquals(m3, ai.get(mark));
84     assertEquals(1, mark[0]);
85    
86     assertFalse(ai.compareAndSet(two, m3, 1, 1));
87     assertEquals(m3, ai.get(mark));
88     assertEquals(1, mark[0]);
89     }
90    
91 dl 1.3 /**
92 dl 1.4 * 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 dl 1.3 */
141 dl 1.1 public void testWeakCompareAndSet(){
142     int[] mark = new int[1];
143     AtomicStampedReference ai = new AtomicStampedReference(one, 0);
144     assertEquals(one, ai.get(mark));
145     assertEquals(0, ai.getStamp ());
146     assertEquals(0, mark[0]);
147    
148     while(!ai.weakCompareAndSet(one, two, 0, 0));
149     assertEquals(two, ai.get(mark));
150     assertEquals(0, mark[0]);
151    
152     while(!ai.weakCompareAndSet(two, m3, 0, 1));
153     assertEquals(m3, ai.get(mark));
154     assertEquals(1, mark[0]);
155     }
156    
157     }