genericopenlibs/cppstdlib/stl/test/unit/bitset_test.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 <e32std.h>
       
    17 #include <bitset>
       
    18 #include <algorithm>
       
    19 #include <sstream>
       
    20 #include <iostream>
       
    21 #include "cppunit/cppunit_proxy.h"
       
    22 
       
    23 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    24 using namespace std;
       
    25 #endif
       
    26 
       
    27 //
       
    28 // TestCase class
       
    29 //
       
    30 class BitsetTest : public CPPUNIT_NS::TestCase
       
    31 {
       
    32   CPPUNIT_TEST_SUITE(BitsetTest);
       
    33   CPPUNIT_TEST(bitset1);
       
    34   CPPUNIT_TEST(iostream);
       
    35   CPPUNIT_TEST(bitset_cov1);
       
    36   CPPUNIT_TEST(bitset_cov2);
       
    37   CPPUNIT_TEST(bitset_cov3);
       
    38   CPPUNIT_TEST_SUITE_END();
       
    39 
       
    40 protected:
       
    41   void bitset1();
       
    42   void iostream();
       
    43   void bitset_cov1();
       
    44   void bitset_cov2();
       
    45   void bitset_cov3();
       
    46 };
       
    47 
       
    48 CPPUNIT_TEST_SUITE_REGISTRATION(BitsetTest);
       
    49 
       
    50 //
       
    51 // tests implementation
       
    52 //
       
    53 void BitsetTest::bitset1()
       
    54 {
       
    55   bitset<13U> b1(0xFFFF);
       
    56   bitset<13U> b2(0x1111);
       
    57   CPPUNIT_ASSERT(b1.size()==13);
       
    58   CPPUNIT_ASSERT(b1==0x1FFF);
       
    59   CPPUNIT_ASSERT(b2.size()==13);
       
    60   CPPUNIT_ASSERT(b2==0x1111);
       
    61 
       
    62   b1 = b1^(b2<<2);
       
    63   CPPUNIT_ASSERT(b1==0x1BBB);
       
    64 
       
    65   CPPUNIT_ASSERT(b1.count()==10);
       
    66   CPPUNIT_ASSERT(b2.count()==4);
       
    67 
       
    68 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
       
    69   size_t __pos = b2._Find_first();
       
    70   CPPUNIT_ASSERT( __pos == 0 );
       
    71   __pos = b2._Find_next(__pos);
       
    72   CPPUNIT_ASSERT( __pos == 4 );
       
    73   __pos = b2._Find_next(__pos);
       
    74   CPPUNIT_ASSERT( __pos == 8 );
       
    75   __pos = b2._Find_next(__pos);
       
    76   CPPUNIT_ASSERT( __pos == 12 );
       
    77   __pos = b2._Find_next(__pos);
       
    78   CPPUNIT_ASSERT( __pos == 13 );
       
    79 #endif
       
    80 
       
    81 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
       
    82   string representation = b2.to_string<char, char_traits<char>, allocator<char> >();
       
    83   CPPUNIT_ASSERT( representation == "1000100010001" );
       
    84 #  if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
       
    85   wstring wrepresentation = b2.to_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >();
       
    86   CPPUNIT_ASSERT( wrepresentation == L"1000100010001" );
       
    87 #  endif
       
    88 #else
       
    89   CPPUNIT_ASSERT( b2.to_string() == "1000100010001" );
       
    90 #endif
       
    91 }
       
    92 
       
    93 void BitsetTest::iostream()
       
    94 {
       
    95   {
       
    96     stringstream sstr;
       
    97     bitset<13U> b(0x1111);
       
    98     sstr << b;
       
    99     CPPUNIT_ASSERT( sstr.str() == "1000100010001" );
       
   100 
       
   101     bitset<13U> b1;
       
   102     sstr >> b1;
       
   103     CPPUNIT_ASSERT( b1.test(0) );
       
   104     CPPUNIT_ASSERT( b1.test(4) );
       
   105     CPPUNIT_ASSERT( b1.test(8) );
       
   106     CPPUNIT_ASSERT( b1.test(12) );
       
   107   }
       
   108 #if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
       
   109   {
       
   110     wstringstream sstr;
       
   111     bitset<13U> b(0x1111);
       
   112     sstr << b;
       
   113     CPPUNIT_ASSERT( sstr.str() == L"1000100010001" );
       
   114 
       
   115     bitset<13U> b1;
       
   116     sstr >> b1;
       
   117     CPPUNIT_ASSERT( b1.test(0) );
       
   118     CPPUNIT_ASSERT( b1.test(4) );
       
   119     CPPUNIT_ASSERT( b1.test(8) );
       
   120     CPPUNIT_ASSERT( b1.test(12) );
       
   121   }
       
   122 #endif
       
   123 }
       
   124 void BitsetTest::bitset_cov1()
       
   125 	{
       
   126 	__UHEAP_MARK;
       
   127 		{
       
   128 		bitset<5> b1 ( 6 );
       
   129 	    bitset<5> rb1;
       
   130 	    
       
   131 		CPPUNIT_ASSERT( b1.any() );
       
   132 		CPPUNIT_ASSERT( !b1.none() );
       
   133 		rb1 = b1.reset ( );
       
   134 	    CPPUNIT_ASSERT( !rb1.any() );
       
   135 	    CPPUNIT_ASSERT( rb1.none() );
       
   136 		}
       
   137 		{
       
   138 		bitset<5> b1 ( 7 );
       
   139 		unsigned long int i;
       
   140 		i = b1.to_ulong( );
       
   141 		CPPUNIT_ASSERT( i == 7 );
       
   142 		}
       
   143 		{
       
   144 		bitset<5> b1 ( 6 );
       
   145 		bitset<5> bs1;
       
   146 		bitset<5> b1s0;
       
   147 		unsigned long int i;
       
   148 		b1s0 = b1.set( 0 );
       
   149 		i = b1s0.to_ulong( );
       
   150 		CPPUNIT_ASSERT( i == 7 );
       
   151 
       
   152 		bs1 = b1.set( );
       
   153 		i = bs1.to_ulong( );
       
   154 		CPPUNIT_ASSERT( i == 31 );
       
   155 		}
       
   156 		{
       
   157 		bitset<5> b1 ( 6 );
       
   158 		bitset<5> fb1;
       
   159 		bitset<5> f3b1;
       
   160 		unsigned long int i;
       
   161 		
       
   162 		fb1 = b1.flip ( );
       
   163 		i = fb1.to_ulong( );
       
   164 		CPPUNIT_ASSERT( i == 25 );	
       
   165 		
       
   166 		f3b1 = b1.flip ( 3 );
       
   167 		i = f3b1.to_ulong( );
       
   168 		CPPUNIT_ASSERT( i == 17 );	
       
   169 		}
       
   170 		{
       
   171 		bitset<5> b1 ( 2 );
       
   172  	    bitset<5> b2 ( 6 );
       
   173  	    
       
   174  	    //x [i] = bool
       
   175  	    b1[ 0 ] = true;
       
   176  	    CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
       
   177  	    
       
   178  	    //x [i] = y [j]
       
   179  	    b2 [4] = b1 [0];
       
   180  	    CPPUNIT_ASSERT( b2.to_ulong( ) == 22 );
       
   181  	    
       
   182         //b = ~x [i]
       
   183  	    bool b = ~b2 [4];
       
   184  	    CPPUNIT_ASSERT( b == false );
       
   185 
       
   186  	    //b = x [i]
       
   187  	    b = b2 [4];
       
   188 	    CPPUNIT_ASSERT( b == true );
       
   189 
       
   190 	    //x [i] . flip ( )
       
   191 	    b2 [4].flip( );
       
   192 	    CPPUNIT_ASSERT( b2.to_ulong( ) == 6 );
       
   193 		}
       
   194 		__UHEAP_MARKEND;
       
   195 	}
       
   196 void BitsetTest::bitset_cov2()
       
   197 	{
       
   198 	__UHEAP_MARK;
       
   199 		{
       
   200 		bitset<5> b1 ( 7 );
       
   201 		bitset<5> b2;
       
   202 		unsigned long int i;
       
   203 		b2 = ~b1;
       
   204 		i = b2.to_ulong( );
       
   205 		CPPUNIT_ASSERT( i == 24 );
       
   206 		}
       
   207 		{
       
   208 		bitset<5> b1 ( 7 );
       
   209 		bitset<5> b2 ( 11 );
       
   210 		unsigned long int i;
       
   211 		b1 |= b2;
       
   212 		i = b1.to_ulong( );
       
   213 		CPPUNIT_ASSERT( i == 15 );
       
   214 		i = b2.to_ulong( );
       
   215 		CPPUNIT_ASSERT( i == 11 );
       
   216 		}
       
   217 		{
       
   218 		bool b;
       
   219 		bitset<2> b1 ( 2 );
       
   220 		bitset<2> b2;
       
   221 		b = b1[ 0 ];
       
   222 		CPPUNIT_ASSERT( b == 0 );
       
   223 		b = b1[ 1 ];
       
   224 		CPPUNIT_ASSERT( b == 1 );
       
   225 		b2[0] = b1[0];
       
   226 	    b2[1] = b1[1];
       
   227 	    CPPUNIT_ASSERT(b1 == b2);
       
   228 		}
       
   229 		{
       
   230 		bitset<5> b1 ( 28 );
       
   231 		b1 >>= 2;
       
   232 		CPPUNIT_ASSERT( b1.to_ulong( ) == 7 );
       
   233 		}
       
   234 		{
       
   235 		bitset<5> b1 ( 28 );
       
   236 		bitset<5> b3 = (b1 >> 1);
       
   237 		CPPUNIT_ASSERT( b3.to_ulong( ) == 14 );
       
   238 		}
       
   239 		{
       
   240 		bitset<5> b1 ( 7 );
       
   241 		bitset<5> b2 ( 11 );
       
   242 		b1 &= b2;
       
   243 		CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
       
   244 		CPPUNIT_ASSERT( b2.to_ulong( ) == 11 );
       
   245 		}
       
   246 		{
       
   247 		bitset<5> b1 ( 7 );
       
   248 		bitset<5> b2 ( 2 );
       
   249 		CPPUNIT_ASSERT( b1 != b2 );
       
   250 		}
       
   251 		__UHEAP_MARKEND;
       
   252 	}
       
   253 void BitsetTest::bitset_cov3()
       
   254 	{
       
   255 	__UHEAP_MARK;
       
   256 		{
       
   257 		bitset<5> b1 ( 13 );
       
   258 		bitset<5> b1r3;
       
   259 		b1r3 = b1.reset( 2 );
       
   260 		CPPUNIT_ASSERT( b1r3.to_ulong( ) == 9 );
       
   261 		}
       
   262 		{
       
   263 		string bitval1 ( "00011" );
       
   264 		bitset<5> b1 ( bitval1 );
       
   265 		CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
       
   266 		string bitval2 ("11110011011");
       
   267 		bitset<6> b2 ( bitval2, 3, 6 );
       
   268 		CPPUNIT_ASSERT( b2.to_ulong( ) == 38 );
       
   269 		bitset<9> b3 ( bitval2, 2 );
       
   270 		CPPUNIT_ASSERT( b3.to_ulong( ) == 411 );
       
   271 		}
       
   272 		{
       
   273 		bitset<5> b1 ( 7 );
       
   274 		bitset<5> b2 ( 11 );
       
   275 		unsigned long int i;
       
   276 		b1 = b1 | b2;
       
   277 		i = b1.to_ulong( );
       
   278 		CPPUNIT_ASSERT( i == 15 );
       
   279 		i = b2.to_ulong( );
       
   280 		CPPUNIT_ASSERT( i == 11 );
       
   281 		}
       
   282 		{
       
   283 		bitset<5> b1 ( 7 );
       
   284 		bitset<5> b2 ( 11 );
       
   285 		b1 = b1 & b2;
       
   286 		CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
       
   287 		CPPUNIT_ASSERT( b2.to_ulong( ) == 11 );
       
   288 		}	
       
   289 		__UHEAP_MARKEND;
       
   290 	}