os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/alg_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/alg_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,360 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <list>
    1.20 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
    1.21 +#  include <slist>
    1.22 +#endif
    1.23 +#include <deque>
    1.24 +#include <vector>
    1.25 +#include <algorithm>
    1.26 +#include <functional>
    1.27 +#include <map>
    1.28 +#include <string>
    1.29 +
    1.30 +#include "cppunit/cppunit_proxy.h"
    1.31 +
    1.32 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.33 +using namespace std;
    1.34 +#endif
    1.35 +
    1.36 +//
    1.37 +// TestCase class
    1.38 +//
    1.39 +class AlgTest : public CPPUNIT_NS::TestCase
    1.40 +{
    1.41 +  CPPUNIT_TEST_SUITE(AlgTest);
    1.42 +  CPPUNIT_TEST(min_max);
    1.43 +  CPPUNIT_TEST(count_test);
    1.44 +  CPPUNIT_TEST(sort_test);
    1.45 +  CPPUNIT_TEST(search_n_test);
    1.46 +  CPPUNIT_TEST(find_first_of_test);
    1.47 +  CPPUNIT_TEST(find_first_of_nsc_test);
    1.48 +  CPPUNIT_TEST(alg_cov);
    1.49 +  CPPUNIT_TEST_SUITE_END();
    1.50 +
    1.51 +protected:
    1.52 +  void min_max();
    1.53 +  void count_test();
    1.54 +  void sort_test();
    1.55 +  void search_n_test();
    1.56 +  void find_first_of_test();
    1.57 +  void find_first_of_nsc_test();
    1.58 +  void alg_cov();
    1.59 +};
    1.60 +
    1.61 +CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest);
    1.62 +
    1.63 +//
    1.64 +// tests implementation
    1.65 +//
    1.66 +bool mypredicate (int i, int j) 
    1.67 +	{
    1.68 +	return (i==j);
    1.69 +	}
    1.70 +void AlgTest::min_max()
    1.71 +{
    1.72 +  int i = min(4, 7);
    1.73 +  CPPUNIT_ASSERT( i == 4 );
    1.74 +  char c = max('a', 'z');
    1.75 +  CPPUNIT_ASSERT( c == 'z' );
    1.76 +
    1.77 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
    1.78 +  c = min('a', 'z', greater<char>());
    1.79 +  CPPUNIT_ASSERT( c == 'z' );
    1.80 +  i = max(4, 7, greater<int>());
    1.81 +  CPPUNIT_ASSERT( i == 4 );
    1.82 +#endif
    1.83 +}
    1.84 +
    1.85 +void AlgTest::count_test()
    1.86 +{
    1.87 +  {
    1.88 +    int i[] = { 1, 4, 2, 8, 2, 2 };
    1.89 +    int n = count(i, i + 6, 2);
    1.90 +    CPPUNIT_ASSERT(n==3);
    1.91 +#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
    1.92 +    n = 0;
    1.93 +    count(i, i + 6, 2, n);
    1.94 +    CPPUNIT_ASSERT(n==3);
    1.95 +#endif
    1.96 +  }
    1.97 +  {
    1.98 +    vector<int> i;
    1.99 +    i.push_back(1);
   1.100 +    i.push_back(4);
   1.101 +    i.push_back(2);
   1.102 +    i.push_back(8);
   1.103 +    i.push_back(2);
   1.104 +    i.push_back(2);
   1.105 +    int n = count(i.begin(), i.end(), 2);
   1.106 +    CPPUNIT_ASSERT(n==3);
   1.107 +#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
   1.108 +    n = 0;
   1.109 +    count(i.begin(), i.end(), 2, n);
   1.110 +    CPPUNIT_ASSERT(n==3);
   1.111 +#endif
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +void AlgTest::sort_test()
   1.116 +{
   1.117 +  {
   1.118 +    vector<int> years;
   1.119 +    years.push_back(1962);
   1.120 +    years.push_back(1992);
   1.121 +    years.push_back(2001);
   1.122 +    years.push_back(1999);
   1.123 +    sort(years.begin(), years.end());
   1.124 +    CPPUNIT_ASSERT(years[0]==1962);
   1.125 +    CPPUNIT_ASSERT(years[1]==1992);
   1.126 +    CPPUNIT_ASSERT(years[2]==1999);
   1.127 +    CPPUNIT_ASSERT(years[3]==2001);
   1.128 +  }
   1.129 +  {
   1.130 +    deque<int> years;
   1.131 +    years.push_back(1962);
   1.132 +    years.push_back(1992);
   1.133 +    years.push_back(2001);
   1.134 +    years.push_back(1999);
   1.135 +    sort(years.begin(), years.end()); // <-- changed!
   1.136 +    CPPUNIT_ASSERT(years[0]==1962);
   1.137 +    CPPUNIT_ASSERT(years[1]==1992);
   1.138 +    CPPUNIT_ASSERT(years[2]==1999);
   1.139 +    CPPUNIT_ASSERT(years[3]==2001);
   1.140 +  }
   1.141 +}
   1.142 +
   1.143 +#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
   1.144 +
   1.145 +void AlgTest::search_n_test()
   1.146 +{
   1.147 +  int ints[] = {0, 1, 2, 3, 3, 4, 4, 4, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
   1.148 +
   1.149 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   1.150 +  //search_n
   1.151 +  //Forward iterator
   1.152 +  {
   1.153 +    slist<int> slint(ints, ints + ARRAY_SIZE(ints));
   1.154 +    slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 2);
   1.155 +    CPPUNIT_ASSERT( slit != slint.end() );
   1.156 +    CPPUNIT_ASSERT( *(slit++) == 2 );
   1.157 +    CPPUNIT_ASSERT( *slit == 2 );
   1.158 +  }
   1.159 +#endif
   1.160 +
   1.161 +  //Bidirectionnal iterator
   1.162 +  {
   1.163 +    list<int> lint(ints, ints + ARRAY_SIZE(ints));
   1.164 +    list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 3);
   1.165 +    CPPUNIT_ASSERT( lit != lint.end() );
   1.166 +    CPPUNIT_ASSERT( *(lit++) == 3 );
   1.167 +    CPPUNIT_ASSERT( *(lit++) == 3 );
   1.168 +    CPPUNIT_ASSERT( *lit == 3 );
   1.169 +  }
   1.170 +
   1.171 +  //Random access iterator
   1.172 +  {
   1.173 +    deque<int> dint(ints, ints + ARRAY_SIZE(ints));
   1.174 +    deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 4);
   1.175 +    CPPUNIT_ASSERT( dit != dint.end() );
   1.176 +    CPPUNIT_ASSERT( *(dit++) == 4 );
   1.177 +    CPPUNIT_ASSERT( *(dit++) == 4 );
   1.178 +    CPPUNIT_ASSERT( *(dit++) == 4 );
   1.179 +    CPPUNIT_ASSERT( *dit == 4 );
   1.180 +  }
   1.181 +
   1.182 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   1.183 +  //search_n with predicate
   1.184 +  //Forward iterator
   1.185 +  {
   1.186 +    slist<int> slint(ints, ints + ARRAY_SIZE(ints));
   1.187 +    slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 1, greater<int>());
   1.188 +    CPPUNIT_ASSERT( slit != slint.end() );
   1.189 +    CPPUNIT_ASSERT( *(slit++) > 1 );
   1.190 +    CPPUNIT_ASSERT( *slit > 2 );
   1.191 +  }
   1.192 +#endif
   1.193 +
   1.194 +  //Bidirectionnal iterator
   1.195 +  {
   1.196 +    list<int> lint(ints, ints + ARRAY_SIZE(ints));
   1.197 +    list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 2, greater<int>());
   1.198 +    CPPUNIT_ASSERT( lit != lint.end() );
   1.199 +    CPPUNIT_ASSERT( *(lit++) > 2 );
   1.200 +    CPPUNIT_ASSERT( *(lit++) > 2 );
   1.201 +    CPPUNIT_ASSERT( *lit > 2 );
   1.202 +  }
   1.203 +
   1.204 +  //Random access iterator
   1.205 +  {
   1.206 +    deque<int> dint(ints, ints + ARRAY_SIZE(ints));
   1.207 +    deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 3, greater<int>());
   1.208 +    CPPUNIT_ASSERT( dit != dint.end() );
   1.209 +    CPPUNIT_ASSERT( *(dit++) > 3 );
   1.210 +    CPPUNIT_ASSERT( *(dit++) > 3 );
   1.211 +    CPPUNIT_ASSERT( *(dit++) > 3 );
   1.212 +    CPPUNIT_ASSERT( *dit > 3 );
   1.213 +  }
   1.214 +
   1.215 +  // test for bug reported by Jim Xochellis
   1.216 +  {
   1.217 +    int array[] = {0, 0, 1, 0, 1, 1};
   1.218 +    int* array_end = array + sizeof(array) / sizeof(*array);
   1.219 +    CPPUNIT_ASSERT(search_n(array, array_end, 3, 1) == array_end);
   1.220 +  }
   1.221 +
   1.222 +  // test for bug with counter == 1, reported by Timmie Smith
   1.223 +  {
   1.224 +    int array[] = {0, 1, 2, 3, 4, 5};
   1.225 +    int* array_end = array + sizeof(array) / sizeof(*array);
   1.226 +    CPPUNIT_ASSERT( search_n(array, array_end, 1, 1, equal_to<int>() ) == &array[1] );
   1.227 +  }
   1.228 +}
   1.229 +
   1.230 +void AlgTest::find_first_of_test()
   1.231 +{
   1.232 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   1.233 +  slist<int> intsl;
   1.234 +  intsl.push_front(1);
   1.235 +  intsl.push_front(2);
   1.236 +
   1.237 +  {
   1.238 +    vector<int> intv;
   1.239 +    intv.push_back(0);
   1.240 +    intv.push_back(1);
   1.241 +    intv.push_back(2);
   1.242 +    intv.push_back(3);
   1.243 +
   1.244 +    vector<int>::iterator first;
   1.245 +    first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
   1.246 +    CPPUNIT_ASSERT( first != intv.end() );
   1.247 +    CPPUNIT_ASSERT( *first == 1 );
   1.248 +  }
   1.249 +  {
   1.250 +    vector<int> intv;
   1.251 +    intv.push_back(3);
   1.252 +    intv.push_back(2);
   1.253 +    intv.push_back(1);
   1.254 +    intv.push_back(0);
   1.255 +
   1.256 +    vector<int>::iterator first;
   1.257 +    first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end());
   1.258 +    CPPUNIT_ASSERT( first != intv.end() );
   1.259 +    CPPUNIT_ASSERT( *first == 2 );
   1.260 +  }
   1.261 +#endif
   1.262 +
   1.263 +  list<int> intl;
   1.264 +  intl.push_front(1);
   1.265 +  intl.push_front(2);
   1.266 +
   1.267 +  {
   1.268 +    vector<int> intv;
   1.269 +    intv.push_back(0);
   1.270 +    intv.push_back(1);
   1.271 +    intv.push_back(2);
   1.272 +    intv.push_back(3);
   1.273 +
   1.274 +    vector<int>::iterator first;
   1.275 +    first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
   1.276 +    CPPUNIT_ASSERT( first != intv.end() );
   1.277 +    CPPUNIT_ASSERT( *first == 1 );
   1.278 +  }
   1.279 +  {
   1.280 +    vector<int> intv;
   1.281 +    intv.push_back(3);
   1.282 +    intv.push_back(2);
   1.283 +    intv.push_back(1);
   1.284 +    intv.push_back(0);
   1.285 +
   1.286 +    vector<int>::iterator first;
   1.287 +    first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end());
   1.288 +    CPPUNIT_ASSERT( first != intv.end() );
   1.289 +    CPPUNIT_ASSERT( *first == 2 );
   1.290 +  }
   1.291 +}
   1.292 +
   1.293 +typedef pair<int, string> Pair;
   1.294 +
   1.295 +struct ValueFinder :
   1.296 +    public binary_function<const Pair&, const string&, bool>
   1.297 +{
   1.298 +    bool operator () ( const Pair &p, const string& value ) const
   1.299 +      { return p.second == value; }
   1.300 +};
   1.301 +
   1.302 +void AlgTest::find_first_of_nsc_test()
   1.303 +{
   1.304 +  // Non-symmetrical comparator
   1.305 +
   1.306 +  map<int, string> m;
   1.307 +  vector<string> values;
   1.308 +
   1.309 +  m[1] = "one";
   1.310 +  m[4] = "four";
   1.311 +  m[10] = "ten";
   1.312 +  m[20] = "twenty";
   1.313 +
   1.314 +  values.push_back( "four" );
   1.315 +  values.push_back( "ten" );
   1.316 +
   1.317 +  map<int, string>::iterator i = find_first_of(m.begin(), m.end(), values.begin(), values.end(), ValueFinder());
   1.318 +
   1.319 +  CPPUNIT_ASSERT( i != m.end() );
   1.320 +  CPPUNIT_ASSERT( i->first == 4 || i->first == 10 );
   1.321 +  CPPUNIT_ASSERT( i->second == "four" || i->second == "ten" );
   1.322 +}
   1.323 +void AlgTest::alg_cov()
   1.324 +	{
   1.325 +		{
   1.326 +		int myints[]={10,20,30,30,20,10,10,20};
   1.327 +		int fwditer;
   1.328 +		
   1.329 +		vector<int> myvector (myints,myints+8);
   1.330 +
   1.331 +		vector<int>::iterator it;
   1.332 +		it = search_n (myvector.begin(), myvector.end(), 2, 30);
   1.333 +		fwditer = int(it-myvector.begin()) ;
   1.334 +		CPPUNIT_ASSERT( fwditer == 2 );
   1.335 +		
   1.336 +		it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
   1.337 +		fwditer = int(it-myvector.begin()) ;
   1.338 +		CPPUNIT_ASSERT( fwditer == 5 );	
   1.339 +		
   1.340 +		// end()-10 goes back to the 2 positions before begin() as the vector contains only 
   1.341 +		// 8 elements so fwditer contains the difference of locations i.e., -2
   1.342 +		it = search_n (myvector.begin(), myvector.end()-10, 2, 30);
   1.343 +		fwditer = int(it-myvector.begin()) ;
   1.344 +		CPPUNIT_ASSERT( fwditer == -2 );
   1.345 +		}
   1.346 +		{
   1.347 +		int N = 10;
   1.348 +		int n=4;
   1.349 +		int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   1.350 +		int B[4];
   1.351 +	
   1.352 +		random_sample(A, A+N, B, B+n); // generates the random numbers
   1.353 +
   1.354 +		}
   1.355 +		{
   1.356 +		int A[] = {1, 4, 2, 8, 5, 7};
   1.357 +		const int N = sizeof(A) / sizeof(int);
   1.358 +
   1.359 +		CPPUNIT_ASSERT(!is_sorted(A, A + N));
   1.360 +		sort(A, A + N);
   1.361 +		CPPUNIT_ASSERT(is_sorted(A, A + N));
   1.362 +		}
   1.363 +	}