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.5 by dl, Sat Oct 25 16:02:13 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines