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