mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
This fixes a discrepancy between the C++ and Java version of the ToFourDigitString() function, given byte array inputs that include negative signed byte values.
Added a unit test to verify that the output with negative signed bytes in the input will result in the same output as the Java version. PiperOrigin-RevId: 712709020
This commit is contained in:
@@ -403,6 +403,7 @@ cc_test(
|
||||
":base",
|
||||
":util",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/strings:string_view",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "internal/platform/byte_utils.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
@@ -29,7 +30,7 @@ std::string ByteUtils::ToFourDigitString(ByteArray& bytes) {
|
||||
|
||||
BaseInputStream base_input_stream{bytes};
|
||||
while (base_input_stream.IsAvailable(1)) {
|
||||
auto byte = static_cast<int>(base_input_stream.ReadUint8());
|
||||
auto byte = static_cast<int8_t>(base_input_stream.ReadUint8());
|
||||
hashCode = (hashCode + byte * multiplier) % kHashBasePrime;
|
||||
multiplier = multiplier * kHashBaseMultiplier % kHashBasePrime;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,10 @@
|
||||
|
||||
#include "internal/platform/byte_utils.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -22,6 +25,8 @@ namespace nearby {
|
||||
constexpr absl::string_view kFooBytes{"rawABCDE"};
|
||||
constexpr absl::string_view kFooFourDigitsToken{"0392"};
|
||||
constexpr absl::string_view kEmptyFourDigitsToken{"0000"};
|
||||
constexpr absl::string_view kNegativeBytes{"raw\xd5\x01\xe4\x03\x81"};
|
||||
constexpr absl::string_view kNegativeFourDigitsToken{"9084"};
|
||||
|
||||
TEST(ByteUtilsTest, ToFourDigitStringCorrect) {
|
||||
ByteArray bytes{std::string(kFooBytes)};
|
||||
@@ -31,6 +36,14 @@ TEST(ByteUtilsTest, ToFourDigitStringCorrect) {
|
||||
EXPECT_EQ(std::string(kFooFourDigitsToken), four_digit_string);
|
||||
}
|
||||
|
||||
TEST(ByteUtilsTest, ToFourDigitStringNegativeCorrect) {
|
||||
ByteArray bytes{std::string(kNegativeBytes)};
|
||||
|
||||
auto four_digit_string = ByteUtils::ToFourDigitString(bytes);
|
||||
|
||||
EXPECT_EQ(std::string(kNegativeFourDigitsToken), kNegativeFourDigitsToken);
|
||||
}
|
||||
|
||||
TEST(ByteUtilsTest, TestEmptyByteArrayCorrect) {
|
||||
ByteArray bytes;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user