1 #include <bitset>
2 #include <algorithm>
3 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
4 #  include <sstream>
5 #endif
6 
7 #include "cppunit/cppunit_proxy.h"
8 
9 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
10 using namespace std;
11 #endif
12 
13 //
14 // TestCase class
15 //
16 class BitsetTest : public CPPUNIT_NS::TestCase
17 {
18   CPPUNIT_TEST_SUITE(BitsetTest);
19   CPPUNIT_TEST(bitset1);
20 #if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS)
21   CPPUNIT_IGNORE;
22 #endif
23   CPPUNIT_TEST(iostream);
24   CPPUNIT_TEST_SUITE_END();
25 
26 protected:
27   void bitset1();
28   void iostream();
29 };
30 
31 CPPUNIT_TEST_SUITE_REGISTRATION(BitsetTest);
32 
33 //
34 // tests implementation
35 //
bitset1()36 void BitsetTest::bitset1()
37 {
38   bitset<13U> b1(0xFFFF);
39   bitset<13U> b2(0x1111);
40   CPPUNIT_ASSERT(b1.size() == 13);
41   CPPUNIT_ASSERT(b1 == 0x1FFF);
42   CPPUNIT_ASSERT(b2.size() == 13);
43   CPPUNIT_ASSERT(b2 == 0x1111);
44 
45 #if !defined (STLPORT) || !defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
46   b1 = b1 ^ (b2 << 2);
47   CPPUNIT_ASSERT(b1 == 0x1BBB);
48 
49   CPPUNIT_ASSERT(b1.count() == 10);
50   CPPUNIT_ASSERT(b2.count() == 4);
51 
52 #  if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
53   size_t __pos = b2._Find_first();
54   CPPUNIT_ASSERT( __pos == 0 );
55   __pos = b2._Find_next(__pos);
56   CPPUNIT_ASSERT( __pos == 4 );
57   __pos = b2._Find_next(__pos);
58   CPPUNIT_ASSERT( __pos == 8 );
59   __pos = b2._Find_next(__pos);
60   CPPUNIT_ASSERT( __pos == 12 );
61   __pos = b2._Find_next(__pos);
62   CPPUNIT_ASSERT( __pos == 13 );
63 #  endif
64 #endif
65 
66 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
67   string representation = b2.to_string<char, char_traits<char>, allocator<char> >();
68   CPPUNIT_ASSERT( representation == "1000100010001" );
69 #  if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
70   wstring wrepresentation = b2.to_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >();
71   CPPUNIT_ASSERT( wrepresentation == L"1000100010001" );
72 #  endif
73 #else
74   CPPUNIT_ASSERT( b2.to_string() == "1000100010001" );
75 #endif
76 }
77 
iostream()78 void BitsetTest::iostream()
79 {
80 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
81   {
82     stringstream sstr;
83     bitset<13U> b(0x1111);
84     sstr << b;
85     CPPUNIT_ASSERT( sstr.str() == "1000100010001" );
86 
87     bitset<13U> b1;
88     sstr >> b1;
89     CPPUNIT_ASSERT( b1.test(0) );
90     CPPUNIT_ASSERT( b1.test(4) );
91     CPPUNIT_ASSERT( b1.test(8) );
92     CPPUNIT_ASSERT( b1.test(12) );
93   }
94 #  if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T)
95   {
96     wstringstream sstr;
97     bitset<13U> b(0x1111);
98     sstr << b;
99     CPPUNIT_ASSERT( sstr.str() == L"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 #  endif
109 #endif
110 }
111