test_tohex.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <gtest/gtest.h>
  2. #include "Utils.h"
  3. using namespace mesh;
  4. #define HEX_BUFFER_SIZE(input) (sizeof(input) * 2 + 1)
  5. TEST(UtilsToHex, ConvertSingleByte) {
  6. uint8_t input[] = {0xAB};
  7. char output[HEX_BUFFER_SIZE(input)];
  8. Utils::toHex(output, input, sizeof(input));
  9. EXPECT_STREQ("AB", output);
  10. }
  11. TEST(UtilsToHex, ConvertMultipleBytes) {
  12. uint8_t input[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
  13. char output[HEX_BUFFER_SIZE(input)];
  14. Utils::toHex(output, input, sizeof(input));
  15. EXPECT_STREQ("0123456789ABCDEF", output);
  16. }
  17. TEST(UtilsToHex, ConvertZeroByte) {
  18. uint8_t input[] = {0x00};
  19. char output[HEX_BUFFER_SIZE(input)];
  20. Utils::toHex(output, input, sizeof(input));
  21. EXPECT_STREQ("00", output);
  22. }
  23. TEST(UtilsToHex, ConvertMaxByte) {
  24. uint8_t input[] = {0xFF};
  25. char output[HEX_BUFFER_SIZE(input)];
  26. Utils::toHex(output, input, sizeof(input));
  27. EXPECT_STREQ("FF", output);
  28. }
  29. TEST(UtilsToHex, NullTerminatesOnEmptyInput) {
  30. uint8_t input[] = {0xAB};
  31. char output[] = "X"; // Pre-fill with X.
  32. Utils::toHex(output, input, 0);
  33. // Should just null-terminate at position 0
  34. EXPECT_EQ('\0', output[0]);
  35. }
  36. int main(int argc, char **argv) {
  37. ::testing::InitGoogleTest(&argc, argv);
  38. return RUN_ALL_TESTS();
  39. }