MyraMath
roundtrip_associative


Source: tests/io/roundtrip.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 // Serialization backend that just saves to std::vector<char>.
13 
14 // Test serialization of basic types.
15 #include <myramath/io/detail/complex.h>
16 #include <myramath/io/detail/pair.h>
17 #include <myramath/io/detail/string.h>
18 
19 // Test serialization of sequence containers.
20 #include <myramath/io/detail/vector.h>
21 #include <myramath/io/detail/list.h>
22 #ifdef MYRAMATH_ENABLE_CPP11
23 #include <myramath/io/detail/array.h>
24 #endif
25 
26 // Test serialization of associative containers.
27 #include <myramath/io/detail/map.h>
28 #include <myramath/io/detail/set.h>
29 #ifdef MYRAMATH_ENABLE_CPP11
30 #include <myramath/io/detail/unordered_map.h>
31 #include <myramath/io/detail/unordered_set.h>
32 #endif
33 
34 // Reporting.
35 #include <tests/myratest.h>
36 
37 using namespace myra;
38 
39 namespace {
40 
41 // Write t1, read back into t2 and compare.
42 template<class T> void test(const T& t1)
43  {
44  VectorStream inout;
45  inout.write(t1);
46  T t2 = inout.read<T>();
47  bool equivalent = (t1 == t2);
48  REQUIRE(equivalent);
49  }
50 
51 }
52 
53 // Basic pod types.
54 ADD_TEST("roundtrip_pod","[io]")
55  {
56  test<size_t>(423876);
57  test<int>(26);
58  test<char>('x');
59  test<float>(0.8f);
60  test<double>( std::sqrt(2.0) );
61  }
62 
63 // Simple aggregate types.
64 ADD_TEST("roundtrip_struct","[io]")
65  {
66  typedef std::complex<float> C;
67  test<C>(C(0.8f,6.2f));
68  typedef std::complex<double> Z;
69  test<Z>(Z(4.1,2.7));
70  typedef std::pair<int,double> P;
71  test<P>(P(12,2.9));
72  }
73 
74 // Sequence containers.
75 ADD_TEST("roundtrip_sequence","[io]")
76  {
77  // std::string
78  test<std::string> ("hello");
79  typedef std::vector<float> V;
80  // std::vector
81  V v; v.push_back(1.0f); v.push_back(3.14159f); v.push_back(2.71828f);
82  test<V> (v);
83  // std::list
84  typedef std::list<double> L;
85  L l; l.push_back(1.0); l.push_back(4.0); l.push_back(9.0); l.push_back(16.0);
86  test<L> (l);
87 #ifdef MYRAMATH_ENABLE_CPP11
88  // std::array
89  typedef std::array<int,3> A;
90  test<A>( A({5,2,9}) );
91 #endif
92  }
93 
94 // Associative containers.
95 ADD_TEST("roundtrip_associative","[io]")
96  {
97  // std::set
98  typedef std::set< std::string > S;
99  S s; s.insert("apple"); s.insert("orange"); s.insert("grape");
100  test<S>(s);
101  // std::map
102  typedef std::map<int,std::string> M;
103  M m; m[0] = "zero"; m[1] = "one"; m[3] = "three";
104  test<M>(m);
105 #ifdef MYRAMATH_ENABLE_CPP11
106  // std::unordered_set
107  typedef std::unordered_set<int> US;
108  US us; us.insert(2); us.insert(3); us.insert(5); us.insert(7); us.insert(9);
109  test<US>(us);
110  // std::unordered_map
111  typedef std::unordered_map< std::string, std::string > UM;
112  UM um; um["KY"] = "Kentucky"; um["OH"] = "Ohio"; um["IA"] = "Indiana";
113  test<UM>(um);
114 #endif
115  }
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
Definition: syntax.dox:1
A stream that serialize/deserializes to std::vector<char> buffer.


Results: [PASS]


Go back to Summary of /test programs.