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.11 by jsr166, Mon Nov 2 20:28:31 2009 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
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 25 | Line 24 | public class AtomicReferenceFieldUpdater
24      /**
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 <        }
62 <        catch (RuntimeException rt) {}
57 >        } catch (RuntimeException success) {}
58      }
59  
60      /**
61 <     *  get returns the last value set or assigned
61 >     * get returns the last value set or assigned
62       */
63 <    public void testGetSet(){
63 >    public void testGetSet() {
64          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
65          try {
66              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 73 | Line 68 | public class AtomicReferenceFieldUpdater
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,m3);
75 <        assertEquals(m3,a.get(this));
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 lazySet by same thread
79 >     * get returns the last value lazySet by same thread
80       */
81 <    public void testGetLazySet(){
81 >    public void testGetLazySet() {
82          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
83          try {
84              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 91 | Line 86 | public class AtomicReferenceFieldUpdater
86              return;
87          }
88          x = one;
89 <        assertEquals(one,a.get(this));
90 <        a.lazySet(this,two);
91 <        assertEquals(two,a.get(this));
92 <        a.lazySet(this,m3);
93 <        assertEquals(m3,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       * compareAndSet succeeds in changing value if equal to expected else fails
98       */
99 <    public void testCompareAndSet(){
99 >    public void testCompareAndSet() {
100          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
101          try {
102              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 109 | Line 104 | public class AtomicReferenceFieldUpdater
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;
123          try {
# Line 131 | Line 126 | public class AtomicReferenceFieldUpdater
126              return;
127          }
128  
129 <        Thread t = new Thread(new Runnable() {
130 <                public void run() {
131 <                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
132 <                }});
133 <        try {
134 <            t.start();
135 <            assertTrue(a.compareAndSet(this, one, two));
136 <            t.join(LONG_DELAY_MS);
137 <            assertFalse(t.isAlive());
138 <            assertEquals(a.get(this), three);
139 <        }
145 <        catch(Exception e) {
146 <            unexpectedException();
147 <        }
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
145       */
146 <    public void testWeakCompareAndSet(){
146 >    public void testWeakCompareAndSet() {
147          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
148          try {
149              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 159 | Line 151 | public class AtomicReferenceFieldUpdater
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(){
164 >    public void testGetAndSet() {
165          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
166          try {
167              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 177 | Line 169 | public class AtomicReferenceFieldUpdater
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