|
| 1 | +#include <cstdint> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "bytes-to-hex.hpp" |
| 5 | +#include "gtest/gtest.h" |
| 6 | + |
| 7 | +TEST(BytesToHexTest, EmptyVector) { |
| 8 | + const std::vector<uint8_t> input; |
| 9 | + EXPECT_EQ(bytes_to_hex_string(input), ""); |
| 10 | +} |
| 11 | + |
| 12 | +TEST(BytesToHexTest, SingleByte) { |
| 13 | + EXPECT_EQ(bytes_to_hex_string({0x00}), "00"); |
| 14 | + EXPECT_EQ(bytes_to_hex_string({0xFF}), "FF"); |
| 15 | + EXPECT_EQ(bytes_to_hex_string({0x0A}), "0A"); |
| 16 | + EXPECT_EQ(bytes_to_hex_string({0xA0}), "A0"); |
| 17 | +} |
| 18 | + |
| 19 | +TEST(BytesToHexTest, CommonCases) { |
| 20 | + EXPECT_EQ(bytes_to_hex_string({0xBA, 0xAD}), "BAAD"); |
| 21 | + EXPECT_EQ(bytes_to_hex_string({0xDE, 0xAD, 0xBE, 0xEF}), "DEADBEEF"); |
| 22 | + EXPECT_EQ(bytes_to_hex_string({0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}), |
| 23 | + "0123456789ABCDEF"); |
| 24 | +} |
| 25 | + |
| 26 | +TEST(BytesToHexTest, AllPossibleBytes) { |
| 27 | + std::vector<uint8_t> all_bytes(256); |
| 28 | + for (int i = 0; i < 256; ++i) { |
| 29 | + all_bytes[i] = static_cast<uint8_t>(i); |
| 30 | + } |
| 31 | + const std::string result = bytes_to_hex_string(all_bytes); |
| 32 | + ASSERT_EQ(result.size(), 512); // 256 bytes * 2 chars |
| 33 | + for (int i = 0; i < 256; ++i) { |
| 34 | + const std::string byte_str = result.substr(i * 2, 2); |
| 35 | + const std::string expected = std::string(1, "0123456789ABCDEF"[i >> 4]) + |
| 36 | + std::string(1, "0123456789ABCDEF"[i & 0x0F]); |
| 37 | + EXPECT_EQ(byte_str, expected); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +TEST(BytesToHexTest, CorrectFormatting) { |
| 42 | + const std::vector<uint8_t> input = {0x1, 0x2, 0x03, 0x10, 0xFF}; |
| 43 | + const std::string result = bytes_to_hex_string(input); |
| 44 | + EXPECT_EQ(result, "01020310FF"); |
| 45 | + EXPECT_TRUE(result.find("01") != std::string::npos); |
| 46 | + EXPECT_TRUE(result.find("02") != std::string::npos); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(BytesToHexTest, LargeInput) { |
| 50 | + const std::vector<uint8_t> large_input(10000, 0xAB); |
| 51 | + const std::string result = bytes_to_hex_string(large_input); |
| 52 | + EXPECT_EQ(result.size(), 20000); |
| 53 | + for (size_t i = 0; i < result.size(); i += 2) { |
| 54 | + EXPECT_EQ(result.substr(i, 2), "AB"); |
| 55 | + } |
| 56 | +} |
0 commit comments