|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, NVIDIA CORPORATION. |
| 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 "reverse_strings.hpp" |
| 18 | + |
| 19 | +#include <cudf_test/base_fixture.hpp> |
| 20 | +#include <cudf_test/column_utilities.hpp> |
| 21 | +#include <cudf_test/column_wrapper.hpp> |
| 22 | + |
| 23 | +#include <cudf/strings/strings_column_view.hpp> |
| 24 | + |
| 25 | +#include <string> |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +std::string bytes_to_string(std::initializer_list<uint8_t> bytes) |
| 30 | +{ |
| 31 | + return std::string(bytes.begin(), bytes.end()); |
| 32 | +} |
| 33 | + |
| 34 | +} // namespace |
| 35 | + |
| 36 | +struct ReverseStringsTests : public cudf::test::BaseFixture {}; |
| 37 | + |
| 38 | +TEST_F(ReverseStringsTests, EmptyAndNulls) |
| 39 | +{ |
| 40 | + auto const input = cudf::test::strings_column_wrapper{}; |
| 41 | + auto const result = spark_rapids_jni::reverse_strings(cudf::strings_column_view{input}); |
| 42 | + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, input); |
| 43 | + |
| 44 | + auto const with_nulls = |
| 45 | + cudf::test::strings_column_wrapper({"abc", "", "世界"}, {true, false, true}); |
| 46 | + auto const expected = |
| 47 | + cudf::test::strings_column_wrapper({"cba", "", "界世"}, {true, false, true}); |
| 48 | + auto const reversed = spark_rapids_jni::reverse_strings(cudf::strings_column_view{with_nulls}); |
| 49 | + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*reversed, expected); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(ReverseStringsTests, WellFormedUtf8) |
| 53 | +{ |
| 54 | + // ASCII, complete 2-/3-/4-byte characters. |
| 55 | + auto const input = cudf::test::strings_column_wrapper( |
| 56 | + {"ABC", |
| 57 | + bytes_to_string({0x41, 0xC3, 0xA9}), // Aé |
| 58 | + bytes_to_string({0x41, 0xE4, 0xB8, 0x96}), // A世 |
| 59 | + bytes_to_string({0x41, 0xF0, 0x90, 0x8D, 0x88})}); // A + U+10048-ish 4-byte |
| 60 | + auto const expected = |
| 61 | + cudf::test::strings_column_wrapper({"CBA", |
| 62 | + bytes_to_string({0xC3, 0xA9, 0x41}), |
| 63 | + bytes_to_string({0xE4, 0xB8, 0x96, 0x41}), |
| 64 | + bytes_to_string({0xF0, 0x90, 0x8D, 0x88, 0x41})}); |
| 65 | + auto const result = spark_rapids_jni::reverse_strings(cudf::strings_column_view{input}); |
| 66 | + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(ReverseStringsTests, TruncatedTrailingUtf8NoOverread) |
| 70 | +{ |
| 71 | + // SPARK-57507 cases. Neighbor rows carry sentinel bytes; an over-read would pull them in. |
| 72 | + auto const row0 = bytes_to_string({0x41, 0xCE}); // A + truncated 2-byte lead |
| 73 | + auto const row1 = bytes_to_string({0xA9, 0x42}); // must not leak into row0 |
| 74 | + auto const row2 = bytes_to_string({0x41, 0xE4, 0xB8}); // A + truncated 3-byte lead |
| 75 | + auto const row3 = bytes_to_string({0x96, 0x43}); // must not leak into row2 |
| 76 | + auto const row4 = bytes_to_string({0x41, 0xF0, 0x90}); // A + truncated 4-byte lead |
| 77 | + auto const row5 = bytes_to_string({0x8D, 0x88, 0x44}); // must not leak into row4 |
| 78 | + auto const row6 = bytes_to_string({0xE4, 0xB8, 0x96, 0xCE}); // 世 + orphan 2-byte lead |
| 79 | + auto const row7 = bytes_to_string({0x45}); |
| 80 | + |
| 81 | + auto const input = |
| 82 | + cudf::test::strings_column_wrapper({row0, row1, row2, row3, row4, row5, row6, row7}); |
| 83 | + auto const expected = cudf::test::strings_column_wrapper( |
| 84 | + {bytes_to_string({0xCE, 0x41}), |
| 85 | + bytes_to_string({0x42, 0xA9}), // 0xA9 is continuation -> Spark width 1, then 'B' |
| 86 | + bytes_to_string({0xE4, 0xB8, 0x41}), |
| 87 | + bytes_to_string({0x43, 0x96}), |
| 88 | + bytes_to_string({0xF0, 0x90, 0x41}), |
| 89 | + bytes_to_string({0x44, 0x88, 0x8D}), |
| 90 | + bytes_to_string({0xCE, 0xE4, 0xB8, 0x96}), |
| 91 | + bytes_to_string({0x45})}); |
| 92 | + |
| 93 | + auto const result = spark_rapids_jni::reverse_strings(cudf::strings_column_view{input}); |
| 94 | + CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected); |
| 95 | +} |
0 commit comments