Home | History | Annotate | Download | only in unit
      1 #include <algorithm>
      2 
      3 #include "cppunit/cppunit_proxy.h"
      4 
      5 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      6 using namespace std;
      7 #endif
      8 
      9 //
     10 // TestCase class
     11 //
     12 class UniqueTest : public CPPUNIT_NS::TestCase
     13 {
     14   CPPUNIT_TEST_SUITE(UniqueTest);
     15   CPPUNIT_TEST(uniqcpy1);
     16   CPPUNIT_TEST(uniqcpy2);
     17   CPPUNIT_TEST(unique1);
     18   CPPUNIT_TEST(unique2);
     19   CPPUNIT_TEST_SUITE_END();
     20 
     21 protected:
     22   void uniqcpy1();
     23   void uniqcpy2();
     24   void unique1();
     25   void unique2();
     26 };
     27 
     28 CPPUNIT_TEST_SUITE_REGISTRATION(UniqueTest);
     29 
     30 static bool str_equal(const char* a_, const char* b_)
     31 { return *a_ == *b_; }
     32 //
     33 // tests implementation
     34 //
     35 void UniqueTest::unique1()
     36 {
     37   int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
     38   unique((int*)numbers, (int*)numbers + 8);
     39   // 0 1 2 3 4 2 3 4
     40   CPPUNIT_ASSERT(numbers[0]==0);
     41   CPPUNIT_ASSERT(numbers[1]==1);
     42   CPPUNIT_ASSERT(numbers[2]==2);
     43   CPPUNIT_ASSERT(numbers[3]==3);
     44   CPPUNIT_ASSERT(numbers[4]==4);
     45   CPPUNIT_ASSERT(numbers[5]==2);
     46   CPPUNIT_ASSERT(numbers[6]==3);
     47   CPPUNIT_ASSERT(numbers[7]==4);
     48 }
     49 
     50 void UniqueTest::unique2()
     51 {
     52   const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
     53 
     54   const unsigned count = sizeof(labels) / sizeof(labels[0]);
     55 
     56   unique((const char**)labels, (const char**)labels + count, str_equal);
     57 
     58   // QWERTY
     59   CPPUNIT_ASSERT(*labels[0] == 'Q');
     60   CPPUNIT_ASSERT(*labels[1] == 'W');
     61   CPPUNIT_ASSERT(*labels[2] == 'E');
     62   CPPUNIT_ASSERT(*labels[3] == 'R');
     63   CPPUNIT_ASSERT(*labels[4] == 'T');
     64   CPPUNIT_ASSERT(*labels[5] == 'Y');
     65 
     66 }
     67 
     68 void UniqueTest::uniqcpy1()
     69 {
     70   int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
     71   int result[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
     72 
     73   unique_copy((int*)numbers, (int*)numbers + 8, (int*)result);
     74 
     75   // 0 1 2 3 4 0 0 0
     76   CPPUNIT_ASSERT(result[0]==0);
     77   CPPUNIT_ASSERT(result[1]==1);
     78   CPPUNIT_ASSERT(result[2]==2);
     79   CPPUNIT_ASSERT(result[3]==3);
     80   CPPUNIT_ASSERT(result[4]==4);
     81   CPPUNIT_ASSERT(result[5]==0);
     82   CPPUNIT_ASSERT(result[6]==0);
     83   CPPUNIT_ASSERT(result[7]==0);
     84 }
     85 
     86 void UniqueTest::uniqcpy2()
     87 {
     88   const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
     89   const char **plabels = (const char**)labels;
     90 
     91   const size_t count = sizeof(labels) / sizeof(labels[0]);
     92   const char* uCopy[count];
     93   const char **puCopy = &uCopy[0];
     94   fill(puCopy, puCopy + count, "");
     95 
     96   unique_copy(plabels, plabels + count, puCopy, str_equal);
     97 
     98   //QWERTY
     99   CPPUNIT_ASSERT(*uCopy[0] == 'Q');
    100   CPPUNIT_ASSERT(*uCopy[1] == 'W');
    101   CPPUNIT_ASSERT(*uCopy[2] == 'E');
    102   CPPUNIT_ASSERT(*uCopy[3] == 'R');
    103   CPPUNIT_ASSERT(*uCopy[4] == 'T');
    104   CPPUNIT_ASSERT(*uCopy[5] == 'Y');
    105 }
    106