00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef DEQUETEST_H_
00017 #define DEQUETEST_H_
00018 #include <cppunit/extensions/HelperMacros.h>
00019 #include <cppunit/TestAssert.h>
00020 #include <stdexcept>
00021 #include <sstream>
00022
00023 #include "baseTest.h"
00024
00025 namespace test {
00026 using namespace amino;
00027
00028 template<typename CounterType, typename ParaType, char const* CLASS_NAME>
00029 class CounterTest: public CppUnit::TestFixture, public BaseTest<ParaType> {
00030 CPPUNIT_TEST_SUITE(CounterTest)
00031 ;
00032 CPPUNIT_TEST(testCounter);
00033 CPPUNIT_TEST_SUITE_END()
00034 ;
00035
00036 private:
00037 CounterType *counter;
00038 public:
00039 CounterTest() {
00040 }
00041
00042 CounterType * getCounter() {
00043 return counter;
00044 }
00045
00046 void setUp() {
00047 counter = new CounterType();
00048 }
00049
00050 void reset() {
00051 delete counter;
00052 counter = new CounterType();
00053 }
00054
00055 void tearDown() {
00056 delete counter;
00057 }
00058
00059 void testCounter();
00060 };
00061
00062 template<typename CounterType, typename ParaType, char const* CLASS_NAME>
00063 class Thread4Dir: public TestThread<ParaType> {
00064 private:
00065 CounterType *counter;
00066 CounterTest<CounterType, ParaType, CLASS_NAME> *testcase;
00067 int operationNum;
00068
00069 public:
00070 Thread4Dir(CounterType * q,
00071 CounterTest<CounterType, ParaType, CLASS_NAME> * qt, int nOperation) :
00072 counter(q), testcase(qt), operationNum(nOperation) {
00073 }
00074
00075 void* run() {
00076 for (int i = 0; i < operationNum; ++i) {
00077 counter->increment((ParaType) i);
00078 counter->increment((ParaType) i + 1);
00079 counter->decrement((ParaType) i + 2);
00080 }
00081 return NULL;
00082 }
00083 };
00084
00085 template<typename CounterType, typename ParaType, char const* CLASS_NAME>
00086 class Thread4DirFactory: public ThreadFactory<ParaType> {
00087 private:
00088 CounterType *counter;
00089 CounterTest<CounterType, ParaType, CLASS_NAME> *testcase;
00090
00091 public:
00092 Thread4DirFactory(CounterType * q, CounterTest<CounterType, ParaType,
00093 CLASS_NAME> * qt) :
00094 counter(q), testcase(qt) {
00095 }
00096
00097 int opNum;
00098
00099 virtual Runnable ** createThreads(int threadNum, int elementNum,
00100 int operationNum) {
00101 opNum=operationNum;
00102 testcase->reset();
00103 counter = testcase->getCounter();
00104
00105 this->inVec.clear();
00106 this->outVec.clear();
00107
00108 Runnable ** threads;
00109 threads = new Runnable*[threadNum];
00110 for (int i = 0; i < threadNum; ++i)
00111 threads[i]
00112 = new Thread4Dir<CounterType, ParaType, CLASS_NAME> (counter, testcase, operationNum);
00113
00114 return threads;
00115 }
00116
00117 virtual void verifyResult(int threadNum, int elementNum) {
00118 stringstream output;
00119 output<<"Expecting "<<threadNum*elementNum<<" but get "<<counter->load()<<endl;
00120 CPPUNIT_ASSERT_MESSAGE(output.str(), counter->load()==threadNum*opNum);
00121 }
00122 };
00123
00124 template<typename CounterType, typename ParaType, const char * CLASS_NAME>
00125 void CounterTest<CounterType, ParaType, CLASS_NAME>::testCounter() {
00126 Thread4DirFactory<CounterType, ParaType, CLASS_NAME> dequeueFactory(
00127 counter, this);
00128 ThreadRunner::runThreads(&dequeueFactory, CLASS_NAME, "testCounterMT");
00129 }
00130 }
00131 #endif