ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.22
Committed: Fri May 27 19:39:07 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +0 -1 lines
Log Message:
whitespace

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 jsr166 1.21 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.11 * 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 jsr166 1.13 public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
14 dl 1.1 volatile Integer x = null;
15     Object z;
16     Integer w;
17    
18 jsr166 1.13 public static void main(String[] args) {
19 dl 1.1 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 jsr166 1.13 public void testConstructor() {
29 jsr166 1.12 try {
30 dl 1.1 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31     a = AtomicReferenceFieldUpdater.newUpdater
32 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
33 dl 1.3 shouldThrow();
34 jsr166 1.17 } catch (RuntimeException success) {}
35 dl 1.1 }
36    
37 dl 1.3 /**
38 dl 1.4 * construction with field not of given type throws RuntimeException
39 dl 1.3 */
40 jsr166 1.13 public void testConstructor2() {
41 jsr166 1.12 try {
42 dl 1.1 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
43     a = AtomicReferenceFieldUpdater.newUpdater
44 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
45 dl 1.3 shouldThrow();
46 jsr166 1.17 } catch (RuntimeException success) {}
47 dl 1.1 }
48    
49 dl 1.3 /**
50 dl 1.4 * Constructor with non-volatile field throws exception
51 dl 1.3 */
52 jsr166 1.13 public void testConstructor3() {
53 jsr166 1.12 try {
54 dl 1.1 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
55     a = AtomicReferenceFieldUpdater.newUpdater
56 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
57 dl 1.3 shouldThrow();
58 jsr166 1.17 } catch (RuntimeException success) {}
59 dl 1.1 }
60    
61 dl 1.3 /**
62 jsr166 1.20 * get returns the last value set or assigned
63 dl 1.3 */
64 jsr166 1.13 public void testGetSet() {
65 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
66     try {
67     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
68     } catch (RuntimeException ok) {
69     return;
70     }
71 dl 1.1 x = one;
72 jsr166 1.18 assertSame(one,a.get(this));
73 jsr166 1.16 a.set(this,two);
74 jsr166 1.18 assertSame(two,a.get(this));
75 jsr166 1.16 a.set(this,m3);
76 jsr166 1.18 assertSame(m3,a.get(this));
77 dl 1.1 }
78 dl 1.10
79     /**
80 jsr166 1.20 * get returns the last value lazySet by same thread
81 dl 1.10 */
82 jsr166 1.13 public void testGetLazySet() {
83 dl 1.10 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
84     try {
85     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
86     } catch (RuntimeException ok) {
87     return;
88     }
89     x = one;
90 jsr166 1.18 assertSame(one,a.get(this));
91 jsr166 1.16 a.lazySet(this,two);
92 jsr166 1.18 assertSame(two,a.get(this));
93 jsr166 1.16 a.lazySet(this,m3);
94 jsr166 1.18 assertSame(m3,a.get(this));
95 dl 1.10 }
96    
97 dl 1.3 /**
98 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
99 dl 1.3 */
100 jsr166 1.13 public void testCompareAndSet() {
101 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
102     try {
103     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
104     } catch (RuntimeException ok) {
105     return;
106     }
107 dl 1.1 x = one;
108 jsr166 1.19 assertTrue(a.compareAndSet(this, one, two));
109     assertTrue(a.compareAndSet(this, two, m4));
110     assertSame(m4, a.get(this));
111     assertFalse(a.compareAndSet(this, m5, seven));
112 jsr166 1.18 assertFalse(seven == a.get(this));
113 jsr166 1.19 assertTrue(a.compareAndSet(this, m4, seven));
114 jsr166 1.18 assertSame(seven,a.get(this));
115 dl 1.1 }
116    
117 dl 1.3 /**
118 dl 1.4 * compareAndSet in one thread enables another waiting for value
119     * to succeed
120     */
121 jsr166 1.14 public void testCompareAndSetInMultipleThreads() throws Exception {
122 dl 1.4 x = one;
123 dl 1.8 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
124     try {
125     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
126     } catch (RuntimeException ok) {
127     return;
128     }
129 dl 1.4
130 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
131     public void realRun() {
132     while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
133     Thread.yield();
134     }});
135 jsr166 1.14
136     t.start();
137     assertTrue(a.compareAndSet(this, one, two));
138     t.join(LONG_DELAY_MS);
139     assertFalse(t.isAlive());
140 jsr166 1.18 assertSame(a.get(this), three);
141 dl 1.4 }
142    
143     /**
144     * repeated weakCompareAndSet succeeds in changing value when equal
145 jsr166 1.11 * to expected
146 dl 1.3 */
147 jsr166 1.13 public void testWeakCompareAndSet() {
148 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
149     try {
150     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
151     } catch (RuntimeException ok) {
152     return;
153     }
154 dl 1.1 x = one;
155 jsr166 1.16 while (!a.weakCompareAndSet(this,one,two));
156     while (!a.weakCompareAndSet(this,two,m4));
157 jsr166 1.18 assertSame(m4,a.get(this));
158 jsr166 1.16 while (!a.weakCompareAndSet(this,m4,seven));
159 jsr166 1.18 assertSame(seven,a.get(this));
160 dl 1.1 }
161    
162 dl 1.3 /**
163 dl 1.4 * getAndSet returns previous value and sets to given value
164 dl 1.3 */
165 jsr166 1.13 public void testGetAndSet() {
166 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
167     try {
168     a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
169     } catch (RuntimeException ok) {
170     return;
171     }
172 dl 1.1 x = one;
173 jsr166 1.18 assertSame(one,a.getAndSet(this, zero));
174     assertSame(zero,a.getAndSet(this,m10));
175     assertSame(m10,a.getAndSet(this,1));
176 dl 1.1 }
177    
178     }