1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.tests.java.util.stream;
  24 
  25 import org.testng.Assert;
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.Random;
  31 import java.util.concurrent.atomic.AtomicLong;
  32 import java.util.function.LongConsumer;
  33 import java.util.stream.Collectors;
  34 import java.util.stream.LongStream;
  35 
  36 import static org.testng.Assert.assertEquals;
  37 
  38 @Test
  39 public class LongPrimitiveOpsTests {
  40 
  41     public void testSum() {
  42         long sum = LongStream.range(1, 10).filter(i -> i % 2 == 0).sum();
  43         assertEquals(sum, 20);
  44     }
  45 
  46     public void testMap() {
  47         long sum = LongStream.range(1, 10).filter(i -> i % 2 == 0).map(i -> i * 2).sum();
  48         assertEquals(sum, 40);
  49     }
  50 
  51     public void testParSum() {
  52         long sum = LongStream.range(1, 10).parallel().filter(i -> i % 2 == 0).sum();
  53         assertEquals(sum, 20);
  54     }
  55 
  56     @Test(groups = { "serialization-hostile" })
  57     public void testTee() {
  58         long[] teeSum = new long[1];
  59         long sum = LongStream.range(1, 10).filter(i -> i % 2 == 0).peek(i -> { teeSum[0] = teeSum[0] + i; }).sum();
  60         assertEquals(teeSum[0], sum);
  61     }
  62 
  63     @Test(groups = { "serialization-hostile" })
  64     public void testForEach() {
  65         long[] sum = new long[1];
  66         LongStream.range(1, 10).filter(i -> i % 2 == 0).forEach(i -> { sum[0] = sum[0] + i; });
  67         assertEquals(sum[0], 20);
  68     }
  69 
  70     @Test(groups = { "serialization-hostile" })
  71     public void testParForEach() {
  72         AtomicLong ai = new AtomicLong(0);
  73         LongStream.range(1, 10).parallel().filter(i -> i % 2 == 0).forEach(ai::addAndGet);
  74         assertEquals(ai.get(), 20);
  75     }
  76 
  77     public void testBox() {
  78         List<Long> l = LongStream.range(1, 10).parallel().boxed().collect(Collectors.toList());
  79         long sum = l.stream().reduce(0L, (a, b) -> a + b);
  80         assertEquals(sum, 45);
  81     }
  82 
  83     public void testUnBox() {
  84         long sum = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().mapToLong(i -> (long) i).sum();
  85         assertEquals(sum, 15);
  86     }
  87 
  88     public void testToArray() {
  89         {
  90             long[] array =  LongStream.range(1, 10).map(i -> i * 2).toArray();
  91             assertEquals(array, new long[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  92         }
  93 
  94         {
  95             long[] array =  LongStream.range(1, 10).parallel().map(i -> i * 2).toArray();
  96             assertEquals(array, new long[]{2, 4, 6, 8, 10, 12, 14, 16, 18});
  97         }
  98     }
  99 
 100     public void testSort() {
 101         Random r = new Random();
 102 
 103         long[] content = LongStream.generate(() -> r.nextLong()).limit(10).toArray();
 104         long[] sortedContent = content.clone();
 105         Arrays.sort(sortedContent);
 106 
 107         {
 108             long[] array =  Arrays.stream(content).sorted().toArray();
 109             assertEquals(array, sortedContent);
 110         }
 111 
 112         {
 113             long[] array =  Arrays.stream(content).parallel().sorted().toArray();
 114             assertEquals(array, sortedContent);
 115         }
 116     }
 117 
 118     public void testSortSort() {
 119         Random r = new Random();
 120 
 121         long[] content = LongStream.generate(() -> r.nextLong()).limit(10).toArray();
 122         long[] sortedContent = content.clone();
 123         Arrays.sort(sortedContent);
 124 
 125         {
 126             long[] array =  Arrays.stream(content).sorted().sorted().toArray();
 127             assertEquals(array, sortedContent);
 128         }
 129 
 130         {
 131             long[] array =  Arrays.stream(content).parallel().sorted().sorted().toArray();
 132             assertEquals(array, sortedContent);
 133         }
 134     }
 135 
 136     public void testSequential() {
 137 
 138         long[] expected = LongStream.range(1, 1000).toArray();
 139 
 140         class AssertingConsumer implements LongConsumer {
 141             private final long[] array;
 142             int offset;
 143 
 144             AssertingConsumer(long[] array) {
 145                 this.array = array;
 146             }
 147 
 148             @Override
 149             public void accept(long value) {
 150                 assertEquals(array[offset++], value);
 151             }
 152 
 153             public int getCount() { return offset; }
 154         }
 155 
 156         {
 157             AssertingConsumer consumer = new AssertingConsumer(expected);
 158             LongStream.range(1, 1000).sequential().forEach(consumer);
 159             assertEquals(expected.length, consumer.getCount());
 160         }
 161 
 162         {
 163             AssertingConsumer consumer = new AssertingConsumer(expected);
 164             LongStream.range(1, 1000).parallel().sequential().forEach(consumer);
 165             assertEquals(expected.length, consumer.getCount());
 166         }
 167     }
 168 
 169     public void testLimit() {
 170         long[] expected = LongStream.range(1, 10).toArray();
 171 
 172         {
 173             long[] actual = LongStream.iterate(1, i -> i + 1).limit(9).toArray();
 174             Assert.assertTrue(Arrays.equals(expected, actual));
 175         }
 176 
 177         {
 178             long[] actual = LongStream.range(1, 100).parallel().limit(9).toArray();
 179             Assert.assertTrue(Arrays.equals(expected, actual));
 180         }
 181     }
 182 
 183 }