pitrou commented on code in PR #38147:
URL: https://github.com/apache/arrow/pull/38147#discussion_r1357258668
##########
cpp/src/arrow/compute/light_array_test.cc:
##########
@@ -226,6 +226,120 @@ TEST(KeyColumnArray, SliceBool) {
}
}
+TEST(KeyColumnArray, SliceBinary) {
+ auto type = binary();
+ // Sample binary data using ArrayFromJSON
+ std::shared_ptr<Array> array =
+ ArrayFromJSON(type, R"(["Hello", "World", "Slice", "Binary", "Test"])");
+
+ // Create KeyColumnArray from the ArrayData
+ KeyColumnArray kc_array =
+ ColumnArrayFromArrayData(array->data(), 0, array->length()).ValueOrDie();
+
+ // Define test cases
+ struct {
+ int offset;
+ int length;
+ std::vector<std::string> expected;
+ } testCases[] = {
+ {0, 1, {"Hello"}},
+ {1, 1, {"World"}},
+ {2, 1, {"Slice"}},
+ {3, 1, {"Binary"}},
+ {4, 1, {"Test"}},
+ {0, 2, {"Hello", "World"}},
+ {1, 2, {"World", "Slice"}},
+ {2, 2, {"Slice", "Binary"}},
+ {3, 2, {"Binary", "Test"}},
+ {0, 3, {"Hello", "World", "Slice"}},
+ {1, 3, {"World", "Slice", "Binary"}},
+ {2, 3, {"Slice", "Binary", "Test"}},
+ {0, 4, {"Hello", "World", "Slice", "Binary"}},
+ {1, 4, {"World", "Slice", "Binary", "Test"}},
+ {0, 5, {"Hello", "World", "Slice", "Binary", "Test"}},
+ };
+
+ for (const auto& testCase : testCases) {
+ ARROW_SCOPED_TRACE("Offset: ", testCase.offset, " Length: ",
testCase.length);
+ KeyColumnArray sliced = kc_array.Slice(testCase.offset, testCase.length);
+
+ // Extract binary data from the sliced KeyColumnArray
+ std::vector<std::string> sliced_data;
+
+ const auto* offset_data = reinterpret_cast<const int32_t*>(sliced.data(1));
+ const auto* string_data = reinterpret_cast<const char*>(sliced.data(2));
+
+ for (auto i = 0; i < testCase.length; ++i) {
+ auto start = offset_data[i];
+ auto end = offset_data[i + 1];
+ sliced_data.push_back(std::string(string_data + start, string_data +
end));
+ }
+
+ // Compare the sliced values to the expected string
+ ASSERT_EQ(testCase.expected, sliced_data);
+ }
+}
+
+TEST(KeyColumnArray, SliceLargeBinary) {
+ auto type = large_binary();
+ // Sample binary data using ArrayFromJSON
+ std::shared_ptr<Array> array =
+ ArrayFromJSON(type, R"(["Hello", "World", "Slice", "Large", "Binary",
"Test"])");
+
+ // Create KeyColumnArray from the ArrayData
+ KeyColumnArray kc_array =
+ ColumnArrayFromArrayData(array->data(), 0, array->length()).ValueOrDie();
+
+ // Define test cases
+ struct {
+ int offset;
+ int length;
+ std::vector<std::string> expected;
+ } testCases[] = {
+ {0, 1, {"Hello"}},
+ {1, 1, {"World"}},
+ {2, 1, {"Slice"}},
+ {3, 1, {"Large"}},
+ {4, 1, {"Binary"}},
+ {5, 1, {"Test"}},
+ {0, 2, {"Hello", "World"}},
+ {1, 2, {"World", "Slice"}},
+ {2, 2, {"Slice", "Large"}},
+ {3, 2, {"Large", "Binary"}},
+ {4, 2, {"Binary", "Test"}},
+ {0, 3, {"Hello", "World", "Slice"}},
+ {1, 3, {"World", "Slice", "Large"}},
+ {2, 3, {"Slice", "Large", "Binary"}},
+ {3, 3, {"Large", "Binary", "Test"}},
+ {0, 4, {"Hello", "World", "Slice", "Large"}},
+ {1, 4, {"World", "Slice", "Large", "Binary"}},
+ {2, 4, {"Slice", "Large", "Binary", "Test"}},
+ {0, 5, {"Hello", "World", "Slice", "Large", "Binary"}},
+ {1, 5, {"World", "Slice", "Large", "Binary", "Test"}},
+ {0, 6, {"Hello", "World", "Slice", "Large", "Binary", "Test"}},
+ };
Review Comment:
> I've tried refactoring the Slice test function. Is this refactoring what
you intended?
Yes, perfect. Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]