Repository navigation
Expand file tree
/
Copy pathAtomicBooleanTest.java
More file actions
108 lines (89 loc) · 2.82 KB
/
Copy pathAtomicBooleanTest.java
File metadata and controls
108 lines (89 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.mapdb;/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.TestCase;
public class AtomicBooleanTest extends TestCase{
DB db;
Atomic.Boolean ai;
@Override
protected void setUp() throws Exception {
db = DBMaker.newMemoryDB().transactionDisable().make();
ai= db.createAtomicBoolean("test", true);;
}
@Override
protected void tearDown() throws Exception {
db.close();
}
/**
* constructor initializes to given value
*/
public void testConstructor() {
assertEquals(true,ai.get());
}
/**
* default constructed initializes to false
*/
public void testConstructor2() {
Atomic.Boolean ai = db.getAtomicBoolean("test2");
assertEquals(false,ai.get());
}
/**
* get returns the last value set
*/
public void testGetSet() {
assertEquals(true,ai.get());
ai.set(false);
assertEquals(false,ai.get());
ai.set(true);
assertEquals(true,ai.get());
}
/**
* compareAndSet succeeds in changing value if equal to expected else fails
*/
public void testCompareAndSet() {
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());
}
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws InterruptedException {
Thread t = new Thread(new Runnable() {
public void run() {
while(!ai.compareAndSet(false, true)) Thread.yield();
}});
t.start();
assertTrue(ai.compareAndSet(true, false));
t.join(0);
assertFalse(t.isAlive());
}
/**
* getAndSet returns previous value and sets to given value
*/
public void testGetAndSet() {
assertEquals(true,ai.getAndSet(false));
assertEquals(false,ai.getAndSet(false));
assertEquals(false,ai.getAndSet(true));
assertEquals(true,ai.get());
}
/**
* toString returns current value.
*/
public void testToString() {
Atomic.Boolean ai = db.getAtomicBoolean( "test2");
assertEquals(ai.toString(), Boolean.toString(false));
ai.set(true);
assertEquals(ai.toString(), Boolean.toString(true));
}
}