ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
Revision: 1.5
Committed: Sat Oct 25 16:02:13 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +8 -8 lines
Log Message:
Added 	PrivilegedFutureTaskTest

File Contents

# User Rev Content
1 dl 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.
6     */
7    
8     import java.util.concurrent.atomic.*;
9     import junit.framework.*;
10     import java.util.*;
11    
12 dl 1.2 public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
13 dl 1.1 volatile Integer x = null;
14     Object z;
15     Integer w;
16    
17     public static void main(String[] args){
18     junit.textui.TestRunner.run(suite());
19     }
20     public static Test suite() {
21     return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
22     }
23    
24 dl 1.3 /**
25 dl 1.4 * Contruction with non-existent field throws RuntimeException
26 dl 1.3 */
27 dl 1.1 public void testConstructor(){
28     try{
29     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
30     a = AtomicReferenceFieldUpdater.newUpdater
31 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
32 dl 1.3 shouldThrow();
33 dl 1.1 }
34     catch (RuntimeException rt) {}
35     }
36    
37    
38 dl 1.3 /**
39 dl 1.4 * construction with field not of given type throws RuntimeException
40 dl 1.3 */
41 dl 1.1 public void testConstructor2(){
42     try{
43     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
44     a = AtomicReferenceFieldUpdater.newUpdater
45 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
46 dl 1.3 shouldThrow();
47 dl 1.1 }
48     catch (RuntimeException rt) {}
49     }
50    
51 dl 1.3 /**
52 dl 1.4 * Constructor with non-volatile field throws exception
53 dl 1.3 */
54 dl 1.1 public void testConstructor3(){
55     try{
56     AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
57     a = AtomicReferenceFieldUpdater.newUpdater
58 dl 1.5 (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
59 dl 1.3 shouldThrow();
60 dl 1.1 }
61     catch (RuntimeException rt) {}
62     }
63    
64 dl 1.3 /**
65 dl 1.4 * get returns the last value set or assigned
66 dl 1.3 */
67 dl 1.1 public void testGetSet(){
68 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
69 dl 1.1 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 dl 1.3 /**
78 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
79 dl 1.3 */
80 dl 1.1 public void testCompareAndSet(){
81 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
82 dl 1.1 x = one;
83     assertTrue(a.compareAndSet(this,one,two));
84     assertTrue(a.compareAndSet(this,two,m4));
85     assertEquals(m4,a.get(this));
86     assertFalse(a.compareAndSet(this,m5,seven));
87     assertFalse((seven == a.get(this)));
88     assertTrue(a.compareAndSet(this,m4,seven));
89     assertEquals(seven,a.get(this));
90     }
91    
92 dl 1.3 /**
93 dl 1.4 * compareAndSet in one thread enables another waiting for value
94     * to succeed
95     */
96     public void testCompareAndSetInMultipleThreads() {
97     x = one;
98 dl 1.5 final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
99 dl 1.4
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 dl 1.3 */
120 dl 1.1 public void testWeakCompareAndSet(){
121 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
122 dl 1.1 x = one;
123     while(!a.weakCompareAndSet(this,one,two));
124     while(!a.weakCompareAndSet(this,two,m4));
125     assertEquals(m4,a.get(this));
126     while(!a.weakCompareAndSet(this,m4,seven));
127     assertEquals(seven,a.get(this));
128     }
129    
130 dl 1.3 /**
131 dl 1.4 * getAndSet returns previous value and sets to given value
132 dl 1.3 */
133 dl 1.1 public void testGetAndSet(){
134 dl 1.5 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
135 dl 1.1 x = one;
136     assertEquals(one,a.getAndSet(this, zero));
137     assertEquals(zero,a.getAndSet(this,m10));
138     assertEquals(m10,a.getAndSet(this,1));
139     }
140    
141     }