1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import java.util.concurrent.atomic.*; |
9 |
import junit.framework.*; |
10 |
import java.util.*; |
11 |
|
12 |
public class AtomicReferenceFieldUpdaterTest extends TestCase{ |
13 |
volatile Integer x = null; |
14 |
Object z; |
15 |
Integer w; |
16 |
|
17 |
public static void main(String[] args){ |
18 |
junit.textui.TestRunner.run(suite()); |
19 |
} |
20 |
|
21 |
|
22 |
public static Test suite() { |
23 |
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
24 |
} |
25 |
|
26 |
static final Integer zero = new Integer(0); |
27 |
static final Integer one = new Integer(1); |
28 |
static final Integer two = new Integer(2); |
29 |
static final Integer m3 = new Integer(-3); |
30 |
static final Integer m4 = new Integer(-4); |
31 |
static final Integer m5 = new Integer(-5); |
32 |
static final Integer seven = new Integer(7); |
33 |
static final Integer m10 = new Integer(-10); |
34 |
|
35 |
public void testConstructor(){ |
36 |
try{ |
37 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
38 |
a = AtomicReferenceFieldUpdater.newUpdater |
39 |
(getClass(), Integer.class, "y"); |
40 |
fail("Exception not thrown"); |
41 |
} |
42 |
|
43 |
catch (RuntimeException rt) {} |
44 |
} |
45 |
|
46 |
|
47 |
public void testConstructor2(){ |
48 |
try{ |
49 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
50 |
a = AtomicReferenceFieldUpdater.newUpdater |
51 |
(getClass(), Integer.class, "z"); |
52 |
fail("Exception not thrown"); |
53 |
} |
54 |
|
55 |
catch (RuntimeException rt) {} |
56 |
} |
57 |
|
58 |
public void testConstructor3(){ |
59 |
try{ |
60 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
61 |
a = AtomicReferenceFieldUpdater.newUpdater |
62 |
(getClass(), Integer.class, "w"); |
63 |
fail("Exception not thrown"); |
64 |
} |
65 |
|
66 |
catch (RuntimeException rt) {} |
67 |
} |
68 |
|
69 |
public void testGetSet(){ |
70 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
71 |
x = one; |
72 |
assertEquals(one,a.get(this)); |
73 |
a.set(this,two); |
74 |
assertEquals(two,a.get(this)); |
75 |
a.set(this,-3); |
76 |
assertEquals(-3,a.get(this)); |
77 |
|
78 |
} |
79 |
public void testCompareAndSet(){ |
80 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
81 |
x = one; |
82 |
assertTrue(a.compareAndSet(this,one,two)); |
83 |
assertTrue(a.compareAndSet(this,two,m4)); |
84 |
assertEquals(m4,a.get(this)); |
85 |
assertFalse(a.compareAndSet(this,m5,seven)); |
86 |
assertFalse((seven == a.get(this))); |
87 |
assertTrue(a.compareAndSet(this,m4,seven)); |
88 |
assertEquals(seven,a.get(this)); |
89 |
} |
90 |
|
91 |
public void testWeakCompareAndSet(){ |
92 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
93 |
x = one; |
94 |
while(!a.weakCompareAndSet(this,one,two)); |
95 |
while(!a.weakCompareAndSet(this,two,m4)); |
96 |
assertEquals(m4,a.get(this)); |
97 |
while(!a.weakCompareAndSet(this,m4,seven)); |
98 |
assertEquals(seven,a.get(this)); |
99 |
} |
100 |
|
101 |
public void testGetAndSet(){ |
102 |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); |
103 |
x = one; |
104 |
assertEquals(one,a.getAndSet(this, zero)); |
105 |
assertEquals(zero,a.getAndSet(this,m10)); |
106 |
assertEquals(m10,a.getAndSet(this,1)); |
107 |
} |
108 |
|
109 |
} |