test11.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <pqxx/transaction>
  5. #include "test_helpers.hxx"
  6. using namespace pqxx;
  7. // Test program for libpqxx. Query a table and report its metadata.
  8. namespace
  9. {
  10. void test_011()
  11. {
  12. connection conn;
  13. work tx{conn};
  14. std::string const Table{"pg_tables"};
  15. result R(tx.exec("SELECT * FROM " + Table));
  16. // Print column names
  17. for (pqxx::row::size_type c{0}; c < R.columns(); ++c)
  18. {
  19. std::string N{R.column_name(c)};
  20. PQXX_CHECK_EQUAL(R.column_number(N), c, "Inconsistent column numbers.");
  21. }
  22. // If there are rows in R, compare their metadata to R's.
  23. if (not std::empty(R))
  24. {
  25. PQXX_CHECK_EQUAL(R[0].rownumber(), 0, "Row 0 has wrong number.");
  26. if (std::size(R) >= 2)
  27. PQXX_CHECK_EQUAL(R[1].rownumber(), 1, "Row 1 has wrong number.");
  28. // Test result::iterator::swap()
  29. pqxx::result::const_iterator const T1(R[0]), T2(R[1]);
  30. PQXX_CHECK_NOT_EQUAL(T1, T2, "Values are identical--can't test swap().");
  31. pqxx::result::const_iterator T1s(T1), T2s(T2);
  32. PQXX_CHECK_EQUAL(T1s, T1, "Result iterator copy-construction is wrong.");
  33. PQXX_CHECK_EQUAL(
  34. T2s, T2, "Result iterator copy-construction is inconsistently wrong.");
  35. T1s.swap(T2s);
  36. PQXX_CHECK_NOT_EQUAL(T1s, T1, "Result iterator swap doesn't work.");
  37. PQXX_CHECK_NOT_EQUAL(
  38. T2s, T2, "Result iterator swap inconsistently wrong.");
  39. PQXX_CHECK_EQUAL(T2s, T1, "Result iterator swap is asymmetric.");
  40. PQXX_CHECK_EQUAL(
  41. T1s, T2, "Result iterator swap is inconsistently asymmetric.");
  42. for (pqxx::row::size_type c{0}; c < std::size(R[0]); ++c)
  43. {
  44. std::string N{R.column_name(c)};
  45. PQXX_CHECK_EQUAL(
  46. std::string{R[0].at(c).c_str()}, R[0].at(N).c_str(),
  47. "Field by name != field by number.");
  48. PQXX_CHECK_EQUAL(
  49. std::string{R[0][c].c_str()}, R[0][N].c_str(),
  50. "at() is inconsistent with operator[].");
  51. PQXX_CHECK_EQUAL(R[0][c].name(), N, "Field names are inconsistent.");
  52. PQXX_CHECK_EQUAL(
  53. std::size(R[0][c]), strlen(R[0][c].c_str()),
  54. "Field size is not what we expected.");
  55. }
  56. }
  57. }
  58. PQXX_REGISTER_TEST(test_011);
  59. } // namespace