Changeset 1380

Show
Ignore:
Timestamp:
08/18/08 23:57:21 (5 months ago)
Author:
jtv
Message:

Replaced all throws with test helpers.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r1379 r1380  
     12008-08-19  Jeroen T. Vermeulen <jtv@xs4all.nl> 
     2 test/test000.cxx, test/test001.cxx, test/test002.cxx, test/test004.cxx, 
     3 test/test006.cxx, test/test007.cxx, test/test020.cxx, test/test023.cxx, 
     4 test/test075.cxx, test/test079.cxx, test/test084.cxx, test/test087.cxx, 
     5 test/test089.cxx, test/test092.cxx, test/test094.cxx: 
     6  - Replaced throws with test helpers. 
    172008-08-15  Jeroen T. Vermeulen <jtv@xs4all.nl> 
    28 test/Makefile.am.template, test/runner.cxx, test/test027.cxx, 
  • trunk/test/test000.cxx

    r1375 r1380  
    8989        "cause problems in libpqxx."); 
    9090 
    91   if (cursor_base::prior() >= 0 || cursor_base::backward_all() >= 0) 
    92     throw logic_error("cursor_base::difference_type appears to be unsigned"); 
     91  PQXX_CHECK( 
     92        cursor_base::prior() < 0 && cursor_base::backward_all() < 0, 
     93        "cursor_base::difference_type appears to be unsigned."); 
    9394 
    9495  cout << "Testing items template..." << endl; 
     
    106107  testitems(I5,5); 
    107108  const string l = separated_list(",",I5.begin(),I5.end(),intderef()); 
    108   if (l != "1,2,3,4,5") throw logic_error("Separated list was '" + l + "'"); 
     109  PQXX_CHECK_EQUAL(l, "1,2,3,4,5", "separated_list is broken."); 
    109110  vector<int> V2(I2); 
    110111  testitems(items<int>(V2),2); 
     
    170171  string zero; 
    171172  from_string(zerobuf, zero, sizeof(zerobuf)-1); 
    172   if (zero != zerobuf) 
    173     throw logic_error("Converting \"0\" with explicit length failed!"); 
     173  PQXX_CHECK_EQUAL( 
     174        zero, 
     175        zerobuf, 
     176        "Converting \"0\" with explicit length failed."); 
    174177 
    175178  const char nulbuf[] = "\0string\0with\0nuls\0"; 
     
    177180  string nully_parsed; 
    178181  from_string(nulbuf, nully_parsed, sizeof(nulbuf)-1); 
    179   if (nully_parsed != nully) 
    180     throw logic_error("String with nuls now " + 
    181         to_string(nully_parsed.size()) + " bytes!"); 
     182  PQXX_CHECK_EQUAL(nully_parsed.size(), nully.size(), "Nul truncates string."); 
     183  PQXX_CHECK_EQUAL(nully_parsed, nully, "String conversion breaks on nuls."); 
    182184  from_string(nully.c_str(), nully_parsed, nully.size()); 
    183   if (nully_parsed != nully) 
    184     throw logic_error("Nul conversion worked, but not on strings!"); 
     185  PQXX_CHECK_EQUAL(nully_parsed, nully, "Nul conversion breaks on strings."); 
    185186 
    186187  stringstream ss; 
     
    193194#ifdef PQXX_HAVE_PQENCRYPTPASSWORD 
    194195  const string pw = encrypt_password("foo", "bar"); 
    195   if (pw.empty()) 
    196     throw logic_error("Encrypting a password returned no data"); 
    197   if (pw == encrypt_password("splat", "blub")) 
    198     throw logic_error("Password encryption does not work"); 
    199   if (pw.find("bar") != string::npos) 
    200     throw logic_error("Encrypted password contains original"); 
     196  PQXX_CHECK(!pw.empty(), "Encrypting a password returned no data."); 
     197  PQXX_CHECK_NOT_EQUAL( 
     198        pw, 
     199        encrypt_password("splat", "blub"),  
     200        "Password encryption is broken."); 
     201  PQXX_CHECK( 
     202        pw.find("bar") == string::npos, 
     203        "Encrypted password contains original."); 
    201204#endif 
    202205 
    203206  cout << "Testing error handling for failed connections..." << endl; 
    204   try 
    205207  { 
    206208    nullconnection nc; 
    207     work w(nc); 
    208     throw logic_error("nullconnection failed to fail!"); 
    209   } 
    210   catch (broken_connection &c) 
    211   { 
    212     cout << "(Expected) " << c.what() << endl; 
    213   } 
    214   try 
     209    PQXX_CHECK_THROWS( 
     210        work w(nc), 
     211        broken_connection, 
     212        "nullconnection fails to fail."); 
     213  } 
    215214  { 
    216215    nullconnection nc(""); 
    217     work w(nc); 
    218     throw logic_error("nullconnection(const char[]) failed to fail!"); 
    219   } 
    220   catch (broken_connection &c) 
    221   { 
    222     cout << "(Expected) " << c.what() << endl; 
    223   } 
    224   try 
     216    PQXX_CHECK_THROWS( 
     217        work w(nc), 
     218        broken_connection, 
     219        "nullconnection(const char[]) is broken."); 
     220  } 
    225221  { 
    226222    string n; 
    227223    nullconnection nc(n); 
    228     work w(nc); 
    229     throw logic_error("nullconnection(const std::string &) failed to fail!"); 
    230   } 
    231   catch (broken_connection &c) 
    232   { 
    233     cout << "(Expected) " << c.what() << endl; 
     224    PQXX_CHECK_THROWS( 
     225        work w(nc), 
     226        broken_connection, 
     227        "nullconnection(const string &) is broken."); 
    234228  } 
    235229 
     
    243237  catch (const pqxx_exception &e) 
    244238  { 
    245     if (!dynamic_cast<const broken_connection *>(&e.base())) 
    246       throw logic_error("Downcast pqxx_exception is not a broken_connection"); 
     239    PQXX_CHECK( 
     240        dynamic_cast<const broken_connection *>(&e.base()), 
     241        "Downcast pqxx_exception is not a broken_connection"); 
    247242    cout << "(Expected) " << e.base().what() << endl; 
    248     if (dynamic_cast<const broken_connection &>(e.base()).what() != 
    249         e.base().what()) 
    250       throw logic_error("Inconsistent what() message in exception!"); 
     243    PQXX_CHECK_EQUAL( 
     244        dynamic_cast<const broken_connection &>(e.base()).what(), 
     245        e.base().what(), 
     246        "Inconsistent what() message in exception."); 
    251247  } 
    252248} 
  • trunk/test/test001.cxx

    r1377 r1380  
    3131 
    3232  // We're expecting to find some tables... 
    33   if (R.empty()) throw logic_error("No tables found!"); 
     33  PQXX_CHECK(!R.empty(), "No tables found.  Cannot test."); 
    3434 
    3535  // Process each successive result tuple 
  • trunk/test/test002.cxx

    r1375 r1380  
    8080#ifdef PQXX_HAVE_PQFTABLE 
    8181    const oid ftable = R[i][0].table(); 
    82     if (ftable != rtable) 
    83       throw logic_error("Field says it comes from '" + to_string(ftable) + "'; " 
    84                           "expected '" + to_string(rtable) + "'"); 
     82    PQXX_CHECK_EQUAL(ftable, rtable, "result::field::table() is broken."); 
     83 
    8584    const oid ttable = R[i].column_table(0); 
    86     if (ttable != R[i].column_table(result::tuple::size_type(0))) 
    87       throw logic_error("Inconsistent result::tuple::column_table()"); 
    88     if (ttable != rtable) 
    89       throw logic_error("Tuple says field comes from " 
    90                           "'" + to_string(ttable) + "'; " 
    91                           "expected '" + to_string(rtable) + "'"); 
     85 
     86    PQXX_CHECK_EQUAL( 
     87        ttable, 
     88        R[i].column_table(result::tuple::size_type(0)), 
     89        "Inconsistent result::tuple::column_table()."); 
     90 
     91    PQXX_CHECK_EQUAL(ttable, rtable, "Inconsistent result::column_table()."); 
     92 
    9293    const oid cttable = R[i].column_table(rcol); 
    93     if (cttable != rtable) 
    94       throw logic_error("Field comes from '" + to_string(rtable) + "', " 
    95                           "but by name, tuple says it's from '" + 
    96                           to_string(cttable) + "'"); 
     94 
     95    PQXX_CHECK_EQUAL( 
     96        cttable, 
     97        rtable, 
     98        "result::tuple::column_table() is broken."); 
    9799#endif 
    98100  } 
  • trunk/test/test004.cxx

    r1375 r1380  
    3535  { 
    3636    m_Done = true; 
    37     if (be_pid != Backend_PID) 
    38       throw logic_error("Expected notification from backend process " + 
    39                         to_string(Backend_PID) + 
    40                         ", but got one from " + 
    41                         to_string(be_pid)); 
     37    PQXX_CHECK_EQUAL( 
     38        be_pid, 
     39        Backend_PID, 
     40        "Notification came from wrong backend process."); 
    4241 
    4342    cout << "Received notification: " << name() << " pid=" << be_pid << endl; 
  • trunk/test/test006.cxx

    r1375 r1380  
    5858void CheckState(tablereader &R) 
    5959{ 
     60  PQXX_CHECK_EQUAL( 
     61        !R, 
     62        !bool(R), 
     63        "tablereader " + R.name() + " is in inconsistent state."); 
    6064  if (!R != !bool(R)) 
    6165    throw logic_error("tablereader " + R.name() + " in inconsistent state!"); 
  • trunk/test/test007.cxx

    r1375 r1380  
    9090      // See if type identifiers are consistent 
    9191      const oid tctype = r->column_type(0); 
    92       if (tctype != r->column_type(result::tuple::size_type(0))) 
    93         throw logic_error("Inconsistent result::tuple::column_type()"); 
    94       if (tctype != rctype) 
    95         throw logic_error("Column has type " + rct + ", " 
    96                           "but tuple says it's " + to_string(tctype)); 
     92 
     93      PQXX_CHECK_EQUAL( 
     94        tctype, 
     95        r->column_type(result::tuple::size_type(0)), 
     96        "Inconsistent result::tuple::column_type()"); 
     97 
     98      PQXX_CHECK_EQUAL( 
     99        tctype, 
     100        rctype, 
     101        "tuple::column_type() is inconsistent with result::column_type()."); 
     102 
    97103      const oid ctctype = r->column_type(rcol); 
    98       if (ctctype != rctype) 
    99         throw logic_error("Column has type " + rct + ", " 
    100                           "but by name, tuple says it's " + to_string(ctctype)); 
     104 
     105      PQXX_CHECK_EQUAL( 
     106        ctctype, 
     107        rctype, 
     108        "Column type lookup by column name is broken."); 
     109 
    101110      const oid rawctctype = r->column_type(rcol.c_str()); 
    102       if (rawctctype != rctype) 
    103         throw logic_error("Column has type " + rct + ", " 
    104                           "but by C-style name, tuple says it's " + 
    105                           to_string(rawctctype)); 
     111 
     112      PQXX_CHECK_EQUAL( 
     113        rawctctype, 
     114        rctype, 
     115        "Column type lookup by C-style name is broken."); 
     116 
    106117      const oid fctype = r[0].type(); 
    107       if (fctype != rctype) 
    108         throw logic_error("Column has type " + rct + ", " 
    109                           "but field says it's " + to_string(fctype)); 
     118      PQXX_CHECK_EQUAL( 
     119        fctype, 
     120        rctype, 
     121        "Field type lookup is broken."); 
    110122    } 
    111123 
  • trunk/test/test020.cxx

    r1377 r1380  
    2727  result R( T1.exec(("SELECT * FROM " + Table + " " 
    2828                       "WHERE year=" + to_string(BoringYear)).c_str()) ); 
    29   if (R.size() != 0) 
    30     throw runtime_error("There is already a record for " + 
    31                           to_string(BoringYear) + ". " 
    32                          "Can't run test."); 
     29  PQXX_CHECK_EQUAL( 
     30        R.size(), 
     31        0u, 
     32        "Already have a row for " + to_string(BoringYear) + ", cannot test."); 
    3333 
    3434  // (Not needed, but verify that clear() works on empty containers) 
    3535  R.clear(); 
    36   if (!R.empty()) 
    37     throw logic_error("Result non-empty after clear()!"); 
     36  PQXX_CHECK(R.empty(), "result::clear() is broken."); 
    3837 
    3938  // OK.  Having laid that worry to rest, add a record for 1977. 
  • trunk/test/test023.cxx

    r1377 r1380  
    3232  { 
    3333    m_Done = true; 
    34     if (be_pid != Conn().backendpid()) 
    35       throw logic_error("Expected notification from backend process " + 
    36                         to_string(Conn().backendpid()) + 
    37                         ", but got one from " + 
    38                         to_string(be_pid)); 
     34    PQXX_CHECK_EQUAL( 
     35        be_pid, 
     36        Conn().backendpid(), 
     37        "Notification came from wrong backend process."); 
    3938 
    4039    cout << "Received notification: " << name() << " pid=" << be_pid << endl; 
  • trunk/test/test075.cxx

    r1378 r1380  
    2121{ 
    2222  const result R( W.exec("SELECT year FROM pqxxevents") ); 
    23  
    24   if (R.empty()) throw runtime_error("No events found, can't test!"); 
     23  PQXX_CHECK(!R.empty(), "No events found, cannot test."); 
    2524 
    2625  PQXX_CHECK_EQUAL(R[0], R.at(0), "Inconsistent result indexing."); 
  • trunk/test/test079.cxx

    r1378 r1380  
    3232  { 
    3333    m_Done = true; 
    34     if (be_pid != Conn().backendpid()) 
    35       throw logic_error("Expected notification from backend process " + 
    36                         to_string(Conn().backendpid()) + 
    37                         ", but got one from " + 
    38                         to_string(be_pid)); 
     34    PQXX_CHECK_EQUAL( 
     35        be_pid, 
     36        Conn().backendpid(), 
     37        "Notification came from wrong backend."); 
    3938 
    4039    cout << "Received notification: " << name() << " pid=" << be_pid << endl; 
  • trunk/test/test084.cxx

    r1378 r1380  
    2929        cerr << '\t' << f; 
    3030    cerr << endl; 
    31   } 
    32 } 
    33  
    34  
    35 void compare_results(const result &lhs, const result &rhs, string desc) 
    36 { 
    37   if (lhs != rhs) 
    38   { 
    39     cerr << "Outputs at " << desc << ':' << endl; 
    40     cerr << "lhs:" << endl; 
    41     dump(lhs); 
    42     cerr << "rhs:" << endl; 
    43     dump(rhs); 
    44     throw logic_error("Different results at " + desc); 
    4531  } 
    4632} 
     
    10591        "Got unexpected number of rows."); 
    10692 
    107   compare_results(R, R2, "[1]"); 
     93  PQXX_CHECK_EQUAL(R, R2, "Unexpected result at [1]"); 
    10894 
    10995  C.get(R); 
    11096  R2 = *i2; 
    111   compare_results(R, R2, "[2]"); 
     97  PQXX_CHECK_EQUAL(R, R2, "Unexpected result at [2]"); 
    11298  i2 += 1; 
    11399 
     
    116102  R2 = *++i2; 
    117103 
    118   compare_results(R, R2, "[3]"); 
     104  PQXX_CHECK_EQUAL(R, R2, "Unexpected result at [3]"); 
    119105 
    120106  ++i2; 
    121107  R2 = *i2++; 
    122108  for (int i=1; C.get(R) && i2 != iend; R2 = *i2++, ++i) 
    123     compare_results(R, R2, "iteration " + to_string(i)); 
     109    PQXX_CHECK_EQUAL( 
     110        R, 
     111        R2, 
     112        "Unexpected result in iteration at " + to_string(i)); 
    124113 
    125114  PQXX_CHECK(i2 == iend, "Adopted cursor terminated early."); 
  • trunk/test/test087.cxx

    r1378 r1380  
    5050  { 
    5151    m_Done = true; 
    52     if (be_pid != Conn().backendpid()) 
    53       throw logic_error("Expected notification from backend process " + 
    54                         to_string(Conn().backendpid()) + 
    55                         ", but got one from " + 
    56                         to_string(be_pid)); 
     52    PQXX_CHECK_EQUAL( 
     53        be_pid, 
     54        Conn().backendpid(), 
     55        "Notification came from wrong backend process."); 
    5756 
    5857    cout << "Received notification: " << name() << " pid=" << be_pid << endl; 
  • trunk/test/test089.cxx

    r1378 r1380  
    9898      catch (const exception &) 
    9999      { 
    100         throw logic_error
     100        PQXX_CHECK_NOTREACHED
    101101                "First asyncconnection supported nested " 
    102102                "transactions, but second one doesn't!"); 
  • trunk/test/test092.cxx

    r1378 r1380  
    6767 
    6868  const result t( T.exec("SELECT * FROM tuple") ); 
    69   if (t.size() != 1) 
    70     throw logic_error("Expected 1 tuple, got " + to_string(t.size())); 
    71   if (t[0][0].as<string>() != "6") 
    72     throw logic_error("Expected value 6, got " + t[0][0].as<string>()); 
    73   if (t[0][1].c_str() != f) 
    74     throw logic_error("Expected string '" + f + "', " 
    75         "got '" + t[0][1].c_str() + "'"); 
     69  PQXX_CHECK_EQUAL(t.size(), 1u, "Wrong result size."); 
     70  PQXX_CHECK_EQUAL(t[0][0].as<string>(), "6", "Unexpected result value."); 
     71  PQXX_CHECK_EQUAL(t[0][1].c_str(), f, "Unexpected string result."); 
    7672} 
    7773} // namespace 
  • trunk/test/test094.cxx

    r1378 r1380  
    4343      if (conn().is_open()) 
    4444      { 
    45         if (simulate_failure) 
    46           throw logic_error("Connection did not simulate failure"); 
     45        PQXX_CHECK(!simulate_failure, "Connection did not simulate failure."); 
    4746        cerr << "Unexpected exception (connection still open)" << endl; 
    4847        throw; 
     
    121120 
    122121 
    123 void simulate(connection_base &C, int failures, int attempts) 
    124 { 
    125   bool failed = true; 
    126   try 
    127   { 
    128     C.perform(FlakyTransactor(failures), attempts); 
    129     failed = false; 
    130   } 
    131   catch (const in_doubt_error &e) 
    132   { 
    133     if (!failures) throw; 
    134     cout << "(Expected) " << e.what() << endl; 
    135   } 
    136   if (failures && !failed) 
    137     throw logic_error("Simulated failure did not lead to in-doubt error"); 
    138 } 
    139  
    140  
    141122void test_094(connection_base &C, transaction_base &orgT) 
    142123{ 
     
    145126  // Run without simulating failure 
    146127  cout << "Playing transactor without simulating failure..." << endl; 
    147   simulate(C, 0, 1); 
     128  C.perform(FlakyTransactor(0), 1); 
    148129 
    149   // Simulate one failure, but succeed on retry 
     130  // Simulate one failure.  The transactor will succeed on a second attempt, but 
     131  // since this is an in-doubt error, the framework does not retry. 
    150132  cout << "Playing transactor with simulated failure..." << endl; 
    151   simulate(C, 1, 2); 
     133  PQXX_CHECK_THROWS( 
     134        C.perform(FlakyTransactor(1), 2), 
     135        in_doubt_error, 
     136        "Simulated failure did not lead to in-doubt error."); 
    152137} 
    153138} // namespace