ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicStampedReferenceTest.java
Revision: 1.25
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +8 -8 lines
Log Message:
use diamond <> pervasively

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.5 * 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.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.5 */
8 dl 1.1
9 jsr166 1.18 import java.util.concurrent.atomic.AtomicStampedReference;
10 dl 1.1
11 jsr166 1.21 import junit.framework.Test;
12     import junit.framework.TestSuite;
13    
14 jsr166 1.10 public class AtomicStampedReferenceTest extends JSR166TestCase {
15 jsr166 1.15 public static void main(String[] args) {
16 jsr166 1.23 main(suite(), args);
17 dl 1.1 }
18     public static Test suite() {
19     return new TestSuite(AtomicStampedReferenceTest.class);
20     }
21 jsr166 1.8
22 dl 1.3 /**
23 dl 1.7 * constructor initializes to given reference and stamp
24 dl 1.3 */
25 jsr166 1.10 public void testConstructor() {
26 jsr166 1.25 AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
27 jsr166 1.19 assertSame(one, ai.getReference());
28 jsr166 1.13 assertEquals(0, ai.getStamp());
29 jsr166 1.25 AtomicStampedReference<Item> a2 = new AtomicStampedReference<>(null, 1);
30 jsr166 1.13 assertNull(a2.getReference());
31     assertEquals(1, a2.getStamp());
32 dl 1.1 }
33    
34 dl 1.3 /**
35 jsr166 1.16 * get returns the last values of reference and stamp set
36 dl 1.3 */
37 jsr166 1.10 public void testGetSet() {
38 dl 1.1 int[] mark = new int[1];
39 jsr166 1.25 AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
40 jsr166 1.19 assertSame(one, ai.getReference());
41 jsr166 1.13 assertEquals(0, ai.getStamp());
42 jsr166 1.14 assertSame(one, ai.get(mark));
43 dl 1.1 assertEquals(0, mark[0]);
44 jsr166 1.13 ai.set(two, 0);
45 jsr166 1.19 assertSame(two, ai.getReference());
46 jsr166 1.13 assertEquals(0, ai.getStamp());
47 jsr166 1.14 assertSame(two, ai.get(mark));
48 dl 1.1 assertEquals(0, mark[0]);
49 jsr166 1.13 ai.set(one, 1);
50 jsr166 1.19 assertSame(one, ai.getReference());
51 jsr166 1.13 assertEquals(1, ai.getStamp());
52 jsr166 1.14 assertSame(one, ai.get(mark));
53 jsr166 1.19 assertEquals(1, mark[0]);
54 dl 1.1 }
55    
56 dl 1.3 /**
57 jsr166 1.16 * attemptStamp succeeds in single thread
58 dl 1.3 */
59 jsr166 1.10 public void testAttemptStamp() {
60 dl 1.1 int[] mark = new int[1];
61 jsr166 1.25 AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
62 dl 1.1 assertEquals(0, ai.getStamp());
63     assertTrue(ai.attemptStamp(one, 1));
64 jsr166 1.13 assertEquals(1, ai.getStamp());
65 jsr166 1.14 assertSame(one, ai.get(mark));
66 dl 1.1 assertEquals(1, mark[0]);
67     }
68    
69 dl 1.3 /**
70 dl 1.4 * compareAndSet succeeds in changing values if equal to expected reference
71     * and stamp else fails
72 dl 1.3 */
73 jsr166 1.10 public void testCompareAndSet() {
74 dl 1.1 int[] mark = new int[1];
75 jsr166 1.25 AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
76 jsr166 1.14 assertSame(one, ai.get(mark));
77 dl 1.1 assertEquals(0, ai.getStamp());
78 jsr166 1.13 assertEquals(0, mark[0]);
79 dl 1.1
80     assertTrue(ai.compareAndSet(one, two, 0, 0));
81 jsr166 1.14 assertSame(two, ai.get(mark));
82 jsr166 1.13 assertEquals(0, mark[0]);
83 dl 1.1
84 dl 1.24 assertTrue(ai.compareAndSet(two, minusThree, 0, 1));
85     assertSame(minusThree, ai.get(mark));
86 jsr166 1.13 assertEquals(1, mark[0]);
87 dl 1.1
88 dl 1.24 assertFalse(ai.compareAndSet(two, minusThree, 1, 1));
89     assertSame(minusThree, ai.get(mark));
90 jsr166 1.13 assertEquals(1, mark[0]);
91 dl 1.1 }
92    
93 dl 1.3 /**
94 dl 1.4 * compareAndSet in one thread enables another waiting for reference value
95     * to succeed
96     */
97 jsr166 1.11 public void testCompareAndSetInMultipleThreads() throws Exception {
98 jsr166 1.25 final AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
99 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
100     public void realRun() {
101     while (!ai.compareAndSet(two, three, 0, 0))
102     Thread.yield();
103     }});
104 jsr166 1.11
105     t.start();
106     assertTrue(ai.compareAndSet(one, two, 0, 0));
107     t.join(LONG_DELAY_MS);
108     assertFalse(t.isAlive());
109 jsr166 1.20 assertSame(three, ai.getReference());
110     assertEquals(0, ai.getStamp());
111 dl 1.4 }
112    
113     /**
114     * compareAndSet in one thread enables another waiting for stamp value
115     * to succeed
116     */
117 jsr166 1.11 public void testCompareAndSetInMultipleThreads2() throws Exception {
118 jsr166 1.25 final AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
119 jsr166 1.12 Thread t = new Thread(new CheckedRunnable() {
120     public void realRun() {
121     while (!ai.compareAndSet(one, one, 1, 2))
122     Thread.yield();
123     }});
124 jsr166 1.11
125     t.start();
126     assertTrue(ai.compareAndSet(one, one, 0, 1));
127     t.join(LONG_DELAY_MS);
128     assertFalse(t.isAlive());
129 jsr166 1.20 assertSame(one, ai.getReference());
130     assertEquals(2, ai.getStamp());
131 dl 1.4 }
132    
133     /**
134     * repeated weakCompareAndSet succeeds in changing values when equal
135 jsr166 1.8 * to expected
136 dl 1.3 */
137 jsr166 1.10 public void testWeakCompareAndSet() {
138 dl 1.1 int[] mark = new int[1];
139 jsr166 1.25 AtomicStampedReference<Item> ai = new AtomicStampedReference<>(one, 0);
140 jsr166 1.14 assertSame(one, ai.get(mark));
141 jsr166 1.15 assertEquals(0, ai.getStamp());
142 jsr166 1.13 assertEquals(0, mark[0]);
143 dl 1.1
144 jsr166 1.22 do {} while (!ai.weakCompareAndSet(one, two, 0, 0));
145 jsr166 1.14 assertSame(two, ai.get(mark));
146 jsr166 1.13 assertEquals(0, mark[0]);
147 dl 1.1
148 dl 1.24 do {} while (!ai.weakCompareAndSet(two, minusThree, 0, 1));
149     assertSame(minusThree, ai.get(mark));
150 jsr166 1.13 assertEquals(1, mark[0]);
151 dl 1.1 }
152    
153     }