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.26 by jsr166, Mon Apr 1 20:58:58 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 TestCase{
12 > public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
13      volatile Integer x = null;
14      Object z;
15      Integer w;
16  
17 <    public static void main(String[] args){
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);
27 <    static final Integer m3  = new Integer(-3);
28 <    static final Integer m4 = new Integer(-4);
29 <    static final Integer m5 = new Integer(-5);
30 <    static final Integer seven = new Integer(7);
31 <    static final Integer m10 = new Integer(-10);
32 <
33 <    public void testConstructor(){
34 <        try{
35 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
36 <                a = AtomicReferenceFieldUpdater.newUpdater
37 <                (getClass(), Integer.class, "y");
38 <            fail("Exception not thrown");
39 <        }
40 <
41 <        catch (RuntimeException rt) {}
42 <    }
43 <
44 <
45 <    public void testConstructor2(){
46 <        try{
47 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
48 <                a = AtomicReferenceFieldUpdater.newUpdater
49 <                (getClass(), Integer.class, "z");
50 <            fail("Exception not thrown");
51 <        }
52 <
53 <        catch (RuntimeException rt) {}
54 <    }
55 <
56 <    public void testConstructor3(){
57 <        try{
58 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
59 <                a = AtomicReferenceFieldUpdater.newUpdater
60 <                (getClass(), Integer.class, "w");
61 <            fail("Exception not thrown");
62 <        }
63 <
64 <        catch (RuntimeException rt) {}
65 <    }
66 <
67 <    public void testGetSet(){
68 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
69 <        x = one;
70 <        assertEquals(one,a.get(this));
71 <        a.set(this,two);
72 <        assertEquals(two,a.get(this));
73 <        a.set(this,-3);
74 <        assertEquals(-3,a.get(this));
75 <        
76 <    }
77 <    public void testCompareAndSet(){
78 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
79 <        x = one;
80 <        assertTrue(a.compareAndSet(this,one,two));
81 <        assertTrue(a.compareAndSet(this,two,m4));
82 <        assertEquals(m4,a.get(this));
83 <        assertFalse(a.compareAndSet(this,m5,seven));
84 <        assertFalse((seven == a.get(this)));
85 <        assertTrue(a.compareAndSet(this,m4,seven));
86 <        assertEquals(seven,a.get(this));
87 <    }
88 <
89 <    public void testWeakCompareAndSet(){
90 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
91 <        x = one;
92 <        while(!a.weakCompareAndSet(this,one,two));
93 <        while(!a.weakCompareAndSet(this,two,m4));
94 <        assertEquals(m4,a.get(this));
95 <        while(!a.weakCompareAndSet(this,m4,seven));
96 <        assertEquals(seven,a.get(this));
97 <    }
98 <
99 <    public void testGetAndSet(){
100 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");
101 <        x = one;
102 <        assertEquals(one,a.getAndSet(this, zero));
103 <        assertEquals(zero,a.getAndSet(this,m10));
104 <        assertEquals(m10,a.getAndSet(this,1));
24 >    AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
25 >        return AtomicReferenceFieldUpdater.newUpdater
26 >            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
27 >    }
28 >
29 >    /**
30 >     * Construction with non-existent field throws RuntimeException
31 >     */
32 >    public void testConstructor() {
33 >        try {
34 >            updaterFor("y");
35 >            shouldThrow();
36 >        } catch (RuntimeException success) {}
37 >    }
38 >
39 >    /**
40 >     * construction with field not of given type throws RuntimeException
41 >     */
42 >    public void testConstructor2() {
43 >        try {
44 >            updaterFor("z");
45 >            shouldThrow();
46 >        } catch (RuntimeException success) {}
47 >    }
48 >
49 >    /**
50 >     * Constructor with non-volatile field throws exception
51 >     */
52 >    public void testConstructor3() {
53 >        try {
54 >            updaterFor("w");
55 >            shouldThrow();
56 >        } catch (RuntimeException success) {}
57 >    }
58 >
59 >    /**
60 >     * get returns the last value set or assigned
61 >     */
62 >    public void testGetSet() {
63 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
64 >        a = updaterFor("x");
65 >        x = one;
66 >        assertSame(one, a.get(this));
67 >        a.set(this, two);
68 >        assertSame(two, a.get(this));
69 >        a.set(this, m3);
70 >        assertSame(m3, a.get(this));
71 >    }
72 >
73 >    /**
74 >     * get returns the last value lazySet by same thread
75 >     */
76 >    public void testGetLazySet() {
77 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
78 >        a = updaterFor("x");
79 >        x = one;
80 >        assertSame(one, a.get(this));
81 >        a.lazySet(this, two);
82 >        assertSame(two, a.get(this));
83 >        a.lazySet(this, m3);
84 >        assertSame(m3, a.get(this));
85 >    }
86 >
87 >    /**
88 >     * compareAndSet succeeds in changing value if equal to expected else fails
89 >     */
90 >    public void testCompareAndSet() {
91 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
92 >        a = updaterFor("x");
93 >        x = one;
94 >        assertTrue(a.compareAndSet(this, one, two));
95 >        assertTrue(a.compareAndSet(this, two, m4));
96 >        assertSame(m4, a.get(this));
97 >        assertFalse(a.compareAndSet(this, m5, seven));
98 >        assertFalse(seven == a.get(this));
99 >        assertTrue(a.compareAndSet(this, m4, seven));
100 >        assertSame(seven, a.get(this));
101 >    }
102 >
103 >    /**
104 >     * compareAndSet in one thread enables another waiting for value
105 >     * to succeed
106 >     */
107 >    public void testCompareAndSetInMultipleThreads() throws Exception {
108 >        x = one;
109 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
110 >        a = updaterFor("x");
111 >
112 >        Thread t = new Thread(new CheckedRunnable() {
113 >            public void realRun() {
114 >                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
115 >                    Thread.yield();
116 >            }});
117 >
118 >        t.start();
119 >        assertTrue(a.compareAndSet(this, one, two));
120 >        t.join(LONG_DELAY_MS);
121 >        assertFalse(t.isAlive());
122 >        assertSame(three, a.get(this));
123 >    }
124 >
125 >    /**
126 >     * repeated weakCompareAndSet succeeds in changing value when equal
127 >     * to expected
128 >     */
129 >    public void testWeakCompareAndSet() {
130 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
131 >        a = updaterFor("x");
132 >        x = one;
133 >        while (!a.weakCompareAndSet(this, one, two));
134 >        while (!a.weakCompareAndSet(this, two, m4));
135 >        assertSame(m4, a.get(this));
136 >        while (!a.weakCompareAndSet(this, m4, seven));
137 >        assertSame(seven, a.get(this));
138 >    }
139 >
140 >    /**
141 >     * getAndSet returns previous value and sets to given value
142 >     */
143 >    public void testGetAndSet() {
144 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
145 >        a = updaterFor("x");
146 >        x = one;
147 >        assertSame(one, a.getAndSet(this, zero));
148 >        assertSame(zero, a.getAndSet(this, m10));
149 >        assertSame(m10, a.getAndSet(this, 1));
150      }
151  
152   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines