00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef UTILITYSTRING_H
00020 #define UTILITYSTRING_H
00021
00022 #include <iostream>
00023 #include <iomanip>
00024 #include <bit/bit.h>
00025 #include <cppunit/TestFixture.h>
00026
00027 using namespace bit;
00028
00029 class UtilityString : public CppUnit::TestFixture {
00030 public:
00031
00032 CPPUNIT_TEST_SUITE( UtilityString );
00033 CPPUNIT_TEST( hex_string_uppercase );
00034 CPPUNIT_TEST( hex_string_lowercase );
00035 CPPUNIT_TEST( hex_string_w_separator );
00036 CPPUNIT_TEST( binary_string_wo_separator );
00037 CPPUNIT_TEST( binary_string_w_separator8 );
00038 CPPUNIT_TEST( binary_string_w_separator4 );
00039 CPPUNIT_TEST( binary_string_w_separator2 );
00040 CPPUNIT_TEST( binary_string_w_separator1 );
00041 CPPUNIT_TEST( binary_string_w_separator0 );
00042 CPPUNIT_TEST_SUITE_END();
00043
00044 public:
00045 void setUp() { }
00046
00047 void tearDown() { }
00048
00049 void hex_string_uppercase() { CPPUNIT_ASSERT( std::string("0x123456789ABCDEF1") == hex_string_test(0x123456789ABCDEF1ULL) ); }
00050 void hex_string_lowercase() { CPPUNIT_ASSERT( std::string("0x123456789abcdef1") == hex_string_test(0x123456789ABCDEF1ULL, false) ); }
00051 void hex_string_w_separator() { CPPUNIT_ASSERT( std::string("0x12 34 56 78 9A BC DE F1") == hex_string_test(0x123456789ABCDEF1ULL, true, " ") ); }
00052
00053 std::string hex_string_test(uint64_t x, bool uppercase=true, std::string separator="") {
00054 x = host_to_net(x);
00055 std::string s = hex_string(&x, sizeof(uint64_t), uppercase, separator );
00056
00057 return s;
00058 }
00059
00060 void binary_string_wo_separator() { CPPUNIT_ASSERT( std::string("0001001000110100010101100111100010011010101111001101111011110001") == binary_string_test(0x123456789ABCDEF1ULL) ); }
00061 void binary_string_w_separator8() { CPPUNIT_ASSERT( std::string("00010010 00110100 01010110 01111000 10011010 10111100 11011110 11110001") == binary_string_test(0x123456789ABCDEF1ULL, " ") ); }
00062 void binary_string_w_separator4() { CPPUNIT_ASSERT( std::string("0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0001") == binary_string_test(0x123456789ABCDEF1ULL, " ", 4) ); }
00063 void binary_string_w_separator2() { CPPUNIT_ASSERT( std::string("00 01 00 10 00 11 01 00 01 01 01 10 01 11 10 00 10 01 10 10 10 11 11 00 11 01 11 10 11 11 00 01") == binary_string_test(0x123456789ABCDEF1ULL, " ", 2) ); }
00064 void binary_string_w_separator1() { CPPUNIT_ASSERT( std::string("0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 1") == binary_string_test(0x123456789ABCDEF1ULL, " ", 1) ); }
00065 void binary_string_w_separator0() { CPPUNIT_ASSERT( std::string("0001001000110100010101100111100010011010101111001101111011110001") == binary_string_test(0x123456789ABCDEF1ULL, " ", 0) ); }
00066
00067 std::string binary_string_test(uint64_t x, std::string separator="", size_t separator_digits=8) {
00068 x = host_to_net(x);
00069 std::string s = binary_string(&x, sizeof(uint64_t), separator, separator_digits );
00070
00071 return s;
00072 }
00073 };
00074
00075 #endif