diff --git a/src/libCom/test/Makefile b/src/libCom/test/Makefile index 73b900154..cdbb72ec7 100644 --- a/src/libCom/test/Makefile +++ b/src/libCom/test/Makefile @@ -6,6 +6,11 @@ PROD_LIBS += Com USR_LIBS_hpux += cma +PROD += epicsListTest +OBJS_IOC += epicsListTest + +PROD += epicsExceptTest +OBJS_IOC += epicsExceptTest epicsTimeTestHost_SRCS += epicsTimeTestMain.cpp epicsTimeTest.cpp PROD += epicsTimeTestHost diff --git a/src/libCom/test/epicsExceptTest.cpp b/src/libCom/test/epicsExceptTest.cpp new file mode 100644 index 000000000..8552e0ea6 --- /dev/null +++ b/src/libCom/test/epicsExceptTest.cpp @@ -0,0 +1,87 @@ +// $Id$ +// Author: Andrew Johnson +// Date: December 2000 + +// Uncomment the following to check non-exception code on an +// exception target - NB, only runs one test... +//#define noExceptionsFromCXX + +#include "epicsExcept.h" +#include "epicsCppStd.h" +#include +#include +#include + +#ifdef vxWorks + #define MAIN epicsExceptTest +#else + #define MAIN main +#endif + +int MAIN(int /*argc*/, char* /*argv[]*/) { + bool caught = false; + + #define EXCEPTION_MSG "test string" + try { + epicsThrow(STD_ logic_error, EXCEPTION_MSG); + } + catch (STD_ logic_error& it) { + if (strcmp(it.what(), EXCEPTION_MSG)) + cout << "what() returned \"" << it.what() + << "\", expected \"" << EXCEPTION_MSG << "\"!\n"; + caught = true; + } + catch (...) { + cout << "exception not caught by own class!\n"; + caught = true; + } + if (!caught) + cout << "exception not caught!\n"; + + caught = false; + try { + epicsThrow(STD_ domain_error, EXCEPTION_MSG); + } + catch (STD_ logic_error& /* dummy */) { + caught = true; + } + catch (STD_ exception& /* dummy */) { + cout << "exception caught by grandparent but not parent class!\n"; + caught = true; + } + catch (...) { + cout << "exception not caught by parent or grand-parent class!\n"; + caught = true; + } + if (!caught) + cout << "exception not caught!\n"; + + caught = false; + int throwLine = 0; + try { + throwLine = __LINE__; epicsThrowHere(STD_ domain_error, EXCEPTION_MSG); + } + catch (STD_ exception& it) { + sourceLocation* here = dynamic_cast(&it); + if (here) { + if (strcmp(here->fileName(), __FILE__)) + cout << "fileName() returned \"" << here->fileName() + << "\", expected \"" << __FILE__ << "\"!\n"; + if (throwLine != here->lineNumber()) + cout << "lineNumber() returned " << here->lineNumber() + << ", expected " << __LINE__ << "!\n"; + } else + cout << "dynamic cast failed!\n"; + caught = true; + } + if (!caught) + cout << "exception not caught!\n"; + + // And finally, demonstrate an uncaught exception + cout << "Behaviour tests completed\n" + << "This is what an uncaught exception does:\n"; + + epicsThrow(STD_ runtime_error, "bye!"); + + return 0; /* stop a warning from g++ */ +} diff --git a/src/libCom/test/epicsListTest.cpp b/src/libCom/test/epicsListTest.cpp new file mode 100644 index 000000000..2ffd41071 --- /dev/null +++ b/src/libCom/test/epicsListTest.cpp @@ -0,0 +1,175 @@ +// $Id$ +// Author: Andrew Johnson +// Date: December 2000 + +// Test code for the epics::List class + +#include "epicsList.h" +#include + +#ifdef vxWorks + #define MAIN epicsListTest +#else + #define MAIN main +#endif + +class fred { +public: + fred(const char* const _name) : name(_name) {} + void show () const {cout << name << ' ';} +private: + const char* const name; +}; + +static int tests = 0; +static int nak = 0; + +void check(bool res, const char* str, int line) { + tests++; + if (!res) { + printf("Test %d failed, line %d: %s\n", tests, line, str); + nak++; + } +} + +#define test(expr) check(expr, #expr, __LINE__) + +int MAIN(int /*argc*/, char* /*argv[]*/) { + + fred* apf[10]; + const char* const names[] = { + "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" + }; + for (int i=0; i<10; i++) + apf[i] = new fred(names[i]); + + epicsList Fred; + test(Fred.empty()); + test(Fred.size() == 0); + test(Fred.begin() == Fred.end()); + + Fred.push_back(apf[0]); + test(!Fred.empty()); + test(Fred.size() == 1); + test(Fred.begin() != Fred.end()); + test(Fred.front() == apf[0]); + test(Fred.back() == apf[0]); + + epicsList::iterator Fi = Fred.begin(); + test(Fi != Fred.end()); + test(*Fi == apf[0]); + + epicsList::const_iterator Fci = Fred.begin(); + test(Fci != Fred.end()); + test(Fci == Fi); + test(Fi == Fci); + test(*Fci == apf[0]); + + ++Fi; + test(Fi == Fred.end()); + test(Fci != Fi); + test(Fi != Fci); + + ++Fci; + test(Fci == Fred.end()); + + Fci = --Fi; + test(Fi == Fred.begin()); + test(Fci == Fred.begin()); + + Fred.push_front(apf[1]); + test(Fred.size() == 2); + test(Fred.front() == apf[1]); + test(Fred.back() == apf[0]); + test(*Fi == apf[0]); + + Fi--; + test(*Fi == apf[1]); + + Fci--; + test(*Fci == apf[1]); + + Fi = Fred.insert(++Fi, apf[2]); + test(Fred.size() == 3); + test(Fred.front() == apf[1]); + test(Fred.back() == apf[0]); + test(Fi != Fci); + test(*Fi == apf[2]); + + Fred.pop_back(); + test(Fred.size() == 2); + + Fred.push_front(apf[0]); + test(Fred.size() == 3); + + Fi = Fred.begin(); + Fred.erase(Fi); + test(Fred.size() == 2); + + Fred.push_front(apf[0]); + test(Fred.size() == 3); + + Fi = Fred.begin(); + for (int i=0; Fi != Fred.end(); i++, ++Fi) + test(*Fi == apf[i]); + + Fci = Fred.begin(); + for (int i=0; Fci != Fred.end(); i++, ++Fci) + test(*Fci == apf[i]); + + for (int i=0; i<10; i++) { + test(Fred.size() == 3); + + epicsList Freda; + test(Freda.empty()); + + swap(Fred, Freda); + test(Fred.empty()); + test(Freda.size() == 3); + test(Freda.front() == apf[0]); + + for (Fi = Freda.begin(); Fi != Freda.end(); Fi++) + Fred.push_back(*Fi); + test(Fred.size() == 3); + + Fi = Freda.begin(); + for (Fci = Fred.begin(); Fci != Fred.end(); ++Fci, ++Fi) { + test(Fi != Freda.end()); + test(*Fci == *Fi); + } + // Freda's destructor returns her nodes to global store, + // from where they are allocated again during the next loop. + } + + Fred.erase(Fred.begin(), --Fred.end()); + test(Fred.size() == 1); + + Fred.clear(); + test(Fred.empty()); + + // Some more complicated stuff, just for the fun of it. + // Try doing this in tsDLList! + + epicsList*> llf; + for (int i=0; i<10; i++) { + llf.push_front(new epicsList); + llf.front()->push_front(apf[i]); + } + test(llf.size() == 10); + + epicsList*>::iterator llfi; + for (llfi = llf.begin(); llfi != llf.end(); ++llfi) + test(llfi->size() == 1); + + for (llfi = llf.begin(); llfi != llf.end(); ++llfi) { + llfi->clear(); + delete *llfi; + } + llf.clear(); + + for (int i=0; i<10; i++) + delete apf[i]; + + cout << tests << " tests completed, " << nak << " failed." << endl; + return 0; +};