os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/alg_test.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <list>
    17 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
    18 #  include <slist>
    19 #endif
    20 #include <deque>
    21 #include <vector>
    22 #include <algorithm>
    23 #include <functional>
    24 #include <map>
    25 #include <string>
    26 
    27 #include "cppunit/cppunit_proxy.h"
    28 
    29 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    30 using namespace std;
    31 #endif
    32 
    33 //
    34 // TestCase class
    35 //
    36 class AlgTest : public CPPUNIT_NS::TestCase
    37 {
    38   CPPUNIT_TEST_SUITE(AlgTest);
    39   CPPUNIT_TEST(min_max);
    40   CPPUNIT_TEST(count_test);
    41   CPPUNIT_TEST(sort_test);
    42   CPPUNIT_TEST(search_n_test);
    43   CPPUNIT_TEST(find_first_of_test);
    44   CPPUNIT_TEST(find_first_of_nsc_test);
    45   CPPUNIT_TEST(alg_cov);
    46   CPPUNIT_TEST_SUITE_END();
    47 
    48 protected:
    49   void min_max();
    50   void count_test();
    51   void sort_test();
    52   void search_n_test();
    53   void find_first_of_test();
    54   void find_first_of_nsc_test();
    55   void alg_cov();
    56 };
    57 
    58 CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest);
    59 
    60 //
    61 // tests implementation
    62 //
    63 bool mypredicate (int i, int j) 
    64 	{
    65 	return (i==j);
    66 	}
    67 void AlgTest::min_max()
    68 {
    69   int i = min(4, 7);
    70   CPPUNIT_ASSERT( i == 4 );
    71   char c = max('a', 'z');
    72   CPPUNIT_ASSERT( c == 'z' );
    73 
    74 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
    75   c = min('a', 'z', greater<char>());
    76   CPPUNIT_ASSERT( c == 'z' );
    77   i = max(4, 7, greater<int>());
    78   CPPUNIT_ASSERT( i == 4 );
    79 #endif
    80 }
    81 
    82 void AlgTest::count_test()
    83 {
    84   {
    85     int i[] = { 1, 4, 2, 8, 2, 2 };
    86     int n = count(i, i + 6, 2);
    87     CPPUNIT_ASSERT(n==3);
    88 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
    89     n = 0;
    90     count(i, i + 6, 2, n);
    91     CPPUNIT_ASSERT(n==3);
    92 #endif
    93   }
    94   {
    95     vector<int> i;
    96     i.push_back(1);
    97     i.push_back(4);
    98     i.push_back(2);
    99     i.push_back(8);
   100     i.push_back(2);
   101     i.push_back(2);
   102     int n = count(i.begin(), i.end(), 2);
   103     CPPUNIT_ASSERT(n==3);
   104 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
   105     n = 0;
   106     count(i.begin(), i.end(), 2, n);
   107     CPPUNIT_ASSERT(n==3);
   108 #endif
   109   }
   110 }
   111 
   112 void AlgTest::sort_test()
   113 {
   114   {
   115     vector<int> years;
   116     years.push_back(1962);
   117     years.push_back(1992);
   118     years.push_back(2001);
   119     years.push_back(1999);
   120     sort(years.begin(), years.end());
   121     CPPUNIT_ASSERT(years[0]==1962);
   122     CPPUNIT_ASSERT(years[1]==1992);
   123     CPPUNIT_ASSERT(years[2]==1999);
   124     CPPUNIT_ASSERT(years[3]==2001);
   125   }
   126   {
   127     deque<int> years;
   128     years.push_back(1962);
   129     years.push_back(1992);
   130     years.push_back(2001);
   131     years.push_back(1999);
   132     sort(years.begin(), years.end()); // <-- changed!
   133     CPPUNIT_ASSERT(years[0]==1962);
   134     CPPUNIT_ASSERT(years[1]==1992);
   135     CPPUNIT_ASSERT(years[2]==1999);
   136     CPPUNIT_ASSERT(years[3]==2001);
   137   }
   138 }
   139 
   140 #define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
   141 
   142 void AlgTest::search_n_test()
   143 {
   144   int ints[] = {0, 1, 2, 3, 3, 4, 4, 4, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
   145 
   146 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   147   //search_n
   148   //Forward iterator
   149   {
   150     slist<int> slint(ints, ints + ARRAY_SIZE(ints));
   151     slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 2);
   152     CPPUNIT_ASSERT( slit != slint.end() );
   153     CPPUNIT_ASSERT( *(slit++) == 2 );
   154     CPPUNIT_ASSERT( *slit == 2 );
   155   }
   156 #endif
   157 
   158   //Bidirectionnal iterator
   159   {
   160     list<int> lint(ints, ints + ARRAY_SIZE(ints));
   161     list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 3);
   162     CPPUNIT_ASSERT( lit != lint.end() );
   163     CPPUNIT_ASSERT( *(lit++) == 3 );
   164     CPPUNIT_ASSERT( *(lit++) == 3 );
   165     CPPUNIT_ASSERT( *lit == 3 );
   166   }
   167 
   168   //Random access iterator
   169   {
   170     deque<int> dint(ints, ints + ARRAY_SIZE(ints));
   171     deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 4);
   172     CPPUNIT_ASSERT( dit != dint.end() );
   173     CPPUNIT_ASSERT( *(dit++) == 4 );
   174     CPPUNIT_ASSERT( *(dit++) == 4 );
   175     CPPUNIT_ASSERT( *(dit++) == 4 );
   176     CPPUNIT_ASSERT( *dit == 4 );
   177   }
   178 
   179 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   180   //search_n with predicate
   181   //Forward iterator
   182   {
   183     slist<int> slint(ints, ints + ARRAY_SIZE(ints));
   184     slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 1, greater<int>());
   185     CPPUNIT_ASSERT( slit != slint.end() );
   186     CPPUNIT_ASSERT( *(slit++) > 1 );
   187     CPPUNIT_ASSERT( *slit > 2 );
   188   }
   189 #endif
   190 
   191   //Bidirectionnal iterator
   192   {
   193     list<int> lint(ints, ints + ARRAY_SIZE(ints));
   194     list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 2, greater<int>());
   195     CPPUNIT_ASSERT( lit != lint.end() );
   196     CPPUNIT_ASSERT( *(lit++) > 2 );
   197     CPPUNIT_ASSERT( *(lit++) > 2 );
   198     CPPUNIT_ASSERT( *lit > 2 );
   199   }
   200 
   201   //Random access iterator
   202   {
   203     deque<int> dint(ints, ints + ARRAY_SIZE(ints));
   204     deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 3, greater<int>());
   205     CPPUNIT_ASSERT( dit != dint.end() );
   206     CPPUNIT_ASSERT( *(dit++) > 3 );
   207     CPPUNIT_ASSERT( *(dit++) > 3 );
   208     CPPUNIT_ASSERT( *(dit++) > 3 );
   209     CPPUNIT_ASSERT( *dit > 3 );
   210   }
   211 
   212   // test for bug reported by Jim Xochellis
   213   {
   214     int array[] = {0, 0, 1, 0, 1, 1};
   215     int* array_end = array + sizeof(array) / sizeof(*array);
   216     CPPUNIT_ASSERT(search_n(array, array_end, 3, 1) == array_end);
   217   }
   218 
   219   // test for bug with counter == 1, reported by Timmie Smith
   220   {
   221     int array[] = {0, 1, 2, 3, 4, 5};
   222     int* array_end = array + sizeof(array) / sizeof(*array);
   223     CPPUNIT_ASSERT( search_n(array, array_end, 1, 1, equal_to<int>() ) == &array[1] );
   224   }
   225 }
   226 
   227 void AlgTest::find_first_of_test()
   228 {
   229 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   230   slist<int> intsl;
   231   intsl.push_front(1);
   232   intsl.push_front(2);
   233 
   234   {
   235     vector<int> intv;
   236     intv.push_back(0);
   237     intv.push_back(1);
   238     intv.push_back(2);
   239     intv.push_back(3);
   240 
   241     vector<int>::iterator first;
   242     first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
   243     CPPUNIT_ASSERT( first != intv.end() );
   244     CPPUNIT_ASSERT( *first == 1 );
   245   }
   246   {
   247     vector<int> intv;
   248     intv.push_back(3);
   249     intv.push_back(2);
   250     intv.push_back(1);
   251     intv.push_back(0);
   252 
   253     vector<int>::iterator first;
   254     first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
   255     CPPUNIT_ASSERT( first != intv.end() );
   256     CPPUNIT_ASSERT( *first == 2 );
   257   }
   258 #endif
   259 
   260   list<int> intl;
   261   intl.push_front(1);
   262   intl.push_front(2);
   263 
   264   {
   265     vector<int> intv;
   266     intv.push_back(0);
   267     intv.push_back(1);
   268     intv.push_back(2);
   269     intv.push_back(3);
   270 
   271     vector<int>::iterator first;
   272     first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
   273     CPPUNIT_ASSERT( first != intv.end() );
   274     CPPUNIT_ASSERT( *first == 1 );
   275   }
   276   {
   277     vector<int> intv;
   278     intv.push_back(3);
   279     intv.push_back(2);
   280     intv.push_back(1);
   281     intv.push_back(0);
   282 
   283     vector<int>::iterator first;
   284     first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
   285     CPPUNIT_ASSERT( first != intv.end() );
   286     CPPUNIT_ASSERT( *first == 2 );
   287   }
   288 }
   289 
   290 typedef pair<int, string> Pair;
   291 
   292 struct ValueFinder :
   293     public binary_function<const Pair&, const string&, bool>
   294 {
   295     bool operator () ( const Pair &p, const string& value ) const
   296       { return p.second == value; }
   297 };
   298 
   299 void AlgTest::find_first_of_nsc_test()
   300 {
   301   // Non-symmetrical comparator
   302 
   303   map<int, string> m;
   304   vector<string> values;
   305 
   306   m[1] = "one";
   307   m[4] = "four";
   308   m[10] = "ten";
   309   m[20] = "twenty";
   310 
   311   values.push_back( "four" );
   312   values.push_back( "ten" );
   313 
   314   map<int, string>::iterator i = find_first_of(m.begin(), m.end(), values.begin(), values.end(), ValueFinder());
   315 
   316   CPPUNIT_ASSERT( i != m.end() );
   317   CPPUNIT_ASSERT( i->first == 4 || i->first == 10 );
   318   CPPUNIT_ASSERT( i->second == "four" || i->second == "ten" );
   319 }
   320 void AlgTest::alg_cov()
   321 	{
   322 		{
   323 		int myints[]={10,20,30,30,20,10,10,20};
   324 		int fwditer;
   325 		
   326 		vector<int> myvector (myints,myints+8);
   327 
   328 		vector<int>::iterator it;
   329 		it = search_n (myvector.begin(), myvector.end(), 2, 30);
   330 		fwditer = int(it-myvector.begin()) ;
   331 		CPPUNIT_ASSERT( fwditer == 2 );
   332 		
   333 		it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
   334 		fwditer = int(it-myvector.begin()) ;
   335 		CPPUNIT_ASSERT( fwditer == 5 );	
   336 		
   337 		// end()-10 goes back to the 2 positions before begin() as the vector contains only 
   338 		// 8 elements so fwditer contains the difference of locations i.e., -2
   339 		it = search_n (myvector.begin(), myvector.end()-10, 2, 30);
   340 		fwditer = int(it-myvector.begin()) ;
   341 		CPPUNIT_ASSERT( fwditer == -2 );
   342 		}
   343 		{
   344 		int N = 10;
   345 		int n=4;
   346 		int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   347 		int B[4];
   348 	
   349 		random_sample(A, A+N, B, B+n); // generates the random numbers
   350 
   351 		}
   352 		{
   353 		int A[] = {1, 4, 2, 8, 5, 7};
   354 		const int N = sizeof(A) / sizeof(int);
   355 
   356 		CPPUNIT_ASSERT(!is_sorted(A, A + N));
   357 		sort(A, A + N);
   358 		CPPUNIT_ASSERT(is_sorted(A, A + N));
   359 		}
   360 	}