ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceTest.java
Revision: 1.29
Committed: Fri Aug 4 03:30:21 2017 UTC (6 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.28: +1 -1 lines
Log Message:
improve javadoc wording for testSerialization methods

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.17 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.9 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.18 import java.util.concurrent.atomic.AtomicReference;
10 dl 1.1
11 jsr166 1.22 import junit.framework.Test;
12     import junit.framework.TestSuite;
13    
14 dl 1.3 public class AtomicReferenceTest extends JSR166TestCase {
15 jsr166 1.16 public static void main(String[] args) {
16 jsr166 1.24 main(suite(), args);
17 dl 1.1 }
18     public static Test suite() {
19     return new TestSuite(AtomicReferenceTest.class);
20     }
21    
22 dl 1.4 /**
23 dl 1.5 * constructor initializes to given value
24 dl 1.4 */
25 jsr166 1.11 public void testConstructor() {
26 dl 1.1 AtomicReference ai = new AtomicReference(one);
27 jsr166 1.19 assertSame(one, ai.get());
28 dl 1.1 }
29    
30 dl 1.4 /**
31 dl 1.5 * default constructed initializes to null
32 dl 1.4 */
33 jsr166 1.11 public void testConstructor2() {
34 dl 1.1 AtomicReference ai = new AtomicReference();
35 jsr166 1.14 assertNull(ai.get());
36 dl 1.1 }
37    
38 dl 1.4 /**
39 dl 1.5 * get returns the last value set
40 dl 1.4 */
41 jsr166 1.11 public void testGetSet() {
42 dl 1.1 AtomicReference ai = new AtomicReference(one);
43 jsr166 1.19 assertSame(one, ai.get());
44 jsr166 1.14 ai.set(two);
45 jsr166 1.19 assertSame(two, ai.get());
46 jsr166 1.14 ai.set(m3);
47 jsr166 1.19 assertSame(m3, ai.get());
48 dl 1.1 }
49 dl 1.8
50     /**
51     * get returns the last value lazySet in same thread
52     */
53 jsr166 1.11 public void testGetLazySet() {
54 dl 1.8 AtomicReference ai = new AtomicReference(one);
55 jsr166 1.19 assertSame(one, ai.get());
56 jsr166 1.14 ai.lazySet(two);
57 jsr166 1.19 assertSame(two, ai.get());
58 jsr166 1.14 ai.lazySet(m3);
59 jsr166 1.19 assertSame(m3, ai.get());
60 dl 1.8 }
61    
62 dl 1.4 /**
63 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
64 dl 1.4 */
65 jsr166 1.11 public void testCompareAndSet() {
66 dl 1.1 AtomicReference ai = new AtomicReference(one);
67 jsr166 1.19 assertTrue(ai.compareAndSet(one, two));
68     assertTrue(ai.compareAndSet(two, m4));
69     assertSame(m4, ai.get());
70     assertFalse(ai.compareAndSet(m5, seven));
71     assertSame(m4, ai.get());
72     assertTrue(ai.compareAndSet(m4, seven));
73     assertSame(seven, ai.get());
74 dl 1.1 }
75    
76 dl 1.4 /**
77 dl 1.5 * compareAndSet in one thread enables another waiting for value
78     * to succeed
79     */
80 jsr166 1.12 public void testCompareAndSetInMultipleThreads() throws Exception {
81 dl 1.5 final AtomicReference ai = new AtomicReference(one);
82 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
83     public void realRun() {
84     while (!ai.compareAndSet(two, three))
85     Thread.yield();
86     }});
87 jsr166 1.12
88     t.start();
89     assertTrue(ai.compareAndSet(one, two));
90     t.join(LONG_DELAY_MS);
91     assertFalse(t.isAlive());
92 jsr166 1.20 assertSame(three, ai.get());
93 dl 1.5 }
94    
95     /**
96     * repeated weakCompareAndSet succeeds in changing value when equal
97 jsr166 1.9 * to expected
98 dl 1.4 */
99 jsr166 1.11 public void testWeakCompareAndSet() {
100 dl 1.1 AtomicReference ai = new AtomicReference(one);
101 jsr166 1.23 do {} while (!ai.weakCompareAndSet(one, two));
102     do {} while (!ai.weakCompareAndSet(two, m4));
103 jsr166 1.19 assertSame(m4, ai.get());
104 jsr166 1.23 do {} while (!ai.weakCompareAndSet(m4, seven));
105 jsr166 1.19 assertSame(seven, ai.get());
106 dl 1.1 }
107    
108 dl 1.4 /**
109 dl 1.5 * getAndSet returns previous value and sets to given value
110 dl 1.4 */
111 jsr166 1.11 public void testGetAndSet() {
112 dl 1.1 AtomicReference ai = new AtomicReference(one);
113 jsr166 1.19 assertSame(one, ai.getAndSet(zero));
114     assertSame(zero, ai.getAndSet(m10));
115     assertSame(m10, ai.getAndSet(one));
116 dl 1.2 }
117    
118 dl 1.4 /**
119 jsr166 1.29 * a deserialized/reserialized atomic holds same value
120 dl 1.4 */
121 jsr166 1.12 public void testSerialization() throws Exception {
122 jsr166 1.18 AtomicReference x = new AtomicReference();
123     AtomicReference y = serialClone(x);
124 jsr166 1.21 assertNotSame(x, y);
125 jsr166 1.18 x.set(one);
126     AtomicReference z = serialClone(x);
127 jsr166 1.21 assertNotSame(y, z);
128 jsr166 1.18 assertEquals(one, x.get());
129 jsr166 1.28 assertNull(y.get());
130 jsr166 1.18 assertEquals(one, z.get());
131 dl 1.1 }
132    
133 dl 1.7 /**
134     * toString returns current value.
135 jsr166 1.9 */
136 dl 1.7 public void testToString() {
137 jsr166 1.27 AtomicReference<Integer> ai = new AtomicReference<>(one);
138 jsr166 1.20 assertEquals(one.toString(), ai.toString());
139 dl 1.7 ai.set(two);
140 jsr166 1.20 assertEquals(two.toString(), ai.toString());
141 dl 1.7 }
142    
143 dl 1.1 }