Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestSizes.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // ----------------------------------------------------------------------
00020 // TestSizes.C - A simple program to make sure that some fundamental
00021 // data types defined in Basics.H are the correct size.  Handy when
00022 // porting to a new platform.  Also tests conversion to/from network
00023 // byte order.
00024 // ----------------------------------------------------------------------
00025 
00026 // Last modified on Thu May 27 00:57:35 EDT 2004 by ken@xorian.net
00027 
00028 // Include the type definitions.
00029 #include <Basics.H>
00030 
00031 // A macro to shorten the following code.
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   // Check the size of the fundamental types.
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   // These are redundant since they should be based on the above, but
00057   // it doesn't hurt.  (And who knows, it could get confused in the
00058   // future.)
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   // Make sure PointerInt is the same size as a pointer.
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   // The SRPC send/recv_float functions expect that "float" is the
00080   // same size as Basics::int32 (i.e. that it's an IEEE
00081   // single-precision floating-point number).
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   // If we make it down to here without exiting, then the sizes must
00093   // be OK.
00094   cout << "All types seem to be the right size!" << endl;
00095 
00096   // Now test the network byte-order conversions.
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   // If we make it here, then the network byte-order conversions must
00152   // be working.
00153   cout << "All network byte-order conversions work!" << endl;
00154 
00155   return 0;
00156 }

Generated on Mon May 8 00:48:29 2006 for Vesta by  doxygen 1.4.2