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

Comparing jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java (file contents):
Revision 1.2 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.7 by dl, Mon Dec 29 19:05:40 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.*;
# Line 11 | Line 12 | import java.util.*;
12  
13   public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
14      volatile long x = 0;
15 <    long z;
16 <    int w;
15 >    int z;
16 >    long w;
17  
18      public static void main(String[] args){
19          junit.textui.TestRunner.run(suite());
20      }
20
21  
21      public static Test suite() {
22          return new TestSuite(AtomicLongFieldUpdaterTest.class);
23      }
24  
25 +    /**
26 +     * Construction with non-existent field throws RuntimeException
27 +     */
28      public void testConstructor(){
29          try{
30              AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
31                  a = AtomicLongFieldUpdater.newUpdater
32 <                (getClass(), "y");
33 <            fail("Exception not thrown");
32 >                (AtomicLongFieldUpdaterTest.class, "y");
33 >            shouldThrow();
34          }
33
35          catch (RuntimeException rt) {}
36      }
37  
38 +    /**
39 +     * construction with field not of given type throws RuntimeException
40 +     */
41      public void testConstructor2(){
42          try{
43              AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
44                  a = AtomicLongFieldUpdater.newUpdater
45 <                (getClass(), "z");
46 <            fail("Exception not thrown");
45 >                (AtomicLongFieldUpdaterTest.class, "z");
46 >            shouldThrow();
47          }
44
48          catch (RuntimeException rt) {}
49      }
50  
51 +    /**
52 +     * construction with non-volatile field throws RuntimeException
53 +     */
54      public void testConstructor3(){
55          try{
56              AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
57                  a = AtomicLongFieldUpdater.newUpdater
58 <                (getClass(), "w");
59 <            fail("Exception not thrown");
58 >                (AtomicLongFieldUpdaterTest.class, "w");
59 >            shouldThrow();
60          }
61  
62          catch (RuntimeException rt) {}
63      }
64  
65 +    /**
66 +     *  get returns the last value set or assigned
67 +     */
68      public void testGetSet(){
69 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
69 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
70          x = 1;
71          assertEquals(1,a.get(this));
72          a.set(this,2);
# Line 66 | Line 75 | public class AtomicLongFieldUpdaterTest
75          assertEquals(-3,a.get(this));
76          
77      }
78 +    /**
79 +     * compareAndSet succeeds in changing value if equal to expected else fails
80 +     */
81      public void testCompareAndSet(){
82 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
82 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
83          x = 1;
84          assertTrue(a.compareAndSet(this,1,2));
85          assertTrue(a.compareAndSet(this,2,-4));
# Line 78 | Line 90 | public class AtomicLongFieldUpdaterTest
90          assertEquals(7,a.get(this));
91      }
92  
93 +
94 +    /**
95 +     * compareAndSet in one thread enables another waiting for value
96 +     * to succeed
97 +     */
98 +    public void testCompareAndSetInMultipleThreads() {
99 +        x = 1;
100 +        final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
101 +
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 +    /**
119 +     * repeated weakCompareAndSet succeeds in changing value when equal
120 +     * to expected
121 +     */
122      public void testWeakCompareAndSet(){
123 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
123 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
124          x = 1;
125          while(!a.weakCompareAndSet(this,1,2));
126          while(!a.weakCompareAndSet(this,2,-4));
# Line 88 | Line 129 | public class AtomicLongFieldUpdaterTest
129          assertEquals(7,a.get(this));
130      }
131  
132 +    /**
133 +     *  getAndSet returns previous value and sets to given value
134 +     */
135      public void testGetAndSet(){
136 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
136 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
137          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 +    /**
144 +     * getAndAdd returns previous value and adds given value
145 +     */
146      public void testGetAndAdd(){
147 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
147 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
148          x = 1;
149          assertEquals(1,a.getAndAdd(this,2));
150          assertEquals(3,a.get(this));
# Line 105 | Line 152 | public class AtomicLongFieldUpdaterTest
152          assertEquals(-1,a.get(this));
153      }
154  
155 +    /**
156 +     * getAndDecrement returns previous value and decrements
157 +     */
158      public void testGetAndDecrement(){
159 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
159 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
160          x = 1;
161          assertEquals(1,a.getAndDecrement(this));
162          assertEquals(0,a.getAndDecrement(this));
163          assertEquals(-1,a.getAndDecrement(this));
164      }
165  
166 +    /**
167 +     * getAndIncrement returns previous value and increments
168 +     */
169      public void testGetAndIncrement(){
170 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
170 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
171          x = 1;
172          assertEquals(1,a.getAndIncrement(this));
173          assertEquals(2,a.get(this));
# Line 125 | Line 178 | public class AtomicLongFieldUpdaterTest
178          assertEquals(1,a.get(this));
179      }
180  
181 +    /**
182 +     * addAndGet adds given value to current, and returns current value
183 +     */
184      public void testAddAndGet(){
185 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
185 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
186          x = 1;
187          assertEquals(3,a.addAndGet(this,2));
188          assertEquals(3,a.get(this));
# Line 134 | Line 190 | public class AtomicLongFieldUpdaterTest
190          assertEquals(-1,a.get(this));
191      }
192  
193 +    /**
194 +     *  decrementAndGet decrements and returns current value
195 +     */
196      public void testDecrementAndGet(){
197 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
197 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
198          x = 1;
199          assertEquals(0,a.decrementAndGet(this));
200          assertEquals(-1,a.decrementAndGet(this));
# Line 143 | Line 202 | public class AtomicLongFieldUpdaterTest
202          assertEquals(-2,a.get(this));
203      }
204  
205 +    /**
206 +     * incrementAndGet increments and returns current value
207 +     */
208      public void testIncrementAndGet(){
209 <        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x");
209 >        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
210          x = 1;
211          assertEquals(2,a.incrementAndGet(this));
212          assertEquals(2,a.get(this));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines