Methods Summary |
---|
public static void | main(java.lang.String[] args)
junit.textui.TestRunner.run (suite());
|
public static junit.framework.Test | suite()
return new TestSuite(AtomicBooleanTest.class);
|
public void | testCompareAndSet()compareAndSet succeeds in changing value if equal to expected else fails
AtomicBoolean ai = new AtomicBoolean(true);
assertTrue(ai.compareAndSet(true,false));
assertEquals(false,ai.get());
assertTrue(ai.compareAndSet(false,false));
assertEquals(false,ai.get());
assertFalse(ai.compareAndSet(true,false));
assertFalse((ai.get()));
assertTrue(ai.compareAndSet(false,true));
assertEquals(true,ai.get());
|
public void | testCompareAndSetInMultipleThreads()compareAndSet in one thread enables another waiting for value
to succeed
final AtomicBoolean ai = new AtomicBoolean(true);
Thread t = new Thread(new Runnable() {
public void run() {
while(!ai.compareAndSet(false, true)) Thread.yield();
}});
try {
t.start();
assertTrue(ai.compareAndSet(true, false));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
}
catch(Exception e) {
unexpectedException();
}
|
public void | testConstructor()constructor initializes to given value
AtomicBoolean ai = new AtomicBoolean(true);
assertEquals(true,ai.get());
|
public void | testConstructor2()default constructed initializes to false
AtomicBoolean ai = new AtomicBoolean();
assertEquals(false,ai.get());
|
public void | testGetAndSet()getAndSet returns previous value and sets to given value
AtomicBoolean ai = new AtomicBoolean(true);
assertEquals(true,ai.getAndSet(false));
assertEquals(false,ai.getAndSet(false));
assertEquals(false,ai.getAndSet(true));
assertEquals(true,ai.get());
|
public void | testGetSet()get returns the last value set
AtomicBoolean ai = new AtomicBoolean(true);
assertEquals(true,ai.get());
ai.set(false);
assertEquals(false,ai.get());
ai.set(true);
assertEquals(true,ai.get());
|
public void | testSerialization()a deserialized serialized atomic holds same value
AtomicBoolean l = new AtomicBoolean();
try {
l.set(true);
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
out.writeObject(l);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
AtomicBoolean r = (AtomicBoolean) in.readObject();
assertEquals(l.get(), r.get());
} catch(Exception e){
e.printStackTrace();
unexpectedException();
}
|
public void | testToString()toString returns current value.
AtomicBoolean ai = new AtomicBoolean();
assertEquals(ai.toString(), Boolean.toString(false));
ai.set(true);
assertEquals(ai.toString(), Boolean.toString(true));
|
public void | testWeakCompareAndSet()repeated weakCompareAndSet succeeds in changing value when equal
to expected
AtomicBoolean ai = new AtomicBoolean(true);
while(!ai.weakCompareAndSet(true,false));
assertEquals(false,ai.get());
while(!ai.weakCompareAndSet(false,false));
assertEquals(false,ai.get());
while(!ai.weakCompareAndSet(false,true));
assertEquals(true,ai.get());
|