buffer2memorytest.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 BUFFER2MEMORYTEST_H
00020 #define BUFFER2MEMORYTEST_H
00021 
00022 #include <bit/bit.h>
00023 #include <cppunit/TestFixture.h>
00024 
00025 using namespace bit;
00026 
00027 class Buffer2MemoryTest : public  CppUnit::TestFixture {
00028   public:
00029 
00030     CPPUNIT_TEST_SUITE( Buffer2MemoryTest );
00031     CPPUNIT_TEST( end_lower_bits );
00032     CPPUNIT_TEST( end_upper_bits );
00033     CPPUNIT_TEST( end_middle_octets );
00034     CPPUNIT_TEST( end_spanning_octets );
00035     CPPUNIT_TEST( end_spanning_octets2 );
00036     CPPUNIT_TEST( end_mid_octet_bits_26_30 );
00037     CPPUNIT_TEST( start_lower_bits );
00038     CPPUNIT_TEST( start_upper_bits );
00039     CPPUNIT_TEST( start_middle_octets );
00040     CPPUNIT_TEST( start_spanning_octets );
00041     CPPUNIT_TEST( start_spanning_octets2 );
00042     CPPUNIT_TEST( start_mid_octet_bits_26_30 );
00043     CPPUNIT_TEST_SUITE_END();
00044 
00045   public:
00046     void setUp() { }
00047 
00048     void tearDown() { }
00049 
00050     // 0x   1    2    3    4    5    6    7    8
00051     //   0001 0010 0011 0100 0101 0110 0111 1000
00052     void end_lower_bits()             { CPPUNIT_ASSERT( 0x00000002 == buf2mem(0x12345678, 4,  4) ); }
00053     void end_upper_bits()             { CPPUNIT_ASSERT( 0x00000003 == buf2mem(0x12345678, 8,  4) ); }
00054     void end_middle_octets()          { CPPUNIT_ASSERT( 0x00003456 == buf2mem(0x12345678, 8, 16) ); }
00055     void end_spanning_octets()        { CPPUNIT_ASSERT( 0x00234567 == buf2mem(0x12345678, 4, 24) ); }
00056     void end_spanning_octets2()       { CPPUNIT_ASSERT( 0x01234567 == buf2mem(0x12345678, 3, 25) ); }
00057     void end_mid_octet_bits_26_30()   { CPPUNIT_ASSERT( 0x0000000e == buf2mem(0x12345678, 26, 4) ); }
00058     void start_lower_bits()           { CPPUNIT_ASSERT( 0x20000000 == buf2mem(0x12345678, 4,  4, START) ); }
00059     void start_upper_bits()           { CPPUNIT_ASSERT( 0x30000000 == buf2mem(0x12345678, 8,  4, START) ); }
00060     void start_middle_octets()        { CPPUNIT_ASSERT( 0x34560000 == buf2mem(0x12345678, 8, 16, START) ); }
00061     void start_spanning_octets()      { CPPUNIT_ASSERT( 0x23456700 == buf2mem(0x12345678, 4, 24, START) ); }
00062     void start_spanning_octets2()     { CPPUNIT_ASSERT( 0x91A2B380 == buf2mem(0x12345678, 3, 25, START) ); }
00063     void start_mid_octet_bits_26_30() { CPPUNIT_ASSERT( 0xe0000000 == buf2mem(0x12345678, 26, 4, START) ); }
00064 
00065     uint32_t buf2mem( uint32_t value, size_t offset, size_t extract, Location loc=END )
00066     {
00067       Buffer b;
00068       uint32_t extracted;
00069       value = host_to_be(value);
00070       b.set_data(&value, sizeof(value));
00071       b.unpack(&extracted, sizeof(uint32_t), loc, offset, extract);
00072       extracted = be_to_host(extracted);
00073       return extracted;
00074     }
00075 };
00076 
00077 #endif

Generated on Thu Jul 6 14:38:08 2006 by  doxygen 1.4.6