test/jdk/java/util/concurrent/tck/LongAdderTest.java
author dl
Sat, 10 Feb 2018 09:23:41 -0800
changeset 48843 21efc1774302
parent 47216 71c04702a3d5
permissions -rw-r--r--
8195590: Miscellaneous changes imported from jsr166 CVS 2018-02 Reviewed-by: martin, psandoz, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     1
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     3
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     7
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    12
 * accompanied this code).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    13
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    17
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    20
 * questions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    21
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    22
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    23
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    27
 * file:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    28
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
import java.util.concurrent.CyclicBarrier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    35
import java.util.concurrent.Executors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
import java.util.concurrent.atomic.LongAdder;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    39
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    40
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
public class LongAdderTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
        return new TestSuite(LongAdderTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    50
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    51
     * default constructed initializes to zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
    public void testConstructor() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
        assertEquals(0, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
     * add adds given value to current, and sum returns current value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
    public void testAddAndSum() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
        ai.add(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
        assertEquals(2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
        ai.add(-4);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
        assertEquals(-2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    70
     * decrement decrements and sum returns current value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
     */
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 47216
diff changeset
    72
    public void testDecrementAndSum() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
        ai.decrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
        assertEquals(-1, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    76
        ai.decrement();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
        assertEquals(-2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    80
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
     * incrementAndGet increments and returns current value
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
     */
48843
21efc1774302 8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents: 47216
diff changeset
    83
    public void testIncrementAndSum() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    84
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    85
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    86
        assertEquals(1, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    87
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    88
        assertEquals(2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    89
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    90
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    91
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
     * reset() causes subsequent sum() to return zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    94
    public void testReset() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    95
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    96
        ai.add(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    97
        assertEquals(2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    98
        ai.reset();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
        assertEquals(0, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   100
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   101
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   102
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
     * sumThenReset() returns sum; subsequent sum() returns zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   105
    public void testSumThenReset() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   106
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   107
        ai.add(2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   108
        assertEquals(2, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
        assertEquals(2, ai.sumThenReset());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
        assertEquals(0, ai.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   111
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   112
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
    /**
46146
b3e220a04d3f 8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents: 35394
diff changeset
   114
     * a deserialized/reserialized adder holds same value
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   115
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   116
    public void testSerialization() throws Exception {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
        LongAdder x = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
        LongAdder y = serialClone(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
        assertNotSame(x, y);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
        x.add(-22);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
        LongAdder z = serialClone(x);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
        assertNotSame(y, z);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
        assertEquals(-22, x.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
        assertEquals(0, y.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   125
        assertEquals(-22, z.sum());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   126
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   127
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   128
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
     * toString returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   130
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   131
    public void testToString() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   132
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
        assertEquals("0", ai.toString());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   135
        assertEquals(Long.toString(1), ai.toString());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   136
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   137
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   138
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
     * intValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   140
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   141
    public void testIntValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
        assertEquals(0, ai.intValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   144
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   145
        assertEquals(1, ai.intValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   146
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   147
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   148
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
     * longValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   150
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   151
    public void testLongValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
        assertEquals(0, ai.longValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
        assertEquals(1, ai.longValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
     * floatValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   161
    public void testFloatValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
        assertEquals(0.0f, ai.floatValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   165
        assertEquals(1.0f, ai.floatValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
     * doubleValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   170
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   171
    public void testDoubleValue() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   172
        LongAdder ai = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   173
        assertEquals(0.0, ai.doubleValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   174
        ai.increment();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   175
        assertEquals(1.0, ai.doubleValue());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   176
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   177
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   178
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   179
     * adds by multiple threads produce correct sum
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   180
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   181
    public void testAddAndSumMT() throws Throwable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   182
        final int incs = 1000000;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   183
        final int nthreads = 4;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
        final ExecutorService pool = Executors.newCachedThreadPool();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
        LongAdder a = new LongAdder();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
        CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   187
        for (int i = 0; i < nthreads; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
            pool.execute(new AdderTask(a, barrier, incs));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   189
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
        barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
        long total = (long)nthreads * incs;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   192
        long sum = a.sum();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   193
        assertEquals(sum, total);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   194
        pool.shutdown();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   195
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   196
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   197
    static final class AdderTask implements Runnable {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   198
        final LongAdder adder;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   199
        final CyclicBarrier barrier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   200
        final int incs;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   201
        volatile long result;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   202
        AdderTask(LongAdder adder, CyclicBarrier barrier, int incs) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   203
            this.adder = adder;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   204
            this.barrier = barrier;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   205
            this.incs = incs;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   206
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   207
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   208
        public void run() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
            try {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
                LongAdder a = adder;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   212
                for (int i = 0; i < incs; ++i)
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
                    a.add(1L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   214
                result = a.sum();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
                barrier.await();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   216
            } catch (Throwable t) { throw new Error(t); }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
        }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   218
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   219
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   220
}