#include #include #include #include #include #include #include "util/endian.h" using namespace std; class EndianTestSuite : public Test::Suite{ public: EndianTestSuite(){ TEST_ADD(EndianTestSuite::test_bitshift); TEST_ADD(EndianTestSuite::test_swap); TEST_ADD(EndianTestSuite::test_le8); TEST_ADD(EndianTestSuite::test_be8); TEST_ADD(EndianTestSuite::test_le4); TEST_ADD(EndianTestSuite::test_be4); TEST_ADD(EndianTestSuite::test_le2); TEST_ADD(EndianTestSuite::test_be2); } protected: virtual void setup(){ cout << endl; } virtual void tear_down(){ cout << endl; } template void raw_out(unsigned char *c){ cout << hex; for(int i = 0; i < sizeof(T); i++){ cout << "0x"; cout << setw(2) << setfill('0') << (unsigned int)(*(c++)); cout << ' '; } cout << dec << endl; } private: void test_bitshift(){ int i = 0x01234567; cout << i << " :\t"; raw_out((unsigned char *)&i); TEST_ASSERT(i == 0x01234567); i <<= 8; cout << i << " :\t"; raw_out((unsigned char *)&i); TEST_ASSERT(i == 0x23456700); i <<= 8; cout << i << " :\t"; raw_out((unsigned char *)&i); TEST_ASSERT(i == 0x45670000); i <<= 8; cout << i << " :\t"; raw_out((unsigned char *)&i); TEST_ASSERT(i == 0x67000000); } typedef struct { unsigned char c[2]; } c2_t; typedef struct { unsigned char c[4]; } c4_t; typedef struct { unsigned char c[8]; } c8_t; void test_swap(){ c8_t c8; c8.c[0] = 0x12; c8.c[1] = 0x34; c8.c[2] = 0x56; c8.c[3] = 0x78; c8.c[4] = 0x9A; c8.c[5] = 0xBC; c8.c[6] = 0xDE; c8.c[7] = 0xF0; raw_out(c8.c); c8 = swap_endian(c8); raw_out(c8.c); TEST_ASSERT(c8.c[7] == 0x12); TEST_ASSERT(c8.c[6] == 0x34); TEST_ASSERT(c8.c[5] == 0x56); TEST_ASSERT(c8.c[4] == 0x78); TEST_ASSERT(c8.c[3] == 0x9A); TEST_ASSERT(c8.c[2] == 0xBC); TEST_ASSERT(c8.c[1] == 0xDE); TEST_ASSERT(c8.c[0] == 0xF0); } void test_le8(){ c8_t c8; c8.c[0] = 0x12; c8.c[1] = 0x34; c8.c[2] = 0x56; c8.c[3] = 0x78; c8.c[4] = 0x9A; c8.c[5] = 0xBC; c8.c[6] = 0xDE; c8.c[7] = 0xF0; unsigned long long ret = le_char8_2_num((char &)c8.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0xF0DEBC9A78563412 : 0x123456789ABCDEF0) ); } void test_be8(){ c8_t c8; c8.c[0] = 0x12; c8.c[1] = 0x34; c8.c[2] = 0x56; c8.c[3] = 0x78; c8.c[4] = 0x9A; c8.c[5] = 0xBC; c8.c[6] = 0xDE; c8.c[7] = 0xF0; unsigned long long ret = be_char8_2_num((char &)c8.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0x123456789ABCDEF0 : 0xF0DEBC9A78563412) ); } void test_le4(){ c4_t c4; c4.c[0] = 0x12; c4.c[1] = 0x34; c4.c[2] = 0x56; c4.c[3] = 0x78; unsigned int ret = le_char4_2_num((char &)c4.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0x78563412 : 0x12345678) ); } void test_be4(){ c4_t c4; c4.c[0] = 0x12; c4.c[1] = 0x34; c4.c[2] = 0x56; c4.c[3] = 0x78; unsigned int ret = be_char4_2_num((char &)c4.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0x12345678 : 0x78563412) ); } void test_le2(){ c2_t c2; c2.c[0] = 0x12; c2.c[1] = 0x34; unsigned short ret = le_char2_2_num((char &)c2.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0x3412 : 0x1234) ); } void test_be2(){ c2_t c2; c2.c[0] = 0x12; c2.c[1] = 0x34; unsigned short ret = be_char2_2_num((char &)c2.c); raw_out((unsigned char *)&ret); TEST_ASSERT( ret == (IS_LITTLE_ENDIAN ? 0x1234 : 0x3412) ); } }; bool run_tests(){ EndianTestSuite test_suits; Test::TextOutput output(Test::TextOutput::Verbose); return test_suits.run(output, false); // Note the 'false' parameter } int main(){run_tests();return 0;}