genericopenlibs/cppstdlib/stl/test/unit/count_test.cpp
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <algorithm>
       
    18 #include <vector>
       
    19 #include "cppunit/cppunit_proxy.h"
       
    20 
       
    21 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    22 using namespace std;
       
    23 #endif
       
    24 
       
    25 //
       
    26 // TestCase class
       
    27 //
       
    28 class CountTest : public CPPUNIT_NS::TestCase
       
    29 {
       
    30   CPPUNIT_TEST_SUITE(CountTest);
       
    31   CPPUNIT_TEST(count0);
       
    32   CPPUNIT_TEST(count1);
       
    33   CPPUNIT_TEST(countif1);
       
    34   CPPUNIT_TEST_SUITE_END();
       
    35 
       
    36 protected:
       
    37   void count0();
       
    38   void count1();
       
    39   void countif1();
       
    40   static int odd(int a_);
       
    41 };
       
    42 
       
    43 CPPUNIT_TEST_SUITE_REGISTRATION(CountTest);
       
    44 
       
    45 //
       
    46 // tests implementation
       
    47 //
       
    48 void CountTest::count0()
       
    49 {
       
    50   int numbers[10] = { 1, 2, 4, 1, 2, 4, 1, 2, 4, 1 };
       
    51 
       
    52   int result = count(numbers, numbers + 10, 1);
       
    53   CPPUNIT_ASSERT(result==4);
       
    54 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
       
    55   result = 0;
       
    56   count(numbers, numbers + 10, 1, result);
       
    57   CPPUNIT_ASSERT(result==4);
       
    58 #endif
       
    59 }
       
    60 void CountTest::count1()
       
    61 {
       
    62   vector <int> numbers(100);
       
    63   for(int i = 0; i < 100; i++)
       
    64   numbers[i] = i % 3;
       
    65   int elements = count(numbers.begin(), numbers.end(), 2);
       
    66   CPPUNIT_ASSERT(elements==33);
       
    67 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
       
    68   elements = 0;
       
    69   count(numbers.begin(), numbers.end(), 2, elements);
       
    70   CPPUNIT_ASSERT(elements==33);
       
    71 #endif
       
    72 }
       
    73 void CountTest::countif1()
       
    74 {
       
    75   vector <int> numbers(100);
       
    76   for(int i = 0; i < 100; i++)
       
    77     numbers[i] = i % 3;
       
    78   int elements = count_if(numbers.begin(), numbers.end(), odd);
       
    79   CPPUNIT_ASSERT(elements==33);
       
    80 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
       
    81   elements = 0;
       
    82   count_if(numbers.begin(), numbers.end(), odd, elements);
       
    83   CPPUNIT_ASSERT(elements==33);
       
    84 #endif
       
    85 }
       
    86 int CountTest::odd(int a_)
       
    87 {
       
    88   return a_ % 2;
       
    89 }