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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
21 #include "cppunit/cppunit_proxy.h"
23 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
30 class BitsetTest : public CPPUNIT_NS::TestCase
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();
48 CPPUNIT_TEST_SUITE_REGISTRATION(BitsetTest);
51 // tests implementation
53 void BitsetTest::bitset1()
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);
63 CPPUNIT_ASSERT(b1==0x1BBB);
65 CPPUNIT_ASSERT(b1.count()==10);
66 CPPUNIT_ASSERT(b2.count()==4);
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 );
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" );
89 CPPUNIT_ASSERT( b2.to_string() == "1000100010001" );
93 void BitsetTest::iostream()
97 bitset<13U> b(0x1111);
99 CPPUNIT_ASSERT( sstr.str() == "1000100010001" );
103 CPPUNIT_ASSERT( b1.test(0) );
104 CPPUNIT_ASSERT( b1.test(4) );
105 CPPUNIT_ASSERT( b1.test(8) );
106 CPPUNIT_ASSERT( b1.test(12) );
108 #if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
111 bitset<13U> b(0x1111);
113 CPPUNIT_ASSERT( sstr.str() == L"1000100010001" );
117 CPPUNIT_ASSERT( b1.test(0) );
118 CPPUNIT_ASSERT( b1.test(4) );
119 CPPUNIT_ASSERT( b1.test(8) );
120 CPPUNIT_ASSERT( b1.test(12) );
124 void BitsetTest::bitset_cov1()
131 CPPUNIT_ASSERT( b1.any() );
132 CPPUNIT_ASSERT( !b1.none() );
134 CPPUNIT_ASSERT( !rb1.any() );
135 CPPUNIT_ASSERT( rb1.none() );
141 CPPUNIT_ASSERT( i == 7 );
149 i = b1s0.to_ulong( );
150 CPPUNIT_ASSERT( i == 7 );
154 CPPUNIT_ASSERT( i == 31 );
164 CPPUNIT_ASSERT( i == 25 );
166 f3b1 = b1.flip ( 3 );
167 i = f3b1.to_ulong( );
168 CPPUNIT_ASSERT( i == 17 );
176 CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
180 CPPUNIT_ASSERT( b2.to_ulong( ) == 22 );
184 CPPUNIT_ASSERT( b == false );
188 CPPUNIT_ASSERT( b == true );
192 CPPUNIT_ASSERT( b2.to_ulong( ) == 6 );
196 void BitsetTest::bitset_cov2()
205 CPPUNIT_ASSERT( i == 24 );
213 CPPUNIT_ASSERT( i == 15 );
215 CPPUNIT_ASSERT( i == 11 );
222 CPPUNIT_ASSERT( b == 0 );
224 CPPUNIT_ASSERT( b == 1 );
227 CPPUNIT_ASSERT(b1 == b2);
232 CPPUNIT_ASSERT( b1.to_ulong( ) == 7 );
236 bitset<5> b3 = (b1 >> 1);
237 CPPUNIT_ASSERT( b3.to_ulong( ) == 14 );
243 CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
244 CPPUNIT_ASSERT( b2.to_ulong( ) == 11 );
249 CPPUNIT_ASSERT( b1 != b2 );
253 void BitsetTest::bitset_cov3()
259 b1r3 = b1.reset( 2 );
260 CPPUNIT_ASSERT( b1r3.to_ulong( ) == 9 );
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 );
278 CPPUNIT_ASSERT( i == 15 );
280 CPPUNIT_ASSERT( i == 11 );
286 CPPUNIT_ASSERT( b1.to_ulong( ) == 3 );
287 CPPUNIT_ASSERT( b2.to_ulong( ) == 11 );