<commit_before>/*
 * Copyright 2015 <NAME>
 * 
 * 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 javax.util.streamex;

import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

import org.junit.Test;

import static org.junit.Assert.*;

public class CustomPoolTest {
    ForkJoinPool pool = new ForkJoinPool(3);

    private void checkThread(Object element) {
        Thread thread = Thread.currentThread();
        if (!(thread instanceof ForkJoinWorkerThread))
            throw new IllegalStateException("Not inside FJP (element: " + element + ")");
        if (((ForkJoinWorkerThread) thread).getPool() != pool)
            throw new IllegalStateException("FJP is incorrect (element: " + element + ")");
    }

    @Test(expected = IllegalStateException.class)
    public void testCheckThreadSequential() {
        StreamEx.of("a", "b").peek(this::checkThread).joining();
    }

    @Test(expected = IllegalStateException.class)
    public void testCheckThreadParallel() {
        StreamEx.of("a", "b").parallel().peek(this::checkThread).joining();
    }

    @Test
    public void testStreamEx() {
        StreamEx.of("a", "b", "c").parallel(pool).forEach(this::checkThread);
        assertEquals(Arrays.asList(1, 2),
            StreamEx.of("a", "bb").parallel(pool).peek(this::checkThread).map(String::length).toList());
        assertEquals("a", StreamEx.of("a").parallel(pool).peek(this::checkThread).findAny().get());
        assertEquals("a", StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).findFirst().get());
        assertTrue(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).anyMatch("a"::equals));
        assertFalse(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).allMatch("a"::equals));
        assertFalse(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).noneMatch("a"::equals));
        assertEquals(Arrays.asList("b", "c"), StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).skip(1)
                .toList());
        assertEquals(
            6,
            StreamEx.of("a", "bb", "ccc").parallel(pool).peek(this::checkThread)
                    .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).length());
        assertArrayEquals(new String[] { "a", "b", "c" },
            StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).toArray(String[]::new));
        assertArrayEquals(new Object[] { "a", "b", "c" },
            StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).toArray());
        assertEquals(
            "{ccc={bb={a={}}}}",
            StreamEx.of("a", "bb", "ccc")
                    .parallel(pool)
                    .peek(this::checkThread)
                    .foldLeft(Collections.emptyMap(),
                        (Map<String, Object> acc, String v) -> Collections.singletonMap(v, acc)).toString());
        assertEquals(1000,
            IntStreamEx.constant(1, 1000).boxed().parallel(pool).peek(this::checkThread).foldLeft(0, Integer::sum)
                    .intValue());

        assertEquals(2,
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2).count());
        assertEquals(
            "bbbcccc",
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce(String::concat).get());
        assertEquals(
            "bbbcccc",
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce("", String::concat));
        assertEquals(7,
            (int) StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce(0, (x, s) -> x + s.length(), Integer::sum));
    }

    @Test
    public void testEntryStream() {
        EntryStream.of("a", 1).parallel(pool).forEach(this::checkThread);
        assertEquals(Integer.valueOf(1), EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).toMap().get("a"));
        assertEquals(Integer.valueOf(1),
            EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).findAny(e -> e.getKey().equals("a")).get()
                    .getValue());
        assertEquals(Integer.valueOf(1),
            EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).findFirst(e -> e.getKey().equals("a")).get()
                    .getValue());
        assertTrue(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).anyMatch(e -> e.getKey().equals("a")));
        assertTrue(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).allMatch(e -> e.getKey().equals("a")));
        assertFalse(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread)
                .noneMatch(e -> e.getKey().equals("a")));
        assertEquals(2,
            EntryStream.of("a", 1, "b", 2, "c", 3).parallel(pool).filterValues(v -> v > 1).peek(this::checkThread)
                    .count());
    }
<commit_msg>CustomPoolTest: better coverage for StreamFactory<commit_after>/*
 * Copyright 2015 <NAME>
 * 
 * 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 javax.util.streamex;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

import org.junit.Test;

import static org.junit.Assert.*;

public class CustomPoolTest {
    ForkJoinPool pool = new ForkJoinPool(3);

    private void checkThread(Object element) {
        Thread thread = Thread.currentThread();
        if (!(thread instanceof ForkJoinWorkerThread))
            throw new IllegalStateException("Not inside FJP (element: " + element + ")");
        if (((ForkJoinWorkerThread) thread).getPool() != pool)
            throw new IllegalStateException("FJP is incorrect (element: " + element + ")");
    }

    @Test(expected = IllegalStateException.class)
    public void testCheckThreadSequential() {
        StreamEx.of("a", "b").peek(this::checkThread).joining();
    }

    @Test(expected = IllegalStateException.class)
    public void testCheckThreadParallel() {
        StreamEx.of("a", "b").parallel().peek(this::checkThread).joining();
    }

    @Test
    public void testStreamEx() {
        StreamEx.of("a", "b", "c").parallel(pool).forEach(this::checkThread);
        assertEquals(Arrays.asList(1, 2),
            StreamEx.of("a", "bb").parallel(pool).peek(this::checkThread).map(String::length).toList());
        assertEquals("a", StreamEx.of("a").parallel(pool).peek(this::checkThread).findAny().get());
        assertEquals("a", StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).findFirst().get());
        assertTrue(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).anyMatch("a"::equals));
        assertFalse(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).allMatch("a"::equals));
        assertFalse(StreamEx.of("a", "b").parallel(pool).peek(this::checkThread).noneMatch("a"::equals));
        assertEquals(Arrays.asList("b", "c"), StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).skip(1)
                .toList());
        assertEquals(
            6,
            StreamEx.of("a", "bb", "ccc").parallel(pool).peek(this::checkThread)
                    .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).length());
        assertArrayEquals(new String[] { "a", "b", "c" },
            StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).toArray(String[]::new));
        assertArrayEquals(new Object[] { "a", "b", "c" },
            StreamEx.of("a", "b", "c").parallel(pool).peek(this::checkThread).toArray());
        assertEquals(
            "{ccc={bb={a={}}}}",
            StreamEx.of("a", "bb", "ccc")
                    .parallel(pool)
                    .peek(this::checkThread)
                    .foldLeft(Collections.emptyMap(),
                        (Map<String, Object> acc, String v) -> Collections.singletonMap(v, acc)).toString());
        assertEquals(1000,
            IntStreamEx.constant(1, 1000).boxed().parallel(pool).peek(this::checkThread).foldLeft(0, Integer::sum)
                    .intValue());

        assertEquals(2,
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2).count());
        assertEquals(
            "bbbcccc",
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce(String::concat).get());
        assertEquals(
            "bbbcccc",
            StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce("", String::concat));
        assertEquals(7,
            (int) StreamEx.of("aa", "bbb", "cccc").parallel(pool).peek(this::checkThread).filter(x -> x.length() > 2)
                    .reduce(0, (x, s) -> x + s.length(), Integer::sum));
		assertEquals(
				"aabbbcccc",
				StreamEx.of("aa", "bbb", "cccc").parallel(pool)
						.peek(this::checkThread).foldLeft("", String::concat));
    }

    @Test
    public void testEntryStream() {
        EntryStream.of("a", 1).parallel(pool).forEach(this::checkThread);
        assertEquals(Integer.valueOf(1), EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).toMap().get("a"));
        assertEquals(Integer.valueOf(1),
            EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).findAny(e -> e.getKey().equals("a")).get()
                    .getValue());
        assertEquals(Integer.valueOf(1),
            EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).findFirst(e -> e.getKey().equals("a")).get()
                    .getValue());
        assertTrue(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).anyMatch(e -> e.getKey().equals("a")));
        assertTrue(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread).allMatch(e -> e.getKey().equals("a")));
        assertFalse(EntryStream.of("a", 1).parallel(pool).peek(this::checkThread)
                .noneMatch(e -> e.getKey().equals("a")));
        assertEquals(2,
            EntryStream.of("a", 1, "b", 2, "c", 3).parallel(pool).peek(this::checkThread).filterValues(v -> v > 1)
                    .count());
        List<Integer> res = Collections.synchronizedList(new ArrayList<>());
        EntryStream.of("a", 1, "b", 2, "c", 3).parallel(pool).peek(this::checkThread).filterValues(v -> v > 1)
            .forEachOrdered(entry -> res.add(entry.getValue()));
        assertEquals(Arrays.asList(2, 3), res);
		assertEquals(
				6,
				(int) EntryStream
						.of("a", 1, "b", 2, "c", 3)
						.parallel(pool)
						.peek(this::checkThread)
						.reduce(0, (sum, e) -> sum + e.getValue(), Integer::sum));
		assertEquals(Arrays.asList(1, 2, 3), EntryStream.of("a", 1, "b", 2, "c", 3).parallel(pool).peek(this::checkThread)
            .collect(ArrayList::new, (List<Integer> list, Entry<String, Integer> e) -> list.add(e.getValue()), List::addAll));
    }

{'fraction_non_alphanumeric': 0.18335740651580806, 'fraction_numerical': 0.008586101749317926, 'mean_word_length': 3.15018315018315, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6536551', 'n_tokens_mistral': 3835, 'n_tokens_neox': 3701, 'n_words': 669}