aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/test/java/com/yahoo/search/predicate/optimization/BooleanSimplifierTest.java
blob: 5c9bd050b29e039ac41fc89ec3d8559e6922547e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.optimization;

import com.yahoo.document.predicate.Predicate;
import org.junit.jupiter.api.Test;

import static com.yahoo.document.predicate.Predicates.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
 */
public class BooleanSimplifierTest {

    @Test
    void requireThatOrOfTrueIsTrue() {
        assertSimplifiedTo(value(true), or(feature("a").inSet("b"), value(true)));
    }

    @Test
    void requireThatFalseChildrenOfOrAreRemoved() {
        assertSimplifiedTo(feature("a").inSet("b"), or(feature("a").inSet("b"), value(false)));
    }

    @Test
    void requireThatAndOfFalseIsFalse() {
        assertSimplifiedTo(value(false), and(feature("a").inSet("b"), value(false)));
    }

    @Test
    void requireThatTrueChildrenOfAndAreRemoved() {
        assertSimplifiedTo(feature("a").inSet("b"), and(feature("a").inSet("b"), value(true)));
    }

    @Test
    void requireThatSingleChildAndOrAreRemoved() {
        assertSimplifiedTo(feature("a").inSet("b"), and(or(and(feature("a").inSet("b")))));
    }

    @Test
    void requireThatValueChildrenOfNotAreInverted() {
        assertSimplifiedTo(value(true), not(value(false)));
        assertSimplifiedTo(value(false), not(value(true)));
        assertSimplifiedTo(value(true), not(not(not(value(false)))));
        assertSimplifiedTo(value(true), not(not(not(and(feature("a").inSet("b"), value(false))))));
    }

    @Test
    void requireThatComplexExpressionIsSimplified() {
        assertSimplifiedTo(
                Predicate.fromString("'pub_entity' not in [301951]"),
                Predicate.fromString("true and true and true and true and true and 'pub_entity' not in [301951] and ((true and true and true and true) or (true and true and true and true) or (true and true and true and true and 'pub_entity' in [86271]))"));
    }

    private void assertSimplifiedTo(Predicate expected, Predicate input) {
        BooleanSimplifier simplifier = new BooleanSimplifier();
        Predicate actual = simplifier.process(input, new PredicateOptions(10));
        assertEquals(expected, actual);
    }
}