MyraMath
ByteStreams


Source: tests/io/ByteStreams.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
11 // Containers.
12 #include <map>
13 #include <vector>
14 
15 // Seralization bits.
17 #include <myramath/io/iosize.h>
18 #include <myramath/io/detail/map.h>
19 
20 // Reporting.
21 #include <tests/myratest.h>
22 
23 using namespace myra;
24 
25 ADD_TEST("ByteStreams","[io]")
26  {
27  // Populate a Map instance, m1.
28  typedef std::map<int,double> Map;
29  Map m1;
30  m1[1] = 1.111;
31  m1[2] = 2.222;
32  m1[3] = 3.333;
33  myra::out() << "m1 = [ ";
34  for (auto i = m1.begin(); i != m1.end(); ++i)
35  myra::out() << "(" << i->first << "," << i->second << ") ";
36  myra::out() << "]" << std::endl;
37  // Allocate char* buffer big enough to serialize m1 into.
38  size_t N = iosize(m1);
39  std::vector<char> buffer(N,0);
40  // Serialize m.
41  ByteOutputStream out(buffer.data(),N);
42  out << m1;
43  // Examine buffer.
44  myra::out() << "buffer = [ ";
45  for (size_t n = 0; n < N; ++n)
46  myra::out() << (int) buffer[n] << " ";
47  myra::out() << "]" << std::endl;
48  // Deserialize into another Map, m2.
49  ByteInputStream in(buffer.data(),N);
50  auto m2 = in.read<Map>();
51  myra::out() << "m2 = [ ";
52  for (auto i = m2.begin(); i != m2.end(); ++i)
53  myra::out() << "(" << i->first << "," << i->second << ") ";
54  myra::out() << "]" << std::endl;
55  // Verify the contents of m2.
56  REQUIRE(m2.at(1) == 1.111);
57  REQUIRE(m2.at(2) == 2.222);
58  REQUIRE(m2.at(3) == 3.333);
59  REQUIRE(m2.size() == 3);
60  }
Counts up the size (in bytes) of an instance t of type T, that possesses a T::write(OutputStream) met...
An InputStream that thinly wraps a const char* buffer.
Definition: ByteStreams.h:51
Definition: syntax.dox:1
size_t iosize(const T &t)
Returns the serialized size (in bytes) of a serializable type T.
Definition: iosize.h:45
Streams that serialize/deserialize to char* buffer.
An OutputStream that thinly wraps a char* buffer.
Definition: ByteStreams.h:20


Results: [PASS]

m1 = [ (1,1.111) (2,2.222) (3,3.333) ]
buffer = [ 3 0 0 0 0 0 0 0 1 0 0 0 45 -78 -99 -17 -89 -58 -15 63 2 0 0 0 45 -78 -99 -17 -89 -58 1 64 3 0 0 0 68 -117 108 -25 -5 -87 10 64 ]
m2 = [ (1,1.111) (2,2.222) (3,3.333) ]


Go back to Summary of /test programs.