ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.6 by dl, Sat Dec 27 19:26:43 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines