Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27cfb243 authored by Chris Manton's avatar Chris Manton Committed by Gerrit Code Review
Browse files

Merge "unit_test: gd::common::byte_array"

parents dfe226b6 8cce4ff9
Loading
Loading
Loading
Loading
+113 −17
Original line number Diff line number Diff line
@@ -22,32 +22,128 @@

using bluetooth::common::ByteArray;

static const char* test_bytes = "4c68384139f574d836bcf34e9dfb01bf\0";
static uint8_t test_data[16] = {
namespace {
const char* byte_string16 = "4c68384139f574d836bcf34e9dfb01bf\0";
const uint8_t byte_data16[16] = {
    0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf};
static uint8_t data[16] = {
    0x4c, 0x87, 0x49, 0xe1, 0x2e, 0x55, 0x0f, 0x7f, 0x60, 0x8b, 0x4f, 0x96, 0xd7, 0xc5, 0xbc, 0x2a};
const char* byte_string21 = "4c68384139f574d836bcf34e9dfb01bf0011223344\0";
const uint8_t byte_data21[21] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3,
                                 0x4e, 0x9d, 0xfb, 0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44};
const char* byte_string23 = "4c68384139f574d836bcf34e9dfb01bf00112233445566\0";
const uint8_t byte_data23[23] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e,
                                 0x9d, 0xfb, 0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
const char* byte_string28 = "4c68384139f574d836bcf34e9dfb01bf00112233445566778899aabb\0";
const uint8_t byte_data28[28] = {0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb,
                                 0x01, 0xbf, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb};

TEST(ByteArrayTest, test_constructor_array) {
  ByteArray<16> byte_array(data);

  for (size_t i = 0; i < ByteArray<16>::kLength; i++) {
template <typename T, size_t N>
void simple_constructor_test(const T (&data)[N]) {
  ByteArray<N> byte_array(data);
  for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
    ASSERT_EQ(data[i], byte_array.bytes[i]);
  }
}

TEST(ByteArrayTest, test_from_str) {
  auto byte_array = ByteArray<16>::FromString(test_bytes);
template <typename T, size_t N>
void simple_const_constructor_test(const T (&data)[N]) {
  const ByteArray<N> byte_array(data);
  for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
    ASSERT_EQ(data[i], byte_array.data()[i]);
  }
}

template <typename T, size_t N>
void simple_array_constructor_test(const T (&data)[N]) {
  std::array<uint8_t, N> array_of_bytes;
  std::copy(data, data + N, std::begin(array_of_bytes));

  ByteArray<N> byte_array(array_of_bytes);
  for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
    ASSERT_EQ(data[i], byte_array.data()[i]);
  }
}

template <typename T, size_t N>
void simple_from_string_test(const char* byte_string, const T (&data)[N]) {
  auto byte_array = ByteArray<N>::FromString(byte_string);
  ASSERT_TRUE(byte_array);

  for (size_t i = 0; i < ByteArray<16>::kLength; i++) {
    ASSERT_EQ(test_data[i], byte_array->bytes[i]);
  for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
    ASSERT_EQ(data[i], byte_array->bytes[i]);
  }
}

TEST(ByteArrayTest, test_to_str) {
  ByteArray<16> byte_array = {
      {0x4C, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8, 0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf}};
template <typename T, size_t N>
void simple_to_string_test(const char* byte_string, const T (&data)[N]) {
  const ByteArray<N> byte_array(data);
  std::string str = byte_array.ToString();
  ASSERT_STREQ(str.c_str(), test_bytes);
  ASSERT_STREQ(str.c_str(), byte_string);
}

template <typename T, size_t N>
void simple_from_legacy_string_test(const char* byte_string, const T (&data)[N]) {
  auto byte_array = ByteArray<N>::FromLegacyConfigString(byte_string);
  ASSERT_TRUE(byte_array);

  for (size_t i = 0; i < ByteArray<N>::kLength; i++) {
    ASSERT_EQ(data[i], byte_array->bytes[i]);
  }
}

template <typename T, size_t N>
void simple_to_legacy_string_test(const char* byte_string, const T (&data)[N]) {
  const ByteArray<N> byte_array(data);
  std::string str = byte_array.ToLegacyConfigString();
  ASSERT_STREQ(str.c_str(), byte_string);
}

}  // namespace

TEST(ByteArrayTest, test_simple_constructor) {
  simple_constructor_test<const uint8_t, 16>(byte_data16);
  simple_constructor_test<const uint8_t, 21>(byte_data21);
  simple_constructor_test<const uint8_t, 23>(byte_data23);
  simple_constructor_test<const uint8_t, 28>(byte_data28);
}

TEST(ByteArrayTest, test_simple_const_constructor) {
  simple_const_constructor_test<const uint8_t, 16>(byte_data16);
  simple_const_constructor_test<const uint8_t, 21>(byte_data21);
  simple_const_constructor_test<const uint8_t, 23>(byte_data23);
  simple_const_constructor_test<const uint8_t, 28>(byte_data28);
}

TEST(ByteArrayTest, test_simple_array_constructor) {
  simple_array_constructor_test<const uint8_t, 16>(byte_data16);
  simple_array_constructor_test<const uint8_t, 21>(byte_data21);
  simple_array_constructor_test<const uint8_t, 23>(byte_data23);
  simple_array_constructor_test<const uint8_t, 28>(byte_data28);
}

TEST(ByteArrayTest, test_from_str) {
  simple_from_string_test<const uint8_t, 16>(byte_string16, byte_data16);
  simple_from_string_test<const uint8_t, 21>(byte_string21, byte_data21);
  simple_from_string_test<const uint8_t, 23>(byte_string23, byte_data23);
  simple_from_string_test<const uint8_t, 28>(byte_string28, byte_data28);
}

TEST(ByteArrayTest, test_from_legacy_str) {
  simple_from_legacy_string_test<const uint8_t, 16>(byte_string16, byte_data16);
  simple_from_legacy_string_test<const uint8_t, 21>(byte_string21, byte_data21);
  simple_from_legacy_string_test<const uint8_t, 23>(byte_string23, byte_data23);
  simple_from_legacy_string_test<const uint8_t, 28>(byte_string28, byte_data28);
}

TEST(ByteArrayTest, test_to_str) {
  simple_to_string_test<const uint8_t, 16>(byte_string16, byte_data16);
  simple_to_string_test<const uint8_t, 21>(byte_string21, byte_data21);
  simple_to_string_test<const uint8_t, 23>(byte_string23, byte_data23);
  simple_to_string_test<const uint8_t, 28>(byte_string28, byte_data28);
}

TEST(ByteArrayTest, test_to_legacy_str) {
  simple_to_legacy_string_test<const uint8_t, 16>(byte_string16, byte_data16);
  simple_to_legacy_string_test<const uint8_t, 21>(byte_string21, byte_data21);
  simple_to_legacy_string_test<const uint8_t, 23>(byte_string23, byte_data23);
  simple_to_legacy_string_test<const uint8_t, 28>(byte_string28, byte_data28);
}