Home | History | Annotate | Download | only in unit
      1 #include <cstring>
      2 #include <vector>
      3 #include <algorithm>
      4 
      5 #include "cppunit/cppunit_proxy.h"
      6 
      7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      8 using namespace std;
      9 #endif
     10 
     11 //
     12 // TestCase class
     13 //
     14 class IncludesTest : public CPPUNIT_NS::TestCase
     15 {
     16   CPPUNIT_TEST_SUITE(IncludesTest);
     17   CPPUNIT_TEST(incl0);
     18   CPPUNIT_TEST(incl1);
     19   CPPUNIT_TEST(incl2);
     20   CPPUNIT_TEST_SUITE_END();
     21 
     22 protected:
     23   void incl0();
     24   void incl1();
     25   void incl2();
     26 
     27   static bool compare_strings(const char* s1_, const char* s2_)
     28   {
     29     return strcmp(s1_, s2_) < 0 ? 1 : 0;
     30   }
     31 };
     32 
     33 CPPUNIT_TEST_SUITE_REGISTRATION(IncludesTest);
     34 
     35 //
     36 // tests implementation
     37 //
     38 void IncludesTest::incl0()
     39 {
     40   int numbers1[5] = { 1, 2, 3, 4, 5 };
     41   //int numbers2[5] = { 1, 2, 4, 8, 16 };
     42   int numbers3[2] = { 4, 8 };
     43   bool r1=includes(numbers1, numbers1 + 5, numbers3, numbers3 + 2);
     44   CPPUNIT_ASSERT(!r1);
     45 }
     46 void IncludesTest::incl1()
     47 {
     48   vector<int> v1(10);
     49   vector<int> v2(3);
     50   int i;
     51   for (i = 0; (size_t)i < v1.size(); ++i) {
     52     v1[i] = i;
     53   }
     54 
     55   bool r1=includes(v1.begin(), v1.end(), v2.begin(), v2.end());
     56   CPPUNIT_ASSERT(!r1);
     57 
     58   for (i = 0; (size_t)i < v2.size(); ++i)
     59     v2[i] = i + 3;
     60 
     61   bool r2=includes(v1.begin(), v1.end(), v2.begin(), v2.end());
     62   CPPUNIT_ASSERT(r2);
     63 }
     64 void IncludesTest::incl2()
     65 {
     66   char const* names[] = {  "Todd", "Mike", "Graham", "Jack", "Brett"};
     67 
     68   const unsigned nameSize = sizeof(names)/sizeof(names[0]);
     69   vector <char const*> v1(nameSize);
     70   for (int i = 0; (size_t)i < v1.size(); ++i) {
     71     v1[i] = names[i];
     72   }
     73   vector <char const*> v2(2);
     74 
     75   v2[0] = "foo";
     76   v2[1] = "bar";
     77   sort(v1.begin(), v1.end(), compare_strings);
     78   sort(v2.begin(), v2.end(), compare_strings);
     79 
     80   bool r1 = includes(v1.begin(), v1.end(), v2.begin(), v2.end(), compare_strings);
     81   CPPUNIT_ASSERT(!r1);
     82 
     83   v2[0] = "Brett";
     84   v2[1] = "Todd";
     85   bool r2 = includes(v1.begin(), v1.end(), v2.begin(), v2.end(), compare_strings);
     86   CPPUNIT_ASSERT(r2);
     87 }
     88