00001 #include "config.h"
00002
00003 #ifdef HAVE_UNISTD_H
00004 #include <unistd.h>
00005 #endif
00006
00007 #include <iostream>
00008 #include <fstream>
00009 #include <string>
00010 #include <cassert>
00011 #include <cerrno>
00012
00013 #include "asserts.h"
00014 #include "error.h"
00015
00016 void test1(void)
00017 {
00018 error_instance ei;
00019
00020 assert(ei.what() == "");
00021 #ifdef __GNUC__
00022 assert(ei.where() == "");
00023 #endif
00024 assert(ei.file() == "");
00025 assert(ei.line() == 0);
00026 }
00027
00028 void test2(void)
00029 {
00030 error e(0);
00031
00032 e = ERROR(0,"Simulated error instance");
00033 assert(e.num() == 0);
00034 assert(!e.internal());
00035 assert(e.size() == 1);
00036 assert(e[0].what() == "Simulated error instance");
00037
00038 e = INTERNAL_ERROR(0,"Simulated internal error instance");
00039 assert(e.num() == 0);
00040 assert(e.internal());
00041 assert(e.size() == 1);
00042 assert(e[0].what() == "Simulated internal error instance");
00043 }
00044
00045 void test3(void)
00046 {
00047 error e(0);
00048
00049 e = ERROR(1,"Simulated error instance");
00050 assert(e.num() == 1);
00051 assert(!e.internal());
00052 assert(e.size() == 1);
00053 assert(e[0].what() == "Simulated error instance");
00054
00055 e = INTERNAL_ERROR(0,"Simulated internal error instance");
00056 assert(e.num() == 0);
00057 assert(e.internal());
00058 assert(e.size() == 1);
00059 assert(e[0].what() == "Simulated internal error instance");
00060
00061 e = INTERNAL_ERROR(1,"Simulated internal error instance");
00062 assert(e.num() == 1);
00063 assert(e.internal());
00064 assert(e.size() == 1);
00065 assert(e[0].what() == "Simulated internal error instance");
00066 }
00067
00068 void test4b(void)
00069 {
00070 throw(ERROR(1,"Simulated error for test4b"));
00071 }
00072
00073 void test4a(void)
00074 {
00075 try {
00076 test4b();
00077 }
00078 catch(error e) {
00079 e.push_back(ERROR_INSTANCE("Failure in test4a"));
00080 throw(e);
00081 }
00082 }
00083
00084 void test4(void)
00085 {
00086 bool thrown = false;
00087
00088 try {
00089 test4a();
00090 }
00091 catch(error e) {
00092 e.push_back(ERROR_INSTANCE("Failure in test4"));
00093 assert(e.num() == 1);
00094 assert(!e.internal());
00095 assert(e.size() == 3);
00096 assert(e[0].what() == "Simulated error for test4b");
00097 assert(e[1].what() == "Failure in test4a");
00098 assert(e[2].what() == "Failure in test4");
00099 thrown = true;
00100 }
00101
00102 assert(thrown);
00103 }
00104
00105 void test5b(void)
00106 {
00107 throw(INTERNAL_ERROR(1,"Simulated error for test5b"));
00108 }
00109
00110 void test5a(void)
00111 {
00112 try {
00113 test5b();
00114 }
00115 catch(error e) {
00116 e.push_back(ERROR_INSTANCE("Failure in test5a"));
00117 throw(e);
00118 }
00119 }
00120
00121 void test5(void)
00122 {
00123 bool thrown = false;
00124
00125 try {
00126 test5a();
00127 }
00128 catch(error e) {
00129 e.push_back(ERROR_INSTANCE("Failure in test5"));
00130 assert(e.num() == 1);
00131 assert(e.internal());
00132 assert(e.size() == 3);
00133 assert(e[0].what() == "Simulated error for test5b");
00134 assert(e[1].what() == "Failure in test5a");
00135 assert(e[2].what() == "Failure in test5");
00136 thrown = true;
00137 }
00138
00139 assert(thrown);
00140 }
00141
00142 void test6(void)
00143 {
00144 error e1(0), e2(0);
00145
00146 e1.push_back(ERROR_INSTANCE("Testing:"));
00147 e1.push_back(ERROR_INSTANCE("One..."));
00148 e1.push_back(ERROR_INSTANCE("Two..."));
00149 e1.push_back(ERROR_INSTANCE("Three..."));
00150
00151 assert(e1.size() == 4);
00152 assert(e1[0].what() == "Testing:");
00153 assert(e1[1].what() == "One...");
00154 assert(e1[2].what() == "Two...");
00155 assert(e1[3].what() == "Three...");
00156
00157 e2 = e1;
00158
00159 assert(e2.size() == 4);
00160 assert(e2[0].what() == "Testing:");
00161 assert(e2[1].what() == "One...");
00162 assert(e2[2].what() == "Two...");
00163 assert(e2[3].what() == "Three...");
00164 }
00165
00166 #ifndef ASSUME_STL_MEMORY_EXCEPTION
00167 void test7a(void)
00168 {
00169 std::string str = "a";
00170
00171 while (true) {
00172 TRY(str += str,"Could not append to string");
00173 }
00174 }
00175
00176 void test7(void)
00177 {
00178 bool thrown = false;
00179 bool identified = false;
00180
00181 try {
00182 test7a();
00183 }
00184 catch(error e) {
00185
00186 thrown = true;
00187 identified = true;
00188 }
00189 catch(...) {
00190
00191 assert(errno == ENOMEM);
00192 thrown = true;
00193 }
00194
00195 assert(thrown);
00196 assert(identified);
00197 }
00198 #endif
00199
00200 void test8(void)
00201 {
00202 error e(err_nomem);
00203
00204 assert(e.num() == ENOMEM);
00205 assert(e.size() == 1);
00206 assert(e[0].what() == "Out of memory");
00207 }
00208
00209 int main(int argc, char const * argv[])
00210 {
00211 try {
00212 test1();
00213 test2();
00214 test3();
00215 test4();
00216 test5();
00217 test6();
00218 #ifndef ASSUME_STL_MEMORY_EXCEPTION
00219 #ifdef HAVE_EXCEPTION_ON_ALLOC_FAILURE
00220 test7();
00221 #endif
00222 #endif
00223 test8();
00224 }
00225 catch(error e) {
00226 std::cerr << e;
00227 assert(0);
00228 }
00229 catch(...) {
00230 std::cerr << err_unknown;
00231 assert(0);
00232 }
00233 return(0);
00234 }