ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicStampedReferenceTest.java
Revision: 1.17
Committed: Tue Mar 15 19:47:06 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.16: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

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