Skip to content

Commit 94e1906

Browse files
committed
Review comment - add unittest
1 parent abe4dc0 commit 94e1906

File tree

5 files changed

+116
-10
lines changed

5 files changed

+116
-10
lines changed

presto-native-execution/presto_cpp/main/connectors/hive/functions/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
12+
1213
add_library(presto_hive_functions HiveFunctionRegistration.cpp)
13-
target_link_libraries(presto_hive_functions velox_functions_prestosql)
14+
target_link_libraries(presto_hive_functions presto_dynamic_function_registrar
15+
velox_functions_string)
16+
17+
if(PRESTO_ENABLE_TESTING)
18+
add_subdirectory(tests)
19+
endif()

presto-native-execution/presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h"
2020
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h"
21-
#include "velox/functions/FunctionRegistry.h"
2221

22+
using namespace facebook::velox;
2323
namespace facebook::presto::hive::functions {
2424

2525
namespace {

presto-native-execution/presto_cpp/main/connectors/hive/functions/InitcapFunction.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "velox/functions/Macros.h"
1818
#include "velox/functions/lib/string/StringImpl.h"
1919

20-
using namespace facebook::velox;
21-
using namespace facebook::velox::functions;
2220
namespace facebook::presto::hive::functions {
2321

2422
/// The InitCapFunction capitalizes the first character of each word in a
@@ -30,19 +28,19 @@ struct InitCapFunction {
3028
static constexpr bool is_default_ascii_behavior = true;
3129

3230
FOLLY_ALWAYS_INLINE void call(
33-
out_type<Varchar>& result,
34-
const arg_type<Varchar>& input) {
35-
stringImpl::initcap<
31+
out_type<velox::Varchar>& result,
32+
const arg_type<velox::Varchar>& input) {
33+
velox::functions::stringImpl::initcap<
3634
/*strictSpace=*/false,
3735
/*isAscii=*/false,
3836
/*turkishCasing=*/true,
3937
/*greekFinalSigma=*/true>(result, input);
4038
}
4139

4240
FOLLY_ALWAYS_INLINE void callAscii(
43-
out_type<Varchar>& result,
44-
const arg_type<Varchar>& input) {
45-
stringImpl::initcap<
41+
out_type<velox::Varchar>& result,
42+
const arg_type<velox::Varchar>& input) {
43+
velox::functions::stringImpl::initcap<
4644
/*strictSpace=*/false,
4745
/*isAscii=*/true,
4846
/*turkishCasing=*/true,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
add_executable(presto_hive_functions_test InitcapTest.cpp)
14+
15+
add_test(
16+
NAME presto_hive_functions_test
17+
COMMAND presto_hive_functions_test
18+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
19+
20+
target_link_libraries(
21+
presto_hive_functions_test presto_hive_functions presto_common
22+
velox_functions_test_lib GTest::gtest GTest::gtest_main)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
#include <gtest/gtest.h>
15+
16+
#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
17+
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
18+
19+
namespace facebook::presto::functions::test {
20+
class InitcapTest : public velox::functions::test::FunctionBaseTest {
21+
protected:
22+
static void SetUpTestCase() {
23+
velox::functions::test::FunctionBaseTest::SetUpTestCase();
24+
facebook::presto::hive::functions::registerHiveNativeFunctions();
25+
}
26+
};
27+
28+
TEST_F(InitcapTest, initcap) {
29+
const auto initcap = [&](const std::optional<std::string>& value) {
30+
return evaluateOnce<std::string>("\"hive.default.initcap\"(c0)", value);
31+
};
32+
33+
// Unicode only.
34+
EXPECT_EQ(
35+
initcap("àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ"),
36+
"Àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ");
37+
EXPECT_EQ(initcap("αβγδεζηθικλμνξοπρςστυφχψ"), "Αβγδεζηθικλμνξοπρςστυφχψ");
38+
// Mix of ascii and unicode.
39+
EXPECT_EQ(initcap("αβγδεζ world"), "Αβγδεζ World");
40+
EXPECT_EQ(initcap("αfoo wβ"), "Αfoo Wβ");
41+
// Ascii only.
42+
EXPECT_EQ(initcap("hello world"), "Hello World");
43+
EXPECT_EQ(initcap("HELLO WORLD"), "Hello World");
44+
EXPECT_EQ(initcap("1234"), "1234");
45+
EXPECT_EQ(initcap("a b c d"), "A B C D");
46+
EXPECT_EQ(initcap("abcd"), "Abcd");
47+
// Numbers.
48+
EXPECT_EQ(initcap("123"), "123");
49+
EXPECT_EQ(initcap("1abc"), "1abc");
50+
// Edge cases.
51+
EXPECT_EQ(initcap(""), "");
52+
EXPECT_EQ(initcap(std::nullopt), std::nullopt);
53+
54+
// Test with various whitespace characters
55+
EXPECT_EQ(initcap("YQ\tY"), "Yq\tY");
56+
EXPECT_EQ(initcap("YQ\nY"), "Yq\nY");
57+
EXPECT_EQ(initcap("YQ\rY"), "Yq\rY");
58+
EXPECT_EQ(initcap("hello\tworld\ntest"), "Hello\tWorld\nTest");
59+
EXPECT_EQ(initcap("foo\r\nbar"), "Foo\r\nBar");
60+
61+
// Test with multiple consecutive whitespaces
62+
EXPECT_EQ(initcap("hello world"), "Hello World");
63+
EXPECT_EQ(initcap("a b c"), "A B C");
64+
EXPECT_EQ(initcap("test\t\tvalue"), "Test\t\tValue");
65+
EXPECT_EQ(initcap("line\n\n\nbreak"), "Line\n\n\nBreak");
66+
67+
// Test with leading and trailing whitespaces
68+
EXPECT_EQ(initcap(" hello"), " Hello");
69+
EXPECT_EQ(initcap("world "), "World ");
70+
EXPECT_EQ(initcap(" spaces "), " Spaces ");
71+
EXPECT_EQ(initcap("\thello"), "\tHello");
72+
EXPECT_EQ(initcap("\nworld"), "\nWorld");
73+
EXPECT_EQ(initcap("test\n"), "Test\n");
74+
75+
// Test with mixed whitespace types
76+
EXPECT_EQ(initcap("hello \t\nworld"), "Hello \t\nWorld");
77+
EXPECT_EQ(initcap("a\tb\nc\rd"), "A\tB\nC\rD");
78+
EXPECT_EQ(initcap(" \t\n "), " \t\n ");
79+
}
80+
} // namespace facebook::presto::functions::test

0 commit comments

Comments
 (0)