ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.3
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +42 -5 lines
Log Message:
Documentation scaffolding

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