1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/unique_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,105 @@
1.4 +#include <algorithm>
1.5 +
1.6 +#include "cppunit/cppunit_proxy.h"
1.7 +
1.8 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
1.9 +using namespace std;
1.10 +#endif
1.11 +
1.12 +//
1.13 +// TestCase class
1.14 +//
1.15 +class UniqueTest : public CPPUNIT_NS::TestCase
1.16 +{
1.17 + CPPUNIT_TEST_SUITE(UniqueTest);
1.18 + CPPUNIT_TEST(uniqcpy1);
1.19 + CPPUNIT_TEST(uniqcpy2);
1.20 + CPPUNIT_TEST(unique1);
1.21 + CPPUNIT_TEST(unique2);
1.22 + CPPUNIT_TEST_SUITE_END();
1.23 +
1.24 +protected:
1.25 + void uniqcpy1();
1.26 + void uniqcpy2();
1.27 + void unique1();
1.28 + void unique2();
1.29 +};
1.30 +
1.31 +CPPUNIT_TEST_SUITE_REGISTRATION(UniqueTest);
1.32 +
1.33 +static bool str_equal(const char* a_, const char* b_)
1.34 +{ return *a_ == *b_; }
1.35 +//
1.36 +// tests implementation
1.37 +//
1.38 +void UniqueTest::unique1()
1.39 +{
1.40 + int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
1.41 + unique((int*)numbers, (int*)numbers + 8);
1.42 + // 0 1 2 3 4 2 3 4
1.43 + CPPUNIT_ASSERT(numbers[0]==0);
1.44 + CPPUNIT_ASSERT(numbers[1]==1);
1.45 + CPPUNIT_ASSERT(numbers[2]==2);
1.46 + CPPUNIT_ASSERT(numbers[3]==3);
1.47 + CPPUNIT_ASSERT(numbers[4]==4);
1.48 + CPPUNIT_ASSERT(numbers[5]==2);
1.49 + CPPUNIT_ASSERT(numbers[6]==3);
1.50 + CPPUNIT_ASSERT(numbers[7]==4);
1.51 +}
1.52 +
1.53 +void UniqueTest::unique2()
1.54 +{
1.55 + const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
1.56 +
1.57 + const unsigned count = sizeof(labels) / sizeof(labels[0]);
1.58 +
1.59 + unique((const char**)labels, (const char**)labels + count, str_equal);
1.60 +
1.61 + // QWERTY
1.62 + CPPUNIT_ASSERT(*labels[0] == 'Q');
1.63 + CPPUNIT_ASSERT(*labels[1] == 'W');
1.64 + CPPUNIT_ASSERT(*labels[2] == 'E');
1.65 + CPPUNIT_ASSERT(*labels[3] == 'R');
1.66 + CPPUNIT_ASSERT(*labels[4] == 'T');
1.67 + CPPUNIT_ASSERT(*labels[5] == 'Y');
1.68 +
1.69 +}
1.70 +
1.71 +void UniqueTest::uniqcpy1()
1.72 +{
1.73 + int numbers[8] = { 0, 1, 1, 2, 2, 2, 3, 4 };
1.74 + int result[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1.75 +
1.76 + unique_copy((int*)numbers, (int*)numbers + 8, (int*)result);
1.77 +
1.78 + // 0 1 2 3 4 0 0 0
1.79 + CPPUNIT_ASSERT(result[0]==0);
1.80 + CPPUNIT_ASSERT(result[1]==1);
1.81 + CPPUNIT_ASSERT(result[2]==2);
1.82 + CPPUNIT_ASSERT(result[3]==3);
1.83 + CPPUNIT_ASSERT(result[4]==4);
1.84 + CPPUNIT_ASSERT(result[5]==0);
1.85 + CPPUNIT_ASSERT(result[6]==0);
1.86 + CPPUNIT_ASSERT(result[7]==0);
1.87 +}
1.88 +
1.89 +void UniqueTest::uniqcpy2()
1.90 +{
1.91 + const char* labels[] = {"Q", "Q", "W", "W", "E", "E", "R", "T", "T", "Y", "Y"};
1.92 + const char **plabels = (const char**)labels;
1.93 +
1.94 + const size_t count = sizeof(labels) / sizeof(labels[0]);
1.95 + char* uCopy[count];
1.96 + const char **puCopy = (const char**)uCopy;
1.97 + fill(puCopy, puCopy + count, "");
1.98 +
1.99 + unique_copy(plabels, plabels + count, puCopy, str_equal);
1.100 +
1.101 + //QWERTY
1.102 + CPPUNIT_ASSERT(*uCopy[0] == 'Q');
1.103 + CPPUNIT_ASSERT(*uCopy[1] == 'W');
1.104 + CPPUNIT_ASSERT(*uCopy[2] == 'E');
1.105 + CPPUNIT_ASSERT(*uCopy[3] == 'R');
1.106 + CPPUNIT_ASSERT(*uCopy[4] == 'T');
1.107 + CPPUNIT_ASSERT(*uCopy[5] == 'Y');
1.108 +}