byte_array_converters.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <vector>
15 #include <gtest/gtest.h>
17 #include "../../include/ecl/converters/to_byte_array.hpp"
18 #include "../../include/ecl/converters/from_byte_array.hpp"
19 
20 /*****************************************************************************
21 ** Using
22 *****************************************************************************/
23 
24 using ecl::Converter;
25 
26 /*****************************************************************************
27 ** Typedefs
28 *****************************************************************************/
29 
30 typedef std::vector<unsigned char> ByteArray;
31 
32 /*****************************************************************************
33 ** Globals
34 *****************************************************************************/
35 
36 bool debug_output = false;
37 
38 void print(const ByteArray &byte_array) {
39  if( debug_output ) {
40  for ( unsigned int i = 0; i < byte_array.size(); ++i ) {
41  std::cout << "0x" << std::hex << static_cast<unsigned int>(byte_array[i]) << " ";
42  }
43  std::cout << std::endl;
44  }
45 }
46 
47 void print(const std::vector<char> &byte_array) {
48  if( debug_output ) {
49  for ( unsigned int i = 0; i < byte_array.size(); ++i ) {
50  std::cout << "0x" << std::hex << static_cast<unsigned int>(byte_array[i]) << " ";
51  }
52  std::cout << std::endl;
53  }
54 }
55 /*****************************************************************************
56 ** Tests
57 *****************************************************************************/
58 
59 TEST(Converter,fromByteArray) {
60  ByteArray byte_array;
61  byte_array.push_back(0x01);
62  byte_array.push_back(0x02);
63  byte_array.push_back(0x03);
64  Converter<int,ByteArray> converter;
65 // std::cout << "Byte Array Conversion : " << converter(byte_array) << std::endl;
66  EXPECT_EQ(197121,converter(byte_array));
67 }
68 
69 TEST(Converter,toByteArray) {
71  ByteArray byte_array(4,0x00);
72  toByteArray(byte_array, 363);
73  print(byte_array);
74  EXPECT_EQ(107,byte_array[0]);
75  toByteArray(byte_array,-2);
76  print(byte_array);
77  EXPECT_EQ(254,byte_array[0]);
79  int i = fromByteArray(byte_array);
80  if ( debug_output ) {
81  std::cout << std::dec << "i: " << i << std::endl;
82  }
83  EXPECT_EQ(-2,i);
84  toByteArray(byte_array, 258);
85  print(byte_array);
86  i = fromByteArray(byte_array);
87  if ( debug_output ) {
88  std::cout << std::dec << "i: " << i << std::endl;
89  }
90 }
91 TEST(Converter,toByteArrayFailedSize) {
92  #if defined(NDEBUG) || defined(ECL_NDEBUG)
93  std::cout << "Skipping toByteArrayFailedSize test, it only throws in debug mode" << std::endl;
94  SUCCEED();
95  #else
97  ecl::int32 i = 363;
98  ByteArray byte_array;
99  try {
100  byte_array = toByteArray(byte_array, i);
101  } catch ( ecl::StandardException &e ) {
102  if ( debug_output ) { std::cout << "Caught an exception from improper size of reserved memory." << std::endl; }
103  }
104  ecl::Error result = toByteArray.error();
105  if ( result.flag() == ecl::ConversionError ) {
106  SUCCEED();
107  } else {
108  FAIL();
109  }
110  #endif
111 }
112 
113 TEST(Converter,charToByteArray) {
114  Converter< std::vector<char> > toCharByteArray;
115  std::vector<char> bytes = toCharByteArray("0x32 0x54");
116  if(debug_output) {
117  print(bytes);
118  }
119  EXPECT_EQ(50,bytes[0]);
120  EXPECT_EQ(84,bytes[1]);
121  std::vector<char> bytes_258(4,0x00);
122  toCharByteArray(bytes_258, ecl::int32(258));
123  print(bytes_258);
125  ecl::int32 i = fromCharByteArray(bytes_258);
126  if ( debug_output ) {
127  std::cout << std::dec << "i: " << i << std::endl;
128  }
129 }
130 
131 TEST(Converter,byteArrayStringConverters) {
132  std::string str("dude");
133  ecl::converters::FromByteArray<ecl::int32,std::string> from_byte_array;
134  int i = from_byte_array(str);
135  if ( debug_output ) {
136  std::cout << "i: " << i << std::endl;
137  }
138  // is this correct? I haven't actually verified this.
139  EXPECT_EQ(1701082468,i);
140 }
141 
142 /*****************************************************************************
143 ** Main
144 *****************************************************************************/
145 
146 int main(int argc, char **argv) {
147 
148  testing::InitGoogleTest(&argc,argv);
149  return RUN_ALL_TESTS();
150 }
151 
152 
153 
virtual ErrorFlag flag() const
ConversionError
TEST(Converter, fromByteArray)
std::vector< unsigned char > ByteArray
bool debug_output
Primary template and general fallback for converter classes.
Definition: converter.hpp:56
void print(const ByteArray &byte_array)
int main(int argc, char **argv)


ecl_converters
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:21