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 junit.framework.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicStampedReference; |
10 |
> |
|
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
|
|
14 |
|
public class AtomicStampedReferenceTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
24 |
|
*/ |
25 |
|
public void testConstructor() { |
26 |
|
AtomicStampedReference ai = new AtomicStampedReference(one, 0); |
27 |
< |
assertSame(one,ai.getReference()); |
27 |
> |
assertSame(one, ai.getReference()); |
28 |
|
assertEquals(0, ai.getStamp()); |
29 |
|
AtomicStampedReference a2 = new AtomicStampedReference(null, 1); |
30 |
|
assertNull(a2.getReference()); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* get returns the last values of reference and stamp set |
35 |
> |
* get returns the last values of reference and stamp set |
36 |
|
*/ |
37 |
|
public void testGetSet() { |
38 |
|
int[] mark = new int[1]; |
39 |
|
AtomicStampedReference ai = new AtomicStampedReference(one, 0); |
40 |
< |
assertSame(one,ai.getReference()); |
40 |
> |
assertSame(one, ai.getReference()); |
41 |
|
assertEquals(0, ai.getStamp()); |
42 |
|
assertSame(one, ai.get(mark)); |
43 |
|
assertEquals(0, mark[0]); |
44 |
|
ai.set(two, 0); |
45 |
< |
assertSame(two,ai.getReference()); |
45 |
> |
assertSame(two, ai.getReference()); |
46 |
|
assertEquals(0, ai.getStamp()); |
47 |
|
assertSame(two, ai.get(mark)); |
48 |
|
assertEquals(0, mark[0]); |
49 |
|
ai.set(one, 1); |
50 |
< |
assertSame(one,ai.getReference()); |
50 |
> |
assertSame(one, ai.getReference()); |
51 |
|
assertEquals(1, ai.getStamp()); |
52 |
|
assertSame(one, ai.get(mark)); |
53 |
< |
assertEquals(1,mark[0]); |
53 |
> |
assertEquals(1, mark[0]); |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* attemptStamp succeeds in single thread |
57 |
> |
* attemptStamp succeeds in single thread |
58 |
|
*/ |
59 |
|
public void testAttemptStamp() { |
60 |
|
int[] mark = new int[1]; |
106 |
|
assertTrue(ai.compareAndSet(one, two, 0, 0)); |
107 |
|
t.join(LONG_DELAY_MS); |
108 |
|
assertFalse(t.isAlive()); |
109 |
< |
assertSame(ai.getReference(), three); |
110 |
< |
assertEquals(ai.getStamp(), 0); |
109 |
> |
assertSame(three, ai.getReference()); |
110 |
> |
assertEquals(0, ai.getStamp()); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
126 |
|
assertTrue(ai.compareAndSet(one, one, 0, 1)); |
127 |
|
t.join(LONG_DELAY_MS); |
128 |
|
assertFalse(t.isAlive()); |
129 |
< |
assertSame(ai.getReference(), one); |
130 |
< |
assertEquals(ai.getStamp(), 2); |
129 |
> |
assertSame(one, ai.getReference()); |
130 |
> |
assertEquals(2, ai.getStamp()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
141 |
|
assertEquals(0, ai.getStamp()); |
142 |
|
assertEquals(0, mark[0]); |
143 |
|
|
144 |
< |
while (!ai.weakCompareAndSet(one, two, 0, 0)); |
144 |
> |
do {} while (!ai.weakCompareAndSet(one, two, 0, 0)); |
145 |
|
assertSame(two, ai.get(mark)); |
146 |
|
assertEquals(0, mark[0]); |
147 |
|
|
148 |
< |
while (!ai.weakCompareAndSet(two, m3, 0, 1)); |
148 |
> |
do {} while (!ai.weakCompareAndSet(two, m3, 0, 1)); |
149 |
|
assertSame(m3, ai.get(mark)); |
150 |
|
assertEquals(1, mark[0]); |
151 |
|
} |