container_converters.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <gtest/gtest.h>
15 #include "../../include/ecl/containers/array.hpp"
16 #include "../../include/ecl/containers/converters.hpp"
17 #include "../../include/ecl/containers/stencil.hpp"
18 
19 /*****************************************************************************
20 ** Using
21 *****************************************************************************/
22 
23 using ecl::Array;
24 using ecl::Stencil;
25 
26 /*****************************************************************************
27 ** Globals
28 *****************************************************************************/
29 
30 bool debug_output = true;
31 
32 /*****************************************************************************
33 ** Functions
34 *****************************************************************************/
35 
36 template <typename Container>
37 void print(const Container &byte_array) {
38  if( debug_output ) {
39  std::cout << byte_array << std::endl;
40  }
41 }
42 
43 /*****************************************************************************
44 ** Typedefs
45 *****************************************************************************/
46 
59 
60 /*****************************************************************************
61 ** Tests
62 *****************************************************************************/
63 
64 TEST(ConverterTests,fromArray) {
65  ByteArray byte_array;
66  byte_array << 0x01, 0x02, 0x03;
68  if( debug_output ) { std::cout << "Conversion: " << toInt(byte_array) << std::endl; }
69  EXPECT_EQ(197121,toInt(byte_array));
70  UnsignedByteArray unsigned_byte_array;
71  unsigned_byte_array << 0x01, 0x02, 0x03;
73  if( debug_output ) { std::cout << "Conversion: " << utoInt(unsigned_byte_array) << std::endl; }
74  EXPECT_EQ(197121,utoInt(unsigned_byte_array));
75  SignedByteArray signed_byte_array;
76  signed_byte_array << 0x01, 0x02, 0x03;
78  if( debug_output ) { std::cout << "Conversion: " << stoInt(signed_byte_array) << std::endl; }
79  EXPECT_EQ(197121,stoInt(signed_byte_array));
80 }
81 
82 TEST(ConverterTests,toArray) {
83  // Only works for dynamic containers!
84  ecl::int32 i = 197121;
88  DynamicByteArray byte_array(4);
89  DynamicSignedByteArray signed_byte_array(4);
90  DynamicUnsignedByteArray unsigned_byte_array(4);
91  toByteArray(byte_array, i);
92  toSignedByteArray(signed_byte_array,i);
93  toUnsignedByteArray(unsigned_byte_array, i);
94  print(byte_array);
95  print(signed_byte_array);
96  print(unsigned_byte_array);
97  EXPECT_EQ(1,byte_array[0]);
98  EXPECT_EQ(2,byte_array[1]);
99  EXPECT_EQ(3,byte_array[2]);
100  EXPECT_EQ(1,signed_byte_array[0]);
101  EXPECT_EQ(2,signed_byte_array[1]);
102  EXPECT_EQ(3,signed_byte_array[2]);
103  EXPECT_EQ(1,unsigned_byte_array[0]);
104  EXPECT_EQ(2,unsigned_byte_array[1]);
105  EXPECT_EQ(3,unsigned_byte_array[2]);
106 
107 }
108 TEST(ConverterTests,fromStencil) {
109  ByteArray byte_array;
110  SignedByteArray signed_byte_array;
111  UnsignedByteArray unsigned_byte_array;
112  byte_array << 0x01, 0x02, 0x03;
113  signed_byte_array << 0x01, 0x02, 0x03;
114  unsigned_byte_array << 0x01, 0x02, 0x03;
118  int i = toInt(byte_array.stencil(0,2));
119  int si = stoInt(signed_byte_array.stencil(0,2));
120  int ui = utoInt(unsigned_byte_array.stencil(0,2));
121  if( debug_output ) { std::cout << "Conversion: " << std::dec << i << std::endl; }
122  if( debug_output ) { std::cout << "Conversion: " << std::dec << si << std::endl; }
123  if( debug_output ) { std::cout << "Conversion: " << std::dec << ui << std::endl; }
124  EXPECT_EQ(513,i); // first two bytes only
125  EXPECT_EQ(513,si); // first two bytes only
126  EXPECT_EQ(513,ui); // first two bytes only
127 }
128 TEST(ConverterTests,toStencil) {
129  Array<char,20> byte_array = Array<char,20>::Constant(0x00);
130  Stencil< Array<char, 20> > stencil = byte_array.stencil(2,4);
131  ecl::Converter< Stencil< Array<char, 20> >, ecl::int32 > fromInt;
132  stencil = fromInt(stencil, 197121);
133  print(byte_array);
134  print(stencil);
135  EXPECT_EQ(1,stencil[0]);
136  EXPECT_EQ(2,stencil[1]);
137  EXPECT_EQ(3,stencil[2]);
138 }
139 /*****************************************************************************
140 ** Main program
141 *****************************************************************************/
142 
143 int main(int argc, char **argv) {
144  testing::InitGoogleTest(&argc,argv);
145  return RUN_ALL_TESTS();
146 }
147 
Fixed size container with a few bells and whistles.
void print(const Container &byte_array)
Array< unsigned char > DynamicUnsignedByteArray
std::vector< unsigned char > ByteArray
Array< signed char > DynamicSignedByteArray
Stencil< Array< char, 3 > > ByteStencil
Stencil< Array< Type, Size > > stencil(const unsigned int &start_index, const unsigned int &n)
Open a window (stencil) onto the array.
bool debug_output
Array< signed char, 3 > SignedByteArray
Stencil< Array< signed char > > DynamicSignedByteStencil
Array< char, 3 > ByteArray
Stencil< Array< unsigned char, 3 > > UnsignedByteStencil
A safe windowing class that opens onto array-like containers.
TEST(ConverterTests, fromArray)
int main(int argc, char **argv)
Array< unsigned char, 3 > UnsignedByteArray
Stencil< Array< signed char, 3 > > SignedByteStencil
Stencil< Array< unsigned char > > DynamicUnsignedByteStencil
Stencil< Array< char > > DynamicByteStencil
Array< char > DynamicByteArray


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:43