1 |
|
/* |
2 |
|
* 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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile Integer x = null; |
30 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
31 |
|
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
32 |
|
shouldThrow(); |
33 |
< |
} |
35 |
< |
catch (RuntimeException rt) {} |
33 |
> |
} catch (RuntimeException success) {} |
34 |
|
} |
35 |
|
|
38 |
– |
|
36 |
|
/** |
37 |
|
* construction with field not of given type throws RuntimeException |
38 |
|
*/ |
42 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
43 |
|
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
44 |
|
shouldThrow(); |
45 |
< |
} |
49 |
< |
catch (RuntimeException rt) {} |
45 |
> |
} catch (RuntimeException success) {} |
46 |
|
} |
47 |
|
|
48 |
|
/** |
54 |
|
a = AtomicReferenceFieldUpdater.newUpdater |
55 |
|
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
56 |
|
shouldThrow(); |
57 |
< |
} |
62 |
< |
catch (RuntimeException rt) {} |
57 |
> |
} catch (RuntimeException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* get returns the last value set or assigned |
61 |
> |
* get returns the last value set or assigned |
62 |
|
*/ |
63 |
|
public void testGetSet() { |
64 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
68 |
|
return; |
69 |
|
} |
70 |
|
x = one; |
71 |
< |
assertEquals(one,a.get(this)); |
72 |
< |
a.set(this,two); |
73 |
< |
assertEquals(two,a.get(this)); |
74 |
< |
a.set(this,m3); |
75 |
< |
assertEquals(m3,a.get(this)); |
71 |
> |
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 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
< |
* get returns the last value lazySet by same thread |
79 |
> |
* get returns the last value lazySet by same thread |
80 |
|
*/ |
81 |
|
public void testGetLazySet() { |
82 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
86 |
|
return; |
87 |
|
} |
88 |
|
x = one; |
89 |
< |
assertEquals(one,a.get(this)); |
90 |
< |
a.lazySet(this,two); |
91 |
< |
assertEquals(two,a.get(this)); |
92 |
< |
a.lazySet(this,m3); |
93 |
< |
assertEquals(m3,a.get(this)); |
89 |
> |
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 |
|
} |
95 |
|
|
96 |
|
/** |
104 |
|
return; |
105 |
|
} |
106 |
|
x = one; |
107 |
< |
assertTrue(a.compareAndSet(this,one,two)); |
108 |
< |
assertTrue(a.compareAndSet(this,two,m4)); |
109 |
< |
assertEquals(m4,a.get(this)); |
110 |
< |
assertFalse(a.compareAndSet(this,m5,seven)); |
111 |
< |
assertFalse((seven == a.get(this))); |
112 |
< |
assertTrue(a.compareAndSet(this,m4,seven)); |
113 |
< |
assertEquals(seven,a.get(this)); |
107 |
> |
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 |
> |
assertFalse(seven == a.get(this)); |
112 |
> |
assertTrue(a.compareAndSet(this, m4, seven)); |
113 |
> |
assertSame(seven, a.get(this)); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
136 |
|
assertTrue(a.compareAndSet(this, one, two)); |
137 |
|
t.join(LONG_DELAY_MS); |
138 |
|
assertFalse(t.isAlive()); |
139 |
< |
assertEquals(a.get(this), three); |
139 |
> |
assertSame(three, a.get(this)); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
151 |
|
return; |
152 |
|
} |
153 |
|
x = one; |
154 |
< |
while (!a.weakCompareAndSet(this,one,two)); |
155 |
< |
while (!a.weakCompareAndSet(this,two,m4)); |
156 |
< |
assertEquals(m4,a.get(this)); |
157 |
< |
while (!a.weakCompareAndSet(this,m4,seven)); |
158 |
< |
assertEquals(seven,a.get(this)); |
154 |
> |
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 |
|
} |
160 |
|
|
161 |
|
/** |
169 |
|
return; |
170 |
|
} |
171 |
|
x = one; |
172 |
< |
assertEquals(one,a.getAndSet(this, zero)); |
173 |
< |
assertEquals(zero,a.getAndSet(this,m10)); |
174 |
< |
assertEquals(m10,a.getAndSet(this,1)); |
172 |
> |
assertSame(one, a.getAndSet(this, zero)); |
173 |
> |
assertSame(zero, a.getAndSet(this, m10)); |
174 |
> |
assertSame(m10, a.getAndSet(this, 1)); |
175 |
|
} |
176 |
|
|
177 |
|
} |