test76.cxx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <pqxx/nontransaction>
  2. #include "test_helpers.hxx"
  3. // Simple test program for libpqxx. Test string conversion routines.
  4. namespace
  5. {
  6. void test_076()
  7. {
  8. pqxx::connection conn;
  9. pqxx::nontransaction tx{conn};
  10. auto RFalse{tx.exec1("SELECT 1=0")}, RTrue{tx.exec1("SELECT 1=1")};
  11. auto False{pqxx::from_string<bool>(RFalse[0])},
  12. True{pqxx::from_string<bool>(RTrue[0])};
  13. PQXX_CHECK(not False, "False bool converted to true.");
  14. PQXX_CHECK(True, "True bool converted to false.");
  15. RFalse = tx.exec1("SELECT " + pqxx::to_string(False));
  16. RTrue = tx.exec1("SELECT " + pqxx::to_string(True));
  17. False = pqxx::from_string<bool>(RFalse[0]);
  18. True = pqxx::from_string<bool>(RTrue[0]);
  19. PQXX_CHECK(not False, "False bool converted to true.");
  20. PQXX_CHECK(True, "True bool converted to false.");
  21. short const svals[]{-1, 1, 999, -32767, -32768, 32767, 0};
  22. for (int i{0}; svals[i] != 0; ++i)
  23. {
  24. auto s{pqxx::from_string<short>(pqxx::to_string(svals[i]))};
  25. PQXX_CHECK_EQUAL(s, svals[i], "short/string conversion not bijective.");
  26. s = pqxx::from_string<short>(
  27. tx.exec1("SELECT " + pqxx::to_string(svals[i]))[0].c_str());
  28. PQXX_CHECK_EQUAL(s, svals[i], "Roundtrip through backend changed short.");
  29. }
  30. unsigned short const uvals[]{1, 999, 32767, 32768, 65535, 0};
  31. for (int i{0}; uvals[i] != 0; ++i)
  32. {
  33. auto u{pqxx::from_string<unsigned short>(pqxx::to_string(uvals[i]))};
  34. PQXX_CHECK_EQUAL(
  35. u, uvals[i], "unsigned short/string conversion not bijective.");
  36. u = pqxx::from_string<unsigned short>(
  37. tx.exec1("SELECT " + pqxx::to_string(uvals[i]))[0].c_str());
  38. PQXX_CHECK_EQUAL(
  39. u, uvals[i], "Roundtrip through backend changed unsigned short.");
  40. }
  41. }
  42. } // namespace
  43. PQXX_REGISTER_TEST(test_076);