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.19 by jsr166, Tue Mar 15 19:47:06 2011 UTC vs.
Revision 1.28 by jsr166, Wed Dec 31 19:21:20 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import java.util.concurrent.atomic.*;
10 < import junit.framework.*;
11 < import java.util.*;
9 > import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
15      volatile int x = 0;
# Line 21 | Line 22 | public class AtomicIntegerFieldUpdaterTe
22          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
23      }
24  
25 +    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
26 +        return AtomicIntegerFieldUpdater.newUpdater
27 +            (AtomicIntegerFieldUpdaterTest.class, fieldName);
28 +    }
29 +
30      /**
31       * Construction with non-existent field throws RuntimeException
32       */
33      public void testConstructor() {
34          try {
35 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30 <                a = AtomicIntegerFieldUpdater.newUpdater
31 <                (AtomicIntegerFieldUpdaterTest.class, "y");
35 >            updaterFor("y");
36              shouldThrow();
37 <        } catch (RuntimeException success) {}
37 >        } catch (RuntimeException success) {
38 >            assertNotNull(success.getCause());
39 >        }
40      }
41  
42      /**
43 <     * construction with field not of given type throws RuntimeException
43 >     * construction with field not of given type throws IllegalArgumentException
44       */
45      public void testConstructor2() {
46          try {
47 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
42 <                a = AtomicIntegerFieldUpdater.newUpdater
43 <                (AtomicIntegerFieldUpdaterTest.class, "z");
47 >            updaterFor("z");
48              shouldThrow();
49 <        } catch (RuntimeException success) {}
49 >        } catch (IllegalArgumentException success) {}
50      }
51  
52      /**
53 <     * construction with non-volatile field throws RuntimeException
53 >     * construction with non-volatile field throws IllegalArgumentException
54       */
55      public void testConstructor3() {
56          try {
57 <            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
54 <                a = AtomicIntegerFieldUpdater.newUpdater
55 <                (AtomicIntegerFieldUpdaterTest.class, "w");
57 >            updaterFor("w");
58              shouldThrow();
59 <        } catch (RuntimeException success) {}
59 >        } catch (IllegalArgumentException success) {}
60      }
61  
62      /**
# Line 62 | Line 64 | public class AtomicIntegerFieldUpdaterTe
64       */
65      public void testGetSet() {
66          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
67 <        try {
66 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
67 >        a = updaterFor("x");
68          x = 1;
69 <        assertEquals(1,a.get(this));
70 <        a.set(this,2);
71 <        assertEquals(2,a.get(this));
72 <        a.set(this,-3);
73 <        assertEquals(-3,a.get(this));
69 >        assertEquals(1, a.get(this));
70 >        a.set(this, 2);
71 >        assertEquals(2, a.get(this));
72 >        a.set(this, -3);
73 >        assertEquals(-3, a.get(this));
74      }
75  
76      /**
# Line 80 | Line 78 | public class AtomicIntegerFieldUpdaterTe
78       */
79      public void testGetLazySet() {
80          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
81 <        try {
84 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
81 >        a = updaterFor("x");
82          x = 1;
83 <        assertEquals(1,a.get(this));
84 <        a.lazySet(this,2);
85 <        assertEquals(2,a.get(this));
86 <        a.lazySet(this,-3);
87 <        assertEquals(-3,a.get(this));
83 >        assertEquals(1, a.get(this));
84 >        a.lazySet(this, 2);
85 >        assertEquals(2, a.get(this));
86 >        a.lazySet(this, -3);
87 >        assertEquals(-3, a.get(this));
88      }
89  
90      /**
# Line 98 | Line 92 | public class AtomicIntegerFieldUpdaterTe
92       */
93      public void testCompareAndSet() {
94          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
95 <        try {
102 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
95 >        a = updaterFor("x");
96          x = 1;
97 <        assertTrue(a.compareAndSet(this,1,2));
98 <        assertTrue(a.compareAndSet(this,2,-4));
99 <        assertEquals(-4,a.get(this));
100 <        assertFalse(a.compareAndSet(this,-5,7));
101 <        assertEquals(-4,a.get(this));
102 <        assertTrue(a.compareAndSet(this,-4,7));
103 <        assertEquals(7,a.get(this));
97 >        assertTrue(a.compareAndSet(this, 1, 2));
98 >        assertTrue(a.compareAndSet(this, 2, -4));
99 >        assertEquals(-4, a.get(this));
100 >        assertFalse(a.compareAndSet(this, -5, 7));
101 >        assertEquals(-4, a.get(this));
102 >        assertTrue(a.compareAndSet(this, -4, 7));
103 >        assertEquals(7, a.get(this));
104      }
105  
116
106      /**
107       * compareAndSet in one thread enables another waiting for value
108       * to succeed
109       */
110      public void testCompareAndSetInMultipleThreads() throws Exception {
111          x = 1;
112 <        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
113 <        try {
125 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
126 <        } catch (RuntimeException ok) {
127 <            return;
128 <        }
112 >        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
113 >        a = updaterFor("x");
114  
115          Thread t = new Thread(new CheckedRunnable() {
116              public void realRun() {
# Line 137 | Line 122 | public class AtomicIntegerFieldUpdaterTe
122          assertTrue(a.compareAndSet(this, 1, 2));
123          t.join(LONG_DELAY_MS);
124          assertFalse(t.isAlive());
125 <        assertEquals(a.get(this), 3);
125 >        assertEquals(3, a.get(this));
126      }
127  
128      /**
# Line 146 | Line 131 | public class AtomicIntegerFieldUpdaterTe
131       */
132      public void testWeakCompareAndSet() {
133          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
134 <        try {
150 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
151 <        } catch (RuntimeException ok) {
152 <            return;
153 <        }
134 >        a = updaterFor("x");
135          x = 1;
136 <        while (!a.weakCompareAndSet(this,1,2));
137 <        while (!a.weakCompareAndSet(this,2,-4));
138 <        assertEquals(-4,a.get(this));
139 <        while (!a.weakCompareAndSet(this,-4,7));
140 <        assertEquals(7,a.get(this));
136 >        do {} while (!a.weakCompareAndSet(this, 1, 2));
137 >        do {} while (!a.weakCompareAndSet(this, 2, -4));
138 >        assertEquals(-4, a.get(this));
139 >        do {} while (!a.weakCompareAndSet(this, -4, 7));
140 >        assertEquals(7, a.get(this));
141      }
142  
143      /**
# Line 164 | Line 145 | public class AtomicIntegerFieldUpdaterTe
145       */
146      public void testGetAndSet() {
147          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
148 <        try {
168 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
169 <        } catch (RuntimeException ok) {
170 <            return;
171 <        }
148 >        a = updaterFor("x");
149          x = 1;
150 <        assertEquals(1,a.getAndSet(this, 0));
151 <        assertEquals(0,a.getAndSet(this,-10));
152 <        assertEquals(-10,a.getAndSet(this,1));
150 >        assertEquals(1, a.getAndSet(this, 0));
151 >        assertEquals(0, a.getAndSet(this, -10));
152 >        assertEquals(-10, a.getAndSet(this, 1));
153      }
154  
155      /**
# Line 180 | Line 157 | public class AtomicIntegerFieldUpdaterTe
157       */
158      public void testGetAndAdd() {
159          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
160 <        try {
184 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
185 <        } catch (RuntimeException ok) {
186 <            return;
187 <        }
160 >        a = updaterFor("x");
161          x = 1;
162 <        assertEquals(1,a.getAndAdd(this,2));
163 <        assertEquals(3,a.get(this));
164 <        assertEquals(3,a.getAndAdd(this,-4));
165 <        assertEquals(-1,a.get(this));
162 >        assertEquals(1, a.getAndAdd(this, 2));
163 >        assertEquals(3, a.get(this));
164 >        assertEquals(3, a.getAndAdd(this, -4));
165 >        assertEquals(-1, a.get(this));
166      }
167  
168      /**
# Line 197 | Line 170 | public class AtomicIntegerFieldUpdaterTe
170       */
171      public void testGetAndDecrement() {
172          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
173 <        try {
201 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
202 <        } catch (RuntimeException ok) {
203 <            return;
204 <        }
173 >        a = updaterFor("x");
174          x = 1;
175 <        assertEquals(1,a.getAndDecrement(this));
176 <        assertEquals(0,a.getAndDecrement(this));
177 <        assertEquals(-1,a.getAndDecrement(this));
175 >        assertEquals(1, a.getAndDecrement(this));
176 >        assertEquals(0, a.getAndDecrement(this));
177 >        assertEquals(-1, a.getAndDecrement(this));
178      }
179  
180      /**
# Line 213 | Line 182 | public class AtomicIntegerFieldUpdaterTe
182       */
183      public void testGetAndIncrement() {
184          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
185 <        try {
217 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
218 <        } catch (RuntimeException ok) {
219 <            return;
220 <        }
185 >        a = updaterFor("x");
186          x = 1;
187 <        assertEquals(1,a.getAndIncrement(this));
188 <        assertEquals(2,a.get(this));
189 <        a.set(this,-2);
190 <        assertEquals(-2,a.getAndIncrement(this));
191 <        assertEquals(-1,a.getAndIncrement(this));
192 <        assertEquals(0,a.getAndIncrement(this));
193 <        assertEquals(1,a.get(this));
187 >        assertEquals(1, a.getAndIncrement(this));
188 >        assertEquals(2, a.get(this));
189 >        a.set(this, -2);
190 >        assertEquals(-2, a.getAndIncrement(this));
191 >        assertEquals(-1, a.getAndIncrement(this));
192 >        assertEquals(0, a.getAndIncrement(this));
193 >        assertEquals(1, a.get(this));
194      }
195  
196      /**
# Line 233 | Line 198 | public class AtomicIntegerFieldUpdaterTe
198       */
199      public void testAddAndGet() {
200          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
201 <        try {
237 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
238 <        } catch (RuntimeException ok) {
239 <            return;
240 <        }
201 >        a = updaterFor("x");
202          x = 1;
203 <        assertEquals(3,a.addAndGet(this,2));
204 <        assertEquals(3,a.get(this));
205 <        assertEquals(-1,a.addAndGet(this,-4));
206 <        assertEquals(-1,a.get(this));
203 >        assertEquals(3, a.addAndGet(this, 2));
204 >        assertEquals(3, a.get(this));
205 >        assertEquals(-1, a.addAndGet(this, -4));
206 >        assertEquals(-1, a.get(this));
207      }
208  
209      /**
# Line 250 | Line 211 | public class AtomicIntegerFieldUpdaterTe
211       */
212      public void testDecrementAndGet() {
213          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
214 <        try {
254 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
255 <        } catch (RuntimeException ok) {
256 <            return;
257 <        }
214 >        a = updaterFor("x");
215          x = 1;
216 <        assertEquals(0,a.decrementAndGet(this));
217 <        assertEquals(-1,a.decrementAndGet(this));
218 <        assertEquals(-2,a.decrementAndGet(this));
219 <        assertEquals(-2,a.get(this));
216 >        assertEquals(0, a.decrementAndGet(this));
217 >        assertEquals(-1, a.decrementAndGet(this));
218 >        assertEquals(-2, a.decrementAndGet(this));
219 >        assertEquals(-2, a.get(this));
220      }
221  
222      /**
# Line 267 | Line 224 | public class AtomicIntegerFieldUpdaterTe
224       */
225      public void testIncrementAndGet() {
226          AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
227 <        try {
271 <            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
272 <        } catch (RuntimeException ok) {
273 <            return;
274 <        }
227 >        a = updaterFor("x");
228          x = 1;
229 <        assertEquals(2,a.incrementAndGet(this));
230 <        assertEquals(2,a.get(this));
231 <        a.set(this,-2);
232 <        assertEquals(-1,a.incrementAndGet(this));
233 <        assertEquals(0,a.incrementAndGet(this));
234 <        assertEquals(1,a.incrementAndGet(this));
235 <        assertEquals(1,a.get(this));
229 >        assertEquals(2, a.incrementAndGet(this));
230 >        assertEquals(2, a.get(this));
231 >        a.set(this, -2);
232 >        assertEquals(-1, a.incrementAndGet(this));
233 >        assertEquals(0, a.incrementAndGet(this));
234 >        assertEquals(1, a.incrementAndGet(this));
235 >        assertEquals(1, a.get(this));
236      }
237  
238   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines