/**
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * 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.
 */
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.speedment.runtime.field.internal.predicate;

import com.speedment.runtime.field.predicate.CombinedPredicate;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 *
 * @author Per Minborg
 */
public class AbstractCombinedPredicateTest {

    private static final Predicate<Integer> MOD2 = new Predicate<Integer>() {
        @Override
        public boolean test(Integer i) {
            return i % 2 == 0;
        }

        @Override
        public String toString() {
            return "MOD2";
        }
    };
    private static final Predicate<Integer> MOD4 = new Predicate<Integer>() {
        @Override
        public boolean test(Integer i) {
            return i % 4 == 0;
        }

        @Override
        public String toString() {
            return "MOD4";
        }
    };
    private static final Predicate<Integer> MOD8 = new Predicate<Integer>() {
        @Override
        public boolean test(Integer i) {
            return i % 8 == 0;
        }

        @Override
        public String toString() {
            return "MOD8";
        }
    };

    private static final Predicate<Integer> MOD3 = new Predicate<Integer>() {
        @Override
        public boolean test(Integer i) {
            return i % 3 == 0;
        }

        @Override
        public String toString() {
            return "MOD3";
        }
    };

    @Test
    public void testStream() {
        final Set<Predicate<Integer>> set = Stream.of(MOD2, MOD4, MOD8).collect(toSet());

        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8);
        assertTrue(p.stream().allMatch(set::contains));
    }

    @Test
    public void testSize() {
        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8);
        final long actual = p.stream().count();
        assertEquals(3, actual);
    }

    @Test
    public void testGetPredicates() {
        CombinedPredicate<Integer> p = new AbstractCombinedPredicate.AndCombinedBasePredicate<>(Arrays.asList(MOD2, MOD4));
        p = p.and(MOD8);
        final List<Predicate<Integer>> expected = Arrays.asList(MOD2, MOD4, MOD8);
        final List<Predicate<? super Integer>> actual = p.stream().collect(toList());
        assertEquals(expected, actual);
    }

    @Test
    public void testGetType() {
        final CombinedPredicate<Integer> or = CombinedPredicate.or(MOD2, MOD4);
        assertEquals(CombinedPredicate.Type.OR, or.getType());
        final CombinedPredicate<Integer> and = CombinedPredicate.and(MOD2, MOD4);
        assertEquals(CombinedPredicate.Type.AND, and.getType());

        final CombinedPredicate<Integer> orComposed = CombinedPredicate.or(and, or);
        assertEquals(CombinedPredicate.Type.OR, orComposed.getType());
        final CombinedPredicate<Integer> andComposed = CombinedPredicate.and(and, or);
        assertEquals(CombinedPredicate.Type.AND, andComposed.getType());

    }

    @Test
    public void testAnd() {
        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8);

        for (int i = 0; i < 100; i++) {
            boolean expected = MOD2.test(i) && MOD4.test(i) && MOD8.test(i);
            boolean actual = p.test(i);
            assertEquals(expected, actual);
        }

    }

    @Test
    public void testOr() {
        CombinedPredicate<Integer> p = CombinedPredicate.or(MOD2, MOD4);
        p = p.or(MOD8);

        for (int i = 0; i < 100; i++) {
            boolean expected = MOD2.test(i) || MOD4.test(i) || MOD8.test(i);
            boolean actual = p.test(i);
            assertEquals(expected, actual);
        }
    }

    @Test
    public void testComposed() {
        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8);
        p = p.or(MOD3);
        for (int i = 0; i < 100; i++) {
            boolean expected = (MOD2.test(i) && MOD4.test(i) && MOD8.test(i)) || MOD3.test(i);
            boolean actual = p.test(i);
            assertEquals(expected, actual);
        }
    }

    @Test
    public void testNegate() {
        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8);
        p = p.negate();
        for (int i = 0; i < 100; i++) {
            boolean expected = !(MOD2.test(i) && MOD4.test(i) && MOD8.test(i));
            boolean actual = p.test(i);
            assertEquals(expected, actual);
        }
    }

    @Test
    public void testToString() {
        CombinedPredicate<Integer> p = CombinedPredicate.and(MOD2, MOD4);
        p = p.and(MOD8).negate();
        final String toString = p.toString();
        System.out.println(toString);
        assertTrue(toString.contains("negated=true"));
        assertTrue(toString.contains("MOD2"));
        assertTrue(toString.contains("MOD4"));
        assertTrue(toString.contains("MOD8"));
        assertTrue(toString.contains("type=AND"));
    }
}

@@ -5557,496 +5557,7 @@
    %7D
-%0A%0A    @Test%0A    public void testToString() %7B%0A        CombinedPredicate%3CInteger%3E p = CombinedPredicate.and(MOD2, MOD4);%0A        p = p.and(MOD8).negate();%0A        final String toString = p.toString();%0A        System.out.println(toString);%0A        assertTrue(toString.contains(%22negated=true%22));%0A        assertTrue(toString.contains(%22MOD2%22));%0A        assertTrue(toString.contains(%22MOD4%22));%0A        assertTrue(toString.contains(%22MOD8%22));%0A        assertTrue(toString.contains(%22type=AND%22));%0A    %7D
 %0A%7D%0A

Remove unnescessary toString()-test