ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.5
Committed: Sat Oct 25 16:02:13 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +14 -14 lines
Log Message:
Added 	PrivilegedFutureTaskTest

File Contents

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