utilitystring.h

00001 /***************************************************************************
00002 *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003 *   rvinyard@cs.nmsu.edu                                                  *
00004 *                                                                         *
00005 *   This program is free software; you can redistribute it and/or modify  *
00006 *   it under the terms of the GNU Lesser General Public License as        *
00007 *   published by the Free Software Foundation version 2.1.                *
00008 *                                                                         *
00009 *   This program is distributed in the hope that it will be useful,       *
00010 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012 *   GNU General Public License for more details.                          *
00013 *                                                                         *
00014 *   You should have received a copy of the GNU Lesser General Public      *
00015 *   License along with this library; if not, write to the                 *
00016 *   Free Software Foundation, Inc.,                                       *
00017 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
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 //       std::cout << "  " << s;
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 //       std::cout << "  [" << s << "]";
00071       return s;
00072     }
00073 };
00074 
00075 #endif

Generated on Tue Mar 13 20:00:01 2007 by  doxygen 1.5.1