byte_array_converters.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <vector>
00015 #include <gtest/gtest.h>
00016 #include <ecl/config/portable_types.hpp>
00017 #include "../../include/ecl/converters/to_byte_array.hpp"
00018 #include "../../include/ecl/converters/from_byte_array.hpp"
00019 
00020 /*****************************************************************************
00021 ** Using
00022 *****************************************************************************/
00023 
00024 using ecl::Converter;
00025 
00026 /*****************************************************************************
00027 ** Typedefs
00028 *****************************************************************************/
00029 
00030 typedef std::vector<unsigned char> ByteArray;
00031 
00032 /*****************************************************************************
00033 ** Globals
00034 *****************************************************************************/
00035 
00036 bool debug_output = false;
00037 
00038 void print(const ByteArray &byte_array) {
00039         if( debug_output ) {
00040                 for ( unsigned int i = 0; i < byte_array.size(); ++i ) {
00041                         std::cout << "0x" << std::hex << static_cast<unsigned int>(byte_array[i]) << " ";
00042                 }
00043                 std::cout << std::endl;
00044         }
00045 }
00046 
00047 void print(const std::vector<char> &byte_array) {
00048         if( debug_output ) {
00049                 for ( unsigned int i = 0; i < byte_array.size(); ++i ) {
00050                         std::cout << "0x" << std::hex << static_cast<unsigned int>(byte_array[i]) << " ";
00051                 }
00052                 std::cout << std::endl;
00053         }
00054 }
00055 /*****************************************************************************
00056 ** Tests
00057 *****************************************************************************/
00058 
00059 TEST(Converter,fromByteArray) {
00060         ByteArray byte_array;
00061         byte_array.push_back(0x01);
00062         byte_array.push_back(0x02);
00063         byte_array.push_back(0x03);
00064         Converter<int,ByteArray> converter;
00065 //      std::cout << "Byte Array Conversion : " << converter(byte_array) << std::endl;
00066     EXPECT_EQ(197121,converter(byte_array));
00067 }
00068 
00069 TEST(Converter,toByteArray) {
00070         ecl::Converter< ByteArray, int > toByteArray;
00071         ByteArray byte_array(4,0x00);
00072         toByteArray(byte_array, 363);
00073         print(byte_array);
00074     EXPECT_EQ(107,byte_array[0]);
00075     toByteArray(byte_array,-2);
00076         print(byte_array);
00077     EXPECT_EQ(254,byte_array[0]);
00078     ecl::Converter< int, ByteArray > fromByteArray;
00079     int i = fromByteArray(byte_array);
00080     if ( debug_output ) {
00081         std::cout << std::dec << "i: " << i << std::endl;
00082     }
00083     EXPECT_EQ(-2,i);
00084     toByteArray(byte_array, 258);
00085         print(byte_array);
00086     i = fromByteArray(byte_array);
00087     if ( debug_output ) {
00088         std::cout << std::dec << "i: " << i << std::endl;
00089     }
00090 }
00091 TEST(Converter,toByteArrayFailedSize) {
00092   #if defined(NDEBUG) || defined(ECL_NDEBUG)
00093     std::cout << "Skipping toByteArrayFailedSize test, it only throws in debug mode" << std::endl;
00094     SUCCEED();
00095   #else
00096     ecl::Converter< ByteArray, ecl::int32 > toByteArray;
00097     ecl::int32 i = 363;
00098     ByteArray byte_array;
00099     try {
00100       byte_array = toByteArray(byte_array, i);
00101     } catch ( ecl::StandardException &e ) {
00102       if ( debug_output ) { std::cout << "Caught an exception from improper size of reserved memory." << std::endl; }
00103     }
00104     ecl::Error result = toByteArray.error();
00105     if ( result.flag() == ecl::ConversionError ) {
00106       SUCCEED();
00107     } else {
00108       FAIL();
00109     }
00110   #endif
00111 }
00112 
00113 TEST(Converter,charToByteArray) {
00114     Converter< std::vector<char> > toCharByteArray;
00115     std::vector<char> bytes = toCharByteArray("0x32 0x54");
00116     if(debug_output) {
00117         print(bytes);
00118     }
00119     EXPECT_EQ(50,bytes[0]);
00120     EXPECT_EQ(84,bytes[1]);
00121     std::vector<char> bytes_258(4,0x00);
00122     toCharByteArray(bytes_258, ecl::int32(258));
00123     print(bytes_258);
00124     ecl::Converter< ecl::int32, std::vector<char> > fromCharByteArray;
00125     ecl::int32 i = fromCharByteArray(bytes_258);
00126     if ( debug_output ) {
00127         std::cout << std::dec << "i: " << i << std::endl;
00128     }
00129 }
00130 
00131 TEST(Converter,byteArrayStringConverters) {
00132         std::string str("dude");
00133         ecl::converters::FromByteArray<ecl::int32,std::string> from_byte_array;
00134         int i = from_byte_array(str);
00135     if ( debug_output ) {
00136         std::cout << "i: " << i << std::endl;
00137     }
00138     // is this correct? I haven't actually verified this.
00139         EXPECT_EQ(1701082468,i);
00140 }
00141 
00142 /*****************************************************************************
00143 ** Main
00144 *****************************************************************************/
00145 
00146 int main(int argc, char **argv) {
00147 
00148     testing::InitGoogleTest(&argc,argv);
00149     return RUN_ALL_TESTS();
00150 }
00151 
00152 
00153 


ecl_converters
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:34