ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.25
Committed: Fri Jun 10 20:17:11 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.24: +1 -1 lines
Log Message:
fix order of arguments to assert(Equals|Same) methods

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