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/parse-number-eval.h"
18
19 #include <vector>
20
21 #include "utils/base/statusor.h"
22 #include "utils/flatbuffers/flatbuffers.h"
23 #include "utils/flatbuffers/reflection.h"
24 #include "utils/flatbuffers/test-utils.h"
25 #include "utils/grammar/semantics/evaluator.h"
26 #include "utils/grammar/semantics/evaluators/const-eval.h"
27 #include "utils/grammar/semantics/expression_generated.h"
28 #include "utils/grammar/testing/utils.h"
29 #include "utils/grammar/testing/value_generated.h"
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include "flatbuffers/flatbuffers.h"
33
34 namespace libtextclassifier3::grammar {
35 namespace {
36
37 template <typename T>
38 class ParseNumberEvaluatorTest : public GrammarTest {
39 protected:
Eval(const StringPiece value)40 T Eval(const StringPiece value) {
41 ParseNumberExpressionT parse_number_expression;
42 parse_number_expression.base_type = flatbuffers_base_type<T>::value;
43 parse_number_expression.value =
44 CreatePrimitiveConstExpression<StringPiece>(value);
45 OwnedFlatbuffer<SemanticExpression> expression =
46 CreateExpression(std::move(parse_number_expression));
47
48 ConstEvaluator const_eval(semantic_values_schema_.get());
49 ParseNumberEvaluator parse_number_eval(&const_eval);
50
51 StatusOr<const SemanticValue*> result =
52 parse_number_eval.Apply(/*context=*/{}, expression.get(), &arena_);
53
54 EXPECT_TRUE(result.ok());
55 const SemanticValue* result_value = result.ValueOrDie();
56 EXPECT_NE(result_value, nullptr);
57 return result_value->Value<T>();
58 }
59 };
60
61 using NumberTypes = ::testing::Types<int8, uint8, int16, uint16, int32, uint32,
62 int64, uint64, double, float>;
63 TYPED_TEST_SUITE(ParseNumberEvaluatorTest, NumberTypes);
64
TYPED_TEST(ParseNumberEvaluatorTest,ParsesNumber)65 TYPED_TEST(ParseNumberEvaluatorTest, ParsesNumber) {
66 EXPECT_EQ(this->Eval("42"), 42);
67 }
68
TEST_F(GrammarTest,FailsOnInvalidArgument)69 TEST_F(GrammarTest, FailsOnInvalidArgument) {
70 ParseNumberExpressionT parse_number_expression;
71 parse_number_expression.base_type = flatbuffers_base_type<int32>::value;
72 parse_number_expression.value = CreatePrimitiveConstExpression<int32>(42);
73 OwnedFlatbuffer<SemanticExpression> expression =
74 CreateExpression(std::move(parse_number_expression));
75
76 ConstEvaluator const_eval(semantic_values_schema_.get());
77 ParseNumberEvaluator parse_number_eval(&const_eval);
78
79 StatusOr<const SemanticValue*> result =
80 parse_number_eval.Apply(/*context=*/{}, expression.get(), &arena_);
81
82 EXPECT_FALSE(result.ok());
83 }
84
85 } // namespace
86 } // namespace libtextclassifier3::grammar
87