23 |
|
* constructor initializes to given value |
24 |
|
*/ |
25 |
|
public void testConstructor() { |
26 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
26 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
27 |
|
assertSame(one, ai.get()); |
28 |
|
} |
29 |
|
|
31 |
|
* default constructed initializes to null |
32 |
|
*/ |
33 |
|
public void testConstructor2() { |
34 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(); |
34 |
> |
AtomicReference<Item> ai = new AtomicReference<>(); |
35 |
|
assertNull(ai.get()); |
36 |
|
} |
37 |
|
|
39 |
|
* get returns the last value set |
40 |
|
*/ |
41 |
|
public void testGetSet() { |
42 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
42 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
43 |
|
assertSame(one, ai.get()); |
44 |
|
ai.set(two); |
45 |
|
assertSame(two, ai.get()); |
51 |
|
* get returns the last value lazySet in same thread |
52 |
|
*/ |
53 |
|
public void testGetLazySet() { |
54 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
54 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
55 |
|
assertSame(one, ai.get()); |
56 |
|
ai.lazySet(two); |
57 |
|
assertSame(two, ai.get()); |
63 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
64 |
|
*/ |
65 |
|
public void testCompareAndSet() { |
66 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
66 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
67 |
|
assertTrue(ai.compareAndSet(one, two)); |
68 |
|
assertTrue(ai.compareAndSet(two, minusFour)); |
69 |
|
assertSame(minusFour, ai.get()); |
78 |
|
* to succeed |
79 |
|
*/ |
80 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
81 |
< |
final AtomicReference<Item> ai = new AtomicReference<Item>(one); |
81 |
> |
final AtomicReference<Item> ai = new AtomicReference<>(one); |
82 |
|
Thread t = new Thread(new CheckedRunnable() { |
83 |
|
public void realRun() { |
84 |
|
while (!ai.compareAndSet(two, three)) |
98 |
|
*/ |
99 |
|
@SuppressWarnings("deprecation") |
100 |
|
public void testWeakCompareAndSet() { |
101 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
101 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
102 |
|
do {} while (!ai.weakCompareAndSet(one, two)); |
103 |
|
do {} while (!ai.weakCompareAndSet(two, minusFour)); |
104 |
|
assertSame(minusFour, ai.get()); |
110 |
|
* getAndSet returns previous value and sets to given value |
111 |
|
*/ |
112 |
|
public void testGetAndSet() { |
113 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
113 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
114 |
|
assertSame(one, ai.getAndSet(zero)); |
115 |
|
assertSame(zero, ai.getAndSet(minusTen)); |
116 |
|
assertSame(minusTen, ai.getAndSet(one)); |
120 |
|
* a deserialized/reserialized atomic holds same value |
121 |
|
*/ |
122 |
|
public void testSerialization() throws Exception { |
123 |
< |
AtomicReference<Item> x = new AtomicReference<Item>(); |
123 |
> |
AtomicReference<Item> x = new AtomicReference<>(); |
124 |
|
AtomicReference<Item> y = serialClone(x); |
125 |
|
assertNotSame(x, y); |
126 |
|
x.set(one); |
135 |
|
* toString returns current value. |
136 |
|
*/ |
137 |
|
public void testToString() { |
138 |
< |
AtomicReference<Item> ai = new AtomicReference<Item>(one); |
138 |
> |
AtomicReference<Item> ai = new AtomicReference<>(one); |
139 |
|
assertEquals(one.toString(), ai.toString()); |
140 |
|
ai.set(two); |
141 |
|
assertEquals(two.toString(), ai.toString()); |