00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef TEST_ITERATOR_H_
00024 #define TEST_ITERATOR_H_
00025
00026 #include <cppunit/extensions/HelperMacros.h>
00027 #include <cppunit/TestAssert.h>
00028 #include "baseTest.h"
00029
00030 namespace test {
00031
00032 using namespace amino;
00033
00034 template<typename ListType, typename ParaType, char const* CLASS_NAME> class IteratorTest: public CppUnit::TestFixture,
00035 public BaseTest<ParaType> {
00036 CPPUNIT_TEST_SUITE(IteratorTest);
00037 CPPUNIT_TEST(testIteratorST);
00038 CPPUNIT_TEST(testConstIteratorST);
00039 CPPUNIT_TEST_SUITE_END();
00040 private:
00041 ListType *list;
00042
00043 public:
00044 IteratorTest() {
00045 }
00046
00047 ListType * getList() {
00048 return list;
00049 }
00050
00051 void setUp() {
00052 list = new ListType();
00053 }
00054
00055 void reset() {
00056 delete list;
00057 list = new ListType();
00058 }
00059
00060 void tearDown() {
00061 delete list;
00062 }
00063
00064 void testIteratorST();
00065 void testConstIteratorST();
00066 };
00067
00068 #define ThreadInsert_T ThreadInsert<ListType, ParaType, CLASS_NAME>
00069
00070 template<typename ListType, typename ParaType, char const* CLASS_NAME> class ThreadInsert :
00071 public TestThread<ParaType> {
00072 private:
00073 ListType *list;
00074 IteratorTest<ListType, ParaType, CLASS_NAME> *testcase;
00075 int elementNum;
00076 int operationNum;
00077
00078 public:
00079
00080 ThreadInsert(ListType *l, IteratorTest<ListType, ParaType, CLASS_NAME>* lt,
00081 int nElement,int nOperation, int threadId = 0) :
00082 TestThread<ParaType>(threadId), list(l), testcase(lt), elementNum(nElement),operationNum(nOperation)
00083 {
00084 }
00085
00086 void* run() {
00087 for (int i = 0; i< operationNum; ++i) {
00088 bool ret = list->insert(0, testcase->data[i%((this->threadId+1)*elementNum)]);
00089 if (ret) {
00090 this->inVec.push_back(testcase->data[i%((this->threadId+1)*elementNum)]);
00091 }
00092 }
00093 return NULL;
00094 }
00095 };
00096
00097 template<typename ListType, typename ParaType, char const* CLASS_NAME> class ThreadInsertFactory :
00098 public ThreadFactory<ParaType> {
00099 private:
00100 ListType *list;
00101 IteratorTest<ListType, ParaType, CLASS_NAME> *testcase;
00102 public:
00103 ThreadInsertFactory(ListType * l,
00104 IteratorTest<ListType, ParaType, CLASS_NAME> * lt) :
00105 list(l), testcase(lt) {
00106 }
00107
00108 virtual Runnable ** createThreads(int threadNum, int elementNum, int operationNum) {
00109 testcase->reset();
00110 list = testcase->getList();
00111 this->inVec.clear();
00112 this->outVec.clear();
00113
00114 Runnable ** threads;
00115 threads = new Runnable*[threadNum];
00116 for (int i = 0; i < threadNum; ++i)
00117 threads[i] = new ThreadInsert<ListType, ParaType, CLASS_NAME>(list, testcase, elementNum, operationNum, i);
00118
00119 return threads;
00120 }
00121
00122 virtual void verifyResult(int threadNum, int elementNum) {
00123 ParaType tmp;
00124 while (!list->empty()) {
00125 list->front(tmp);
00126 this->outVec.push_back(tmp);
00127 list->remove(tmp);
00128 }
00129
00130 ThreadFactory<ParaType>::verifyResult(threadNum, elementNum);
00131 }
00132 };
00133
00134 template<typename ListType, typename ParaType, char const* CLASS_NAME> void IteratorTest<
00135 ListType, ParaType, CLASS_NAME>::testIteratorST() {
00136 ThreadInsert_T threadInsert(list, this, BaseTest<ParaType>::NELEMENT, BaseTest<ParaType>::NOPERATION);
00137 threadInsert.run();
00138
00139 vector<ParaType> outVec;
00140 typename ListType::iterator ite = list->begin();
00141 for (ite = list->begin(); ite != list->end(); ++ite) {
00142 outVec.push_back(*ite);
00143 }
00144
00145 sort(threadInsert.inVec.begin(), threadInsert.inVec.end());
00146 sort(outVec.begin(), outVec.end());
00147
00148 CPPUNIT_ASSERT(threadInsert.inVec == outVec);
00149 }
00150
00151 template<typename ListType, typename ParaType, char const* CLASS_NAME> void IteratorTest<
00152 ListType, ParaType, CLASS_NAME>::testConstIteratorST() {
00153 ThreadInsert_T threadInsert(list, this, BaseTest<ParaType>::NELEMENT, BaseTest<ParaType>::NOPERATION);
00154 threadInsert.run();
00155
00156 vector<ParaType> outVec;
00157 typename ListType::const_iterator ite = list->begin();
00158 for (ite = list->begin(); ite != list->end(); ++ite) {
00159 outVec.push_back(*ite);
00160 }
00161
00162 sort(threadInsert.inVec.begin(), threadInsert.inVec.end());
00163 sort(outVec.begin(), outVec.end());
00164
00165 CPPUNIT_ASSERT(threadInsert.inVec == outVec);
00166 }
00167
00168 }
00169 #endif