converters.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <string>
14 #include <gtest/gtest.h>
15 #include "../../include/ecl/converters/to_byte_array.hpp"
16 #include "../../include/ecl/converters/char.hpp"
17 #include "../../include/ecl/converters/char_strings.hpp"
18 #include "../../include/ecl/converters/string.hpp"
19 #include "../../include/ecl/converters/integers.hpp"
20 
21 /*****************************************************************************
22 ** Using
23 *****************************************************************************/
24 
25 using std::string;
26 using ecl::Converter;
27 
28 /*****************************************************************************
29 ** Globals
30 *****************************************************************************/
31 
32 const bool debug_output = true;
33 //const bool debug_output = false;
34 
35 /*****************************************************************************
36 ** Tests
37 *****************************************************************************/
38 
39 TEST(Converter,toChar) {
40  Converter<char,unsigned int> uintToAscii;
41  Converter<char,int> intToAscii;
42  Converter<char> toAscii;
43 
44  if(debug_output) {
45  std::cout << "uintToAscii(8U): " << uintToAscii(8U) << std::endl;
46  std::cout << "intToAscii(8): " << intToAscii(8) << std::endl;
47  std::cout << "toAscii(8): " << toAscii(8) << std::endl;
48  std::cout << "toAscii(8U): " << toAscii(8U) << std::endl;
49  }
50 
51  EXPECT_EQ('8',uintToAscii(8U));
52  EXPECT_EQ('8',intToAscii(8));
53  EXPECT_EQ('8',toAscii(8U));
54  EXPECT_EQ('8',toAscii(8));
55 }
56 
57 TEST(Converter,toString) {
58  Converter<string,int> intToString;
59  if(debug_output) {
60  std::cout << "intToString(123): " << intToString(123) << std::endl;
61  std::cout << "intToString(-123): " << intToString(-123) << std::endl;
62  }
63  EXPECT_EQ(string("123"),intToString(123));
64  EXPECT_EQ(string("-123"),intToString(-123));
65 }
66 
67 TEST(Converter,toInt) {
68  Converter<int,void> toInt;
69  if(debug_output) {
70  std::cout << "toInt:" << std::endl;
71  std::cout << " string -> " << toInt(string("123")) << std::endl;
72 // std::cout << " char* -> " << toInt("-123") << std::endl;
73  std::cout << " char -> " << toInt('3') << std::endl;
74  }
75  EXPECT_EQ(123,toInt(string("123")));
76 // EXPECT_EQ(-123,toInt("-123")); // fix this once i have the proper byte array converter in.
77  EXPECT_EQ(3,toInt(char('3')));
78 }
79 
80 TEST(Converter,toCharString) {
81  // Bit hard to test these.
82  Converter<char*> toCharString;
83  char c = 'A';
84  unsigned char uc = 'A';
85  short s = -111;
86  unsigned short us = 111;
87  int i = -111;
88  unsigned int ui = 111;
89  long l = -111;
90  unsigned long ul = 111;
91  long long ll = -111111;
92  unsigned long long ull = 11111111;
93  float f = -321.23;
94  double d = -321.23;
95  char buffer[44];
96  Converter<char*,float> floatToCharString(buffer, buffer+4);
97 
98  if(debug_output) {
99  std::cout << "toCharString:" << std::endl;
100  std::cout << " char -> " << toCharString(c) << std::endl;
101  std::cout << " unsigned char -> " << toCharString(uc) << std::endl;
102  std::cout << " short -> " << toCharString(s) << std::endl;
103  std::cout << " unsigned short-> " << toCharString(us) << std::endl;
104  std::cout << " int -> " << toCharString(i) << std::endl;
105  std::cout << " unsigned int -> " << toCharString(ui) << std::endl;
106  std::cout << " long -> " << toCharString(l) << std::endl;
107  std::cout << " unsigned long -> " << toCharString(ul) << std::endl;
108  std::cout << " long long -> " << toCharString(ll) << std::endl;
109  std::cout << " unsigned llong-> " << toCharString(ull) << std::endl;
110  std::cout << " float -> " << toCharString(f) << std::endl;
111  std::cout << " double -> " << toCharString(d) << std::endl;
112  std::cout << " float(2) -> " << toCharString(f,2) << std::endl;
113  std::cout << " double(2) -> " << toCharString(d,2) << std::endl;
114  std::cout << " float (w/ buf)-> " << floatToCharString(f) << std::endl;
115  }
116  SUCCEED();
117 }
118 
119 /*****************************************************************************
120 ** Main program
121 *****************************************************************************/
122 
123 int main(int argc, char **argv) {
124  testing::InitGoogleTest(&argc,argv);
125  return RUN_ALL_TESTS();
126 }
127 
debug_output
const bool debug_output
Definition: converters.cpp:32
TEST
TEST(Converter, toChar)
Definition: converters.cpp:39
f
void f(int i)
main
int main(int argc, char **argv)
Definition: converters.cpp:123
ecl::Converter
Primary template and general fallback for converter classes.
Definition: converter.hpp:62


ecl_converters
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:25