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.4 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 9 | Line 9 | import java.util.concurrent.atomic.*;
9   import junit.framework.*;
10   import java.util.*;
11  
12 < public class AtomicIntegerFieldUpdaterTest extends TestCase{
12 > public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
13      volatile int x = 0;
14 <
14 >    int w;
15 >    long z;
16      public static void main(String[] args){
17          junit.textui.TestRunner.run(suite());
18      }
18
19  
19      public static Test suite() {
20          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
21      }
22  
23 <    public void testConstructor(){
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 <            fail("Exception not thrown");
31 >            shouldThrow();
32          }
33          catch (RuntimeException rt) {}
34      }
35  
36 <    public void testGetSet(){
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));
# Line 41 | Line 72 | public class AtomicIntegerFieldUpdaterTe
72          assertEquals(-3,a.get(this));
73          
74      }
75 <    public void testCompareAndSet(){
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));
# Line 53 | Line 87 | public class AtomicIntegerFieldUpdaterTe
87          assertEquals(7,a.get(this));
88      }
89  
90 <    public void testWeakCompareAndSet(){
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));
# Line 63 | Line 126 | public class AtomicIntegerFieldUpdaterTe
126          assertEquals(7,a.get(this));
127      }
128  
129 <    public void testGetAndSet(){
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));
# Line 71 | Line 137 | public class AtomicIntegerFieldUpdaterTe
137          assertEquals(-10,a.getAndSet(this,1));
138      }
139  
140 <    public void testGetAndAdd(){
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));
# Line 80 | Line 149 | public class AtomicIntegerFieldUpdaterTe
149          assertEquals(-1,a.get(this));
150      }
151  
152 <    public void testGetAndDecrement(){
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));
# Line 88 | Line 160 | public class AtomicIntegerFieldUpdaterTe
160          assertEquals(-1,a.getAndDecrement(this));
161      }
162  
163 <    public void testGetAndIncrement(){
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));
# Line 100 | Line 175 | public class AtomicIntegerFieldUpdaterTe
175          assertEquals(1,a.get(this));
176      }
177  
178 <    public void testAddAndGet(){
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));
# Line 109 | Line 187 | public class AtomicIntegerFieldUpdaterTe
187          assertEquals(-1,a.get(this));
188      }
189  
190 <    public void testDecrementAndGet(){
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));
# Line 118 | Line 199 | public class AtomicIntegerFieldUpdaterTe
199          assertEquals(-2,a.get(this));
200      }
201  
202 <    public void testIncrementAndGet(){
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));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines