ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java (file contents):
Revision 1.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.25 by jsr166, Fri Jun 10 20:17:11 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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  
8 import java.util.concurrent.atomic.*;
9   import junit.framework.*;
10 < import java.util.*;
10 > import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
11  
12 < public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
12 > public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
13      volatile Integer x = null;
14      Object z;
15      Integer w;
16  
17 <    public static void main(String[] args){
17 >    public static void main(String[] args) {
18          junit.textui.TestRunner.run(suite());
19      }
20
21  
20      public static Test suite() {
21          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
22      }
23  
24      /**
25 <     *
25 >     * Construction with non-existent field throws RuntimeException
26       */
27 <    public void testConstructor(){
28 <        try{
27 >    public void testConstructor() {
28 >        try {
29              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
30                  a = AtomicReferenceFieldUpdater.newUpdater
31 <                (getClass(), Integer.class, "y");
31 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
32              shouldThrow();
33 <        }
36 <        catch (RuntimeException rt) {}
33 >        } catch (RuntimeException success) {}
34      }
35  
39
36      /**
37 <     *
37 >     * construction with field not of given type throws RuntimeException
38       */
39 <    public void testConstructor2(){
40 <        try{
39 >    public void testConstructor2() {
40 >        try {
41              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
42                  a = AtomicReferenceFieldUpdater.newUpdater
43 <                (getClass(), Integer.class, "z");
43 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
44              shouldThrow();
45 <        }
50 <        catch (RuntimeException rt) {}
45 >        } catch (RuntimeException success) {}
46      }
47  
48      /**
49 <     *
49 >     * Constructor with non-volatile field throws exception
50       */
51 <    public void testConstructor3(){
52 <        try{
51 >    public void testConstructor3() {
52 >        try {
53              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
54                  a = AtomicReferenceFieldUpdater.newUpdater
55 <                (getClass(), Integer.class, "w");
55 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
56              shouldThrow();
57 <        }
63 <        catch (RuntimeException rt) {}
57 >        } catch (RuntimeException success) {}
58      }
59  
60      /**
61 <     *
61 >     * get returns the last value set or assigned
62       */
63 <    public void testGetSet(){
64 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
63 >    public void testGetSet() {
64 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
65 >        try {
66 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
67 >        } catch (RuntimeException ok) {
68 >            return;
69 >        }
70          x = one;
71 <        assertEquals(one,a.get(this));
72 <        a.set(this,two);
73 <        assertEquals(two,a.get(this));
74 <        a.set(this,-3);
75 <        assertEquals(-3,a.get(this));
77 <        
71 >        assertSame(one, a.get(this));
72 >        a.set(this, two);
73 >        assertSame(two, a.get(this));
74 >        a.set(this, m3);
75 >        assertSame(m3, a.get(this));
76      }
77 +
78      /**
79 <     *
80 <     */
81 <    public void testCompareAndSet(){
82 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
79 >     * get returns the last value lazySet by same thread
80 >     */
81 >    public void testGetLazySet() {
82 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
83 >        try {
84 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
85 >        } catch (RuntimeException ok) {
86 >            return;
87 >        }
88          x = one;
89 <        assertTrue(a.compareAndSet(this,one,two));
90 <        assertTrue(a.compareAndSet(this,two,m4));
91 <        assertEquals(m4,a.get(this));
92 <        assertFalse(a.compareAndSet(this,m5,seven));
93 <        assertFalse((seven == a.get(this)));
90 <        assertTrue(a.compareAndSet(this,m4,seven));
91 <        assertEquals(seven,a.get(this));
89 >        assertSame(one, a.get(this));
90 >        a.lazySet(this, two);
91 >        assertSame(two, a.get(this));
92 >        a.lazySet(this, m3);
93 >        assertSame(m3, a.get(this));
94      }
95  
96      /**
97 <     *
98 <     */
99 <    public void testWeakCompareAndSet(){
100 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
97 >     * compareAndSet succeeds in changing value if equal to expected else fails
98 >     */
99 >    public void testCompareAndSet() {
100 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
101 >        try {
102 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
103 >        } catch (RuntimeException ok) {
104 >            return;
105 >        }
106          x = one;
107 <        while(!a.weakCompareAndSet(this,one,two));
108 <        while(!a.weakCompareAndSet(this,two,m4));
109 <        assertEquals(m4,a.get(this));
110 <        while(!a.weakCompareAndSet(this,m4,seven));
111 <        assertEquals(seven,a.get(this));
107 >        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 >        assertFalse(seven == a.get(this));
112 >        assertTrue(a.compareAndSet(this, m4, seven));
113 >        assertSame(seven, a.get(this));
114      }
115  
116      /**
117 <     *
117 >     * compareAndSet in one thread enables another waiting for value
118 >     * to succeed
119       */
120 <    public void testGetAndSet(){
121 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
120 >    public void testCompareAndSetInMultipleThreads() throws Exception {
121 >        x = one;
122 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
123 >        try {
124 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
125 >        } catch (RuntimeException ok) {
126 >            return;
127 >        }
128 >
129 >        Thread t = new Thread(new CheckedRunnable() {
130 >            public void realRun() {
131 >                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
132 >                    Thread.yield();
133 >            }});
134 >
135 >        t.start();
136 >        assertTrue(a.compareAndSet(this, one, two));
137 >        t.join(LONG_DELAY_MS);
138 >        assertFalse(t.isAlive());
139 >        assertSame(three, a.get(this));
140 >    }
141 >
142 >    /**
143 >     * repeated weakCompareAndSet succeeds in changing value when equal
144 >     * to expected
145 >     */
146 >    public void testWeakCompareAndSet() {
147 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
148 >        try {
149 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
150 >        } catch (RuntimeException ok) {
151 >            return;
152 >        }
153 >        x = one;
154 >        while (!a.weakCompareAndSet(this, one, two));
155 >        while (!a.weakCompareAndSet(this, two, m4));
156 >        assertSame(m4, a.get(this));
157 >        while (!a.weakCompareAndSet(this, m4, seven));
158 >        assertSame(seven, a.get(this));
159 >    }
160 >
161 >    /**
162 >     * getAndSet returns previous value and sets to given value
163 >     */
164 >    public void testGetAndSet() {
165 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
166 >        try {
167 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
168 >        } catch (RuntimeException ok) {
169 >            return;
170 >        }
171          x = one;
172 <        assertEquals(one,a.getAndSet(this, zero));
173 <        assertEquals(zero,a.getAndSet(this,m10));
174 <        assertEquals(m10,a.getAndSet(this,1));
172 >        assertSame(one, a.getAndSet(this, zero));
173 >        assertSame(zero, a.getAndSet(this, m10));
174 >        assertSame(m10, a.getAndSet(this, 1));
175      }
176  
177   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines