00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TEST_PSORT_H_
00023 #define TEST_PSORT_H_
00024 #include <cppunit/extensions/HelperMacros.h>
00025 #include <cppunit/TestAssert.h>
00026 #include "baseTest.h"
00027
00028 #include <amino/cstdatomic>
00029 #include <amino/thread.h>
00030 #include <amino/parallel_sort.h>
00031 #include <amino/tp_exec.h>
00032
00033 #include <iostream>
00034 #include <unistd.h>
00035 #include <algorithm>
00036 #include <sstream>
00037
00038 namespace test{
00039 using namespace amino;
00040
00041 template<typename ParaType, char const* CLASS_NAME>
00042 class ParallelSortTest :
00043 public CppUnit::TestFixture, public BaseTest<ParaType> {
00044 CPPUNIT_TEST_SUITE(ParallelSortTest);
00045 CPPUNIT_TEST(testSort);
00046 CPPUNIT_TEST_SUITE_END();
00047
00048 public:
00049 ParallelSortTest() {
00050 }
00051
00052 void setUp() {
00053 }
00054
00055 void reset() {
00056 }
00057
00058 void tearDown() {
00059 }
00060
00061 void testSort(){
00062 Logger log(stderr);
00063 vector<int> thread_v = TestConfig::getInstance()->getThreadNum();
00064 ParaType* backup = new ParaType[this->NELEMENT * this->MAXTHREADN];
00065 std::copy(this->data, this->data + this->NELEMENT * this->MAXTHREADN, backup);
00066 ParaType* verify = new ParaType[this->NELEMENT * this->MAXTHREADN];
00067 for(unsigned int i=0;i<thread_v.size();i++){
00068 ThreadPoolExecutor exec(thread_v[i]);
00069 std::copy(this->data, this->data + this->NELEMENT * thread_v[i], verify);
00070
00071 struct timeval t1, t2, t3;
00072 gettimeofday(&t1, NULL);
00073
00074 parallel_sort(this->data, this->data + this->NELEMENT * thread_v[i], thread_v[i], &exec);
00075 gettimeofday(&t2, NULL);
00076
00077 std::sort(verify, verify + this->NELEMENT * thread_v[i]);
00078
00079 gettimeofday(&t3, NULL);
00080
00081
00082 int takes1 = (t2.tv_sec - t1.tv_sec)* 1000000 + t2.tv_usec - t1.tv_usec;
00083 log.log(
00084 "INFO: class: %s\tnThread:\t%d\tnElement:\t%d\ttestName:\t%s\tTakes:\t%d\tmicroseconds\n",
00085 CLASS_NAME, thread_v[i], this->NELEMENT, "testParallelSort", takes1);
00086
00087 int takes2 = (t3.tv_sec - t2.tv_sec)* 1000000 + t3.tv_usec - t2.tv_usec;
00088 log.log(
00089 "INFO: class: %s\tnThread:\t%d\tnElement:\t%d\ttestName:\t%s\tTakes:\t%d\tmicroseconds\n",
00090 CLASS_NAME, 1, this->NELEMENT*thread_v[i], "testSerialSort", takes2);
00091
00092 for(int k=0;k<this->NELEMENT * thread_v[i];k++){
00093 ostringstream output;
00094 output << k <<"th element is " << this->data[k]<< " while " << "verify is "<< verify[k]<<endl;
00095
00096 CPPUNIT_ASSERT_MESSAGE(output.str(), this->data[k]==verify[k]);
00097 }
00098
00099 std::copy(backup, backup + this->NELEMENT * this->MAXTHREADN, this->data);
00100 exec.shutdown();
00101 exec.waitTermination();
00102 }
00103
00104 delete [] backup;
00105 delete [] verify;
00106 }
00107 };
00108 }
00109 #endif