ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.9
Committed: Tue Dec 28 16:15:59 2004 UTC (19 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.8: +2 -2 lines
Log Message:
Integrate tests for jsr166x classes

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * 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 dl 1.1 */
8    
9     import java.util.concurrent.atomic.*;
10     import junit.framework.*;
11     import java.util.*;
12    
13 dl 1.2 public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
14 dl 1.1 volatile Integer x = null;
15     Object z;
16     Integer w;
17    
18     public static void main(String[] args){
19     junit.textui.TestRunner.run(suite());
20     }
21     public static Test suite() {
22     return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
23     }
24    
25 dl 1.3 /**
26 dl 1.7 * Construction with non-existent field throws RuntimeException
27 dl 1.3 */
28 dl 1.1 public void testConstructor(){
29     try{
30     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31     a = AtomicReferenceFieldUpdater.newUpdater
32 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
33 dl 1.3 shouldThrow();
34 dl 1.1 }
35     catch (RuntimeException rt) {}
36     }
37    
38    
39 dl 1.3 /**
40 dl 1.4 * construction with field not of given type throws RuntimeException
41 dl 1.3 */
42 dl 1.1 public void testConstructor2(){
43     try{
44     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
45     a = AtomicReferenceFieldUpdater.newUpdater
46 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
47 dl 1.3 shouldThrow();
48 dl 1.1 }
49     catch (RuntimeException rt) {}
50     }
51    
52 dl 1.3 /**
53 dl 1.4 * Constructor with non-volatile field throws exception
54 dl 1.3 */
55 dl 1.1 public void testConstructor3(){
56     try{
57     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
58     a = AtomicReferenceFieldUpdater.newUpdater
59 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
60 dl 1.3 shouldThrow();
61 dl 1.1 }
62     catch (RuntimeException rt) {}
63     }
64    
65 dl 1.3 /**
66 dl 1.4 * get returns the last value set or assigned
67 dl 1.3 */
68 dl 1.1 public void testGetSet(){
69 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
70     try {
71     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
72     } catch (RuntimeException ok) {
73     return;
74     }
75 dl 1.1 x = one;
76     assertEquals(one,a.get(this));
77     a.set(this,two);
78     assertEquals(two,a.get(this));
79 dl 1.9 a.set(this,m3);
80     assertEquals(m3,a.get(this));
81 dl 1.1
82     }
83 dl 1.3 /**
84 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
85 dl 1.3 */
86 dl 1.1 public void testCompareAndSet(){
87 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
88     try {
89     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
90     } catch (RuntimeException ok) {
91     return;
92     }
93 dl 1.1 x = one;
94     assertTrue(a.compareAndSet(this,one,two));
95     assertTrue(a.compareAndSet(this,two,m4));
96     assertEquals(m4,a.get(this));
97     assertFalse(a.compareAndSet(this,m5,seven));
98     assertFalse((seven == a.get(this)));
99     assertTrue(a.compareAndSet(this,m4,seven));
100     assertEquals(seven,a.get(this));
101     }
102    
103 dl 1.3 /**
104 dl 1.4 * compareAndSet in one thread enables another waiting for value
105     * to succeed
106     */
107     public void testCompareAndSetInMultipleThreads() {
108     x = one;
109 dl 1.8 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
110     try {
111     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
112     } catch (RuntimeException ok) {
113     return;
114     }
115 dl 1.4
116     Thread t = new Thread(new Runnable() {
117     public void run() {
118     while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
119     }});
120     try {
121     t.start();
122     assertTrue(a.compareAndSet(this, one, two));
123     t.join(LONG_DELAY_MS);
124     assertFalse(t.isAlive());
125     assertEquals(a.get(this), three);
126     }
127     catch(Exception e) {
128     unexpectedException();
129     }
130     }
131    
132     /**
133     * repeated weakCompareAndSet succeeds in changing value when equal
134     * to expected
135 dl 1.3 */
136 dl 1.1 public void testWeakCompareAndSet(){
137 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
138     try {
139     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
140     } catch (RuntimeException ok) {
141     return;
142     }
143 dl 1.1 x = one;
144     while(!a.weakCompareAndSet(this,one,two));
145     while(!a.weakCompareAndSet(this,two,m4));
146     assertEquals(m4,a.get(this));
147     while(!a.weakCompareAndSet(this,m4,seven));
148     assertEquals(seven,a.get(this));
149     }
150    
151 dl 1.3 /**
152 dl 1.4 * getAndSet returns previous value and sets to given value
153 dl 1.3 */
154 dl 1.1 public void testGetAndSet(){
155 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
156     try {
157     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
158     } catch (RuntimeException ok) {
159     return;
160     }
161 dl 1.1 x = one;
162     assertEquals(one,a.getAndSet(this, zero));
163     assertEquals(zero,a.getAndSet(this,m10));
164     assertEquals(m10,a.getAndSet(this,1));
165     }
166    
167     }