[cvs] / jsr166 / src / test / tck / AtomicLongTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/AtomicLongTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.17, Sun Nov 22 18:55:56 2009 UTC revision 1.30, Thu Jun 16 23:35:25 2016 UTC
# Line 1  Line 1 
1  /*  /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain   * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.   * Pat Fisher, Mike Judd.
7   */   */
8    
9  import junit.framework.*;  import java.util.concurrent.atomic.AtomicLong;
10  import java.util.concurrent.atomic.*;  
11  import java.io.*;  import junit.framework.Test;
12    import junit.framework.TestSuite;
13    
14  public class AtomicLongTest extends JSR166TestCase {  public class AtomicLongTest extends JSR166TestCase {
15      public static void main (String[] args) {      public static void main (String[] args) {
16          junit.textui.TestRunner.run (suite());          main(suite(), args);
17      }      }
18      public static Test suite() {      public static Test suite() {
19          return new TestSuite(AtomicLongTest.class);          return new TestSuite(AtomicLongTest.class);
# Line 73  Line 74 
74          assertTrue(ai.compareAndSet(2,-4));          assertTrue(ai.compareAndSet(2,-4));
75          assertEquals(-4,ai.get());          assertEquals(-4,ai.get());
76          assertFalse(ai.compareAndSet(-5,7));          assertFalse(ai.compareAndSet(-5,7));
77          assertFalse((7 == ai.get()));          assertEquals(-4, ai.get());
78          assertTrue(ai.compareAndSet(-4,7));          assertTrue(ai.compareAndSet(-4,7));
79          assertEquals(7,ai.get());          assertEquals(7,ai.get());
80      }      }
# Line 94  Line 95 
95          assertTrue(ai.compareAndSet(1, 2));          assertTrue(ai.compareAndSet(1, 2));
96          t.join(LONG_DELAY_MS);          t.join(LONG_DELAY_MS);
97          assertFalse(t.isAlive());          assertFalse(t.isAlive());
98          assertEquals(ai.get(), 3);          assertEquals(3, ai.get());
99      }      }
100    
101      /**      /**
# Line 103  Line 104 
104       */       */
105      public void testWeakCompareAndSet() {      public void testWeakCompareAndSet() {
106          AtomicLong ai = new AtomicLong(1);          AtomicLong ai = new AtomicLong(1);
107          while (!ai.weakCompareAndSet(1,2));          do {} while (!ai.weakCompareAndSet(1, 2));
108          while (!ai.weakCompareAndSet(2,-4));          do {} while (!ai.weakCompareAndSet(2, -4));
109          assertEquals(-4,ai.get());          assertEquals(-4,ai.get());
110          while (!ai.weakCompareAndSet(-4,7));          do {} while (!ai.weakCompareAndSet(-4, 7));
111          assertEquals(7,ai.get());          assertEquals(7,ai.get());
112      }      }
113    
# Line 195  Line 196 
196       * a deserialized serialized atomic holds same value       * a deserialized serialized atomic holds same value
197       */       */
198      public void testSerialization() throws Exception {      public void testSerialization() throws Exception {
199          AtomicLong l = new AtomicLong();          AtomicLong x = new AtomicLong();
200            AtomicLong y = serialClone(x);
201          l.set(-22);          assertNotSame(x, y);
202          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);          x.set(-22);
203          ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));          AtomicLong z = serialClone(x);
204          out.writeObject(l);          assertNotSame(y, z);
205          out.close();          assertEquals(-22, x.get());
206            assertEquals(0, y.get());
207          ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());          assertEquals(-22, z.get());
         ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));  
         AtomicLong r = (AtomicLong) in.readObject();  
         assertEquals(l.get(), r.get());  
208      }      }
209    
210      /**      /**
# Line 215  Line 213 
213      public void testToString() {      public void testToString() {
214          AtomicLong ai = new AtomicLong();          AtomicLong ai = new AtomicLong();
215          assertEquals("0", ai.toString());          assertEquals("0", ai.toString());
216          for (long i : VALUES) {          for (long x : VALUES) {
217              ai.set(i);              ai.set(x);
218              assertEquals(ai.toString(), Long.toString(i));              assertEquals(Long.toString(x), ai.toString());
219          }          }
220      }      }
221    
# Line 241  Line 239 
239          assertEquals(0L, ai.longValue());          assertEquals(0L, ai.longValue());
240          for (long x : VALUES) {          for (long x : VALUES) {
241              ai.set(x);              ai.set(x);
242              assertEquals((long)x, ai.longValue());              assertEquals(x, ai.longValue());
243          }          }
244      }      }
245    
# Line 269  Line 267 
267          }          }
268      }      }
269    
270        // jdk9
271    
272        /**
273         * getPlain returns the last value set
274         */
275        public void testGetPlainSet() {
276            AtomicLong ai = new AtomicLong(1);
277            assertEquals(1, ai.getPlain());
278            ai.set(2);
279            assertEquals(2, ai.getPlain());
280            ai.set(-3);
281            assertEquals(-3, ai.getPlain());
282        }
283    
284        /**
285         * getOpaque returns the last value set
286         */
287        public void testGetOpaqueSet() {
288            AtomicLong ai = new AtomicLong(1);
289            assertEquals(1, ai.getOpaque());
290            ai.set(2);
291            assertEquals(2, ai.getOpaque());
292            ai.set(-3);
293            assertEquals(-3, ai.getOpaque());
294        }
295    
296        /**
297         * getAcquire returns the last value set
298         */
299        public void testGetAcquireSet() {
300            AtomicLong ai = new AtomicLong(1);
301            assertEquals(1, ai.getAcquire());
302            ai.set(2);
303            assertEquals(2, ai.getAcquire());
304            ai.set(-3);
305            assertEquals(-3, ai.getAcquire());
306        }
307    
308        /**
309         * get returns the last value setPlain
310         */
311        public void testGetSetPlain() {
312            AtomicLong ai = new AtomicLong(1);
313            assertEquals(1, ai.get());
314            ai.setPlain(2);
315            assertEquals(2, ai.get());
316            ai.setPlain(-3);
317            assertEquals(-3, ai.get());
318        }
319    
320        /**
321         * get returns the last value setOpaque
322         */
323        public void testGetSetOpaque() {
324            AtomicLong ai = new AtomicLong(1);
325            assertEquals(1, ai.get());
326            ai.setOpaque(2);
327            assertEquals(2, ai.get());
328            ai.setOpaque(-3);
329            assertEquals(-3, ai.get());
330        }
331    
332        /**
333         * get returns the last value setRelease
334         */
335        public void testGetSetRelease() {
336            AtomicLong ai = new AtomicLong(1);
337            assertEquals(1, ai.get());
338            ai.setRelease(2);
339            assertEquals(2, ai.get());
340            ai.setRelease(-3);
341            assertEquals(-3, ai.get());
342        }
343    
344        /**
345         * compareAndExchange succeeds in changing value if equal to
346         * expected else fails
347         */
348        public void testCompareAndExchange() {
349            AtomicLong ai = new AtomicLong(1);
350            assertEquals(1, ai.compareAndExchange(1, 2));
351            assertEquals(2, ai.compareAndExchange(2, -4));
352            assertEquals(-4, ai.get());
353            assertEquals(-4, ai.compareAndExchange(-5, 7));
354            assertEquals(-4, ai.get());
355            assertEquals(-4, ai.compareAndExchange(-4, 7));
356            assertEquals(7, ai.get());
357        }
358    
359        /**
360         * compareAndExchangeAcquire succeeds in changing value if equal to
361         * expected else fails
362         */
363        public void testCompareAndExchangeAcquire() {
364            AtomicLong ai = new AtomicLong(1);
365            assertEquals(1, ai.compareAndExchangeAcquire(1, 2));
366            assertEquals(2, ai.compareAndExchangeAcquire(2, -4));
367            assertEquals(-4, ai.get());
368            assertEquals(-4, ai.compareAndExchangeAcquire(-5, 7));
369            assertEquals(-4, ai.get());
370            assertEquals(-4, ai.compareAndExchangeAcquire(-4, 7));
371            assertEquals(7, ai.get());
372        }
373    
374        /**
375         * compareAndExchangeRelease succeeds in changing value if equal to
376         * expected else fails
377         */
378        public void testCompareAndExchangeRelease() {
379            AtomicLong ai = new AtomicLong(1);
380            assertEquals(1, ai.compareAndExchangeRelease(1, 2));
381            assertEquals(2, ai.compareAndExchangeRelease(2, -4));
382            assertEquals(-4, ai.get());
383            assertEquals(-4, ai.compareAndExchangeRelease(-5, 7));
384            assertEquals(-4, ai.get());
385            assertEquals(-4, ai.compareAndExchangeRelease(-4, 7));
386            assertEquals(7, ai.get());
387        }
388    
389        /**
390         * repeated weakCompareAndSetVolatile succeeds in changing value when equal
391         * to expected
392         */
393        public void testWeakCompareAndSetVolatile() {
394            AtomicLong ai = new AtomicLong(1);
395            do {} while (!ai.weakCompareAndSetVolatile(1, 2));
396            do {} while (!ai.weakCompareAndSetVolatile(2, -4));
397            assertEquals(-4, ai.get());
398            do {} while (!ai.weakCompareAndSetVolatile(-4, 7));
399            assertEquals(7, ai.get());
400        }
401    
402        /**
403         * repeated weakCompareAndSetAcquire succeeds in changing value when equal
404         * to expected
405         */
406        public void testWeakCompareAndSetAcquire() {
407            AtomicLong ai = new AtomicLong(1);
408            do {} while (!ai.weakCompareAndSetAcquire(1, 2));
409            do {} while (!ai.weakCompareAndSetAcquire(2, -4));
410            assertEquals(-4, ai.get());
411            do {} while (!ai.weakCompareAndSetAcquire(-4, 7));
412            assertEquals(7, ai.get());
413        }
414    
415        /**
416         * repeated weakCompareAndSetRelease succeeds in changing value when equal
417         * to expected
418         */
419        public void testWeakCompareAndSetRelease() {
420            AtomicLong ai = new AtomicLong(1);
421            do {} while (!ai.weakCompareAndSetRelease(1, 2));
422            do {} while (!ai.weakCompareAndSetRelease(2, -4));
423            assertEquals(-4, ai.get());
424            do {} while (!ai.weakCompareAndSetRelease(-4, 7));
425            assertEquals(7, ai.get());
426        }
427    
428  }  }

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.30

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8