test00.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <locale>
  2. #include <pqxx/cursor>
  3. #include <pqxx/strconv>
  4. #include "test_helpers.hxx"
  5. using namespace pqxx;
  6. // Initial test program for libpqxx. Test functionality that doesn't require a
  7. // running database.
  8. namespace
  9. {
  10. template<typename T>
  11. inline void
  12. strconv(std::string const &type, T const &Obj, std::string const &expected)
  13. {
  14. std::string const Objstr{to_string(Obj)};
  15. PQXX_CHECK_EQUAL(Objstr, expected, "String mismatch for " + type + ".");
  16. T NewObj;
  17. from_string(Objstr, NewObj);
  18. PQXX_CHECK_EQUAL(
  19. to_string(NewObj), expected, "String mismatch for recycled " + type + ".");
  20. }
  21. // There's no from_string<char const *>()...
  22. inline void
  23. strconv(std::string const &type, char const Obj[], std::string const &expected)
  24. {
  25. std::string const Objstr(to_string(Obj));
  26. PQXX_CHECK_EQUAL(Objstr, expected, "String mismatch for " + type + ".");
  27. }
  28. constexpr double not_a_number{std::numeric_limits<double>::quiet_NaN()};
  29. struct intderef
  30. {
  31. template<typename ITER> int operator()(ITER i) const noexcept
  32. {
  33. return int(*i);
  34. }
  35. };
  36. void test_000()
  37. {
  38. PQXX_CHECK_EQUAL(
  39. oid_none, 0u,
  40. "InvalidIod is not zero as it used to be. This may conceivably "
  41. "cause problems in libpqxx.");
  42. PQXX_CHECK(
  43. cursor_base::prior() < 0 and cursor_base::backward_all() < 0,
  44. "cursor_base::difference_type appears to be unsigned.");
  45. constexpr char weird[]{"foo\t\n\0bar"};
  46. std::string const weirdstr(weird, std::size(weird) - 1);
  47. // Test string conversions
  48. strconv("char const[]", "", "");
  49. strconv("char const[]", "foo", "foo");
  50. strconv("int", 0, "0");
  51. strconv("int", 100, "100");
  52. strconv("int", -1, "-1");
  53. #if defined(_MSC_VER)
  54. long const long_min{LONG_MIN}, long_max{LONG_MAX};
  55. #else
  56. long const long_min{std::numeric_limits<long>::min()},
  57. long_max{std::numeric_limits<long>::max()};
  58. #endif
  59. std::stringstream lminstr, lmaxstr, llminstr, llmaxstr, ullmaxstr;
  60. lminstr.imbue(std::locale("C"));
  61. lmaxstr.imbue(std::locale("C"));
  62. llminstr.imbue(std::locale("C"));
  63. llmaxstr.imbue(std::locale("C"));
  64. ullmaxstr.imbue(std::locale("C"));
  65. lminstr << long_min;
  66. lmaxstr << long_max;
  67. auto const ullong_max{std::numeric_limits<unsigned long long>::max()};
  68. auto const llong_max{std::numeric_limits<long long>::max()},
  69. llong_min{std::numeric_limits<long long>::min()};
  70. llminstr << llong_min;
  71. llmaxstr << llong_max;
  72. ullmaxstr << ullong_max;
  73. strconv("long", 0, "0");
  74. strconv("long", long_min, lminstr.str());
  75. strconv("long", long_max, lmaxstr.str());
  76. strconv("double", not_a_number, "nan");
  77. strconv("string", std::string{}, "");
  78. strconv("string", weirdstr, weirdstr);
  79. strconv("long long", 0LL, "0");
  80. strconv("long long", llong_min, llminstr.str());
  81. strconv("long long", llong_max, llmaxstr.str());
  82. strconv("unsigned long long", 0ULL, "0");
  83. strconv("unsigned long long", ullong_max, ullmaxstr.str());
  84. std::stringstream ss;
  85. strconv("empty stringstream", ss, "");
  86. ss << -3.1415;
  87. strconv("stringstream", ss, ss.str());
  88. }
  89. PQXX_REGISTER_TEST(test_000);
  90. } // namespace