ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicMarkableReferenceTest.java
Revision: 1.17
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
use serialClone in serialization tests; update imports

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.AtomicMarkableReference;
11
12 public class AtomicMarkableReferenceTest 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(AtomicMarkableReferenceTest.class);
18 }
19
20 /**
21 * constructor initializes to given reference and mark
22 */
23 public void testConstructor() {
24 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
25 assertSame(one,ai.getReference());
26 assertFalse(ai.isMarked());
27 AtomicMarkableReference a2 = new AtomicMarkableReference(null, true);
28 assertNull(a2.getReference());
29 assertTrue(a2.isMarked());
30 }
31
32 /**
33 * get returns the last values of reference and mark set
34 */
35 public void testGetSet() {
36 boolean[] mark = new boolean[1];
37 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
38 assertSame(one,ai.getReference());
39 assertFalse(ai.isMarked());
40 assertSame(one, ai.get(mark));
41 assertFalse(mark[0]);
42 ai.set(two, false);
43 assertSame(two,ai.getReference());
44 assertFalse(ai.isMarked());
45 assertSame(two, ai.get(mark));
46 assertFalse(mark[0]);
47 ai.set(one, true);
48 assertSame(one,ai.getReference());
49 assertTrue(ai.isMarked());
50 assertSame(one, ai.get(mark));
51 assertTrue(mark[0]);
52 }
53
54 /**
55 * attemptMark succeeds in single thread
56 */
57 public void testAttemptMark() {
58 boolean[] mark = new boolean[1];
59 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
60 assertFalse(ai.isMarked());
61 assertTrue(ai.attemptMark(one, true));
62 assertTrue(ai.isMarked());
63 assertSame(one, ai.get(mark));
64 assertTrue(mark[0]);
65 }
66
67 /**
68 * compareAndSet succeeds in changing values if equal to expected reference
69 * and mark else fails
70 */
71 public void testCompareAndSet() {
72 boolean[] mark = new boolean[1];
73 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
74 assertSame(one, ai.get(mark));
75 assertFalse(ai.isMarked());
76 assertFalse(mark[0]);
77
78 assertTrue(ai.compareAndSet(one, two, false, false));
79 assertSame(two, ai.get(mark));
80 assertFalse(mark[0]);
81
82 assertTrue(ai.compareAndSet(two, m3, false, true));
83 assertSame(m3, ai.get(mark));
84 assertTrue(mark[0]);
85
86 assertFalse(ai.compareAndSet(two, m3, true, true));
87 assertSame(m3, ai.get(mark));
88 assertTrue(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 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
97 Thread t = new Thread(new CheckedRunnable() {
98 public void realRun() {
99 while (!ai.compareAndSet(two, three, false, false))
100 Thread.yield();
101 }});
102
103 t.start();
104 assertTrue(ai.compareAndSet(one, two, false, false));
105 t.join(LONG_DELAY_MS);
106 assertFalse(t.isAlive());
107 assertSame(ai.getReference(), three);
108 assertFalse(ai.isMarked());
109 }
110
111 /**
112 * compareAndSet in one thread enables another waiting for mark value
113 * to succeed
114 */
115 public void testCompareAndSetInMultipleThreads2() throws Exception {
116 final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
117 Thread t = new Thread(new CheckedRunnable() {
118 public void realRun() {
119 while (!ai.compareAndSet(one, one, true, false))
120 Thread.yield();
121 }});
122
123 t.start();
124 assertTrue(ai.compareAndSet(one, one, false, true));
125 t.join(LONG_DELAY_MS);
126 assertFalse(t.isAlive());
127 assertSame(ai.getReference(), one);
128 assertFalse(ai.isMarked());
129 }
130
131 /**
132 * repeated weakCompareAndSet succeeds in changing values when equal
133 * to expected
134 */
135 public void testWeakCompareAndSet() {
136 boolean[] mark = new boolean[1];
137 AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
138 assertSame(one, ai.get(mark));
139 assertFalse(ai.isMarked());
140 assertFalse(mark[0]);
141
142 while (!ai.weakCompareAndSet(one, two, false, false));
143 assertSame(two, ai.get(mark));
144 assertFalse(mark[0]);
145
146 while (!ai.weakCompareAndSet(two, m3, false, true));
147 assertSame(m3, ai.get(mark));
148 assertTrue(mark[0]);
149 }
150
151 }