Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This fixes a discrepancy between the C++ and Java version of the ToFourDigitString() function, given byte array inputs that include negative signed byte values. #3125

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ cc_test(
":base",
":util",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
3 changes: 2 additions & 1 deletion internal/platform/byte_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "internal/platform/byte_utils.h"

#include <cstdint>
#include <cstdlib>
#include <string>

Expand All @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions internal/platform/byte_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@

#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 {

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)};
Expand All @@ -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;

Expand Down
Loading