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

Comparing jsr166/src/test/tck/AtomicReferenceTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.31 by jsr166, Wed Jan 27 01:57:24 2021 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  
9 < import junit.framework.*;
10 < import java.util.concurrent.atomic.*;
11 < import java.io.*;
12 <
13 < public class AtomicReferenceTest extends TestCase {
14 <    public static void main (String[] args) {
15 <        junit.textui.TestRunner.run (suite());
9 > import java.util.concurrent.atomic.AtomicReference;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13 >
14 > public class AtomicReferenceTest extends JSR166TestCase {
15 >    public static void main(String[] args) {
16 >        main(suite(), args);
17      }
18      public static Test suite() {
19          return new TestSuite(AtomicReferenceTest.class);
20      }
21  
22 <    static final Integer zero = new Integer(0);
23 <    static final Integer one = new Integer(1);
24 <    static final Integer two = new Integer(2);
25 <    static final Integer m3  = new Integer(-3);
26 <    static final Integer m4 = new Integer(-4);
27 <    static final Integer m5 = new Integer(-5);
28 <    static final Integer seven = new Integer(7);
29 <    static final Integer m10 = new Integer(-10);
30 <
31 <    public void testConstructor(){
32 <        AtomicReference ai = new AtomicReference(one);
33 <        assertEquals(one,ai.get());
34 <    }
35 <
36 <    public void testConstructor2(){
37 <        AtomicReference ai = new AtomicReference();
38 <        assertNull(ai.get());
39 <    }
40 <
41 <    public void testGetSet(){
42 <        AtomicReference ai = new AtomicReference(one);
43 <        assertEquals(one,ai.get());
44 <        ai.set(two);
45 <        assertEquals(two,ai.get());
46 <        ai.set(m3);
47 <        assertEquals(m3,ai.get());
48 <        
49 <    }
50 <    public void testCompareAndSet(){
51 <        AtomicReference ai = new AtomicReference(one);
52 <        assertTrue(ai.compareAndSet(one,two));
53 <        assertTrue(ai.compareAndSet(two,m4));
54 <        assertEquals(m4,ai.get());
55 <        assertFalse(ai.compareAndSet(m5,seven));
56 <        assertFalse((seven.equals(ai.get())));
57 <        assertTrue(ai.compareAndSet(m4,seven));
58 <        assertEquals(seven,ai.get());
59 <    }
60 <
61 <    public void testWeakCompareAndSet(){
62 <        AtomicReference ai = new AtomicReference(one);
63 <        while(!ai.weakCompareAndSet(one,two));
64 <        while(!ai.weakCompareAndSet(two,m4));
65 <        assertEquals(m4,ai.get());
66 <        while(!ai.weakCompareAndSet(m4,seven));
67 <        assertEquals(seven,ai.get());
68 <    }
69 <
70 <    public void testGetAndSet(){
71 <        AtomicReference ai = new AtomicReference(one);
72 <        assertEquals(one,ai.getAndSet(zero));
73 <        assertEquals(zero,ai.getAndSet(m10));
74 <        assertEquals(m10,ai.getAndSet(one));
75 <    }
76 <
77 <    public void testSerialization() {
78 <        AtomicReference l = new AtomicReference();
79 <
80 <        try {
81 <            l.set(one);
82 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
83 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
84 <            out.writeObject(l);
85 <            out.close();
86 <
87 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
88 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
89 <            AtomicReference r = (AtomicReference) in.readObject();
90 <            assertEquals(l.get(), r.get());
91 <        } catch(Exception e){
92 <            e.printStackTrace();
93 <            fail("unexpected exception");
94 <        }
22 >    /**
23 >     * constructor initializes to given value
24 >     */
25 >    public void testConstructor() {
26 >        AtomicReference<Item> ai = new AtomicReference<>(one);
27 >        assertSame(one, ai.get());
28 >    }
29 >
30 >    /**
31 >     * default constructed initializes to null
32 >     */
33 >    public void testConstructor2() {
34 >        AtomicReference<Item> ai = new AtomicReference<>();
35 >        assertNull(ai.get());
36 >    }
37 >
38 >    /**
39 >     * get returns the last value set
40 >     */
41 >    public void testGetSet() {
42 >        AtomicReference<Item> ai = new AtomicReference<>(one);
43 >        assertSame(one, ai.get());
44 >        ai.set(two);
45 >        assertSame(two, ai.get());
46 >        ai.set(minusThree);
47 >        assertSame(minusThree, ai.get());
48 >    }
49 >
50 >    /**
51 >     * get returns the last value lazySet in same thread
52 >     */
53 >    public void testGetLazySet() {
54 >        AtomicReference<Item> ai = new AtomicReference<>(one);
55 >        assertSame(one, ai.get());
56 >        ai.lazySet(two);
57 >        assertSame(two, ai.get());
58 >        ai.lazySet(minusThree);
59 >        assertSame(minusThree, ai.get());
60 >    }
61 >
62 >    /**
63 >     * compareAndSet succeeds in changing value if equal to expected else fails
64 >     */
65 >    public void testCompareAndSet() {
66 >        AtomicReference<Item> ai = new AtomicReference<>(one);
67 >        assertTrue(ai.compareAndSet(one, two));
68 >        assertTrue(ai.compareAndSet(two, minusFour));
69 >        assertSame(minusFour, ai.get());
70 >        assertFalse(ai.compareAndSet(minusFive, seven));
71 >        assertSame(minusFour, ai.get());
72 >        assertTrue(ai.compareAndSet(minusFour, seven));
73 >        assertSame(seven, ai.get());
74 >    }
75 >
76 >    /**
77 >     * compareAndSet in one thread enables another waiting for value
78 >     * to succeed
79 >     */
80 >    public void testCompareAndSetInMultipleThreads() throws Exception {
81 >        final AtomicReference<Item> ai = new AtomicReference<>(one);
82 >        Thread t = new Thread(new CheckedRunnable() {
83 >            public void realRun() {
84 >                while (!ai.compareAndSet(two, three))
85 >                    Thread.yield();
86 >            }});
87 >
88 >        t.start();
89 >        assertTrue(ai.compareAndSet(one, two));
90 >        t.join(LONG_DELAY_MS);
91 >        assertFalse(t.isAlive());
92 >        assertSame(three, ai.get());
93 >    }
94 >
95 >    /**
96 >     * repeated weakCompareAndSet succeeds in changing value when equal
97 >     * to expected
98 >     */
99 >    @SuppressWarnings("deprecation")
100 >    public void testWeakCompareAndSet() {
101 >        AtomicReference<Item> ai = new AtomicReference<>(one);
102 >        do {} while (!ai.weakCompareAndSet(one, two));
103 >        do {} while (!ai.weakCompareAndSet(two, minusFour));
104 >        assertSame(minusFour, ai.get());
105 >        do {} while (!ai.weakCompareAndSet(minusFour, seven));
106 >        assertSame(seven, ai.get());
107 >    }
108 >
109 >    /**
110 >     * getAndSet returns previous value and sets to given value
111 >     */
112 >    public void testGetAndSet() {
113 >        AtomicReference<Item> ai = new AtomicReference<>(one);
114 >        assertSame(one, ai.getAndSet(zero));
115 >        assertSame(zero, ai.getAndSet(minusTen));
116 >        assertSame(minusTen, ai.getAndSet(one));
117 >    }
118 >
119 >    /**
120 >     * a deserialized/reserialized atomic holds same value
121 >     */
122 >    public void testSerialization() throws Exception {
123 >        AtomicReference<Item> x = new AtomicReference<>();
124 >        AtomicReference<Item> y = serialClone(x);
125 >        assertNotSame(x, y);
126 >        x.set(one);
127 >        AtomicReference<Item> z = serialClone(x);
128 >        assertNotSame(y, z);
129 >        assertEquals(one, x.get());
130 >        assertNull(y.get());
131 >        assertEquals(one, z.get());
132 >    }
133 >
134 >    /**
135 >     * toString returns current value.
136 >     */
137 >    public void testToString() {
138 >        AtomicReference<Item> ai = new AtomicReference<>(one);
139 >        assertEquals(one.toString(), ai.toString());
140 >        ai.set(two);
141 >        assertEquals(two.toString(), ai.toString());
142      }
143  
144   }
96

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines