summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/extensions/conditions/BooleanConditionTest.java
blob: f9f1fa85fcc97970d21e74d64f457ee21061d05b (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2018 The Android Open Source Project
//
// 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 com.google.gerrit.extensions.conditions;

import static com.google.gerrit.extensions.conditions.BooleanCondition.and;
import static com.google.gerrit.extensions.conditions.BooleanCondition.not;
import static com.google.gerrit.extensions.conditions.BooleanCondition.or;
import static com.google.gerrit.extensions.conditions.BooleanCondition.valueOf;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class BooleanConditionTest {

  private static final BooleanCondition NO_TRIVIAL_EVALUATION =
      new BooleanCondition() {
        @Override
        public boolean value() {
          throw new UnsupportedOperationException("value() is not supported");
        }

        @Override
        public <T> Iterable<T> children(Class<T> type) {
          throw new UnsupportedOperationException("children(Class<T> type) is not supported");
        }

        @Override
        public BooleanCondition reduce() {
          return this;
        }

        @Override
        protected boolean evaluatesTrivially() {
          return false;
        }
      };

  @Test
  public void reduceAnd_CutOffNonTrivialWhenPossible() throws Exception {
    BooleanCondition nonReduced = and(false, NO_TRIVIAL_EVALUATION);
    BooleanCondition reduced = valueOf(false);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceAnd_CutOffNonTrivialWhenPossibleSwapped() throws Exception {
    BooleanCondition nonReduced = and(NO_TRIVIAL_EVALUATION, valueOf(false));
    BooleanCondition reduced = valueOf(false);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceAnd_KeepNonTrivialWhenNoCutOffPossible() throws Exception {
    BooleanCondition nonReduced = and(true, NO_TRIVIAL_EVALUATION);
    BooleanCondition reduced = and(true, NO_TRIVIAL_EVALUATION);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceAnd_KeepNonTrivialWhenNoCutOffPossibleSwapped() throws Exception {
    BooleanCondition nonReduced = and(NO_TRIVIAL_EVALUATION, valueOf(true));
    BooleanCondition reduced = and(NO_TRIVIAL_EVALUATION, valueOf(true));
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceOr_CutOffNonTrivialWhenPossible() throws Exception {
    BooleanCondition nonReduced = or(true, NO_TRIVIAL_EVALUATION);
    BooleanCondition reduced = valueOf(true);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceOr_CutOffNonTrivialWhenPossibleSwapped() throws Exception {
    BooleanCondition nonReduced = or(NO_TRIVIAL_EVALUATION, valueOf(true));
    BooleanCondition reduced = valueOf(true);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceOr_KeepNonTrivialWhenNoCutOffPossible() throws Exception {
    BooleanCondition nonReduced = or(false, NO_TRIVIAL_EVALUATION);
    BooleanCondition reduced = or(false, NO_TRIVIAL_EVALUATION);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceOr_KeepNonTrivialWhenNoCutOffPossibleSwapped() throws Exception {
    BooleanCondition nonReduced = or(NO_TRIVIAL_EVALUATION, valueOf(false));
    BooleanCondition reduced = or(NO_TRIVIAL_EVALUATION, valueOf(false));
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceNot_ReduceIrrelevant() throws Exception {
    BooleanCondition nonReduced = not(valueOf(true));
    BooleanCondition reduced = valueOf(false);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceNot_ReduceIrrelevant2() throws Exception {
    BooleanCondition nonReduced = not(valueOf(false));
    BooleanCondition reduced = valueOf(true);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceNot_KeepNonTrivialWhenNoCutOffPossible() throws Exception {
    BooleanCondition nonReduced = not(NO_TRIVIAL_EVALUATION);
    BooleanCondition reduced = not(NO_TRIVIAL_EVALUATION);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceComplexTreeToSingleValue() throws Exception {
    //        AND
    //       /   \
    //      OR   NOT
    //     /  \    \
    //   NTE NTE  TRUE
    BooleanCondition nonReduced =
        and(or(NO_TRIVIAL_EVALUATION, NO_TRIVIAL_EVALUATION), not(valueOf(true)));
    BooleanCondition reduced = valueOf(false);
    assertEquals(nonReduced.reduce(), reduced);
  }

  @Test
  public void reduceComplexTreeToSmallerTree() throws Exception {
    //        AND
    //       /   \
    //      OR    OR
    //     /  \   / \
    //   NTE NTE  T  F
    BooleanCondition nonReduced =
        and(or(NO_TRIVIAL_EVALUATION, NO_TRIVIAL_EVALUATION), or(valueOf(true), valueOf(false)));
    BooleanCondition reduced = and(or(NO_TRIVIAL_EVALUATION, NO_TRIVIAL_EVALUATION), valueOf(true));
    assertEquals(nonReduced.reduce(), reduced);
  }
}