<commit_before>/*     / \____  _    ______   _____ / \____   ____  _____
 *    /  \__  \/ \  / \__  \ /  __//  \__  \ /    \/ __  \   Javaslang
 *  _/  // _\  \  \/  / _\  \\_  \/  // _\  \  /\  \__/  /   Copyright 2014-2015 <NAME>
 * /___/ \_____/\____/\_____/____/\___\_____/_/  \_/____/    Licensed under the Apache License, Version 2.0
 */
package javaslang;

import javaslang.collection.List;
import org.junit.Test;

import java.lang.*;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;

public class LazyTest {

    // -- of(Supplier)

    @Test(expected = NullPointerException.class)
    public void shouldThrowOnNullSupplier() {
        Lazy.of(null);
    }

    @Test
    public void shouldMemoizeValues() {
        final Lazy<Double> testee = Lazy.of(Math::random);
        final double expected = testee.get();
        for (int i = 0; i < 10; i++) {
            final double actual = testee.get();
            assertThat(actual).isEqualTo(expected);
        }
    }

    // -- of(Supplier, Class) -- Proxy

    @Test
    public void shouldCreateLazyProxy() {

        final String[] evaluated = new String[] { null };

        final CharSequence chars = Lazy.asVal(() -> {
            final String value = "Yay!";
            evaluated[0] = value;
            return value;
        }, CharSequence.class);

        assertThat(evaluated[0]).isEqualTo(null);
        assertThat(chars).isEqualTo("Yay!");
        assertThat(evaluated[0]).isEqualTo("Yay!");
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowWhenCreatingLazyProxyAndSupplierIsNull() {
        Lazy.asVal(null, CharSequence.class);
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowWhenCreatingLazyProxyAndTypeIsNull() {
        Lazy.asVal(() -> "", null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowWhenCreatingLazyProxyOfObjectType() {
        Lazy.asVal(() -> "", String.class);
    }

    @Test
    public void shouldBehaveLikeValueWhenCreatingProxy() {
        final CharSequence chars = Lazy.asVal(() -> "Yay!", CharSequence.class);
        assertThat(chars.toString()).isEqualTo("Yay!");
    }

    // -- isEmpty()

    @Test
    public void shouldNotBeEmpty() {
        assertThat(Lazy.of(() -> null).isEmpty()).isEqualTo(false);
    }

    // -- isEvaluated()

    @Test
    public void shouldBeAwareOfEvaluated() {
        final Lazy<Void> lazy = Lazy.of(() -> null);
        assertThat(lazy.isEvaluated()).isFalse();
        assertThat(lazy.isEvaluated()).isFalse(); // remains not evaluated
        lazy.get();
        assertThat(lazy.isEvaluated()).isTrue();
    }

    // -- get

    @Test(expected = NoSuchElementException.class)
    public void shouldThrowGetEmpty() {
        assertThat(Lazy.empty().get()).isEqualTo(1);
    }

    // -- filter

    @Test(expected = NoSuchElementException.class)
    public void shouldThrowFilterEmptyLazy() {
        assertThat(Lazy.empty().filter(ignored -> true)).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFilterNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).filter(i -> true)).isEqualTo(Lazy.of(() -> 1));
    }

    @Test(expected = NoSuchElementException.class)
    public void shouldEmptyFilterNonEmptyLazy() {
        assertThat(Lazy.empty().filter(i -> true)).isEqualTo(Lazy.of(() -> 1));
    }

    // -- flatten()

    @Test(expected = NoSuchElementException.class)
    public void shouldFlattenEmptyLazy() {
        assertThat(Lazy.empty().flatten()).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFlattenNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).flatten()).isEqualTo(Lazy.of(() -> 1));
    }

    @Test
    public void shouldFlattenLazyOfLazy() {
        assertThat(Lazy.of(Lazy.of(() -> 1)).flatten()).isEqualTo(Lazy.of(() -> 1));
    }

    // -- flatMap

    @Test(expected = NoSuchElementException.class)
    public void shouldFlatMapEmptyLazy() {
        assertThat(Lazy.empty().flatMap(List::of)).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFlatMapNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).flatMap(List::of)).isEqualTo(Lazy.of(() -> 1));
    }

    // -- map

    @Test(expected = NoSuchElementException.class)
    public void shouldMapEmpty() {
        assertThat(Lazy.empty().map(Function.identity())).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldMapNonEmpty() {
        assertThat(Lazy.of(() -> 1).map(i -> i + 1)).isEqualTo(Lazy.of(() -> 2));
    }

    // -- peek

    @Test(expected = NoSuchElementException.class)
    public void shouldPeekEmpty() {
        assertThat(Lazy.empty().peek(t -> {
        })).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldPeekSingleValuePerformingAnAction() {
        final int[] effect = { 0 };
        assertThat(Lazy.of(() -> 1).peek(i -> effect[0] = i)).isEqualTo(Lazy.of(() -> 1));
        assertThat(effect[0]).isEqualTo(1);
    }

    // -- equals

    @Test
    public void shouldDetectEqualObject() {
        assertThat(Lazy.of(() -> 1).equals(Lazy.of(() -> 1))).isTrue();
    }

    @Test
    public void shouldDetectUnequalObject() {
        assertThat(Lazy.of(() -> 1).equals(Lazy.of(() -> 2))).isFalse();
    }

    // -- hashCode

    @Test
    public void shouldComputeHashCode() {
        assertThat(Lazy.of(() -> 1).hashCode()).isEqualTo(Objects.hash(1));
    }

    // -- toString

    @Test
    public void shouldConvertNonEvaluatedValueToString() {
        final Lazy<Integer> lazy = Lazy.of(() -> 1);
        assertThat(lazy.toString()).isEqualTo("Lazy(?)");
    }

    @Test
    public void shouldConvertEvaluatedValueToString() {
        final Lazy<Integer> lazy = Lazy.of(() -> 1);
        lazy.get();
        assertThat(lazy.toString()).isEqualTo("Lazy(1)");
    }
}
<commit_msg>fix filter<commit_after>/*     / \____  _    ______   _____ / \____   ____  _____
 *    /  \__  \/ \  / \__  \ /  __//  \__  \ /    \/ __  \   Javaslang
 *  _/  // _\  \  \/  / _\  \\_  \/  // _\  \  /\  \__/  /   Copyright 2014-2015 <NAME>
 * /___/ \_____/\____/\_____/____/\___\_____/_/  \_/____/    Licensed under the Apache License, Version 2.0
 */
package javaslang;

import javaslang.collection.List;
import org.junit.Test;

import java.lang.*;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;

public class LazyTest {

    // -- of(Supplier)

    @Test(expected = NullPointerException.class)
    public void shouldThrowOnNullSupplier() {
        Lazy.of(null);
    }

    @Test
    public void shouldMemoizeValues() {
        final Lazy<Double> testee = Lazy.of(Math::random);
        final double expected = testee.get();
        for (int i = 0; i < 10; i++) {
            final double actual = testee.get();
            assertThat(actual).isEqualTo(expected);
        }
    }

    // -- of(Supplier, Class) -- Proxy

    @Test
    public void shouldCreateLazyProxy() {

        final String[] evaluated = new String[] { null };

        final CharSequence chars = Lazy.asVal(() -> {
            final String value = "Yay!";
            evaluated[0] = value;
            return value;
        }, CharSequence.class);

        assertThat(evaluated[0]).isEqualTo(null);
        assertThat(chars).isEqualTo("Yay!");
        assertThat(evaluated[0]).isEqualTo("Yay!");
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowWhenCreatingLazyProxyAndSupplierIsNull() {
        Lazy.asVal(null, CharSequence.class);
    }

    @Test(expected = NullPointerException.class)
    public void shouldThrowWhenCreatingLazyProxyAndTypeIsNull() {
        Lazy.asVal(() -> "", null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowWhenCreatingLazyProxyOfObjectType() {
        Lazy.asVal(() -> "", String.class);
    }

    @Test
    public void shouldBehaveLikeValueWhenCreatingProxy() {
        final CharSequence chars = Lazy.asVal(() -> "Yay!", CharSequence.class);
        assertThat(chars.toString()).isEqualTo("Yay!");
    }

    // -- isEmpty()

    @Test
    public void shouldNotBeEmpty() {
        assertThat(Lazy.of(() -> null).isEmpty()).isEqualTo(false);
    }

    // -- isEvaluated()

    @Test
    public void shouldBeAwareOfEvaluated() {
        final Lazy<Void> lazy = Lazy.of(() -> null);
        assertThat(lazy.isEvaluated()).isFalse();
        assertThat(lazy.isEvaluated()).isFalse(); // remains not evaluated
        lazy.get();
        assertThat(lazy.isEvaluated()).isTrue();
    }

    // -- get

    @Test(expected = NoSuchElementException.class)
    public void shouldThrowGetEmpty() {
        assertThat(Lazy.empty().get()).isEqualTo(1);
    }

    // -- filter

    @Test(expected = NoSuchElementException.class)
    public void shouldThrowFilterEmptyLazy() {
        assertThat(Lazy.empty().filter(ignored -> true)).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFilterNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).filter(i -> true)).isEqualTo(Lazy.of(() -> 1));
    }

    @Test(expected = NoSuchElementException.class)
    public void shouldEmptyFilterNonEmptyLazy() {
        assertThat(Lazy.empty().filter(i -> false)).isEqualTo(Lazy.of(() -> 1));
    }

    @Test
    public void shouldNonEmptyFilterNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).filter(i -> true)).isEqualTo(Lazy.of(() -> 1));
    }

    // -- flatten()

    @Test(expected = NoSuchElementException.class)
    public void shouldFlattenEmptyLazy() {
        assertThat(Lazy.empty().flatten()).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFlattenNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).flatten()).isEqualTo(Lazy.of(() -> 1));
    }

    @Test
    public void shouldFlattenLazyOfLazy() {
        assertThat(Lazy.of(Lazy.of(() -> 1)).flatten()).isEqualTo(Lazy.of(() -> 1));
    }

    // -- flatMap

    @Test(expected = NoSuchElementException.class)
    public void shouldFlatMapEmptyLazy() {
        assertThat(Lazy.empty().flatMap(List::of)).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldFlatMapNonEmptyLazy() {
        assertThat(Lazy.of(() -> 1).flatMap(List::of)).isEqualTo(Lazy.of(() -> 1));
    }

    // -- map

    @Test(expected = NoSuchElementException.class)
    public void shouldMapEmpty() {
        assertThat(Lazy.empty().map(Function.identity())).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldMapNonEmpty() {
        assertThat(Lazy.of(() -> 1).map(i -> i + 1)).isEqualTo(Lazy.of(() -> 2));
    }

    // -- peek

    @Test(expected = NoSuchElementException.class)
    public void shouldPeekEmpty() {
        assertThat(Lazy.empty().peek(t -> {
        })).isEqualTo(Lazy.empty());
    }

    @Test
    public void shouldPeekSingleValuePerformingAnAction() {
        final int[] effect = { 0 };
        assertThat(Lazy.of(() -> 1).peek(i -> effect[0] = i)).isEqualTo(Lazy.of(() -> 1));
        assertThat(effect[0]).isEqualTo(1);
    }

    // -- equals

    @Test
    public void shouldDetectEqualObject() {
        assertThat(Lazy.of(() -> 1).equals(Lazy.of(() -> 1))).isTrue();
    }

    @Test
    public void shouldDetectUnequalObject() {
        assertThat(Lazy.of(() -> 1).equals(Lazy.of(() -> 2))).isFalse();
    }

    // -- hashCode

    @Test
    public void shouldComputeHashCode() {
        assertThat(Lazy.of(() -> 1).hashCode()).isEqualTo(Objects.hash(1));
    }

    // -- toString

    @Test
    public void shouldConvertNonEvaluatedValueToString() {
        final Lazy<Integer> lazy = Lazy.of(() -> 1);
        assertThat(lazy.toString()).isEqualTo("Lazy(?)");
    }

    @Test
    public void shouldConvertEvaluatedValueToString() {
        final Lazy<Integer> lazy = Lazy.of(() -> 1);
        lazy.get();
        assertThat(lazy.toString()).isEqualTo("Lazy(1)");
    }
}

{'fraction_non_alphanumeric': 0.1630308076602831, 'fraction_numerical': 0.00749375520399667, 'mean_word_length': 2.5274596182085167, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5270677', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3817, 'n_words': 601}