1 /*
2 * Copyright (C) 2018 The Android Open Source Project
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 "utils/grammar/semantics/evaluators/const-eval.h"
18
19 #include "utils/flatbuffers/flatbuffers.h"
20 #include "utils/grammar/semantics/expression_generated.h"
21 #include "utils/grammar/testing/utils.h"
22 #include "utils/grammar/testing/value_generated.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "flatbuffers/flatbuffers.h"
26
27 namespace libtextclassifier3::grammar {
28 namespace {
29
30 class ConstEvaluatorTest : public GrammarTest {
31 protected:
ConstEvaluatorTest()32 explicit ConstEvaluatorTest() : const_eval_(semantic_values_schema_.get()) {}
33
34 const ConstEvaluator const_eval_;
35 };
36
TEST_F(ConstEvaluatorTest,CreatesConstantSemanticValues)37 TEST_F(ConstEvaluatorTest, CreatesConstantSemanticValues) {
38 TestValueT value;
39 value.a_float_value = 64.42;
40 value.test_string = "test string";
41 OwnedFlatbuffer<SemanticExpression> expression =
42 CreateAndPackConstExpression(value);
43
44 StatusOr<const SemanticValue*> result =
45 const_eval_.Apply(/*context=*/{}, expression.get(), &arena_);
46
47 EXPECT_TRUE(result.ok());
48 const SemanticValue* result_value = result.ValueOrDie();
49 ASSERT_NE(result_value, nullptr);
50 EXPECT_EQ(result_value->type()->name()->str(),
51 "libtextclassifier3.grammar.TestValue");
52 const TestValue* result_test_value = result_value->Table<TestValue>();
53 EXPECT_EQ(result_test_value->test_string()->str(), "test string");
54 EXPECT_FLOAT_EQ(result_test_value->a_float_value(), 64.42);
55 }
56
57 template <typename T>
58 class PrimitiveValueTest : public ConstEvaluatorTest {
59 protected:
Eval(const T value)60 T Eval(const T value) {
61 OwnedFlatbuffer<SemanticExpression> expression =
62 CreateAndPackPrimitiveConstExpression<T>(value);
63 StatusOr<const SemanticValue*> result =
64 const_eval_.Apply(/*context=*/{}, expression.get(), &arena_);
65 EXPECT_TRUE(result.ok());
66 const SemanticValue* result_value = result.ValueOrDie();
67 EXPECT_NE(result_value, nullptr);
68 return result_value->Value<T>();
69 }
70 };
71
72 using PrimitiveTypes = ::testing::Types<int8, uint8, int16, uint16, int32,
73 uint32, int64, uint64, double, float>;
74 TYPED_TEST_SUITE(PrimitiveValueTest, PrimitiveTypes);
75
TYPED_TEST(PrimitiveValueTest,CreatesConstantPrimitiveValues)76 TYPED_TEST(PrimitiveValueTest, CreatesConstantPrimitiveValues) {
77 EXPECT_EQ(this->Eval(42), 42);
78 }
79
TEST_F(ConstEvaluatorTest,CreatesStringValues)80 TEST_F(ConstEvaluatorTest, CreatesStringValues) {
81 OwnedFlatbuffer<SemanticExpression> expression =
82 CreateAndPackPrimitiveConstExpression<StringPiece>("this is a test.");
83 StatusOr<const SemanticValue*> result =
84 const_eval_.Apply(/*context=*/{}, expression.get(), &arena_);
85
86 EXPECT_TRUE(result.ok());
87 const SemanticValue* result_value = result.ValueOrDie();
88 ASSERT_NE(result_value, nullptr);
89 EXPECT_EQ(result_value->Value<StringPiece>().ToString(), "this is a test.");
90 }
91
TEST_F(ConstEvaluatorTest,CreatesBoolValues)92 TEST_F(ConstEvaluatorTest, CreatesBoolValues) {
93 OwnedFlatbuffer<SemanticExpression> expression =
94 CreateAndPackPrimitiveConstExpression<bool>(true);
95 StatusOr<const SemanticValue*> result =
96 const_eval_.Apply(/*context=*/{}, expression.get(), &arena_);
97
98 EXPECT_TRUE(result.ok());
99 const SemanticValue* result_value = result.ValueOrDie();
100 ASSERT_NE(result_value, nullptr);
101 EXPECT_TRUE(result_value->Value<bool>());
102 }
103
104 } // namespace
105 } // namespace libtextclassifier3::grammar
106