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.20 by jsr166, Sat Oct 9 19:30:34 2010 UTC vs.
Revision 1.29 by jsr166, Thu May 30 03:28:55 2013 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 {
13      volatile Integer x = null;
14      Object z;
15      Integer w;
16 +    volatile int i;
17  
18      public static void main(String[] args) {
19          junit.textui.TestRunner.run(suite());
# Line 22 | Line 22 | public class AtomicReferenceFieldUpdater
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       * Construction with non-existent field throws RuntimeException
32       */
33      public void testConstructor() {
34          try {
35 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31 <                a = AtomicReferenceFieldUpdater.newUpdater
32 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
35 >            updaterFor("y");
36              shouldThrow();
37 <        } catch (RuntimeException success) {}
37 >        } catch (RuntimeException success) {
38 >            assertNotNull(success.getCause());
39 >        }
40      }
41  
37
42      /**
43 <     * construction with field not of given type throws RuntimeException
43 >     * construction with field not of given type throws ClassCastException
44       */
45      public void testConstructor2() {
46          try {
47 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
44 <                a = AtomicReferenceFieldUpdater.newUpdater
45 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
47 >            updaterFor("z");
48              shouldThrow();
49 <        } catch (RuntimeException success) {}
49 >        } catch (ClassCastException success) {}
50      }
51  
52      /**
53 <     * Constructor with non-volatile field throws exception
53 >     * Constructor with non-volatile field throws IllegalArgumentException
54       */
55      public void testConstructor3() {
56          try {
57 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
58 <                a = AtomicReferenceFieldUpdater.newUpdater
59 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
57 >            updaterFor("w");
58 >            shouldThrow();
59 >        } catch (IllegalArgumentException success) {}
60 >    }
61 >
62 >    /**
63 >     * Constructor with non-reference field throws ClassCastException
64 >     */
65 >    public void testConstructor4() {
66 >        try {
67 >            updaterFor("i");
68              shouldThrow();
69 <        } catch (RuntimeException success) {}
69 >        } catch (ClassCastException success) {}
70      }
71  
72      /**
# Line 64 | Line 74 | public class AtomicReferenceFieldUpdater
74       */
75      public void testGetSet() {
76          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
77 <        try {
68 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
69 <        } catch (RuntimeException ok) {
70 <            return;
71 <        }
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));
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      /**
# Line 82 | Line 88 | public class AtomicReferenceFieldUpdater
88       */
89      public void testGetLazySet() {
90          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
91 <        try {
86 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
87 <        } catch (RuntimeException ok) {
88 <            return;
89 <        }
91 >        a = updaterFor("x");
92          x = one;
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));
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      /**
# Line 100 | Line 102 | public class AtomicReferenceFieldUpdater
102       */
103      public void testCompareAndSet() {
104          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
105 <        try {
104 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
105 <        } catch (RuntimeException ok) {
106 <            return;
107 <        }
105 >        a = updaterFor("x");
106          x = one;
107          assertTrue(a.compareAndSet(this, one, two));
108          assertTrue(a.compareAndSet(this, two, m4));
# Line 112 | Line 110 | public class AtomicReferenceFieldUpdater
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));
113 >        assertSame(seven, a.get(this));
114      }
115  
116      /**
# Line 122 | Line 120 | public class AtomicReferenceFieldUpdater
120      public void testCompareAndSetInMultipleThreads() throws Exception {
121          x = one;
122          final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
123 <        try {
126 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
127 <        } catch (RuntimeException ok) {
128 <            return;
129 <        }
123 >        a = updaterFor("x");
124  
125          Thread t = new Thread(new CheckedRunnable() {
126              public void realRun() {
# Line 138 | Line 132 | public class AtomicReferenceFieldUpdater
132          assertTrue(a.compareAndSet(this, one, two));
133          t.join(LONG_DELAY_MS);
134          assertFalse(t.isAlive());
135 <        assertSame(a.get(this), three);
135 >        assertSame(three, a.get(this));
136      }
137  
138      /**
# Line 147 | Line 141 | public class AtomicReferenceFieldUpdater
141       */
142      public void testWeakCompareAndSet() {
143          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
144 <        try {
151 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
152 <        } catch (RuntimeException ok) {
153 <            return;
154 <        }
144 >        a = updaterFor("x");
145          x = one;
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));
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      /**
# Line 165 | Line 155 | public class AtomicReferenceFieldUpdater
155       */
156      public void testGetAndSet() {
157          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
158 <        try {
169 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
170 <        } catch (RuntimeException ok) {
171 <            return;
172 <        }
158 >        a = updaterFor("x");
159          x = one;
160 <        assertSame(one,a.getAndSet(this, zero));
161 <        assertSame(zero,a.getAndSet(this,m10));
162 <        assertSame(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