test_bin_to_hex.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "includes.h"
  2. #include "test_sink.h"
  3. #include "spdlog/fmt/bin_to_hex.h"
  4. TEST_CASE("to_hex", "[to_hex]") {
  5. std::ostringstream oss;
  6. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  7. spdlog::logger oss_logger("oss", oss_sink);
  8. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  9. oss_logger.info("{}", spdlog::to_hex(v));
  10. auto output = oss.str();
  11. REQUIRE(ends_with(output,
  12. "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
  13. }
  14. TEST_CASE("to_hex_upper", "[to_hex]") {
  15. std::ostringstream oss;
  16. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  17. spdlog::logger oss_logger("oss", oss_sink);
  18. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  19. oss_logger.info("{:X}", spdlog::to_hex(v));
  20. auto output = oss.str();
  21. REQUIRE(ends_with(output,
  22. "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
  23. }
  24. TEST_CASE("to_hex_no_delimiter", "[to_hex]") {
  25. std::ostringstream oss;
  26. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  27. spdlog::logger oss_logger("oss", oss_sink);
  28. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  29. oss_logger.info("{:sX}", spdlog::to_hex(v));
  30. auto output = oss.str();
  31. REQUIRE(
  32. ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
  33. }
  34. TEST_CASE("to_hex_show_ascii", "[to_hex]") {
  35. std::ostringstream oss;
  36. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  37. spdlog::logger oss_logger("oss", oss_sink);
  38. std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
  39. oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8));
  40. REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." +
  41. std::string(spdlog::details::os::default_eol)));
  42. }
  43. TEST_CASE("to_hex_different_size_per_line", "[to_hex]") {
  44. std::ostringstream oss;
  45. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  46. spdlog::logger oss_logger("oss", oss_sink);
  47. std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
  48. oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10));
  49. REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." +
  50. std::string(spdlog::details::os::default_eol)));
  51. oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
  52. REQUIRE(ends_with(oss.str(),
  53. "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
  54. oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
  55. REQUIRE(ends_with(
  56. oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) +
  57. "0006: FFFF .." + std::string(spdlog::details::os::default_eol)));
  58. oss_logger.info("{:Xs}", spdlog::to_hex(v, 6));
  59. REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" +
  60. std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
  61. std::string(spdlog::details::os::default_eol)));
  62. }
  63. TEST_CASE("to_hex_no_ascii", "[to_hex]") {
  64. std::ostringstream oss;
  65. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  66. spdlog::logger oss_logger("oss", oss_sink);
  67. std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
  68. oss_logger.info("{:Xs}", spdlog::to_hex(v, 8));
  69. REQUIRE(ends_with(oss.str(),
  70. "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
  71. oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8));
  72. REQUIRE(
  73. ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
  74. }