1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/***********************************************************************
test/array_index.cpp - Tests operator[] and at() on indexable objects
to ensure they throw exceptions when given bad indices.
Copyright (c) 2008 by Educational Technology Resources, Inc.
Others may also hold copyrights on code in this file. See the
CREDITS.txt file in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#include <mysql++.h>
#include <iostream>
template <class ContainerT>
static bool
test_exception(const ContainerT& container, int index)
{
try {
container[index];
container.at(index);
std::cerr << "Bad index " << index << " allowed in " <<
typeid(container).name() << '!' << std::endl;
return false;
}
catch (const mysqlpp::BadIndex&) {
return true;
}
catch (const mysqlpp::Exception& e) {
std::cerr << "Unexpected mysqlpp::Exception caught for " <<
typeid(container).name() << ", index " << index <<
": " << e.what() << std::endl;
return false;
}
catch (const std::exception& e) {
std::cerr << "Unexpected std::exception caught for " <<
typeid(container).name() << ", index " << index <<
": " << e.what() << std::endl;
return false;
}
catch (...) {
std::cerr << "Unexpected exception type caught for " <<
typeid(container).name() << ", index " << index <<
'!' << std::endl;
return false;
}
}
template <class ContainerT>
static bool
test_no_exception(const ContainerT& container)
{
mysqlpp::NoExceptions ne(container);
try {
container[-1];
std::cerr << "Mandatory exception suppressed for " <<
typeid(container).name() << "::operator[]()!" <<
std::endl;
return false;
}
catch (...) {
}
try {
container.at(-1);
std::cerr << "Mandatory exception suppressed for " <<
typeid(container).name() << "::at()!" << std::endl;
return false;
}
catch (...) {
return true;
}
}
// Separate test needed because string indices work different from
// integer indices. Exceptions for bad field names *can* be suppressed;
// SSQLS needs this to support population of partial structures, with
// the un-queried fields getting default values.
template <class ContainerT>
static bool
test_string_index(const ContainerT& container)
{
// This test should cause an exception...
try {
container["fred"];
std::cerr << "Bad string index allowed in " <<
typeid(container).name() << '!' << std::endl;
return false;
}
catch (const mysqlpp::BadFieldName&) {
// Good; fall through to next test
}
catch (const mysqlpp::Exception& e) {
std::cerr << "Unexpected mysqlpp::Exception caught for "
"bad string index in " << typeid(container).name() <<
": " << e.what() << std::endl;
return false;
}
catch (const std::exception& e) {
std::cerr << "Unexpected std::exception caught for "
"bad string index in " << typeid(container).name() <<
": " << e.what() << std::endl;
return false;
}
catch (...) {
std::cerr << "Unexpected exception type caught for "
"bad string index in " << typeid(container).name() <<
'!' << std::endl;
return false;
}
// ...but not this one
mysqlpp::NoExceptions ne(container);
try {
container["fred"];
return true;
}
catch (const mysqlpp::BadFieldName&) {
std::cerr << "Exception not suppressed for nonexistent field "
"in " << typeid(container).name() << '!' << std::endl;
return false;
}
}
template <class ContainerT>
static bool
test_numeric_index(const ContainerT& container)
{
return test_exception(container, -1) &&
test_exception(container, 0) &&
test_exception(container, 1);
}
int
main()
{
try {
return test_no_exception(mysqlpp::Row()) &&
test_numeric_index(mysqlpp::Row()) &&
test_string_index(mysqlpp::Row()) ? 0 : 1;
}
catch (...) {
std::cerr << "Unhandled exception caught by array_index!" <<
std::endl;
return 2;
}
}
|