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         ecl::Converter< ByteArray, ecl::int32 > toByteArray;
00093         ecl::int32 i = 363;
00094         ByteArray byte_array;
00095         try {
00096                 byte_array = toByteArray(byte_array, i);
00097         } catch ( ecl::StandardException &e ) {
00098             if ( debug_output ) { std::cout << "Caught an exception from improper size of reserved memory." << std::endl; }
00099         }
00100         SUCCEED();
00101 }
00102 
00103 TEST(Converter,charToByteArray) {
00104     Converter< std::vector<char> > toCharByteArray;
00105     std::vector<char> bytes = toCharByteArray("0x32 0x54");
00106     if(debug_output) {
00107         print(bytes);
00108     }
00109     EXPECT_EQ(50,bytes[0]);
00110     EXPECT_EQ(84,bytes[1]);
00111     std::vector<char> bytes_258(4,0x00);
00112     toCharByteArray(bytes_258, ecl::int32(258));
00113     print(bytes_258);
00114     ecl::Converter< ecl::int32, std::vector<char> > fromCharByteArray;
00115     ecl::int32 i = fromCharByteArray(bytes_258);
00116     if ( debug_output ) {
00117         std::cout << std::dec << "i: " << i << std::endl;
00118     }
00119 }
00120 
00121 TEST(Converter,byteArrayStringConverters) {
00122         std::string str("dude");
00123         ecl::converters::FromByteArray<ecl::int32,std::string> from_byte_array;
00124         int i = from_byte_array(str);
00125     if ( debug_output ) {
00126         std::cout << "i: " << i << std::endl;
00127     }
00128     // is this correct? I haven't actually verified this.
00129         EXPECT_EQ(1701082468,i);
00130 }
00131 
00132 /*****************************************************************************
00133 ** Main
00134 *****************************************************************************/
00135 
00136 int main(int argc, char **argv) {
00137 
00138     testing::InitGoogleTest(&argc,argv);
00139     return RUN_ALL_TESTS();
00140 }
00141 
00142 
00143 


ecl_converters
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:10