Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenadj_test.cpp
Go to the documentation of this file.
00001 #include <vector> 00002 #include <numeric> 00003 #include <algorithm> 00004 00005 #include "cppunit/cppunit_proxy.h" 00006 00007 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00008 using namespace std; 00009 #endif 00010 00011 // 00012 // TestCase class 00013 // 00014 class AdjTest : public CPPUNIT_NS::TestCase 00015 { 00016 CPPUNIT_TEST_SUITE(AdjTest); 00017 CPPUNIT_TEST(adjfind0); 00018 CPPUNIT_TEST(adjfind1); 00019 CPPUNIT_TEST(adjfind2); 00020 CPPUNIT_TEST(adjdiff0); 00021 CPPUNIT_TEST(adjdiff1); 00022 CPPUNIT_TEST(adjdiff2); 00023 CPPUNIT_TEST_SUITE_END(); 00024 00025 protected: 00026 void adjfind0(); 00027 void adjfind1(); 00028 void adjfind2(); 00029 void adjdiff0(); 00030 void adjdiff1(); 00031 void adjdiff2(); 00032 static int equal_length(const char* v1_, const char* v2_); 00033 static int mult(int a_, int b_); 00034 }; 00035 00036 CPPUNIT_TEST_SUITE_REGISTRATION(AdjTest); 00037 00038 // 00039 // tests implementation 00040 // 00041 void AdjTest::adjfind0() 00042 { 00043 int numbers1 [5] = { 1, 2, 4, 8, 16 }; 00044 int numbers2 [5] = { 5, 3, 2, 1, 1 }; 00045 00046 int* location = adjacent_find((int*)numbers1, (int*)numbers1 + 5); 00047 CPPUNIT_ASSERT(location == numbers1 + 5); // no adj so loc should be _last 00048 00049 location = adjacent_find((int*)numbers2, (int*)numbers2 + 5); 00050 CPPUNIT_ASSERT(location != numbers2 + 5); // adj location off should be 3 (first 1) 00051 CPPUNIT_ASSERT((location - numbers2)==3); 00052 } 00053 void AdjTest::adjfind1() 00054 { 00055 typedef vector<int> IntVector; 00056 IntVector v(10); 00057 for (int i = 0; (size_t)i < v.size(); ++i) 00058 v[i] = i; 00059 IntVector::iterator location; 00060 location = adjacent_find(v.begin(), v.end()); 00061 CPPUNIT_ASSERT(location == v.end()); 00062 v[6] = 7; 00063 location = adjacent_find(v.begin(), v.end()); 00064 CPPUNIT_ASSERT(location != v.end()); 00065 } 00066 void AdjTest::adjfind2() 00067 { 00068 typedef vector <const char*> CStrVector; 00069 00070 const char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" }; 00071 00072 const int nameCount = sizeof(names)/sizeof(names[0]); 00073 CStrVector v(nameCount); 00074 for(int i = 0; i < nameCount; i++) 00075 v[i] = names[i]; 00076 CStrVector::iterator location; 00077 location = adjacent_find(v.begin(), v.end(), equal_length); 00078 00079 CPPUNIT_ASSERT(location != v.end()); 00080 } 00081 int AdjTest::equal_length(const char* v1_, const char* v2_) 00082 { 00083 return ::strlen(v1_) == ::strlen(v2_); 00084 } 00085 void AdjTest::adjdiff0() 00086 { 00087 int numbers[5] = { 1, 2, 4, 8, 16 }; 00088 int difference[5]; 00089 adjacent_difference(numbers, numbers + 5, (int*)difference); 00090 CPPUNIT_ASSERT(difference[0]==1); 00091 CPPUNIT_ASSERT(difference[1]==1); 00092 CPPUNIT_ASSERT(difference[2]==2); 00093 CPPUNIT_ASSERT(difference[3]==4); 00094 CPPUNIT_ASSERT(difference[4]==8); 00095 } 00096 void AdjTest::adjdiff1() 00097 { 00098 vector <int> v(10); 00099 for(int i = 0; (size_t)i < v.size(); ++i) 00100 v[i] = i * i; 00101 vector<int> result(v.size()); 00102 adjacent_difference(v.begin(), v.end(), result.begin()); 00103 CPPUNIT_ASSERT(result[0]==0) 00104 CPPUNIT_ASSERT(result[1]==1) 00105 CPPUNIT_ASSERT(result[2]==3) 00106 CPPUNIT_ASSERT(result[3]==5) 00107 CPPUNIT_ASSERT(result[4]==7) 00108 CPPUNIT_ASSERT(result[5]==9) 00109 CPPUNIT_ASSERT(result[6]==11) 00110 CPPUNIT_ASSERT(result[7]==13) 00111 CPPUNIT_ASSERT(result[8]==15) 00112 CPPUNIT_ASSERT(result[9]==17) 00113 } 00114 void AdjTest::adjdiff2() 00115 { 00116 vector <int> v(10); 00117 for (int i = 0; (size_t)i < v.size(); ++i) 00118 v[i] = i + 1; 00119 vector <int> result(v.size()); 00120 adjacent_difference(v.begin(), v.end(), result.begin(), mult); 00121 CPPUNIT_ASSERT(result[0]==1) 00122 CPPUNIT_ASSERT(result[1]==2) 00123 CPPUNIT_ASSERT(result[2]==6) 00124 CPPUNIT_ASSERT(result[3]==12) 00125 CPPUNIT_ASSERT(result[4]==20) 00126 CPPUNIT_ASSERT(result[5]==30) 00127 CPPUNIT_ASSERT(result[6]==42) 00128 CPPUNIT_ASSERT(result[7]==56) 00129 CPPUNIT_ASSERT(result[8]==72) 00130 CPPUNIT_ASSERT(result[9]==90) 00131 } 00132 int AdjTest::mult(int a_, int b_) 00133 { 00134 return a_ * b_; 00135 } Generated on Sun May 27 2012 04:35:16 for ReactOS by
1.7.6.1
|