test-error.cc

Go to the documentation of this file.
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 void test7a(void)
00167 {
00168         std::string str = "a";
00169 
00170         while (true) {
00171                 TRY(str += str,"Could not append to string");
00172         }
00173 }
00174 
00175 void test7(void)
00176 {
00177         bool thrown = false;
00178         bool identified = false;
00179 
00180         try {
00181                 test7a();
00182         }
00183         catch(error e) {
00184                 // std::cerr << e;
00185                 thrown = true;
00186                 identified = true;
00187         }
00188         catch(...) {
00189                 // std::cerr << err_unknown;
00190                 assert(errno == ENOMEM);
00191                 thrown = true;
00192         }
00193 
00194         assert(thrown);
00195         assert(identified);
00196 }
00197 
00198 void test8(void)
00199 {
00200         error e(err_nomem);
00201 
00202         assert(e.num() == ENOMEM);
00203         assert(e.size() == 1);
00204         assert(e[0].what() == "Out of memory");
00205 }
00206 
00207 int main(int argc, char *argv[])
00208 {
00209         try {
00210                 test1();
00211                 test2();
00212                 test3();
00213                 test4();
00214                 test5();
00215                 test6();
00216 #ifdef HAVE_EXCEPTION_ON_ALLOC_FAILURE
00217                 test7();
00218 #endif
00219                 test8();
00220         }
00221         catch(error e) {
00222                 std::cerr << e;
00223                 assert(0);
00224         }
00225         catch(...) {
00226                 std::cerr << err_unknown;
00227                 assert(0);
00228         }
00229         return(0);
00230 }

Generated on Thu Jun 5 11:12:57 2008 for rvm by  doxygen 1.5.1