|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2018 Dremio Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include "gandiva/gandiva_aliases.h" |
| 19 | +#include "gandiva/tree_expr_builder.h" |
| 20 | +#include "codegen/annotator.h" |
| 21 | +#include "codegen/dex.h" |
| 22 | +#include "codegen/expr_decomposer.h" |
| 23 | +#include "codegen/function_signature.h" |
| 24 | +#include "codegen/function_registry.h" |
| 25 | +#include "codegen/node.h" |
| 26 | + |
| 27 | +namespace gandiva { |
| 28 | + |
| 29 | +using arrow::int32; |
| 30 | + |
| 31 | +class TestExprDecomposer : public ::testing::Test { |
| 32 | + protected: |
| 33 | + FunctionRegistry registry_; |
| 34 | +}; |
| 35 | + |
| 36 | +TEST_F(TestExprDecomposer, TestStackSimple) { |
| 37 | + Annotator annotator; |
| 38 | + ExprDecomposer decomposer(registry_, annotator); |
| 39 | + |
| 40 | + // if (a) _ |
| 41 | + // else _ |
| 42 | + IfNode node_a(nullptr, nullptr, nullptr, int32()); |
| 43 | + |
| 44 | + int idx_a = decomposer.PushThenEntry(node_a); |
| 45 | + EXPECT_EQ(idx_a, 0); |
| 46 | + decomposer.PopThenEntry(node_a); |
| 47 | + |
| 48 | + decomposer.PushElseEntry(node_a, idx_a); |
| 49 | + bool is_terminal_a = decomposer.PopElseEntry(node_a); |
| 50 | + EXPECT_EQ(is_terminal_a, true); |
| 51 | + EXPECT_EQ(decomposer.if_entries_stack_.empty(), true); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_F(TestExprDecomposer, TestNested) { |
| 55 | + Annotator annotator; |
| 56 | + ExprDecomposer decomposer(registry_, annotator); |
| 57 | + |
| 58 | + // if (a) _ |
| 59 | + // else _ |
| 60 | + // if (b) _ |
| 61 | + // else _ |
| 62 | + IfNode node_a(nullptr, nullptr, nullptr, int32()); |
| 63 | + IfNode node_b(nullptr, nullptr, nullptr, int32()); |
| 64 | + |
| 65 | + int idx_a = decomposer.PushThenEntry(node_a); |
| 66 | + EXPECT_EQ(idx_a, 0); |
| 67 | + decomposer.PopThenEntry(node_a); |
| 68 | + |
| 69 | + decomposer.PushElseEntry(node_a, idx_a); |
| 70 | + |
| 71 | + { // start b |
| 72 | + int idx_b = decomposer.PushThenEntry(node_b); |
| 73 | + EXPECT_EQ(idx_b, 0); // must reuse bitmap. |
| 74 | + decomposer.PopThenEntry(node_b); |
| 75 | + |
| 76 | + decomposer.PushElseEntry(node_b, idx_b); |
| 77 | + bool is_terminal_b = decomposer.PopElseEntry(node_b); |
| 78 | + EXPECT_EQ(is_terminal_b, true); |
| 79 | + } // end b |
| 80 | + |
| 81 | + bool is_terminal_a = decomposer.PopElseEntry(node_a); |
| 82 | + EXPECT_EQ(is_terminal_a, false); // there was a nested if. |
| 83 | + |
| 84 | + EXPECT_EQ(decomposer.if_entries_stack_.empty(), true); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(TestExprDecomposer, TestInternalIf) { |
| 88 | + Annotator annotator; |
| 89 | + ExprDecomposer decomposer(registry_, annotator); |
| 90 | + |
| 91 | + // if (a) _ |
| 92 | + // if (b) _ |
| 93 | + // else _ |
| 94 | + // else _ |
| 95 | + IfNode node_a(nullptr, nullptr, nullptr, int32()); |
| 96 | + IfNode node_b(nullptr, nullptr, nullptr, int32()); |
| 97 | + |
| 98 | + int idx_a = decomposer.PushThenEntry(node_a); |
| 99 | + EXPECT_EQ(idx_a, 0); |
| 100 | + |
| 101 | + { // start b |
| 102 | + int idx_b = decomposer.PushThenEntry(node_b); |
| 103 | + EXPECT_EQ(idx_b, 1); // must not reuse bitmap. |
| 104 | + decomposer.PopThenEntry(node_b); |
| 105 | + |
| 106 | + decomposer.PushElseEntry(node_b, idx_b); |
| 107 | + bool is_terminal_b = decomposer.PopElseEntry(node_b); |
| 108 | + EXPECT_EQ(is_terminal_b, true); |
| 109 | + } // end b |
| 110 | + |
| 111 | + decomposer.PopThenEntry(node_a); |
| 112 | + decomposer.PushElseEntry(node_a, idx_a); |
| 113 | + |
| 114 | + bool is_terminal_a = decomposer.PopElseEntry(node_a); |
| 115 | + EXPECT_EQ(is_terminal_a, true); // there was no nested if. |
| 116 | + |
| 117 | + EXPECT_EQ(decomposer.if_entries_stack_.empty(), true); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(TestExprDecomposer, TestParallelIf) { |
| 121 | + Annotator annotator; |
| 122 | + ExprDecomposer decomposer(registry_, annotator); |
| 123 | + |
| 124 | + // if (a) _ |
| 125 | + // else _ |
| 126 | + // if (b) _ |
| 127 | + // else _ |
| 128 | + IfNode node_a(nullptr, nullptr, nullptr, int32()); |
| 129 | + IfNode node_b(nullptr, nullptr, nullptr, int32()); |
| 130 | + |
| 131 | + int idx_a = decomposer.PushThenEntry(node_a); |
| 132 | + EXPECT_EQ(idx_a, 0); |
| 133 | + |
| 134 | + decomposer.PopThenEntry(node_a); |
| 135 | + decomposer.PushElseEntry(node_a, idx_a); |
| 136 | + |
| 137 | + bool is_terminal_a = decomposer.PopElseEntry(node_a); |
| 138 | + EXPECT_EQ(is_terminal_a, true); // there was no nested if. |
| 139 | + |
| 140 | + // start b |
| 141 | + int idx_b = decomposer.PushThenEntry(node_b); |
| 142 | + EXPECT_EQ(idx_b, 1); // must not reuse bitmap. |
| 143 | + decomposer.PopThenEntry(node_b); |
| 144 | + |
| 145 | + decomposer.PushElseEntry(node_b, idx_b); |
| 146 | + bool is_terminal_b = decomposer.PopElseEntry(node_b); |
| 147 | + EXPECT_EQ(is_terminal_b, true); |
| 148 | + |
| 149 | + EXPECT_EQ(decomposer.if_entries_stack_.empty(), true); |
| 150 | +} |
| 151 | + |
| 152 | +int main(int argc, char **argv) { |
| 153 | + ::testing::InitGoogleTest(&argc, argv); |
| 154 | + return RUN_ALL_TESTS(); |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace gandiva |
0 commit comments