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