test74.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <cmath>
  2. #include <pqxx/transaction>
  3. #include "test_helpers.hxx"
  4. using namespace pqxx;
  5. // Test program for libpqxx. Test fieldstream.
  6. namespace
  7. {
  8. void test_074()
  9. {
  10. connection conn;
  11. work tx{conn};
  12. result R{tx.exec("SELECT * FROM pg_tables")};
  13. std::string const sval{R.at(0).at(1).c_str()};
  14. std::string sval2;
  15. fieldstream fs1(R.front()[1]);
  16. fs1 >> sval2;
  17. PQXX_CHECK_EQUAL(sval2, sval, "fieldstream returned wrong value.");
  18. R = tx.exec("SELECT count(*) FROM pg_tables");
  19. int ival;
  20. fieldstream fs2(R.at(0).at(0));
  21. fs2 >> ival;
  22. PQXX_CHECK_EQUAL(
  23. ival, R.front().front().as<int>(), "fieldstream::front() is broken.");
  24. double dval;
  25. (fieldstream(R.at(0).at(0))) >> dval;
  26. PQXX_CHECK_BOUNDS(
  27. dval, R[0][0].as<double>() - 0.1, R[0][0].as<double>() + 0.1,
  28. "Got wrong double from fieldstream.");
  29. auto const roughpi{static_cast<float>(3.1415926435)};
  30. R = tx.exec("SELECT " + to_string(roughpi));
  31. float pival;
  32. (fieldstream(R.at(0).at(0))) >> pival;
  33. PQXX_CHECK_BOUNDS(
  34. pival, roughpi - 0.001, roughpi + 0.001,
  35. "Pi approximation came back wrong from fieldstream.");
  36. PQXX_CHECK_EQUAL(
  37. to_string(R[0][0]), R[0][0].c_str(),
  38. "to_string(result::field) is inconsistent with c_str().");
  39. float float_pi;
  40. from_string(to_string(roughpi), float_pi);
  41. PQXX_CHECK_BOUNDS(
  42. float_pi, roughpi - 0.00001, roughpi + 0.00001,
  43. "Float changed in conversion.");
  44. double double_pi;
  45. pqxx::from_string(pqxx::to_string(static_cast<double>(roughpi)), double_pi);
  46. PQXX_CHECK_BOUNDS(
  47. double_pi, roughpi - 0.00001, roughpi + 0.00001,
  48. "Double changed in conversion.");
  49. long double const ld{roughpi};
  50. long double long_double_pi;
  51. from_string(to_string(ld), long_double_pi);
  52. PQXX_CHECK_BOUNDS(
  53. long_double_pi, roughpi - 0.00001, roughpi + 0.00001,
  54. "long double changed in conversion.");
  55. }
  56. } // namespace
  57. PQXX_REGISTER_TEST(test_074);