ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LongAccumulatorTest.java
Revision: 1.10
Committed: Mon Sep 9 01:05:22 2019 UTC (4 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +1 -1 lines
Log Message:
run fewer iterations when !expensiveTests

File Contents

# Content
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/publicdomain/zero/1.0/
5 */
6
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.Phaser;
10 import java.util.concurrent.ThreadLocalRandom;
11 import java.util.concurrent.atomic.LongAccumulator;
12
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 public class LongAccumulatorTest extends JSR166TestCase {
17 public static void main(String[] args) {
18 main(suite(), args);
19 }
20 public static Test suite() {
21 return new TestSuite(LongAccumulatorTest.class);
22 }
23
24 /**
25 * new instance initialized to supplied identity
26 */
27 public void testConstructor() {
28 for (long identity : new long[] { Long.MIN_VALUE, 0, Long.MAX_VALUE })
29 assertEquals(identity,
30 new LongAccumulator(Long::max, identity).get());
31 }
32
33 /**
34 * accumulate accumulates given value to current, and get returns current value
35 */
36 public void testAccumulateAndGet() {
37 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
38 acc.accumulate(2);
39 assertEquals(2, acc.get());
40 acc.accumulate(-4);
41 assertEquals(2, acc.get());
42 acc.accumulate(4);
43 assertEquals(4, acc.get());
44 }
45
46 /**
47 * reset() causes subsequent get() to return zero
48 */
49 public void testReset() {
50 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
51 acc.accumulate(2);
52 assertEquals(2, acc.get());
53 acc.reset();
54 assertEquals(0, acc.get());
55 }
56
57 /**
58 * getThenReset() returns current value; subsequent get() returns zero
59 */
60 public void testGetThenReset() {
61 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
62 acc.accumulate(2);
63 assertEquals(2, acc.get());
64 assertEquals(2, acc.getThenReset());
65 assertEquals(0, acc.get());
66 }
67
68 /**
69 * toString returns current value.
70 */
71 public void testToString() {
72 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
73 assertEquals("0", acc.toString());
74 acc.accumulate(1);
75 assertEquals(Long.toString(1), acc.toString());
76 }
77
78 /**
79 * intValue returns current value.
80 */
81 public void testIntValue() {
82 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
83 assertEquals(0, acc.intValue());
84 acc.accumulate(1);
85 assertEquals(1, acc.intValue());
86 }
87
88 /**
89 * longValue returns current value.
90 */
91 public void testLongValue() {
92 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
93 assertEquals(0, acc.longValue());
94 acc.accumulate(1);
95 assertEquals(1, acc.longValue());
96 }
97
98 /**
99 * floatValue returns current value.
100 */
101 public void testFloatValue() {
102 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
103 assertEquals(0.0f, acc.floatValue());
104 acc.accumulate(1);
105 assertEquals(1.0f, acc.floatValue());
106 }
107
108 /**
109 * doubleValue returns current value.
110 */
111 public void testDoubleValue() {
112 LongAccumulator acc = new LongAccumulator(Long::max, 0L);
113 assertEquals(0.0, acc.doubleValue());
114 acc.accumulate(1);
115 assertEquals(1.0, acc.doubleValue());
116 }
117
118 /**
119 * accumulates by multiple threads produce correct result
120 */
121 public void testAccumulateAndGetMT() {
122 final LongAccumulator acc
123 = new LongAccumulator((x, y) -> x + y, 0L);
124 final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
125 final Phaser phaser = new Phaser(nThreads + 1);
126 final int incs = expensiveTests ? 1_000_000 : 100_000;
127 final long total = nThreads * incs/2L * (incs - 1); // Gauss
128 final Runnable task = () -> {
129 phaser.arriveAndAwaitAdvance();
130 for (int i = 0; i < incs; i++) {
131 acc.accumulate((long) i);
132 assertTrue(acc.get() <= total);
133 }
134 phaser.arrive();
135 };
136 final ExecutorService p = Executors.newCachedThreadPool();
137 try (PoolCleaner cleaner = cleaner(p)) {
138 for (int i = nThreads; i-->0; )
139 p.execute(task);
140 phaser.arriveAndAwaitAdvance();
141 phaser.arriveAndAwaitAdvance();
142 assertEquals(total, acc.get());
143 }
144 }
145
146 }