ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicStampedReferenceTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicStampedReferenceTest.java (file contents):
Revision 1.6 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.21 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 1 | Line 1
1   /*
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.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.concurrent.atomic.*;
9 > import java.util.concurrent.atomic.AtomicStampedReference;
10  
11 < public class AtomicStampedReferenceTest extends JSR166TestCase{
12 <    public static void main (String[] args) {
13 <        junit.textui.TestRunner.run (suite());
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13 >
14 > public class AtomicStampedReferenceTest extends JSR166TestCase {
15 >    public static void main(String[] args) {
16 >        junit.textui.TestRunner.run(suite());
17      }
18      public static Test suite() {
19          return new TestSuite(AtomicStampedReferenceTest.class);
20      }
21 <    
21 >
22      /**
23 <     * constructeor initializes to given reference and stamp
23 >     * constructor initializes to given reference and stamp
24       */
25 <    public void testConstructor(){
25 >    public void testConstructor() {
26          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
27 <        assertEquals(one,ai.getReference());
28 <        assertEquals(0, ai.getStamp());
27 >        assertSame(one, ai.getReference());
28 >        assertEquals(0, ai.getStamp());
29          AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
30 <        assertNull(a2.getReference());
31 <        assertEquals(1, a2.getStamp());
30 <
30 >        assertNull(a2.getReference());
31 >        assertEquals(1, a2.getStamp());
32      }
33  
34      /**
35 <     *  get returns the last values of reference and stamp set
35 >     * get returns the last values of reference and stamp set
36       */
37 <    public void testGetSet(){
37 >    public void testGetSet() {
38          int[] mark = new int[1];
39          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
40 <        assertEquals(one,ai.getReference());
41 <        assertEquals(0, ai.getStamp());
42 <        assertEquals(one, ai.get(mark));
40 >        assertSame(one, ai.getReference());
41 >        assertEquals(0, ai.getStamp());
42 >        assertSame(one, ai.get(mark));
43          assertEquals(0, mark[0]);
44 <        ai.set(two, 0);
45 <        assertEquals(two,ai.getReference());
46 <        assertEquals(0, ai.getStamp());
47 <        assertEquals(two, ai.get(mark));
44 >        ai.set(two, 0);
45 >        assertSame(two, ai.getReference());
46 >        assertEquals(0, ai.getStamp());
47 >        assertSame(two, ai.get(mark));
48          assertEquals(0, mark[0]);
49 <        ai.set(one, 1);
50 <        assertEquals(one,ai.getReference());
51 <        assertEquals(1, ai.getStamp());
52 <        assertEquals(one, ai.get(mark));
53 <        assertEquals(1,mark[0]);
49 >        ai.set(one, 1);
50 >        assertSame(one, ai.getReference());
51 >        assertEquals(1, ai.getStamp());
52 >        assertSame(one, ai.get(mark));
53 >        assertEquals(1, mark[0]);
54      }
55  
56      /**
57 <     *  attemptStamp succeeds in single thread
57 >     * attemptStamp succeeds in single thread
58       */
59 <    public void testAttemptStamp(){
59 >    public void testAttemptStamp() {
60          int[] mark = new int[1];
61          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
62          assertEquals(0, ai.getStamp());
63          assertTrue(ai.attemptStamp(one, 1));
64 <        assertEquals(1, ai.getStamp());
65 <        assertEquals(one, ai.get(mark));
64 >        assertEquals(1, ai.getStamp());
65 >        assertSame(one, ai.get(mark));
66          assertEquals(1, mark[0]);
67      }
68  
# Line 69 | Line 70 | public class AtomicStampedReferenceTest
70       * compareAndSet succeeds in changing values if equal to expected reference
71       * and stamp else fails
72       */
73 <    public void testCompareAndSet(){
73 >    public void testCompareAndSet() {
74          int[] mark = new int[1];
75          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
76 <        assertEquals(one, ai.get(mark));
76 >        assertSame(one, ai.get(mark));
77          assertEquals(0, ai.getStamp());
78 <        assertEquals(0, mark[0]);
78 >        assertEquals(0, mark[0]);
79  
80          assertTrue(ai.compareAndSet(one, two, 0, 0));
81 <        assertEquals(two, ai.get(mark));
82 <        assertEquals(0, mark[0]);
81 >        assertSame(two, ai.get(mark));
82 >        assertEquals(0, mark[0]);
83  
84          assertTrue(ai.compareAndSet(two, m3, 0, 1));
85 <        assertEquals(m3, ai.get(mark));
86 <        assertEquals(1, mark[0]);
85 >        assertSame(m3, ai.get(mark));
86 >        assertEquals(1, mark[0]);
87  
88          assertFalse(ai.compareAndSet(two, m3, 1, 1));
89 <        assertEquals(m3, ai.get(mark));
90 <        assertEquals(1, mark[0]);
89 >        assertSame(m3, ai.get(mark));
90 >        assertEquals(1, mark[0]);
91      }
92  
93      /**
94       * compareAndSet in one thread enables another waiting for reference value
95       * to succeed
96       */
97 <    public void testCompareAndSetInMultipleThreads() {
97 >    public void testCompareAndSetInMultipleThreads() throws Exception {
98          final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
99 <        Thread t = new Thread(new Runnable() {
100 <                public void run() {
101 <                    while(!ai.compareAndSet(two, three, 0, 0)) Thread.yield();
102 <                }});
103 <        try {
104 <            t.start();
105 <            assertTrue(ai.compareAndSet(one, two, 0, 0));
106 <            t.join(LONG_DELAY_MS);
107 <            assertFalse(t.isAlive());
108 <            assertEquals(ai.getReference(), three);
109 <            assertEquals(ai.getStamp(), 0);
110 <        }
110 <        catch(Exception e) {
111 <            unexpectedException();
112 <        }
99 >        Thread t = new Thread(new CheckedRunnable() {
100 >            public void realRun() {
101 >                while (!ai.compareAndSet(two, three, 0, 0))
102 >                    Thread.yield();
103 >            }});
104 >
105 >        t.start();
106 >        assertTrue(ai.compareAndSet(one, two, 0, 0));
107 >        t.join(LONG_DELAY_MS);
108 >        assertFalse(t.isAlive());
109 >        assertSame(three, ai.getReference());
110 >        assertEquals(0, ai.getStamp());
111      }
112  
113      /**
114       * compareAndSet in one thread enables another waiting for stamp value
115       * to succeed
116       */
117 <    public void testCompareAndSetInMultipleThreads2() {
117 >    public void testCompareAndSetInMultipleThreads2() throws Exception {
118          final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
119 <        Thread t = new Thread(new Runnable() {
120 <                public void run() {
121 <                    while(!ai.compareAndSet(one, one, 1, 2)) Thread.yield();
122 <                }});
123 <        try {
124 <            t.start();
125 <            assertTrue(ai.compareAndSet(one, one, 0, 1));
126 <            t.join(LONG_DELAY_MS);
127 <            assertFalse(t.isAlive());
128 <            assertEquals(ai.getReference(), one);
129 <            assertEquals(ai.getStamp(), 2);
130 <        }
133 <        catch(Exception e) {
134 <            unexpectedException();
135 <        }
119 >        Thread t = new Thread(new CheckedRunnable() {
120 >            public void realRun() {
121 >                while (!ai.compareAndSet(one, one, 1, 2))
122 >                    Thread.yield();
123 >            }});
124 >
125 >        t.start();
126 >        assertTrue(ai.compareAndSet(one, one, 0, 1));
127 >        t.join(LONG_DELAY_MS);
128 >        assertFalse(t.isAlive());
129 >        assertSame(one, ai.getReference());
130 >        assertEquals(2, ai.getStamp());
131      }
132  
133      /**
134       * repeated weakCompareAndSet succeeds in changing values when equal
135 <     * to expected
135 >     * to expected
136       */
137 <    public void testWeakCompareAndSet(){
137 >    public void testWeakCompareAndSet() {
138          int[] mark = new int[1];
139          AtomicStampedReference ai = new AtomicStampedReference(one, 0);
140 <        assertEquals(one, ai.get(mark));
141 <        assertEquals(0, ai.getStamp ());
142 <        assertEquals(0, mark[0]);
143 <
144 <        while(!ai.weakCompareAndSet(one, two, 0, 0));
145 <        assertEquals(two, ai.get(mark));
146 <        assertEquals(0, mark[0]);
147 <
148 <        while(!ai.weakCompareAndSet(two, m3, 0, 1));
149 <        assertEquals(m3, ai.get(mark));
150 <        assertEquals(1, mark[0]);
140 >        assertSame(one, ai.get(mark));
141 >        assertEquals(0, ai.getStamp());
142 >        assertEquals(0, mark[0]);
143 >
144 >        while (!ai.weakCompareAndSet(one, two, 0, 0));
145 >        assertSame(two, ai.get(mark));
146 >        assertEquals(0, mark[0]);
147 >
148 >        while (!ai.weakCompareAndSet(two, m3, 0, 1));
149 >        assertSame(m3, ai.get(mark));
150 >        assertEquals(1, mark[0]);
151      }
152  
153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines