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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.4 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 11 | Line 11 | import java.util.*;
11  
12   public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
13      volatile int x = 0;
14 +    int w;
15      long z;
15
16      public static void main(String[] args){
17          junit.textui.TestRunner.run(suite());
18      }
19
20  
19      public static Test suite() {
20          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
21      }
22  
23      /**
24 <     *
24 >     * Contruction with non-existent field throws RuntimeException
25       */
26      public void testConstructor() {
27          try{
# Line 36 | Line 34 | public class AtomicIntegerFieldUpdaterTe
34      }
35  
36      /**
37 <     *
37 >     * construction with field not of given type throws RuntimeException
38       */
39      public void testConstructor2() {
40          try{
# Line 49 | Line 47 | public class AtomicIntegerFieldUpdaterTe
47      }
48  
49      /**
50 <     *
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");
# Line 62 | Line 73 | public class AtomicIntegerFieldUpdaterTe
73          
74      }
75      /**
76 <     *
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");
# Line 76 | Line 87 | public class AtomicIntegerFieldUpdaterTe
87          assertEquals(7,a.get(this));
88      }
89  
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 <     *
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");
# Line 90 | Line 127 | public class AtomicIntegerFieldUpdaterTe
127      }
128  
129      /**
130 <     *
130 >     *  getAndSet returns previous value and sets to given value
131       */
132      public void testGetAndSet() {
133          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 101 | Line 138 | public class AtomicIntegerFieldUpdaterTe
138      }
139  
140      /**
141 <     *
141 >     * getAndAdd returns previous value and adds given value
142       */
143      public void testGetAndAdd() {
144          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 113 | Line 150 | public class AtomicIntegerFieldUpdaterTe
150      }
151  
152      /**
153 <     *
153 >     * getAndDecrement returns previous value and decrements
154       */
155      public void testGetAndDecrement() {
156          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 124 | Line 161 | public class AtomicIntegerFieldUpdaterTe
161      }
162  
163      /**
164 <     *
164 >     * getAndIncrement returns previous value and increments
165       */
166      public void testGetAndIncrement() {
167          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 139 | Line 176 | public class AtomicIntegerFieldUpdaterTe
176      }
177  
178      /**
179 <     *
179 >     * addAndGet adds given value to current, and returns current value
180       */
181      public void testAddAndGet() {
182          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 151 | Line 188 | public class AtomicIntegerFieldUpdaterTe
188      }
189  
190      /**
191 <     *
191 >     * decrementAndGet decrements and returns current value
192       */
193      public void testDecrementAndGet() {
194          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");
# Line 163 | Line 200 | public class AtomicIntegerFieldUpdaterTe
200      }
201  
202      /**
203 <     *
203 >     * incrementAndGet increments and returns current value
204       */
205      public void testIncrementAndGet() {
206          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines