]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | #include "gandiva/replace_holder.h" | |
19 | ||
20 | #include <gtest/gtest.h> | |
21 | ||
22 | #include <memory> | |
23 | #include <vector> | |
24 | ||
25 | namespace gandiva { | |
26 | ||
27 | class TestReplaceHolder : public ::testing::Test { | |
28 | protected: | |
29 | ExecutionContext execution_context_; | |
30 | }; | |
31 | ||
32 | TEST_F(TestReplaceHolder, TestMultipleReplace) { | |
33 | std::shared_ptr<ReplaceHolder> replace_holder; | |
34 | ||
35 | auto status = ReplaceHolder::Make("ana", &replace_holder); | |
36 | EXPECT_EQ(status.ok(), true) << status.message(); | |
37 | ||
38 | std::string input_string = "banana"; | |
39 | std::string replace_string; | |
40 | int32_t out_length = 0; | |
41 | ||
42 | auto& replace = *replace_holder; | |
43 | const char* ret = | |
44 | replace(&execution_context_, input_string.c_str(), | |
45 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
46 | static_cast<int32_t>(replace_string.length()), &out_length); | |
47 | std::string ret_as_str(ret, out_length); | |
48 | EXPECT_EQ(out_length, 3); | |
49 | EXPECT_EQ(ret_as_str, "bna"); | |
50 | ||
51 | input_string = "bananaana"; | |
52 | ||
53 | ret = replace(&execution_context_, input_string.c_str(), | |
54 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
55 | static_cast<int32_t>(replace_string.length()), &out_length); | |
56 | ret_as_str = std::string(ret, out_length); | |
57 | EXPECT_EQ(out_length, 3); | |
58 | EXPECT_EQ(ret_as_str, "bna"); | |
59 | ||
60 | input_string = "bananana"; | |
61 | ||
62 | ret = replace(&execution_context_, input_string.c_str(), | |
63 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
64 | static_cast<int32_t>(replace_string.length()), &out_length); | |
65 | ret_as_str = std::string(ret, out_length); | |
66 | EXPECT_EQ(out_length, 2); | |
67 | EXPECT_EQ(ret_as_str, "bn"); | |
68 | ||
69 | input_string = "anaana"; | |
70 | ||
71 | ret = replace(&execution_context_, input_string.c_str(), | |
72 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
73 | static_cast<int32_t>(replace_string.length()), &out_length); | |
74 | ret_as_str = std::string(ret, out_length); | |
75 | EXPECT_EQ(out_length, 0); | |
76 | EXPECT_FALSE(execution_context_.has_error()); | |
77 | EXPECT_EQ(ret_as_str, ""); | |
78 | } | |
79 | ||
80 | TEST_F(TestReplaceHolder, TestNoMatchPattern) { | |
81 | std::shared_ptr<ReplaceHolder> replace_holder; | |
82 | ||
83 | auto status = ReplaceHolder::Make("ana", &replace_holder); | |
84 | EXPECT_EQ(status.ok(), true) << status.message(); | |
85 | ||
86 | std::string input_string = "apple"; | |
87 | std::string replace_string; | |
88 | int32_t out_length = 0; | |
89 | ||
90 | auto& replace = *replace_holder; | |
91 | const char* ret = | |
92 | replace(&execution_context_, input_string.c_str(), | |
93 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
94 | static_cast<int32_t>(replace_string.length()), &out_length); | |
95 | std::string ret_as_string(ret, out_length); | |
96 | EXPECT_EQ(out_length, 5); | |
97 | EXPECT_EQ(ret_as_string, "apple"); | |
98 | } | |
99 | ||
100 | TEST_F(TestReplaceHolder, TestReplaceSameSize) { | |
101 | std::shared_ptr<ReplaceHolder> replace_holder; | |
102 | ||
103 | auto status = ReplaceHolder::Make("a", &replace_holder); | |
104 | EXPECT_EQ(status.ok(), true) << status.message(); | |
105 | ||
106 | std::string input_string = "ananindeua"; | |
107 | std::string replace_string = "b"; | |
108 | int32_t out_length = 0; | |
109 | ||
110 | auto& replace = *replace_holder; | |
111 | const char* ret = | |
112 | replace(&execution_context_, input_string.c_str(), | |
113 | static_cast<int32_t>(input_string.length()), replace_string.c_str(), | |
114 | static_cast<int32_t>(replace_string.length()), &out_length); | |
115 | std::string ret_as_string(ret, out_length); | |
116 | EXPECT_EQ(out_length, 10); | |
117 | EXPECT_EQ(ret_as_string, "bnbnindeub"); | |
118 | } | |
119 | ||
120 | TEST_F(TestReplaceHolder, TestReplaceInvalidPattern) { | |
121 | std::shared_ptr<ReplaceHolder> replace_holder; | |
122 | ||
123 | auto status = ReplaceHolder::Make("+", &replace_holder); | |
124 | EXPECT_EQ(status.ok(), false) << status.message(); | |
125 | ||
126 | execution_context_.Reset(); | |
127 | } | |
128 | ||
129 | } // namespace gandiva |