/*
 * Copyright 2015, 2020 StreamEx contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package one.util.streamex;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;

import org.junit.Test;

import static one.util.streamex.TestHelpers.consumeElement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class PrefixOpsTest {
  @Test
  public void testNoSplitAfterAdvance() {
    Spliterator<Integer> spliterator = IntStreamEx.range(100).boxed().unordered().prefix(Integer::sum).spliterator();
    consumeElement(spliterator, 0);
    consumeElement(spliterator, 1);
    consumeElement(spliterator, 3);
    consumeElement(spliterator, 6);
    assertNull(spliterator.trySplit());
  }

  @Test
  public void testIntNoSplitAfterAdvance() {
    Spliterator.OfInt spliterator = IntStreamEx.range(100).unordered().prefix(Integer::sum).spliterator();
    consumeElement(spliterator, 0);
    consumeElement(spliterator, 1);
    consumeElement(spliterator, 3);
    consumeElement(spliterator, 6);
    assertNull(spliterator.trySplit());
  }

  @Test
  public void testLongNoSplitAfterAdvance() {
    Spliterator.OfLong spliterator = LongStreamEx.range(100).unordered().prefix(Long::sum).spliterator();
    assertNull(spliterator.trySplit());
    consumeElement(spliterator, 0L);
    consumeElement(spliterator, 1L);
    consumeElement(spliterator, 3L);
    consumeElement(spliterator, 6L);
    assertNull(spliterator.trySplit());
  }
  
  @Test
  public void testForEachAfterSplitAndAdvance() {
    Spliterator<String> spliterator = StreamEx.constant("a", 5).unordered().prefix(String::concat).spliterator();
    Spliterator<String> spliterator2 = spliterator.trySplit();
    assertNotNull(spliterator2);
    Set<String> remainingElements = new HashSet<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa"));
    consumeElement(spliterator, remainingElements);
    consumeElement(spliterator2, remainingElements);
    spliterator.forEachRemaining(x -> assertTrue(remainingElements.remove(x)));
    spliterator2.forEachRemaining(x -> assertTrue(remainingElements.remove(x)));
    assertEquals(0, remainingElements.size());
  }
  
  @Test
  public void testIntForEachAfterSplitAndAdvance() {
    Spliterator.OfInt spliterator = IntStreamEx.constant(1, 5).unordered().prefix(Integer::sum).spliterator();
    Spliterator.OfInt spliterator2 = spliterator.trySplit();
    assertNotNull(spliterator2);
    Set<Integer> remainingElements = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
    consumeElement(spliterator, remainingElements);
    consumeElement(spliterator2, remainingElements);
    spliterator.forEachRemaining((IntConsumer) x -> assertTrue(remainingElements.remove(x)));
    spliterator2.forEachRemaining((IntConsumer) x -> assertTrue(remainingElements.remove(x)));
    assertEquals(0, remainingElements.size());
  }
  
  @Test
  public void testLongForEachAfterSplitAndAdvance() {
    Spliterator.OfLong spliterator = LongStreamEx.constant(1, 5).unordered().prefix(Long::sum).spliterator();
    Spliterator.OfLong spliterator2 = spliterator.trySplit();
    assertNotNull(spliterator2);
    Set<Long> remainingElements = new HashSet<>(Arrays.asList(1L, 2L, 3L, 4L, 5L));
    consumeElement(spliterator, remainingElements);
    consumeElement(spliterator2, remainingElements);
    spliterator.forEachRemaining((LongConsumer) x -> assertTrue(remainingElements.remove(x)));
    spliterator2.forEachRemaining((LongConsumer) x -> assertTrue(remainingElements.remove(x)));
    assertEquals(0, remainingElements.size());
  }
}

@@ -1173,23 +1173,22 @@
 terator%3C
-Integer
+String
 %3E splite
@@ -1187,35 +1187,32 @@
 %3E spliterator = 
-Int
 StreamEx.range(1
@@ -1208,25 +1208,25 @@
 mEx.
-range(100).boxed(
+constant(%22a%22, 100
 ).un
@@ -1238,36 +1238,38 @@
 ed().prefix(
-Integer::sum
+String::concat
 ).spliterato
@@ -1297,33 +1297,35 @@
 nt(spliterator, 
-0
+%22a%22
 );%0A    consumeEl
@@ -1335,33 +1335,36 @@
 nt(spliterator, 
-1
+%22aa%22
 );%0A    consumeEl
@@ -1374,33 +1374,37 @@
 nt(spliterator, 
-3
+%22aaa%22
 );%0A    consumeEl
@@ -1414,33 +1414,38 @@
 nt(spliterator, 
-6
+%22aaaa%22
 );%0A    assertNul
@@ -1569,38 +1569,44 @@
 r = IntStreamEx.
-range(
+constant(1, 
 100).unordered()
@@ -1674,17 +1674,17 @@
 erator, 
-0
+1
 );%0A    c
@@ -1702,33 +1702,33 @@
 nt(spliterator, 
-1
+2
 );%0A    consumeEl
@@ -1782,17 +1782,17 @@
 erator, 
-6
+4
 );%0A    a
@@ -1939,14 +1939,20 @@
 mEx.
-range(
+constant(1, 
 100)
@@ -2001,48 +2001,8 @@
 ();%0A
-    assertNull(spliterator.trySplit());%0A
     
@@ -2029,17 +2029,17 @@
 erator, 
-0
+1
 L);%0A    
@@ -2058,33 +2058,33 @@
 nt(spliterator, 
-1
+2
 L);%0A    consumeE
@@ -2144,9 +2144,9 @@
 or, 
-6
+4
 L);%0A

rework always passing *noSplitAfterAdvance() tests, so that they would fail for the expected reason (streams used in tests previously were not splitting notwithstanding tryAdvance() method calls)