Home | History | Annotate | Download | only in unit
      1 #include <set>
      2 #include <functional>
      3 
      4 #include "cppunit/cppunit_proxy.h"
      5 
      6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      7 using namespace std;
      8 #endif
      9 
     10 //
     11 // TestCase class
     12 //
     13 class MultisetTest : public CPPUNIT_NS::TestCase
     14 {
     15   typedef multiset<int, less<int> > mset;
     16 
     17   CPPUNIT_TEST_SUITE(MultisetTest);
     18   CPPUNIT_TEST(mset1);
     19   CPPUNIT_TEST(mset3);
     20   CPPUNIT_TEST(mset5);
     21   CPPUNIT_TEST_SUITE_END();
     22 
     23 protected:
     24   void mset1();
     25   void mset3();
     26   void mset5();
     27 
     28   static bool less_than(int a_, int b_)
     29   {
     30     return a_ < b_;
     31   }
     32 
     33   static bool greater_than(int a_, int b_)
     34   {
     35     return a_ > b_;
     36   }
     37 };
     38 
     39 CPPUNIT_TEST_SUITE_REGISTRATION(MultisetTest);
     40 
     41 //
     42 // tests implementation
     43 //
     44 void MultisetTest::mset1()
     45 {
     46   mset s;
     47   CPPUNIT_ASSERT(s.count(42) == 0);
     48   s.insert(42);
     49   CPPUNIT_ASSERT(s.count(42) == 1);
     50   s.insert(42);
     51   CPPUNIT_ASSERT(s.count(42) == 2);
     52 
     53   mset::iterator i = s.find(40);
     54   CPPUNIT_ASSERT(i == s.end());
     55 
     56   i = s.find(42);
     57   CPPUNIT_ASSERT(i != s.end());
     58   size_t count = s.erase(42);
     59   CPPUNIT_ASSERT(count == 2);
     60 }
     61 void MultisetTest::mset3()
     62 {
     63   int array [] = { 3, 6, 1, 2, 3, 2, 6, 7, 9 };
     64 
     65   //Check iterator on a mutable set
     66   mset s(array, array + 9);
     67   mset::iterator i;
     68   i = s.lower_bound(3);
     69   CPPUNIT_ASSERT(*i == 3);
     70   i = s.upper_bound(3);
     71   CPPUNIT_ASSERT(*i == 6);
     72   pair<mset::iterator, mset::iterator> p = s.equal_range(5);
     73   CPPUNIT_ASSERT(*(p.first) == 6);
     74   CPPUNIT_ASSERT(*(p.second) == 6);
     75 
     76   //Check const_iterator on a mutable multiset
     77   mset::const_iterator ci;
     78   ci = s.lower_bound(3);
     79   CPPUNIT_ASSERT(*ci == 3);
     80   ci = s.upper_bound(3);
     81   CPPUNIT_ASSERT(*ci == 6);
     82   pair<mset::const_iterator, mset::const_iterator> cp;
     83 #ifdef _STLP_MEMBER_TEMPLATES
     84   cp = s.equal_range(5);
     85   CPPUNIT_ASSERT(*(cp.first) == 6);
     86   CPPUNIT_ASSERT(*(cp.second) == 6);
     87 #endif
     88 
     89   //Check const_iterator on a const multiset
     90   mset const& crs = s;
     91   ci = crs.lower_bound(3);
     92   CPPUNIT_ASSERT(*ci == 3);
     93   ci = crs.upper_bound(3);
     94   CPPUNIT_ASSERT(*ci == 6);
     95   cp = crs.equal_range(5);
     96   CPPUNIT_ASSERT(*(cp.first) == 6);
     97   CPPUNIT_ASSERT(*(cp.second) == 6);
     98 }
     99 void MultisetTest::mset5()
    100 {
    101   int array [] = { 3, 6, 1, 9 };
    102   int j;
    103 
    104   typedef pointer_to_binary_function<int, int, bool> fn_type;
    105   typedef multiset<int, fn_type, allocator<int> > fn_mset;
    106 
    107   fn_type f(less_than);
    108   fn_mset s1(array+0, array + 4 , f );
    109   fn_mset::const_iterator i = s1.begin();
    110   for (j = 0; i != s1.end(); ++i, ++j) {
    111     CPPUNIT_ASSERT(j != 0 || *i == 1);
    112     CPPUNIT_ASSERT(j != 1 || *i == 3);
    113     CPPUNIT_ASSERT(j != 2 || *i == 6);
    114     CPPUNIT_ASSERT(j != 3 || *i == 9);
    115   }
    116 
    117   fn_type g(greater_than);
    118   fn_mset s2(array, array + 4, g);
    119   i = s2.begin();
    120   for (j = 0; i != s2.end(); ++i, ++j) {
    121     CPPUNIT_ASSERT(j != 0 || *i == 9);
    122     CPPUNIT_ASSERT(j != 1 || *i == 6);
    123     CPPUNIT_ASSERT(j != 2 || *i == 3);
    124     CPPUNIT_ASSERT(j != 3 || *i == 1);
    125   }
    126 
    127 }
    128