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.3 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.8 by dl, Tue Jan 20 20:20:56 2004 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 17 | Line 18 | public class AtomicReferenceFieldUpdater
18      public static void main(String[] args){
19          junit.textui.TestRunner.run(suite());
20      }
20
21  
21      public static Test suite() {
22          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
23      }
24  
25      /**
26 <     *
26 >     * Construction with non-existent field throws RuntimeException
27       */
28      public void testConstructor(){
29          try{
30              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31                  a = AtomicReferenceFieldUpdater.newUpdater
32 <                (getClass(), Integer.class, "y");
32 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
33              shouldThrow();
34          }
35          catch (RuntimeException rt) {}
# Line 38 | Line 37 | public class AtomicReferenceFieldUpdater
37  
38  
39      /**
40 <     *
40 >     * construction with field not of given type throws RuntimeException
41       */
42      public void testConstructor2(){
43          try{
44              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
45                  a = AtomicReferenceFieldUpdater.newUpdater
46 <                (getClass(), Integer.class, "z");
46 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
47              shouldThrow();
48          }
49          catch (RuntimeException rt) {}
50      }
51  
52      /**
53 <     *
53 >     * Constructor with non-volatile field throws exception
54       */
55      public void testConstructor3(){
56          try{
57              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
58                  a = AtomicReferenceFieldUpdater.newUpdater
59 <                (getClass(), Integer.class, "w");
59 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
60              shouldThrow();
61          }
62          catch (RuntimeException rt) {}
63      }
64  
65      /**
66 <     *
66 >     *  get returns the last value set or assigned
67       */
68      public void testGetSet(){
69 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), 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);
# Line 77 | Line 81 | public class AtomicReferenceFieldUpdater
81          
82      }
83      /**
84 <     *
84 >     * compareAndSet succeeds in changing value if equal to expected else fails
85       */
86      public void testCompareAndSet(){
87 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
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          assertTrue(a.compareAndSet(this,one,two));
95          assertTrue(a.compareAndSet(this,two,m4));
# Line 92 | Line 101 | public class AtomicReferenceFieldUpdater
101      }
102  
103      /**
104 <     *
104 >     * compareAndSet in one thread enables another waiting for value
105 >     * to succeed
106 >     */
107 >    public void testCompareAndSetInMultipleThreads() {
108 >        x = one;
109 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
110 >        try {
111 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
112 >        } catch (RuntimeException ok) {
113 >            return;
114 >        }
115 >
116 >        Thread t = new Thread(new Runnable() {
117 >                public void run() {
118 >                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
119 >                }});
120 >        try {
121 >            t.start();
122 >            assertTrue(a.compareAndSet(this, one, two));
123 >            t.join(LONG_DELAY_MS);
124 >            assertFalse(t.isAlive());
125 >            assertEquals(a.get(this), three);
126 >        }
127 >        catch(Exception e) {
128 >            unexpectedException();
129 >        }
130 >    }
131 >
132 >    /**
133 >     * repeated weakCompareAndSet succeeds in changing value when equal
134 >     * to expected
135       */
136      public void testWeakCompareAndSet(){
137 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
137 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
138 >        try {
139 >            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
140 >        } catch (RuntimeException ok) {
141 >            return;
142 >        }
143          x = one;
144          while(!a.weakCompareAndSet(this,one,two));
145          while(!a.weakCompareAndSet(this,two,m4));
# Line 105 | Line 149 | public class AtomicReferenceFieldUpdater
149      }
150  
151      /**
152 <     *
152 >     * getAndSet returns previous value and sets to given value
153       */
154      public void testGetAndSet(){
155 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), 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          assertEquals(one,a.getAndSet(this, zero));
163          assertEquals(zero,a.getAndSet(this,m10));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines