genericopenlibs/cppstdlib/stl/test/unit/count_test.cpp
author Pat Downey <patd@symbian.org>
Fri, 04 Jun 2010 16:20:51 +0100
changeset 31 ce057bb09d0b
child 34 5fae379060a7
permissions -rw-r--r--
Revert last code drop.

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/
#include <algorithm>
#include <vector>
#include "cppunit/cppunit_proxy.h"

#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
#endif

//
// TestCase class
//
class CountTest : public CPPUNIT_NS::TestCase
{
  CPPUNIT_TEST_SUITE(CountTest);
  CPPUNIT_TEST(count0);
  CPPUNIT_TEST(count1);
  CPPUNIT_TEST(countif1);
  CPPUNIT_TEST_SUITE_END();

protected:
  void count0();
  void count1();
  void countif1();
  static int odd(int a_);
};

CPPUNIT_TEST_SUITE_REGISTRATION(CountTest);

//
// tests implementation
//
void CountTest::count0()
{
  int numbers[10] = { 1, 2, 4, 1, 2, 4, 1, 2, 4, 1 };

  int result = count(numbers, numbers + 10, 1);
  CPPUNIT_ASSERT(result==4);
#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
  result = 0;
  count(numbers, numbers + 10, 1, result);
  CPPUNIT_ASSERT(result==4);
#endif
}
void CountTest::count1()
{
  vector <int> numbers(100);
  for(int i = 0; i < 100; i++)
  numbers[i] = i % 3;
  int elements = count(numbers.begin(), numbers.end(), 2);
  CPPUNIT_ASSERT(elements==33);
#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
  elements = 0;
  count(numbers.begin(), numbers.end(), 2, elements);
  CPPUNIT_ASSERT(elements==33);
#endif
}
void CountTest::countif1()
{
  vector <int> numbers(100);
  for(int i = 0; i < 100; i++)
    numbers[i] = i % 3;
  int elements = count_if(numbers.begin(), numbers.end(), odd);
  CPPUNIT_ASSERT(elements==33);
#if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS)
  elements = 0;
  count_if(numbers.begin(), numbers.end(), odd, elements);
  CPPUNIT_ASSERT(elements==33);
#endif
}
int CountTest::odd(int a_)
{
  return a_ % 2;
}