shifttest.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 SHIFTTEST_H
00020 #define SHIFTTEST_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 ShiftTest : public  CppUnit::TestFixture {
00030   public:
00031 
00032     CPPUNIT_TEST_SUITE( ShiftTest );
00033     CPPUNIT_TEST( left_shift_2_octets );
00034     CPPUNIT_TEST( left_shift_2_bits );
00035     CPPUNIT_TEST( left_shift_6_bits );
00036     CPPUNIT_TEST( left_shift_12_bits );
00037     CPPUNIT_TEST( left_shift_63_bits );
00038     CPPUNIT_TEST( left_shift_64_bits );
00039     CPPUNIT_TEST( left_shift_65_bits );
00040     CPPUNIT_TEST( right_shift_2_octets );
00041     CPPUNIT_TEST( right_shift_2_bits );
00042     CPPUNIT_TEST( right_shift_6_bits );
00043     CPPUNIT_TEST( right_shift_12_bits );
00044     CPPUNIT_TEST( right_shift_61_bits );
00045     CPPUNIT_TEST( right_shift_62_bits );
00046     CPPUNIT_TEST( right_shift_63_bits );
00047     CPPUNIT_TEST( right_shift_64_bits );
00048     CPPUNIT_TEST( right_shift_65_bits );
00049     CPPUNIT_TEST_SUITE_END();
00050 
00051   public:
00052     void setUp() { }
00053 
00054     void tearDown() { }
00055 
00056     // 0x   1    2    3    4    5    6    7    8      9    A    B    C    D    E    F    1
00057     //   0001 0010 0011 0100 0101 0110 0111 1000 | 1001 1010 1011 1100 1101 1110 1111 0001
00058     void left_shift_2_octets() { CPPUNIT_ASSERT( 0x56789ABCDEF10000ULL == left_shift( 0x123456789ABCDEF1ULL, 16)); }
00059     void left_shift_2_bits()   { CPPUNIT_ASSERT( 0x48D159E26AF37BC4ULL == left_shift( 0x123456789ABCDEF1ULL, 2)); }
00060     void left_shift_6_bits()   { CPPUNIT_ASSERT( 0x8D159E26AF37BC40ULL == left_shift( 0x123456789ABCDEF1ULL, 6)); }
00061     void left_shift_12_bits()  { CPPUNIT_ASSERT( 0x456789ABCDEF1000ULL == left_shift( 0x123456789ABCDEF1ULL, 12)); }
00062     void left_shift_63_bits()  { CPPUNIT_ASSERT( 0x8000000000000000ULL == left_shift( 0x123456789ABCDEF1ULL, 63)); }
00063     void left_shift_64_bits()  { CPPUNIT_ASSERT( 0x0000000000000000ULL == left_shift( 0x123456789ABCDEF1ULL, 64)); }
00064     void left_shift_65_bits()  { CPPUNIT_ASSERT( 0x0000000000000000ULL == left_shift( 0x123456789ABCDEF1ULL, 65)); }
00065 
00066     uint64_t left_shift(uint64_t value, size_t shift) {
00067       value = host_to_be(value);
00068       bit::left_shift( &value, sizeof(value), shift);
00069       value = be_to_host(value);
00070 //       std::cout << " 0x" << std::hex << value << std::dec << std::endl;
00071       return value;
00072     }
00073 
00074     //   00000000010010001101000101011001 | 11100010011010101111001101111011
00075     void right_shift_2_octets() { CPPUNIT_ASSERT( 0x0000123456789ABCULL == right_shift( 0x123456789ABCDEF1ULL, 16)); }
00076     void right_shift_2_bits()   { CPPUNIT_ASSERT( 0x048D159E26AF37BCULL == right_shift( 0x123456789ABCDEF1ULL, 2)); }
00077     void right_shift_6_bits()   { CPPUNIT_ASSERT( 0x0048D159E26AF37BULL == right_shift( 0x123456789ABCDEF1ULL, 6)); }
00078     void right_shift_12_bits()  { CPPUNIT_ASSERT( 0x000123456789ABCDULL == right_shift( 0x123456789ABCDEF1ULL, 12)); }
00079     void right_shift_61_bits()  { CPPUNIT_ASSERT( 0x0000000000000005ULL == right_shift( 0xB23456789ABCDEF1ULL, 61)); }
00080     void right_shift_62_bits()  { CPPUNIT_ASSERT( 0x0000000000000003ULL == right_shift( 0xF23456789ABCDEF1ULL, 62)); }
00081     void right_shift_63_bits()  { CPPUNIT_ASSERT( 0x0000000000000001ULL == right_shift( 0xF23456789ABCDEF1ULL, 63)); }
00082     void right_shift_64_bits()  { CPPUNIT_ASSERT( 0x0000000000000000ULL == right_shift( 0x123456789ABCDEF1ULL, 64)); }
00083     void right_shift_65_bits()  { CPPUNIT_ASSERT( 0x0000000000000000ULL == right_shift( 0x123456789ABCDEF1ULL, 65)); }
00084 
00085     uint64_t right_shift(uint64_t value, size_t shift) {
00086       value = host_to_be(value);
00087       bit::right_shift( &value, sizeof(value), shift);
00088       value = be_to_host(value);
00089 //       std::cout << " 0x" << std::hex << value << std::dec << std::endl;
00090       return value;
00091     }
00092 };
00093 
00094 #endif

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