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.4 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.29 by jsr166, Thu May 30 03:28:55 2013 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 +    volatile int i;
17  
18 <    public static void main(String[] args){
18 >    public static void main(String[] args) {
19          junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
23      }
24  
25 +    AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
26 +        return AtomicReferenceFieldUpdater.newUpdater
27 +            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
28 +    }
29 +
30      /**
31 <     * Contruction with non-existent field throws RuntimeException
31 >     * Construction with non-existent field throws RuntimeException
32       */
33 <    public void testConstructor(){
34 <        try{
35 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
30 <                a = AtomicReferenceFieldUpdater.newUpdater
31 <                (getClass(), Integer.class, "y");
33 >    public void testConstructor() {
34 >        try {
35 >            updaterFor("y");
36              shouldThrow();
37 +        } catch (RuntimeException success) {
38 +            assertNotNull(success.getCause());
39          }
34        catch (RuntimeException rt) {}
40      }
41  
42 +    /**
43 +     * construction with field not of given type throws ClassCastException
44 +     */
45 +    public void testConstructor2() {
46 +        try {
47 +            updaterFor("z");
48 +            shouldThrow();
49 +        } catch (ClassCastException success) {}
50 +    }
51  
52      /**
53 <     * construction with field not of given type throws RuntimeException
53 >     * Constructor with non-volatile field throws IllegalArgumentException
54       */
55 <    public void testConstructor2(){
56 <        try{
57 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
44 <                a = AtomicReferenceFieldUpdater.newUpdater
45 <                (getClass(), Integer.class, "z");
55 >    public void testConstructor3() {
56 >        try {
57 >            updaterFor("w");
58              shouldThrow();
59 <        }
48 <        catch (RuntimeException rt) {}
59 >        } catch (IllegalArgumentException success) {}
60      }
61  
62      /**
63 <     * Constructor with non-volatile field throws exception
63 >     * Constructor with non-reference field throws ClassCastException
64       */
65 <    public void testConstructor3(){
66 <        try{
67 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
57 <                a = AtomicReferenceFieldUpdater.newUpdater
58 <                (getClass(), Integer.class, "w");
65 >    public void testConstructor4() {
66 >        try {
67 >            updaterFor("i");
68              shouldThrow();
69 <        }
70 <        catch (RuntimeException rt) {}
69 >        } catch (ClassCastException success) {}
70 >    }
71 >
72 >    /**
73 >     * get returns the last value set or assigned
74 >     */
75 >    public void testGetSet() {
76 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
77 >        a = updaterFor("x");
78 >        x = one;
79 >        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      }
85  
86      /**
87 <     *  get returns the last value set or assigned
87 >     * get returns the last value lazySet by same thread
88       */
89 <    public void testGetSet(){
90 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
89 >    public void testGetLazySet() {
90 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
91 >        a = updaterFor("x");
92          x = one;
93 <        assertEquals(one,a.get(this));
94 <        a.set(this,two);
95 <        assertEquals(two,a.get(this));
96 <        a.set(this,-3);
97 <        assertEquals(-3,a.get(this));
75 <        
93 >        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      }
99 +
100      /**
101       * compareAndSet succeeds in changing value if equal to expected else fails
102       */
103 <    public void testCompareAndSet(){
104 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
103 >    public void testCompareAndSet() {
104 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
105 >        a = updaterFor("x");
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(getClass(), Integer.class, "x");
122 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
123 >        a = updaterFor("x");
124  
125 <        Thread t = new Thread(new Runnable() {
126 <                public void run() {
127 <                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
128 <                }});
129 <        try {
130 <            t.start();
131 <            assertTrue(a.compareAndSet(this, one, two));
132 <            t.join(LONG_DELAY_MS);
133 <            assertFalse(t.isAlive());
134 <            assertEquals(a.get(this), three);
135 <        }
111 <        catch(Exception e) {
112 <            unexpectedException();
113 <        }
125 >        Thread t = new Thread(new CheckedRunnable() {
126 >            public void realRun() {
127 >                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
128 >                    Thread.yield();
129 >            }});
130 >
131 >        t.start();
132 >        assertTrue(a.compareAndSet(this, one, two));
133 >        t.join(LONG_DELAY_MS);
134 >        assertFalse(t.isAlive());
135 >        assertSame(three, a.get(this));
136      }
137  
138      /**
139       * repeated weakCompareAndSet succeeds in changing value when equal
140 <     * to expected
140 >     * to expected
141       */
142 <    public void testWeakCompareAndSet(){
143 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
142 >    public void testWeakCompareAndSet() {
143 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
144 >        a = updaterFor("x");
145          x = one;
146 <        while(!a.weakCompareAndSet(this,one,two));
147 <        while(!a.weakCompareAndSet(this,two,m4));
148 <        assertEquals(m4,a.get(this));
149 <        while(!a.weakCompareAndSet(this,m4,seven));
150 <        assertEquals(seven,a.get(this));
146 >        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      }
152  
153      /**
154       * getAndSet returns previous value and sets to given value
155       */
156 <    public void testGetAndSet(){
157 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
156 >    public void testGetAndSet() {
157 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
158 >        a = updaterFor("x");
159          x = one;
160 <        assertEquals(one,a.getAndSet(this, zero));
161 <        assertEquals(zero,a.getAndSet(this,m10));
162 <        assertEquals(m10,a.getAndSet(this,1));
160 >        assertSame(one, a.getAndSet(this, zero));
161 >        assertSame(zero, a.getAndSet(this, m10));
162 >        assertSame(m10, a.getAndSet(this, 1));
163      }
164  
165   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines