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.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.24 by jsr166, Fri Jun 10 20:01:21 2011 UTC

# Line 1 | Line 1
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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 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      public static Test suite() {
# Line 23 | Line 22 | public class AtomicReferenceFieldUpdater
22      }
23  
24      /**
25 <     * Contruction with non-existent field throws RuntimeException
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                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
32              shouldThrow();
33 <        }
35 <        catch (RuntimeException rt) {}
33 >        } catch (RuntimeException success) {}
34      }
35  
38
36      /**
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                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
44              shouldThrow();
45 <        }
49 <        catch (RuntimeException rt) {}
45 >        } catch (RuntimeException success) {}
46      }
47  
48      /**
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                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
56              shouldThrow();
57 +        } catch (RuntimeException success) {}
58 +    }
59 +
60 +    /**
61 +     * get returns the last value set or assigned
62 +     */
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 <        catch (RuntimeException rt) {}
70 >        x = one;
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 <     *  get returns the last value set or assigned
79 >     * get returns the last value lazySet by same thread
80       */
81 <    public void testGetSet(){
82 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
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 <        assertEquals(one,a.get(this));
90 <        a.set(this,two);
91 <        assertEquals(two,a.get(this));
92 <        a.set(this,-3);
93 <        assertEquals(-3,a.get(this));
76 <        
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       * compareAndSet succeeds in changing value if equal to expected else fails
98       */
99 <    public void testCompareAndSet(){
100 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
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 <        assertTrue(a.compareAndSet(this,one,two));
108 <        assertTrue(a.compareAndSet(this,two,m4));
109 <        assertEquals(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 <        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       * compareAndSet in one thread enables another waiting for value
118       * to succeed
119       */
120 <    public void testCompareAndSetInMultipleThreads() {
120 >    public void testCompareAndSetInMultipleThreads() throws Exception {
121          x = one;
122 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
123 <
124 <        Thread t = new Thread(new Runnable() {
125 <                public void run() {
126 <                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
104 <                }});
105 <        try {
106 <            t.start();
107 <            assertTrue(a.compareAndSet(this, one, two));
108 <            t.join(LONG_DELAY_MS);
109 <            assertFalse(t.isAlive());
110 <            assertEquals(a.get(this), three);
111 <        }
112 <        catch(Exception e) {
113 <            unexpectedException();
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(a.get(this), three);
140      }
141  
142      /**
143       * repeated weakCompareAndSet succeeds in changing value when equal
144 <     * to expected
144 >     * to expected
145       */
146 <    public void testWeakCompareAndSet(){
147 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
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 <        assertEquals(m4,a.get(this));
157 <        while(!a.weakCompareAndSet(this,m4,seven));
158 <        assertEquals(seven,a.get(this));
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 = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
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