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 AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
volatile int x = 0; |
14 |
long z; |
15 |
|
16 |
public static void main(String[] args){ |
17 |
junit.textui.TestRunner.run(suite()); |
18 |
} |
19 |
|
20 |
|
21 |
public static Test suite() { |
22 |
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
23 |
} |
24 |
|
25 |
public void testConstructor(){ |
26 |
try{ |
27 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
28 |
a = AtomicIntegerFieldUpdater.newUpdater |
29 |
(getClass(), "y"); |
30 |
fail("Exception not thrown"); |
31 |
} |
32 |
catch (RuntimeException rt) {} |
33 |
} |
34 |
|
35 |
public void testConstructor2(){ |
36 |
try{ |
37 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
38 |
a = AtomicIntegerFieldUpdater.newUpdater |
39 |
(getClass(), "z"); |
40 |
fail("Exception not thrown"); |
41 |
} |
42 |
catch (RuntimeException rt) {} |
43 |
} |
44 |
|
45 |
public void testGetSet(){ |
46 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
47 |
x = 1; |
48 |
assertEquals(1,a.get(this)); |
49 |
a.set(this,2); |
50 |
assertEquals(2,a.get(this)); |
51 |
a.set(this,-3); |
52 |
assertEquals(-3,a.get(this)); |
53 |
|
54 |
} |
55 |
public void testCompareAndSet(){ |
56 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
57 |
x = 1; |
58 |
assertTrue(a.compareAndSet(this,1,2)); |
59 |
assertTrue(a.compareAndSet(this,2,-4)); |
60 |
assertEquals(-4,a.get(this)); |
61 |
assertFalse(a.compareAndSet(this,-5,7)); |
62 |
assertFalse((7 == a.get(this))); |
63 |
assertTrue(a.compareAndSet(this,-4,7)); |
64 |
assertEquals(7,a.get(this)); |
65 |
} |
66 |
|
67 |
public void testWeakCompareAndSet(){ |
68 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
69 |
x = 1; |
70 |
while(!a.weakCompareAndSet(this,1,2)); |
71 |
while(!a.weakCompareAndSet(this,2,-4)); |
72 |
assertEquals(-4,a.get(this)); |
73 |
while(!a.weakCompareAndSet(this,-4,7)); |
74 |
assertEquals(7,a.get(this)); |
75 |
} |
76 |
|
77 |
public void testGetAndSet(){ |
78 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
79 |
x = 1; |
80 |
assertEquals(1,a.getAndSet(this, 0)); |
81 |
assertEquals(0,a.getAndSet(this,-10)); |
82 |
assertEquals(-10,a.getAndSet(this,1)); |
83 |
} |
84 |
|
85 |
public void testGetAndAdd(){ |
86 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
87 |
x = 1; |
88 |
assertEquals(1,a.getAndAdd(this,2)); |
89 |
assertEquals(3,a.get(this)); |
90 |
assertEquals(3,a.getAndAdd(this,-4)); |
91 |
assertEquals(-1,a.get(this)); |
92 |
} |
93 |
|
94 |
public void testGetAndDecrement(){ |
95 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
96 |
x = 1; |
97 |
assertEquals(1,a.getAndDecrement(this)); |
98 |
assertEquals(0,a.getAndDecrement(this)); |
99 |
assertEquals(-1,a.getAndDecrement(this)); |
100 |
} |
101 |
|
102 |
public void testGetAndIncrement(){ |
103 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
104 |
x = 1; |
105 |
assertEquals(1,a.getAndIncrement(this)); |
106 |
assertEquals(2,a.get(this)); |
107 |
a.set(this,-2); |
108 |
assertEquals(-2,a.getAndIncrement(this)); |
109 |
assertEquals(-1,a.getAndIncrement(this)); |
110 |
assertEquals(0,a.getAndIncrement(this)); |
111 |
assertEquals(1,a.get(this)); |
112 |
} |
113 |
|
114 |
public void testAddAndGet(){ |
115 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
116 |
x = 1; |
117 |
assertEquals(3,a.addAndGet(this,2)); |
118 |
assertEquals(3,a.get(this)); |
119 |
assertEquals(-1,a.addAndGet(this,-4)); |
120 |
assertEquals(-1,a.get(this)); |
121 |
} |
122 |
|
123 |
public void testDecrementAndGet(){ |
124 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
125 |
x = 1; |
126 |
assertEquals(0,a.decrementAndGet(this)); |
127 |
assertEquals(-1,a.decrementAndGet(this)); |
128 |
assertEquals(-2,a.decrementAndGet(this)); |
129 |
assertEquals(-2,a.get(this)); |
130 |
} |
131 |
|
132 |
public void testIncrementAndGet(){ |
133 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
134 |
x = 1; |
135 |
assertEquals(2,a.incrementAndGet(this)); |
136 |
assertEquals(2,a.get(this)); |
137 |
a.set(this,-2); |
138 |
assertEquals(-1,a.incrementAndGet(this)); |
139 |
assertEquals(0,a.incrementAndGet(this)); |
140 |
assertEquals(1,a.incrementAndGet(this)); |
141 |
assertEquals(1,a.get(this)); |
142 |
} |
143 |
|
144 |
} |