ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.27
Committed: Tue Apr 2 04:11:28 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.26: +5 -3 lines
Log Message:
tighten tests to match specs

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.21 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.11 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10 jsr166 1.23 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
11 dl 1.1
12 jsr166 1.13 public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
13 dl 1.1 volatile Integer x = null;
14     Object z;
15     Integer w;
16    
17 jsr166 1.13 public static void main(String[] args) {
18 dl 1.1 junit.textui.TestRunner.run(suite());
19     }
20     public static Test suite() {
21     return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
22     }
23    
24 jsr166 1.26 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
25     return AtomicReferenceFieldUpdater.newUpdater
26     (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
27     }
28    
29 dl 1.3 /**
30 dl 1.7 * Construction with non-existent field throws RuntimeException
31 dl 1.3 */
32 jsr166 1.13 public void testConstructor() {
33 jsr166 1.12 try {
34 jsr166 1.26 updaterFor("y");
35 dl 1.3 shouldThrow();
36 jsr166 1.27 } catch (RuntimeException success) {
37     assertTrue(success.getCause() != null);
38     }
39 dl 1.1 }
40    
41 dl 1.3 /**
42 dl 1.4 * construction with field not of given type throws RuntimeException
43 dl 1.3 */
44 jsr166 1.13 public void testConstructor2() {
45 jsr166 1.12 try {
46 jsr166 1.26 updaterFor("z");
47 dl 1.3 shouldThrow();
48 jsr166 1.17 } catch (RuntimeException success) {}
49 dl 1.1 }
50    
51 dl 1.3 /**
52 jsr166 1.27 * Constructor with non-volatile field throws IllegalArgumentException
53 dl 1.3 */
54 jsr166 1.13 public void testConstructor3() {
55 jsr166 1.12 try {
56 jsr166 1.26 updaterFor("w");
57 dl 1.3 shouldThrow();
58 jsr166 1.27 } catch (IllegalArgumentException success) {}
59 dl 1.1 }
60    
61 dl 1.3 /**
62 jsr166 1.20 * get returns the last value set or assigned
63 dl 1.3 */
64 jsr166 1.13 public void testGetSet() {
65 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
66 jsr166 1.26 a = updaterFor("x");
67 dl 1.1 x = one;
68 jsr166 1.24 assertSame(one, a.get(this));
69     a.set(this, two);
70     assertSame(two, a.get(this));
71     a.set(this, m3);
72     assertSame(m3, a.get(this));
73 dl 1.1 }
74 dl 1.10
75     /**
76 jsr166 1.20 * get returns the last value lazySet by same thread
77 dl 1.10 */
78 jsr166 1.13 public void testGetLazySet() {
79 dl 1.10 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
80 jsr166 1.26 a = updaterFor("x");
81 dl 1.10 x = one;
82 jsr166 1.24 assertSame(one, a.get(this));
83     a.lazySet(this, two);
84     assertSame(two, a.get(this));
85     a.lazySet(this, m3);
86     assertSame(m3, a.get(this));
87 dl 1.10 }
88    
89 dl 1.3 /**
90 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
91 dl 1.3 */
92 jsr166 1.13 public void testCompareAndSet() {
93 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
94 jsr166 1.26 a = updaterFor("x");
95 dl 1.1 x = one;
96 jsr166 1.19 assertTrue(a.compareAndSet(this, one, two));
97     assertTrue(a.compareAndSet(this, two, m4));
98     assertSame(m4, a.get(this));
99     assertFalse(a.compareAndSet(this, m5, seven));
100 jsr166 1.18 assertFalse(seven == a.get(this));
101 jsr166 1.19 assertTrue(a.compareAndSet(this, m4, seven));
102 jsr166 1.24 assertSame(seven, a.get(this));
103 dl 1.1 }
104    
105 dl 1.3 /**
106 dl 1.4 * compareAndSet in one thread enables another waiting for value
107     * to succeed
108     */
109 jsr166 1.14 public void testCompareAndSetInMultipleThreads() throws Exception {
110 dl 1.4 x = one;
111 dl 1.8 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
112 jsr166 1.26 a = updaterFor("x");
113 dl 1.4
114 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
115     public void realRun() {
116     while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
117     Thread.yield();
118     }});
119 jsr166 1.14
120     t.start();
121     assertTrue(a.compareAndSet(this, one, two));
122     t.join(LONG_DELAY_MS);
123     assertFalse(t.isAlive());
124 jsr166 1.25 assertSame(three, a.get(this));
125 dl 1.4 }
126    
127     /**
128     * repeated weakCompareAndSet succeeds in changing value when equal
129 jsr166 1.11 * to expected
130 dl 1.3 */
131 jsr166 1.13 public void testWeakCompareAndSet() {
132 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
133 jsr166 1.26 a = updaterFor("x");
134 dl 1.1 x = one;
135 jsr166 1.24 while (!a.weakCompareAndSet(this, one, two));
136     while (!a.weakCompareAndSet(this, two, m4));
137     assertSame(m4, a.get(this));
138     while (!a.weakCompareAndSet(this, m4, seven));
139     assertSame(seven, a.get(this));
140 dl 1.1 }
141    
142 dl 1.3 /**
143 dl 1.4 * getAndSet returns previous value and sets to given value
144 dl 1.3 */
145 jsr166 1.13 public void testGetAndSet() {
146 dl 1.8 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
147 jsr166 1.26 a = updaterFor("x");
148 dl 1.1 x = one;
149 jsr166 1.24 assertSame(one, a.getAndSet(this, zero));
150     assertSame(zero, a.getAndSet(this, m10));
151     assertSame(m10, a.getAndSet(this, 1));
152 dl 1.1 }
153    
154     }