00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <Basics.H>
00030
00031
00032 #define CHECK_SIZE(type, size) \
00033 if(sizeof(type) != size) \
00034 { \
00035 cout << "Error: " #type " should be " #size " byte(s) but is " \
00036 << sizeof(type) << endl; \
00037 return 1; \
00038 } \
00039 (void) 0
00040
00041 using std::cout;
00042 using std::endl;
00043
00044 int main(void)
00045 {
00046
00047 CHECK_SIZE(Basics::int8, 1);
00048 CHECK_SIZE(Basics::uint8, 1);
00049 CHECK_SIZE(Basics::int16, 2);
00050 CHECK_SIZE(Basics::uint16, 2);
00051 CHECK_SIZE(Basics::int32, 4);
00052 CHECK_SIZE(Basics::uint32, 4);
00053 CHECK_SIZE(Basics::int64, 8);
00054 CHECK_SIZE(Basics::uint64, 8);
00055
00056
00057
00058
00059 CHECK_SIZE(Bit8, 1);
00060 CHECK_SIZE(Bit16, 2);
00061 CHECK_SIZE(Bit32, 4);
00062 CHECK_SIZE(Bit64, 8);
00063 CHECK_SIZE(Word, 8);
00064
00065 CHECK_SIZE(bool8, 1);
00066
00067 CHECK_SIZE(Byte32, 32);
00068
00069
00070 if(sizeof(PointerInt) != sizeof(void *))
00071 {
00072 cout << "Error: PointerInt is not the same size as void *"
00073 << endl
00074 << "\tPointerInt is " << sizeof(PointerInt) << " byte(s)" << endl
00075 << "\tvoid * is " << sizeof(void *) << " byte(s)" << endl;
00076 return 1;
00077 }
00078
00079
00080
00081
00082 if(sizeof(float) != sizeof(Basics::int32))
00083 {
00084 cout << "Error: float is not the same size as Basics::int32"
00085 << endl
00086 << "\tfloat is " << sizeof(float) << " byte(s)" << endl
00087 << "\tBasics::int32 is " << sizeof(Basics::int32) << " byte(s)"
00088 << endl;
00089 return 1;
00090 }
00091
00092
00093
00094 cout << "All types seem to be the right size!" << endl;
00095
00096
00097 Basics::uint16 l_host16 = 0x1234;
00098 Basics::uint16 l_net16 = Basics::hton16(l_host16);
00099 if(Basics::ntoh16(l_net16) != l_host16)
00100 {
00101 char orig_buff[5], net_buff[5], final_buff[5];
00102 sprintf(orig_buff, "%04hx", l_host16);
00103 sprintf(net_buff, "%04hx", l_net16);
00104 sprintf(final_buff, "%04hx", Basics::ntoh16(l_net16));
00105 cout << "Error: Conversion of 16-bit values to/from network "
00106 << "byte-order is broken:" << endl
00107 << "\tx = 0x" << orig_buff << endl
00108 << "\thton16(x) = 0x" << net_buff << endl
00109 << "\tntoh16(hton16(x)) = 0x"
00110 << final_buff << endl
00111 << endl;
00112 return 1;
00113 }
00114
00115 Basics::uint32 l_host32 = 0x12345678;
00116 Basics::uint32 l_net32 = Basics::hton32(l_host32);
00117 if(Basics::ntoh32(l_net32) != l_host32)
00118 {
00119 char orig_buff[9], net_buff[9], final_buff[9];
00120 sprintf(orig_buff, "%08x", l_host32);
00121 sprintf(net_buff, "%08x", l_net32);
00122 sprintf(final_buff, "%08x", Basics::ntoh32(l_net32));
00123 cout << "Error: Conversion of 32-bit values to/from network "
00124 << "byte-order is broken:" << endl
00125 << "\tx = 0x" << orig_buff << endl
00126 << "\thton32(x) = 0x" << net_buff << endl
00127 << "\tntoh32(hton32(x)) = 0x"
00128 << final_buff << endl
00129 << endl;
00130 return 1;
00131 }
00132
00133 Basics::uint64 l_host64 = CONST_INT_64(0x123456789abcdef0);
00134 Basics::uint64 l_net64 = Basics::hton64(l_host64);
00135 if(Basics::ntoh64(l_net64) != l_host64)
00136 {
00137 char orig_buff[17], net_buff[17], final_buff[17];
00138 sprintf(orig_buff, "%016" FORMAT_LENGTH_INT_64 "x", l_host64);
00139 sprintf(net_buff, "%016" FORMAT_LENGTH_INT_64 "x", l_net64);
00140 sprintf(final_buff, "%016" FORMAT_LENGTH_INT_64 "x",
00141 Basics::ntoh64(l_net64));
00142 cout << "Error: Conversion of 64-bit values to/from network "
00143 << "byte-order is broken:" << endl
00144 << "\tx = 0x" << orig_buff << endl
00145 << "\thton64(x) = 0x" << net_buff << endl
00146 << "\tntoh64(hton64(x)) = 0x" << final_buff << endl
00147 << endl;
00148 return 1;
00149 }
00150
00151
00152
00153 cout << "All network byte-order conversions work!" << endl;
00154
00155 return 0;
00156 }