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 |
/** |
26 |
* |
27 |
*/ |
28 |
public void testConstructor() { |
29 |
try{ |
30 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
31 |
a = AtomicIntegerFieldUpdater.newUpdater |
32 |
(getClass(), "y"); |
33 |
shouldThrow(); |
34 |
} |
35 |
catch (RuntimeException rt) {} |
36 |
} |
37 |
|
38 |
/** |
39 |
* |
40 |
*/ |
41 |
public void testConstructor2() { |
42 |
try{ |
43 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
44 |
a = AtomicIntegerFieldUpdater.newUpdater |
45 |
(getClass(), "z"); |
46 |
shouldThrow(); |
47 |
} |
48 |
catch (RuntimeException rt) {} |
49 |
} |
50 |
|
51 |
/** |
52 |
* |
53 |
*/ |
54 |
public void testGetSet() { |
55 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
56 |
x = 1; |
57 |
assertEquals(1,a.get(this)); |
58 |
a.set(this,2); |
59 |
assertEquals(2,a.get(this)); |
60 |
a.set(this,-3); |
61 |
assertEquals(-3,a.get(this)); |
62 |
|
63 |
} |
64 |
/** |
65 |
* |
66 |
*/ |
67 |
public void testCompareAndSet() { |
68 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
69 |
x = 1; |
70 |
assertTrue(a.compareAndSet(this,1,2)); |
71 |
assertTrue(a.compareAndSet(this,2,-4)); |
72 |
assertEquals(-4,a.get(this)); |
73 |
assertFalse(a.compareAndSet(this,-5,7)); |
74 |
assertFalse((7 == a.get(this))); |
75 |
assertTrue(a.compareAndSet(this,-4,7)); |
76 |
assertEquals(7,a.get(this)); |
77 |
} |
78 |
|
79 |
/** |
80 |
* |
81 |
*/ |
82 |
public void testWeakCompareAndSet() { |
83 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
84 |
x = 1; |
85 |
while(!a.weakCompareAndSet(this,1,2)); |
86 |
while(!a.weakCompareAndSet(this,2,-4)); |
87 |
assertEquals(-4,a.get(this)); |
88 |
while(!a.weakCompareAndSet(this,-4,7)); |
89 |
assertEquals(7,a.get(this)); |
90 |
} |
91 |
|
92 |
/** |
93 |
* |
94 |
*/ |
95 |
public void testGetAndSet() { |
96 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
97 |
x = 1; |
98 |
assertEquals(1,a.getAndSet(this, 0)); |
99 |
assertEquals(0,a.getAndSet(this,-10)); |
100 |
assertEquals(-10,a.getAndSet(this,1)); |
101 |
} |
102 |
|
103 |
/** |
104 |
* |
105 |
*/ |
106 |
public void testGetAndAdd() { |
107 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
108 |
x = 1; |
109 |
assertEquals(1,a.getAndAdd(this,2)); |
110 |
assertEquals(3,a.get(this)); |
111 |
assertEquals(3,a.getAndAdd(this,-4)); |
112 |
assertEquals(-1,a.get(this)); |
113 |
} |
114 |
|
115 |
/** |
116 |
* |
117 |
*/ |
118 |
public void testGetAndDecrement() { |
119 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
120 |
x = 1; |
121 |
assertEquals(1,a.getAndDecrement(this)); |
122 |
assertEquals(0,a.getAndDecrement(this)); |
123 |
assertEquals(-1,a.getAndDecrement(this)); |
124 |
} |
125 |
|
126 |
/** |
127 |
* |
128 |
*/ |
129 |
public void testGetAndIncrement() { |
130 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
131 |
x = 1; |
132 |
assertEquals(1,a.getAndIncrement(this)); |
133 |
assertEquals(2,a.get(this)); |
134 |
a.set(this,-2); |
135 |
assertEquals(-2,a.getAndIncrement(this)); |
136 |
assertEquals(-1,a.getAndIncrement(this)); |
137 |
assertEquals(0,a.getAndIncrement(this)); |
138 |
assertEquals(1,a.get(this)); |
139 |
} |
140 |
|
141 |
/** |
142 |
* |
143 |
*/ |
144 |
public void testAddAndGet() { |
145 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
146 |
x = 1; |
147 |
assertEquals(3,a.addAndGet(this,2)); |
148 |
assertEquals(3,a.get(this)); |
149 |
assertEquals(-1,a.addAndGet(this,-4)); |
150 |
assertEquals(-1,a.get(this)); |
151 |
} |
152 |
|
153 |
/** |
154 |
* |
155 |
*/ |
156 |
public void testDecrementAndGet() { |
157 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
158 |
x = 1; |
159 |
assertEquals(0,a.decrementAndGet(this)); |
160 |
assertEquals(-1,a.decrementAndGet(this)); |
161 |
assertEquals(-2,a.decrementAndGet(this)); |
162 |
assertEquals(-2,a.get(this)); |
163 |
} |
164 |
|
165 |
/** |
166 |
* |
167 |
*/ |
168 |
public void testIncrementAndGet() { |
169 |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
170 |
x = 1; |
171 |
assertEquals(2,a.incrementAndGet(this)); |
172 |
assertEquals(2,a.get(this)); |
173 |
a.set(this,-2); |
174 |
assertEquals(-1,a.incrementAndGet(this)); |
175 |
assertEquals(0,a.incrementAndGet(this)); |
176 |
assertEquals(1,a.incrementAndGet(this)); |
177 |
assertEquals(1,a.get(this)); |
178 |
} |
179 |
|
180 |
} |