converters.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <gtest/gtest.h>
00015 #include "../../include/ecl/containers/array.hpp"
00016 #include "../../include/ecl/containers/converters.hpp"
00017 #include "../../include/ecl/containers/stencil.hpp"
00018 
00019 /*****************************************************************************
00020 ** Using
00021 *****************************************************************************/
00022 
00023 using ecl::Array;
00024 using ecl::Stencil;
00025 
00026 /*****************************************************************************
00027 ** Globals
00028 *****************************************************************************/
00029 
00030 bool debug_output = true;
00031 
00032 /*****************************************************************************
00033 ** Functions
00034 *****************************************************************************/
00035 
00036 template <typename Container>
00037 void print(const Container &byte_array) {
00038         if( debug_output ) {
00039                 std::cout << byte_array << std::endl;
00040         }
00041 }
00042 
00043 /*****************************************************************************
00044 ** Typedefs
00045 *****************************************************************************/
00046 
00047 typedef Array<char,3> ByteArray;
00048 typedef Array<char> DynamicByteArray;
00049 typedef Stencil< Array<char,3> > ByteStencil;
00050 typedef Stencil< Array<char> > DynamicByteStencil;
00051 typedef Array<signed char,3> SignedByteArray;
00052 typedef Array<signed char> DynamicSignedByteArray;
00053 typedef Stencil< Array<signed char,3> > SignedByteStencil;
00054 typedef Stencil< Array<signed char> > DynamicSignedByteStencil;
00055 typedef Array<unsigned char,3> UnsignedByteArray;
00056 typedef Array<unsigned char> DynamicUnsignedByteArray;
00057 typedef Stencil< Array<unsigned char,3> > UnsignedByteStencil;
00058 typedef Stencil< Array<unsigned char> > DynamicUnsignedByteStencil;
00059 
00060 /*****************************************************************************
00061 ** Tests
00062 *****************************************************************************/
00063 
00064 TEST(ConverterTests,fromArray) {
00065         ByteArray byte_array;
00066         byte_array << 0x01, 0x02, 0x03;
00067         ecl::Converter<int,ByteArray> toInt;
00068         if( debug_output ) { std::cout << "Conversion: " << toInt(byte_array) << std::endl; }
00069         EXPECT_EQ(197121,toInt(byte_array));
00070         UnsignedByteArray unsigned_byte_array;
00071         unsigned_byte_array << 0x01, 0x02, 0x03;
00072         ecl::Converter<int,UnsignedByteArray> utoInt;
00073         if( debug_output ) { std::cout << "Conversion: " << utoInt(unsigned_byte_array) << std::endl; }
00074         EXPECT_EQ(197121,utoInt(unsigned_byte_array));
00075         SignedByteArray signed_byte_array;
00076         signed_byte_array << 0x01, 0x02, 0x03;
00077         ecl::Converter<int,SignedByteArray> stoInt;
00078         if( debug_output ) { std::cout << "Conversion: " << stoInt(signed_byte_array) << std::endl; }
00079         EXPECT_EQ(197121,stoInt(signed_byte_array));
00080 }
00081 
00082 TEST(ConverterTests,toArray) {
00083         // Only works for dynamic containers!
00084         ecl::int32 i = 197121;
00085         ecl::Converter<DynamicByteArray,ecl::int32> toByteArray;
00086         ecl::Converter<DynamicSignedByteArray,ecl::int32> toSignedByteArray;
00087         ecl::Converter<DynamicUnsignedByteArray,ecl::int32> toUnsignedByteArray;
00088         DynamicByteArray byte_array(4);
00089         DynamicSignedByteArray signed_byte_array(4);
00090         DynamicUnsignedByteArray unsigned_byte_array(4);
00091         toByteArray(byte_array, i);
00092         toSignedByteArray(signed_byte_array,i);
00093         toUnsignedByteArray(unsigned_byte_array, i);
00094         print(byte_array);
00095         print(signed_byte_array);
00096         print(unsigned_byte_array);
00097     EXPECT_EQ(1,byte_array[0]);
00098     EXPECT_EQ(2,byte_array[1]);
00099     EXPECT_EQ(3,byte_array[2]);
00100     EXPECT_EQ(1,signed_byte_array[0]);
00101     EXPECT_EQ(2,signed_byte_array[1]);
00102     EXPECT_EQ(3,signed_byte_array[2]);
00103     EXPECT_EQ(1,unsigned_byte_array[0]);
00104     EXPECT_EQ(2,unsigned_byte_array[1]);
00105     EXPECT_EQ(3,unsigned_byte_array[2]);
00106 
00107 }
00108 TEST(ConverterTests,fromStencil) {
00109         ByteArray byte_array;
00110         SignedByteArray signed_byte_array;
00111         UnsignedByteArray unsigned_byte_array;
00112         byte_array << 0x01, 0x02, 0x03;
00113         signed_byte_array << 0x01, 0x02, 0x03;
00114         unsigned_byte_array << 0x01, 0x02, 0x03;
00115         ecl::Converter<int,ByteStencil> toInt;
00116         ecl::Converter<int,SignedByteStencil> stoInt;
00117         ecl::Converter<int,UnsignedByteStencil> utoInt;
00118         int i = toInt(byte_array.stencil(0,2));
00119         int si = stoInt(signed_byte_array.stencil(0,2));
00120         int ui = utoInt(unsigned_byte_array.stencil(0,2));
00121         if( debug_output ) { std::cout << "Conversion: " << std::dec << i << std::endl; }
00122         if( debug_output ) { std::cout << "Conversion: " << std::dec << si << std::endl; }
00123         if( debug_output ) { std::cout << "Conversion: " << std::dec << ui << std::endl; }
00124         EXPECT_EQ(513,i); // first two bytes only
00125         EXPECT_EQ(513,si); // first two bytes only
00126         EXPECT_EQ(513,ui); // first two bytes only
00127 }
00128 TEST(ConverterTests,toStencil) {
00129         Array<char,20> byte_array = Array<char,20>::Constant(0x00);
00130         Stencil< Array<char, 20> > stencil = byte_array.stencil(2,4);
00131         ecl::Converter< Stencil< Array<char, 20> >, ecl::int32 > fromInt;
00132         stencil = fromInt(stencil, 197121);
00133         print(byte_array);
00134         print(stencil);
00135     EXPECT_EQ(1,stencil[0]);
00136     EXPECT_EQ(2,stencil[1]);
00137     EXPECT_EQ(3,stencil[2]);
00138 }
00139 /*****************************************************************************
00140 ** Main program
00141 *****************************************************************************/
00142 
00143 int main(int argc, char **argv) {
00144     testing::InitGoogleTest(&argc,argv);
00145     return RUN_ALL_TESTS();
00146 }
00147 


ecl_containers
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:12:25