test_strconv.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <memory>
  2. #include <optional>
  3. #include "../test_helpers.hxx"
  4. namespace
  5. {
  6. enum colour
  7. {
  8. red,
  9. green,
  10. blue
  11. };
  12. enum class weather : short
  13. {
  14. hot,
  15. cold,
  16. wet
  17. };
  18. enum class many : unsigned long long
  19. {
  20. bottom = 0,
  21. top = std::numeric_limits<unsigned long long>::max(),
  22. };
  23. } // namespace
  24. namespace pqxx
  25. {
  26. PQXX_DECLARE_ENUM_CONVERSION(colour);
  27. PQXX_DECLARE_ENUM_CONVERSION(weather);
  28. PQXX_DECLARE_ENUM_CONVERSION(many);
  29. } // namespace pqxx
  30. namespace
  31. {
  32. void test_strconv_bool()
  33. {
  34. PQXX_CHECK_EQUAL(pqxx::to_string(false), "false", "Wrong to_string(false).");
  35. PQXX_CHECK_EQUAL(pqxx::to_string(true), "true", "Wrong to_string(true).");
  36. bool result;
  37. pqxx::from_string("false", result);
  38. PQXX_CHECK_EQUAL(result, false, "Wrong from_string('false').");
  39. pqxx::from_string("FALSE", result);
  40. PQXX_CHECK_EQUAL(result, false, "Wrong from_string('FALSE').");
  41. pqxx::from_string("f", result);
  42. PQXX_CHECK_EQUAL(result, false, "Wrong from_string('f').");
  43. pqxx::from_string("F", result);
  44. PQXX_CHECK_EQUAL(result, false, "Wrong from_string('F').");
  45. pqxx::from_string("0", result);
  46. PQXX_CHECK_EQUAL(result, false, "Wrong from_string('0').");
  47. pqxx::from_string("true", result);
  48. PQXX_CHECK_EQUAL(result, true, "Wrong from_string('true').");
  49. pqxx::from_string("TRUE", result);
  50. PQXX_CHECK_EQUAL(result, true, "Wrong from_string('TRUE').");
  51. pqxx::from_string("t", result);
  52. PQXX_CHECK_EQUAL(result, true, "Wrong from_string('t').");
  53. pqxx::from_string("T", result);
  54. PQXX_CHECK_EQUAL(result, true, "Wrong from_string('T').");
  55. pqxx::from_string("1", result);
  56. PQXX_CHECK_EQUAL(result, true, "Wrong from_string('1').");
  57. }
  58. void test_strconv_enum()
  59. {
  60. PQXX_CHECK_EQUAL(pqxx::to_string(red), "0", "Enum value did not convert.");
  61. PQXX_CHECK_EQUAL(pqxx::to_string(green), "1", "Enum value did not convert.");
  62. PQXX_CHECK_EQUAL(pqxx::to_string(blue), "2", "Enum value did not convert.");
  63. colour col;
  64. pqxx::from_string("2", col);
  65. PQXX_CHECK_EQUAL(col, blue, "Could not recover enum value from string.");
  66. }
  67. void test_strconv_class_enum()
  68. {
  69. PQXX_CHECK_EQUAL(
  70. pqxx::to_string(weather::hot), "0", "Class enum value did not convert.");
  71. PQXX_CHECK_EQUAL(
  72. pqxx::to_string(weather::wet), "2", "Enum value did not convert.");
  73. weather w;
  74. pqxx::from_string("2", w);
  75. PQXX_CHECK_EQUAL(
  76. w, weather::wet, "Could not recover class enum value from string.");
  77. PQXX_CHECK_EQUAL(
  78. pqxx::to_string(many::bottom), "0",
  79. "Small wide enum did not convert right.");
  80. PQXX_CHECK_EQUAL(
  81. pqxx::to_string(many::top),
  82. pqxx::to_string(std::numeric_limits<unsigned long long>::max()),
  83. "Large wide enum did not convert right.");
  84. }
  85. void test_strconv_optional()
  86. {
  87. PQXX_CHECK_THROWS(
  88. pqxx::to_string(std::optional<int>{}), pqxx::conversion_error,
  89. "Converting an empty optional did not throw conversion error.");
  90. PQXX_CHECK_EQUAL(
  91. pqxx::to_string(std::optional<int>{std::in_place, 10}), "10",
  92. "std::optional<int> does not convert right.");
  93. PQXX_CHECK_EQUAL(
  94. pqxx::to_string(std::optional<int>{std::in_place, -10000}), "-10000",
  95. "std::optional<int> does not convert right.");
  96. }
  97. void test_strconv_smart_pointer()
  98. {
  99. PQXX_CHECK_THROWS(
  100. pqxx::to_string(std::unique_ptr<int>{}), pqxx::conversion_error,
  101. "Converting an empty unique_ptr did not throw conversion error.");
  102. PQXX_CHECK_EQUAL(
  103. pqxx::to_string(std::make_unique<int>(10)), "10",
  104. "std::unique_ptr<int> does not convert right.");
  105. PQXX_CHECK_EQUAL(
  106. pqxx::to_string(std::make_unique<int>(-10000)), "-10000",
  107. "std::unique_ptr<int> does not convert right.");
  108. PQXX_CHECK_THROWS(
  109. pqxx::to_string(std::shared_ptr<int>{}), pqxx::conversion_error,
  110. "Converting an empty shared_ptr did not throw conversion error.");
  111. PQXX_CHECK_EQUAL(
  112. pqxx::to_string(std::make_shared<int>(10)), "10",
  113. "std::shared_ptr<int> does not convert right.");
  114. PQXX_CHECK_EQUAL(
  115. pqxx::to_string(std::make_shared<int>(-10000)), "-10000",
  116. "std::shared_ptr<int> does not convert right.");
  117. }
  118. PQXX_REGISTER_TEST(test_strconv_bool);
  119. PQXX_REGISTER_TEST(test_strconv_enum);
  120. PQXX_REGISTER_TEST(test_strconv_class_enum);
  121. PQXX_REGISTER_TEST(test_strconv_optional);
  122. PQXX_REGISTER_TEST(test_strconv_smart_pointer);
  123. } // namespace