AntoinePrv commented on code in PR #47294:
URL: https://github.com/apache/arrow/pull/47294#discussion_r2354994158


##########
cpp/src/arrow/util/bit_util_test.cc:
##########
@@ -1997,11 +1997,191 @@ TEST(BitUtil, RoundUpToPowerOf2) {
 #undef U64
 #undef S64
 
+/// Test the maximum number of bytes needed to write a LEB128 of a give size.
+TEST(LEB128, MaxLEB128ByteLenFor) {
+  EXPECT_EQ(bit_util::kMaxLEB128ByteLenFor<int16_t>, 3);
+  EXPECT_EQ(bit_util::kMaxLEB128ByteLenFor<int32_t>, 5);
+  EXPECT_EQ(bit_util::kMaxLEB128ByteLenFor<int64_t>, 10);
+}
+
+/// Utility function to test LEB128 encoding with known input value and 
expected byte
+/// array
+template <typename Int>
+void TestLEB128Encode(Int input_value, const std::vector<uint8_t>& 
expected_data,
+                      std::size_t buffer_size) {
+  std::vector<uint8_t> buffer(buffer_size);
+  auto bytes_written = bit_util::WriteLEB128(input_value, buffer.data(),
+                                             
static_cast<int32_t>(buffer.size()));
+
+  EXPECT_EQ(bytes_written, expected_data.size());
+  // Encoded data
+  for (std::size_t i = 0; i < expected_data.size(); ++i) {
+    EXPECT_EQ(buffer.at(i), expected_data.at(i));
+  }
+
+  // When the value is successfully encoded, the remaining of the buffer is 
untouched
+  if (bytes_written > 0) {
+    for (std::size_t i = bytes_written; i < buffer.size(); ++i) {
+      EXPECT_EQ(buffer.at(i), 0);
+    }
+  }
+}
+
+/// Test encoding to known LEB128 byte sequences with edge cases parameters.
+/// \see LEB128.KnownSuccessfulValues for other known values tested.
+TEST(LEB128, WriteEdgeCases) {
+  // Single byte value 0
+  TestLEB128Encode(0U, {0x00}, 1);
+  // Single byte value 127
+  TestLEB128Encode(127U, {0x7F}, 1);
+  // Three byte value 16384, encoded in larger buffer
+  TestLEB128Encode(16384U, {0x80, 0x80, 0x01}, 10);
+  // Two byte boundary values
+  TestLEB128Encode(129U, {0x81, 0x01}, 2);
+  TestLEB128Encode(16383U, {0xFF, 0x7F}, 2);
+  // Error case: Buffer too small for value 128 (needs 2 bytes but only 1 
provided)
+  TestLEB128Encode(128U, {}, 1);
+  // Error case: Buffer too small for uint32_t max (needs 5 bytes but only 4 
provided)
+  TestLEB128Encode(4294967295U, {}, 4);

Review Comment:
   We have them in `LEB128.KnownSuccessfulValues`.



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to