ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.6
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.5: +5 -4 lines
Log Message:
Headers reference Creative Commons

File Contents

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