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

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.4 * Contruction 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.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
70 dl 1.1 x = one;
71     assertEquals(one,a.get(this));
72     a.set(this,two);
73     assertEquals(two,a.get(this));
74     a.set(this,-3);
75     assertEquals(-3,a.get(this));
76    
77     }
78 dl 1.3 /**
79 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
80 dl 1.3 */
81 dl 1.1 public void testCompareAndSet(){
82 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
83 dl 1.1 x = one;
84     assertTrue(a.compareAndSet(this,one,two));
85     assertTrue(a.compareAndSet(this,two,m4));
86     assertEquals(m4,a.get(this));
87     assertFalse(a.compareAndSet(this,m5,seven));
88     assertFalse((seven == a.get(this)));
89     assertTrue(a.compareAndSet(this,m4,seven));
90     assertEquals(seven,a.get(this));
91     }
92    
93 dl 1.3 /**
94 dl 1.4 * compareAndSet in one thread enables another waiting for value
95     * to succeed
96     */
97     public void testCompareAndSetInMultipleThreads() {
98     x = one;
99 dl 1.5 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
100 dl 1.4
101     Thread t = new Thread(new Runnable() {
102     public void run() {
103     while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
104     }});
105     try {
106     t.start();
107     assertTrue(a.compareAndSet(this, one, two));
108     t.join(LONG_DELAY_MS);
109     assertFalse(t.isAlive());
110     assertEquals(a.get(this), three);
111     }
112     catch(Exception e) {
113     unexpectedException();
114     }
115     }
116    
117     /**
118     * repeated weakCompareAndSet succeeds in changing value when equal
119     * to expected
120 dl 1.3 */
121 dl 1.1 public void testWeakCompareAndSet(){
122 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
123 dl 1.1 x = one;
124     while(!a.weakCompareAndSet(this,one,two));
125     while(!a.weakCompareAndSet(this,two,m4));
126     assertEquals(m4,a.get(this));
127     while(!a.weakCompareAndSet(this,m4,seven));
128     assertEquals(seven,a.get(this));
129     }
130    
131 dl 1.3 /**
132 dl 1.4 * getAndSet returns previous value and sets to given value
133 dl 1.3 */
134 dl 1.1 public void testGetAndSet(){
135 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
136 dl 1.1 x = one;
137     assertEquals(one,a.getAndSet(this, zero));
138     assertEquals(zero,a.getAndSet(this,m10));
139     assertEquals(m10,a.getAndSet(this,1));
140     }
141    
142     }