converters.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <string>
00014 #include <gtest/gtest.h>
00015 #include "../../include/ecl/converters/to_byte_array.hpp"
00016 #include "../../include/ecl/converters/char.hpp"
00017 #include "../../include/ecl/converters/char_strings.hpp"
00018 #include "../../include/ecl/converters/string.hpp"
00019 #include "../../include/ecl/converters/integers.hpp"
00020 
00021 /*****************************************************************************
00022 ** Using
00023 *****************************************************************************/
00024 
00025 using std::string;
00026 using ecl::Converter;
00027 
00028 /*****************************************************************************
00029 ** Globals
00030 *****************************************************************************/
00031 
00032 const bool debug_output = true;
00033 //const bool debug_output = false;
00034 
00035 /*****************************************************************************
00036 ** Tests
00037 *****************************************************************************/
00038 
00039 TEST(Converter,toChar) {
00040     Converter<char,unsigned int> uintToAscii;
00041     Converter<char,int> intToAscii;
00042     Converter<char> toAscii;
00043 
00044     if(debug_output) {
00045         std::cout << "uintToAscii(8U): " << uintToAscii(8U)  << std::endl;
00046         std::cout << "intToAscii(8): " << intToAscii(8)  << std::endl;
00047         std::cout << "toAscii(8): " << toAscii(8)  << std::endl;
00048         std::cout << "toAscii(8U): " << toAscii(8U)  << std::endl;
00049     }
00050 
00051     EXPECT_EQ('8',uintToAscii(8U));
00052     EXPECT_EQ('8',intToAscii(8));
00053     EXPECT_EQ('8',toAscii(8U));
00054     EXPECT_EQ('8',toAscii(8));
00055 }
00056 
00057 TEST(Converter,toString) {
00058     Converter<string,int> intToString;
00059     if(debug_output) {
00060         std::cout << "intToString(123): " << intToString(123) << std::endl;
00061         std::cout << "intToString(-123): " << intToString(-123) << std::endl;
00062     }
00063     EXPECT_EQ(string("123"),intToString(123));
00064     EXPECT_EQ(string("-123"),intToString(-123));
00065 }
00066 
00067 TEST(Converter,toInt) {
00068     Converter<int,void> toInt;
00069     if(debug_output) {
00070                 std::cout << "toInt:" << std::endl;
00071                 std::cout << "  string -> " << toInt(string("123")) << std::endl;
00072 //              std::cout << "  char*  -> " << toInt("-123") << std::endl;
00073                 std::cout << "  char   -> " << toInt('3') << std::endl;
00074     }
00075     EXPECT_EQ(123,toInt(string("123")));
00076 //    EXPECT_EQ(-123,toInt("-123")); // fix this once i have the proper byte array converter in.
00077     EXPECT_EQ(3,toInt(char('3')));
00078 }
00079 
00080 TEST(Converter,toCharString) {
00081     // Bit hard to test these.
00082     Converter<char*> toCharString;
00083     char c = 'A';
00084     unsigned char uc = 'A';
00085     short s = -111;
00086     unsigned short us = 111;
00087     int i = -111;
00088     unsigned int ui = 111;
00089     long l = -111;
00090     unsigned long ul = 111;
00091     long long ll = -111111;
00092     unsigned long long ull = 11111111;
00093     float f = -321.23;
00094     double d = -321.23;
00095     char buffer[44];
00096     Converter<char*,float> floatToCharString(buffer, buffer+4);
00097 
00098     if(debug_output) {
00099         std::cout << "toCharString:" << std::endl;
00100                 std::cout << "  char          -> " << toCharString(c) << std::endl;
00101                 std::cout << "  unsigned char -> " << toCharString(uc) << std::endl;
00102                 std::cout << "  short         -> " << toCharString(s) << std::endl;
00103                 std::cout << "  unsigned short-> " << toCharString(us) << std::endl;
00104                 std::cout << "  int           -> " << toCharString(i) << std::endl;
00105                 std::cout << "  unsigned int  -> " << toCharString(ui) << std::endl;
00106                 std::cout << "  long          -> " << toCharString(l) << std::endl;
00107                 std::cout << "  unsigned long -> " << toCharString(ul) << std::endl;
00108                 std::cout << "  long long     -> " << toCharString(ll) << std::endl;
00109                 std::cout << "  unsigned llong-> " << toCharString(ull) << std::endl;
00110                 std::cout << "  float         -> " << toCharString(f) << std::endl;
00111                 std::cout << "  double        -> " << toCharString(d) << std::endl;
00112                 std::cout << "  float(2)      -> " << toCharString(f,2) << std::endl;
00113                 std::cout << "  double(2)     -> " << toCharString(d,2) << std::endl;
00114             std::cout << "  float (w/ buf)-> " << floatToCharString(f) << std::endl;
00115     }
00116     SUCCEED();
00117 }
00118 
00119 /*****************************************************************************
00120 ** Main program
00121 *****************************************************************************/
00122 
00123 int main(int argc, char **argv) {
00124         testing::InitGoogleTest(&argc,argv);
00125         return RUN_ALL_TESTS();
00126 }
00127 


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