ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
Revision: 1.4
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +52 -15 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

# Content
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 int w;
15 long z;
16 public static void main(String[] args){
17 junit.textui.TestRunner.run(suite());
18 }
19 public static Test suite() {
20 return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
21 }
22
23 /**
24 * Contruction with non-existent field throws RuntimeException
25 */
26 public void testConstructor() {
27 try{
28 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
29 a = AtomicIntegerFieldUpdater.newUpdater
30 (getClass(), "y");
31 shouldThrow();
32 }
33 catch (RuntimeException rt) {}
34 }
35
36 /**
37 * construction with field not of given type throws RuntimeException
38 */
39 public void testConstructor2() {
40 try{
41 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
42 a = AtomicIntegerFieldUpdater.newUpdater
43 (getClass(), "z");
44 shouldThrow();
45 }
46 catch (RuntimeException rt) {}
47 }
48
49 /**
50 * construction with non-volatile field throws RuntimeException
51 */
52 public void testConstructor3() {
53 try{
54 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
55 a = AtomicIntegerFieldUpdater.newUpdater
56 (getClass(), "w");
57 shouldThrow();
58 }
59 catch (RuntimeException rt) {}
60 }
61
62 /**
63 * get returns the last value set or assigned
64 */
65 public void testGetSet() {
66 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
67 x = 1;
68 assertEquals(1,a.get(this));
69 a.set(this,2);
70 assertEquals(2,a.get(this));
71 a.set(this,-3);
72 assertEquals(-3,a.get(this));
73
74 }
75 /**
76 * compareAndSet succeeds in changing value if equal to expected else fails
77 */
78 public void testCompareAndSet() {
79 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
80 x = 1;
81 assertTrue(a.compareAndSet(this,1,2));
82 assertTrue(a.compareAndSet(this,2,-4));
83 assertEquals(-4,a.get(this));
84 assertFalse(a.compareAndSet(this,-5,7));
85 assertFalse((7 == a.get(this)));
86 assertTrue(a.compareAndSet(this,-4,7));
87 assertEquals(7,a.get(this));
88 }
89
90
91 /**
92 * compareAndSet in one thread enables another waiting for value
93 * to succeed
94 */
95 public void testCompareAndSetInMultipleThreads() {
96 x = 1;
97 final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
98
99 Thread t = new Thread(new Runnable() {
100 public void run() {
101 while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
102 }});
103 try {
104 t.start();
105 assertTrue(a.compareAndSet(this, 1, 2));
106 t.join(LONG_DELAY_MS);
107 assertFalse(t.isAlive());
108 assertEquals(a.get(this), 3);
109 }
110 catch(Exception e) {
111 unexpectedException();
112 }
113 }
114
115 /**
116 * repeated weakCompareAndSet succeeds in changing value when equal
117 * to expected
118 */
119 public void testWeakCompareAndSet() {
120 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
121 x = 1;
122 while(!a.weakCompareAndSet(this,1,2));
123 while(!a.weakCompareAndSet(this,2,-4));
124 assertEquals(-4,a.get(this));
125 while(!a.weakCompareAndSet(this,-4,7));
126 assertEquals(7,a.get(this));
127 }
128
129 /**
130 * getAndSet returns previous value and sets to given value
131 */
132 public void testGetAndSet() {
133 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
134 x = 1;
135 assertEquals(1,a.getAndSet(this, 0));
136 assertEquals(0,a.getAndSet(this,-10));
137 assertEquals(-10,a.getAndSet(this,1));
138 }
139
140 /**
141 * getAndAdd returns previous value and adds given value
142 */
143 public void testGetAndAdd() {
144 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
145 x = 1;
146 assertEquals(1,a.getAndAdd(this,2));
147 assertEquals(3,a.get(this));
148 assertEquals(3,a.getAndAdd(this,-4));
149 assertEquals(-1,a.get(this));
150 }
151
152 /**
153 * getAndDecrement returns previous value and decrements
154 */
155 public void testGetAndDecrement() {
156 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
157 x = 1;
158 assertEquals(1,a.getAndDecrement(this));
159 assertEquals(0,a.getAndDecrement(this));
160 assertEquals(-1,a.getAndDecrement(this));
161 }
162
163 /**
164 * getAndIncrement returns previous value and increments
165 */
166 public void testGetAndIncrement() {
167 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
168 x = 1;
169 assertEquals(1,a.getAndIncrement(this));
170 assertEquals(2,a.get(this));
171 a.set(this,-2);
172 assertEquals(-2,a.getAndIncrement(this));
173 assertEquals(-1,a.getAndIncrement(this));
174 assertEquals(0,a.getAndIncrement(this));
175 assertEquals(1,a.get(this));
176 }
177
178 /**
179 * addAndGet adds given value to current, and returns current value
180 */
181 public void testAddAndGet() {
182 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
183 x = 1;
184 assertEquals(3,a.addAndGet(this,2));
185 assertEquals(3,a.get(this));
186 assertEquals(-1,a.addAndGet(this,-4));
187 assertEquals(-1,a.get(this));
188 }
189
190 /**
191 * decrementAndGet decrements and returns current value
192 */
193 public void testDecrementAndGet() {
194 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
195 x = 1;
196 assertEquals(0,a.decrementAndGet(this));
197 assertEquals(-1,a.decrementAndGet(this));
198 assertEquals(-2,a.decrementAndGet(this));
199 assertEquals(-2,a.get(this));
200 }
201
202 /**
203 * incrementAndGet increments and returns current value
204 */
205 public void testIncrementAndGet() {
206 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
207 x = 1;
208 assertEquals(2,a.incrementAndGet(this));
209 assertEquals(2,a.get(this));
210 a.set(this,-2);
211 assertEquals(-1,a.incrementAndGet(this));
212 assertEquals(0,a.incrementAndGet(this));
213 assertEquals(1,a.incrementAndGet(this));
214 assertEquals(1,a.get(this));
215 }
216
217 }