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

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

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

revision 1.16, Tue Dec 1 09:56:28 2009 UTC revision 1.28, Fri Jul 29 21:30:37 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.AtomicBoolean;
10  import java.util.concurrent.atomic.*;  
11  import java.io.*;  import junit.framework.Test;
12    import junit.framework.TestSuite;
13    
14  public class AtomicBooleanTest extends JSR166TestCase {  public class AtomicBooleanTest 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(AtomicBooleanTest.class);          return new TestSuite(AtomicBooleanTest.class);
# Line 96  Line 97 
97       */       */
98      public void testWeakCompareAndSet() {      public void testWeakCompareAndSet() {
99          AtomicBoolean ai = new AtomicBoolean(true);          AtomicBoolean ai = new AtomicBoolean(true);
100          while (!ai.weakCompareAndSet(true,false));          do {} while (!ai.weakCompareAndSet(true, false));
101          assertFalse(ai.get());          assertFalse(ai.get());
102          while (!ai.weakCompareAndSet(false,false));          do {} while (!ai.weakCompareAndSet(false, false));
103          assertFalse(ai.get());          assertFalse(ai.get());
104          while (!ai.weakCompareAndSet(false,true));          do {} while (!ai.weakCompareAndSet(false, true));
105          assertTrue(ai.get());          assertTrue(ai.get());
106      }      }
107    
# Line 108  Line 109 
109       * getAndSet returns previous value and sets to given value       * getAndSet returns previous value and sets to given value
110       */       */
111      public void testGetAndSet() {      public void testGetAndSet() {
112          AtomicBoolean ai = new AtomicBoolean(true);          AtomicBoolean ai = new AtomicBoolean();
113          assertEquals(true,ai.getAndSet(false));          boolean[] booleans = { false, true };
114          assertEquals(false,ai.getAndSet(false));          for (boolean before : booleans)
115          assertEquals(false,ai.getAndSet(true));              for (boolean after : booleans) {
116          assertTrue(ai.get());                  ai.set(before);
117                    assertEquals(before, ai.getAndSet(after));
118                    assertEquals(after, ai.get());
119                }
120      }      }
121    
122      /**      /**
123       * a deserialized serialized atomic holds same value       * a deserialized serialized atomic holds same value
124       */       */
125      public void testSerialization() throws Exception {      public void testSerialization() throws Exception {
126          AtomicBoolean l = new AtomicBoolean();          AtomicBoolean x = new AtomicBoolean();
127            AtomicBoolean y = serialClone(x);
128          l.set(true);          x.set(true);
129          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);          AtomicBoolean z = serialClone(x);
130          ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));          assertTrue(x.get());
131          out.writeObject(l);          assertFalse(y.get());
132          out.close();          assertTrue(z.get());
   
         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());  
         ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));  
         AtomicBoolean r = (AtomicBoolean) in.readObject();  
         assertEquals(l.get(), r.get());  
133      }      }
134    
135      /**      /**
# Line 138  Line 137 
137       */       */
138      public void testToString() {      public void testToString() {
139          AtomicBoolean ai = new AtomicBoolean();          AtomicBoolean ai = new AtomicBoolean();
140          assertEquals(ai.toString(), Boolean.toString(false));          assertEquals(Boolean.toString(false), ai.toString());
141          ai.set(true);          ai.set(true);
142          assertEquals(ai.toString(), Boolean.toString(true));          assertEquals(Boolean.toString(true), ai.toString());
143      }      }
144    
145  }  }

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.28

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8