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.5 by dl, Sat Oct 25 16:02:13 2003 UTC vs.
Revision 1.11 by jsr166, Mon Nov 2 20:28:31 2009 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/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.atomic.*;
# Line 22 | Line 23 | public class AtomicReferenceFieldUpdater
23      }
24  
25      /**
26 <     * Contruction with non-existent field throws RuntimeException
26 >     * Construction with non-existent field throws RuntimeException
27       */
28      public void testConstructor(){
29          try{
# Line 65 | Line 66 | public class AtomicReferenceFieldUpdater
66       *  get returns the last value set or assigned
67       */
68      public void testGetSet(){
69 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
69 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
70 >        try {
71 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
72 >        } catch (RuntimeException ok) {
73 >            return;
74 >        }
75          x = one;
76          assertEquals(one,a.get(this));
77          a.set(this,two);
78          assertEquals(two,a.get(this));
79 <        a.set(this,-3);
80 <        assertEquals(-3,a.get(this));
81 <        
79 >        a.set(this,m3);
80 >        assertEquals(m3,a.get(this));
81 >    }
82 >
83 >    /**
84 >     *  get returns the last value lazySet by same thread
85 >     */
86 >    public void testGetLazySet(){
87 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
88 >        try {
89 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
90 >        } catch (RuntimeException ok) {
91 >            return;
92 >        }
93 >        x = one;
94 >        assertEquals(one,a.get(this));
95 >        a.lazySet(this,two);
96 >        assertEquals(two,a.get(this));
97 >        a.lazySet(this,m3);
98 >        assertEquals(m3,a.get(this));
99      }
100 +
101      /**
102       * compareAndSet succeeds in changing value if equal to expected else fails
103       */
104      public void testCompareAndSet(){
105 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
105 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
106 >        try {
107 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
108 >        } catch (RuntimeException ok) {
109 >            return;
110 >        }
111          x = one;
112          assertTrue(a.compareAndSet(this,one,two));
113          assertTrue(a.compareAndSet(this,two,m4));
# Line 95 | Line 124 | public class AtomicReferenceFieldUpdater
124       */
125      public void testCompareAndSetInMultipleThreads() {
126          x = one;
127 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
127 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
128 >        try {
129 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
130 >        } catch (RuntimeException ok) {
131 >            return;
132 >        }
133  
134          Thread t = new Thread(new Runnable() {
135                  public void run() {
# Line 115 | Line 149 | public class AtomicReferenceFieldUpdater
149  
150      /**
151       * repeated weakCompareAndSet succeeds in changing value when equal
152 <     * to expected
152 >     * to expected
153       */
154      public void testWeakCompareAndSet(){
155 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
155 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
156 >        try {
157 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
158 >        } catch (RuntimeException ok) {
159 >            return;
160 >        }
161          x = one;
162          while(!a.weakCompareAndSet(this,one,two));
163          while(!a.weakCompareAndSet(this,two,m4));
# Line 131 | Line 170 | public class AtomicReferenceFieldUpdater
170       * getAndSet returns previous value and sets to given value
171       */
172      public void testGetAndSet(){
173 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
173 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
174 >        try {
175 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
176 >        } catch (RuntimeException ok) {
177 >            return;
178 >        }
179          x = one;
180          assertEquals(one,a.getAndSet(this, zero));
181          assertEquals(zero,a.getAndSet(this,m10));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines