test/jdk/java/util/concurrent/tck/DoubleAccumulatorTest.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
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.Executors;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    35
import java.util.concurrent.ExecutorService;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import java.util.concurrent.Phaser;
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    37
import java.util.concurrent.ThreadLocalRandom;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    38
import java.util.concurrent.atomic.DoubleAccumulator;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    39
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    40
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    41
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    42
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    43
public class DoubleAccumulatorTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    44
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    46
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
        return new TestSuite(DoubleAccumulatorTest.class);
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
    /**
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    52
     * new instance initialized to supplied identity
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
    public void testConstructor() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    55
        for (double identity : new double[] {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    56
                 Double.NEGATIVE_INFINITY,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    57
                 Double.POSITIVE_INFINITY,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    58
                 Double.MIN_VALUE,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    59
                 Double.MAX_VALUE,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    60
                 0.0,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    61
             })
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    62
            assertEquals(identity,
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    63
                         new DoubleAccumulator(Double::max, identity).get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
     * accumulate accumulates given value to current, and get returns current value
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
    public void testAccumulateAndGet() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    70
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    71
        acc.accumulate(2.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    72
        assertEquals(2.0, acc.get());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    73
        acc.accumulate(-4.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    74
        assertEquals(2.0, acc.get());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    75
        acc.accumulate(4.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    76
        assertEquals(4.0, acc.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
    }
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
     * reset() causes subsequent get() to return zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    81
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
    public void testReset() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    83
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    84
        acc.accumulate(2.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    85
        assertEquals(2.0, acc.get());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    86
        acc.reset();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    87
        assertEquals(0.0, acc.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    88
    }
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
     * getThenReset() returns current value; subsequent get() returns zero
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    92
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    93
    public void testGetThenReset() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    94
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    95
        acc.accumulate(2.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    96
        assertEquals(2.0, acc.get());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    97
        assertEquals(2.0, acc.getThenReset());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
    98
        assertEquals(0.0, acc.get());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    99
    }
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
     * toString returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   103
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   104
    public void testToString() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   105
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   106
        assertEquals("0.0", acc.toString());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   107
        acc.accumulate(1.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   108
        assertEquals(Double.toString(1.0), acc.toString());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   109
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   110
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
     * intValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   114
    public void testIntValue() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   115
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   116
        assertEquals(0, acc.intValue());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   117
        acc.accumulate(1.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   118
        assertEquals(1, acc.intValue());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   119
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   120
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   121
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   122
     * longValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   123
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   124
    public void testLongValue() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   125
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   126
        assertEquals(0, acc.longValue());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   127
        acc.accumulate(1.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   128
        assertEquals(1, acc.longValue());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   129
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   132
     * floatValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   133
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   134
    public void testFloatValue() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   135
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   136
        assertEquals(0.0f, acc.floatValue());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   137
        acc.accumulate(1.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   138
        assertEquals(1.0f, acc.floatValue());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   139
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   142
     * doubleValue returns current value.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   143
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   144
    public void testDoubleValue() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   145
        DoubleAccumulator acc = new DoubleAccumulator(Double::max, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   146
        assertEquals(0.0, acc.doubleValue());
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   147
        acc.accumulate(1.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   148
        assertEquals(1.0, acc.doubleValue());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   149
    }
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
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   152
     * accumulates by multiple threads produce correct result
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   153
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   154
    public void testAccumulateAndGetMT() {
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   155
        final DoubleAccumulator acc
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   156
            = new DoubleAccumulator((x, y) -> x + y, 0.0);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   157
        final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   158
        final Phaser phaser = new Phaser(nThreads + 1);
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   159
        final int incs = expensiveTests ? 1_000_000 : 100_000;
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   160
        final double total = nThreads * incs/2.0 * (incs - 1); // Gauss
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   161
        final Runnable task = () -> {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
            phaser.arriveAndAwaitAdvance();
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   163
            for (int i = 0; i < incs; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   164
                acc.accumulate((double) i);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   165
                assertTrue(acc.get() <= total);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   166
            }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   167
            phaser.arrive();
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   168
        };
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   169
        final ExecutorService p = Executors.newCachedThreadPool();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   170
        try (PoolCleaner cleaner = cleaner(p)) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   171
            for (int i = nThreads; i-->0; )
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   172
                p.execute(task);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   173
            phaser.arriveAndAwaitAdvance();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   174
            phaser.arriveAndAwaitAdvance();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 35394
diff changeset
   175
            assertEquals(total, acc.get());
35394
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
}