|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | +#include "velox/common/base/tests/GTestUtils.h" |
| 17 | +#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h" |
| 18 | + |
| 19 | +namespace facebook::velox::functions::sparksql::test { |
| 20 | +namespace { |
| 21 | + |
| 22 | +class VarcharTypeWriteSideCheckTest : public SparkFunctionBaseTest {}; |
| 23 | + |
| 24 | +TEST_F(VarcharTypeWriteSideCheckTest, varcharTypeWriteSideCheck) { |
| 25 | + const auto varcharTypeWriteSideCheck = |
| 26 | + [&](const std::optional<std::string>& input, |
| 27 | + const std::optional<int32_t>& limit) { |
| 28 | + return evaluateOnce<std::string>( |
| 29 | + "varchar_type_write_side_check(c0, c1)", input, limit); |
| 30 | + }; |
| 31 | + |
| 32 | + // Basic cases - string length <= limit. |
| 33 | + EXPECT_EQ(varcharTypeWriteSideCheck("abc", 3), "abc"); |
| 34 | + EXPECT_EQ(varcharTypeWriteSideCheck("ab", 3), "ab"); |
| 35 | + EXPECT_EQ(varcharTypeWriteSideCheck("", 5), ""); |
| 36 | + |
| 37 | + // Cases with trailing spaces. |
| 38 | + // Edge cases - input string is longer than limit but trims to exactly limit. |
| 39 | + EXPECT_EQ(varcharTypeWriteSideCheck("abc ", 3), "abc"); |
| 40 | + EXPECT_EQ(varcharTypeWriteSideCheck("abc ", 4), "abc "); |
| 41 | + EXPECT_EQ(varcharTypeWriteSideCheck("abc ", 5), "abc "); |
| 42 | + |
| 43 | + // Unicode string cases with trailing spaces. |
| 44 | + EXPECT_EQ(varcharTypeWriteSideCheck("世界 ", 2), "世界"); |
| 45 | + EXPECT_EQ(varcharTypeWriteSideCheck("世界", 2), "世界"); |
| 46 | + |
| 47 | + // Error cases - string length > limit even after trimming trailing spaces. |
| 48 | + VELOX_ASSERT_USER_THROW( |
| 49 | + varcharTypeWriteSideCheck("abcd", 3), |
| 50 | + "Exceeds allowed length limitation: 3"); |
| 51 | + VELOX_ASSERT_USER_THROW( |
| 52 | + varcharTypeWriteSideCheck("世界人", 2), |
| 53 | + "Exceeds allowed length limitation: 2"); |
| 54 | + VELOX_ASSERT_USER_THROW( |
| 55 | + varcharTypeWriteSideCheck("abc def", 5), |
| 56 | + "Exceeds allowed length limitation: 5"); |
| 57 | + |
| 58 | + // Null input cases. |
| 59 | + EXPECT_EQ(varcharTypeWriteSideCheck(std::nullopt, 5), std::nullopt); |
| 60 | + |
| 61 | + // Edge cases - length limit must be positive |
| 62 | + VELOX_ASSERT_USER_THROW( |
| 63 | + varcharTypeWriteSideCheck("abc", 0), |
| 64 | + "The length limit must be greater than 0."); |
| 65 | + VELOX_ASSERT_USER_THROW( |
| 66 | + varcharTypeWriteSideCheck("abc", -1), |
| 67 | + "The length limit must be greater than 0."); |
| 68 | + |
| 69 | + // Edge cases - input string is all spaces. |
| 70 | + EXPECT_EQ(varcharTypeWriteSideCheck(" ", 2), " "); |
| 71 | + EXPECT_EQ(varcharTypeWriteSideCheck(" ", 3), " "); |
| 72 | + EXPECT_EQ(varcharTypeWriteSideCheck(" ", 1), " "); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace |
| 76 | +} // namespace facebook::velox::functions::sparksql::test |
0 commit comments