test46.cxx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <cmath>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <pqxx/transaction>
  5. #include "test_helpers.hxx"
  6. using namespace pqxx;
  7. // Streams test program for libpqxx. Insert a result field into various
  8. // types of streams.
  9. namespace
  10. {
  11. void test_046()
  12. {
  13. connection conn;
  14. work tx{conn};
  15. row R{tx.exec1("SELECT count(*) FROM pg_tables")};
  16. // Read the value into a stringstream.
  17. std::stringstream I;
  18. I << R[0];
  19. // Now convert the stringstream into a numeric type
  20. long L{}, L2{};
  21. I >> L;
  22. R[0].to(L2);
  23. PQXX_CHECK_EQUAL(L, L2, "Inconsistency between conversion methods.");
  24. float F{}, F2{};
  25. std::stringstream I2;
  26. I2 << R[0];
  27. I2 >> F;
  28. R[0].to(F2);
  29. PQXX_CHECK_BOUNDS(F2, F - 0.01, F + 0.01, "Bad floating-point result.");
  30. auto F3{from_string<float>(R[0].c_str())};
  31. PQXX_CHECK_BOUNDS(F3, F - 0.01, F + 0.01, "Bad float from from_string.");
  32. auto D{from_string<double>(R[0].c_str())};
  33. PQXX_CHECK_BOUNDS(D, F - 0.01, F + 0.01, "Bad double from from_string.");
  34. auto LD{from_string<long double>(R[0].c_str())};
  35. PQXX_CHECK_BOUNDS(
  36. LD, F - 0.01, F + 0.01, "Bad long double from from_string.");
  37. auto S{from_string<std::string>(R[0].c_str())},
  38. S2{from_string<std::string>(std::string{R[0].c_str()})},
  39. S3{from_string<std::string>(R[0])};
  40. PQXX_CHECK_EQUAL(
  41. S2, S,
  42. "from_string(char const[], std::string &) "
  43. "is inconsistent with "
  44. "from_string(std::string const &, std::string &).");
  45. PQXX_CHECK_EQUAL(
  46. S3, S2,
  47. "from_string(result::field const &, std::string &) "
  48. "is inconsistent with "
  49. "from_string(std::string const &, std::string &).");
  50. PQXX_CHECK(tx.query_value<bool>("SELECT 1=1"), "1=1 doesn't yield 'true.'");
  51. PQXX_CHECK(not tx.query_value<bool>("SELECT 2+2=5"), "2+2=5 yields 'true.'");
  52. }
  53. PQXX_REGISTER_TEST(test_046);
  54. } // namespace