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.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.5 by dl, Sat Oct 25 16:02:13 2003 UTC

# Line 9 | Line 9 | import java.util.concurrent.atomic.*;
9   import junit.framework.*;
10   import java.util.*;
11  
12 < public class AtomicReferenceFieldUpdaterTest extends TestCase{
12 > public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
13      volatile Integer x = null;
14      Object z;
15      Integer w;
# Line 17 | Line 17 | public class AtomicReferenceFieldUpdater
17      public static void main(String[] args){
18          junit.textui.TestRunner.run(suite());
19      }
20
21  
20      public static Test suite() {
21          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
22      }
23  
24 <    static final Integer zero = new Integer(0);
25 <    static final Integer one = new Integer(1);
26 <    static final Integer two = new Integer(2);
29 <    static final Integer m3  = new Integer(-3);
30 <    static final Integer m4 = new Integer(-4);
31 <    static final Integer m5 = new Integer(-5);
32 <    static final Integer seven = new Integer(7);
33 <    static final Integer m10 = new Integer(-10);
34 <
24 >    /**
25 >     * Contruction with non-existent field throws RuntimeException
26 >     */
27      public void testConstructor(){
28          try{
29              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
30                  a = AtomicReferenceFieldUpdater.newUpdater
31 <                (getClass(), Integer.class, "y");
32 <            fail("Exception not thrown");
31 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
32 >            shouldThrow();
33          }
42
34          catch (RuntimeException rt) {}
35      }
36  
37  
38 +    /**
39 +     * construction with field not of given type throws RuntimeException
40 +     */
41      public void testConstructor2(){
42          try{
43              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
44                  a = AtomicReferenceFieldUpdater.newUpdater
45 <                (getClass(), Integer.class, "z");
46 <            fail("Exception not thrown");
45 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
46 >            shouldThrow();
47          }
54
48          catch (RuntimeException rt) {}
49      }
50  
51 +    /**
52 +     * Constructor with non-volatile field throws exception
53 +     */
54      public void testConstructor3(){
55          try{
56              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
57                  a = AtomicReferenceFieldUpdater.newUpdater
58 <                (getClass(), Integer.class, "w");
59 <            fail("Exception not thrown");
58 >                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
59 >            shouldThrow();
60          }
65
61          catch (RuntimeException rt) {}
62      }
63  
64 +    /**
65 +     *  get returns the last value set or assigned
66 +     */
67      public void testGetSet(){
68 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
68 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
69          x = one;
70          assertEquals(one,a.get(this));
71          a.set(this,two);
# Line 76 | Line 74 | public class AtomicReferenceFieldUpdater
74          assertEquals(-3,a.get(this));
75          
76      }
77 +    /**
78 +     * compareAndSet succeeds in changing value if equal to expected else fails
79 +     */
80      public void testCompareAndSet(){
81 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
81 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
82          x = one;
83          assertTrue(a.compareAndSet(this,one,two));
84          assertTrue(a.compareAndSet(this,two,m4));
# Line 88 | Line 89 | public class AtomicReferenceFieldUpdater
89          assertEquals(seven,a.get(this));
90      }
91  
92 +    /**
93 +     * compareAndSet in one thread enables another waiting for value
94 +     * to succeed
95 +     */
96 +    public void testCompareAndSetInMultipleThreads() {
97 +        x = one;
98 +        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
99 +
100 +        Thread t = new Thread(new Runnable() {
101 +                public void run() {
102 +                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
103 +                }});
104 +        try {
105 +            t.start();
106 +            assertTrue(a.compareAndSet(this, one, two));
107 +            t.join(LONG_DELAY_MS);
108 +            assertFalse(t.isAlive());
109 +            assertEquals(a.get(this), three);
110 +        }
111 +        catch(Exception e) {
112 +            unexpectedException();
113 +        }
114 +    }
115 +
116 +    /**
117 +     * repeated weakCompareAndSet succeeds in changing value when equal
118 +     * to expected
119 +     */
120      public void testWeakCompareAndSet(){
121 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
121 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
122          x = one;
123          while(!a.weakCompareAndSet(this,one,two));
124          while(!a.weakCompareAndSet(this,two,m4));
# Line 98 | Line 127 | public class AtomicReferenceFieldUpdater
127          assertEquals(seven,a.get(this));
128      }
129  
130 +    /**
131 +     * getAndSet returns previous value and sets to given value
132 +     */
133      public void testGetAndSet(){
134 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
134 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
135          x = one;
136          assertEquals(one,a.getAndSet(this, zero));
137          assertEquals(zero,a.getAndSet(this,m10));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines