ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.28
Committed: Tue Apr 2 16:14:39 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.27: +13 -2 lines
Log Message:
fix exception specs to match reality, and update corresponding tck tests

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