Debian Codegraph @

tests/proxyIO.cc

raw
1 #include <iostream>
2 #include <cstdlib>
3 #include <tuple>
4 #include <utility>
5 #include <sstream>
6
7 #include "test_utils.hh"
8
9 #include "ProxyPack.hh"
10
11 using namespace gs;
12 using namespace std;
13
14 int main(int argc, char const* argv[])
15 {
16 typedef CPP11_shared_ptr<double> Dptr;
17 typedef CPP11_shared_ptr<int> Iptr;
18
19 auto tuple1 = std::tuple<int, float, double, long long>();
20 cout << "Tuple class id is " << ClassId::makeId<decltype(tuple1)>().id()
21 << endl;
22
23 std::ostringstream os1;
24 std::get<0>(tuple1) = 2;
25 std::get<1>(tuple1) = 3.f;
26 std::get<2>(tuple1) = 4.0;
27 std::get<3>(tuple1) = 5;
28 write_item(os1, tuple1);
29 auto tuplecopy(tuple1);
30 std::get<0>(tuple1) = 6;
31 std::get<1>(tuple1) = 7.f;
32 std::get<2>(tuple1) = 8.0;
33 std::get<3>(tuple1) = 9;
34 write_item(os1, tuple1, false);
35
36 std::istringstream is1(os1.str());
37 auto tuple2 = std::tuple<int, float, double, long long>();
38 restore_item(is1, &tuple2);
39 assert(tuplecopy == tuple2);
40 assert(tuple1 != tuple2);
41 restore_item(is1, &tuple2, false);
42 assert(tuple1 == tuple2);
43
44 int i1 = 15;
45
46 IOProxy<int> ib(i1, "i1");
47 std::ostringstream os;
48
49 i1 = 10;
50 ib.write(os);
51 i1 = 15;
52 ib.write(os);
53 i1 = 20;
54 ib.write(os);
55
56 Dptr dp(new double());
57 IOProxy<Dptr> bd(dp, "dp");
58 *dp = 1.0;
59 assert(bd.write(os));
60 *dp = 3.0;
61 assert(bd.write(os));
62 *dp = 123.0;
63 assert(bd.write(os));
64
65 auto agw = make_proxy_pack(
66 pack_member(i1),
67 pack_member(dp)
68 );
69 i1 = 30;
70 *dp = 345.0;
71 assert(agw.write(os));
72
73 std::istringstream is(os.str());
74 int ival;
75 IOProxy<int> ivb(ival, "ival");
76 ClassId dummy(ClassId::invalidId());
77 IOProxy<int>::restore(dummy, is, &ivb);
78 assert(ivb.name() == "i1");
79 assert(ival == 10);
80 IOProxy<int>::restore(dummy, is, &ivb);
81 assert(ival == 15);
82 IOProxy<int>::restore(dummy, is, &ivb);
83 assert(ival == 20);
84
85 Dptr rbd;
86 IOProxy<Dptr> br(rbd, "name should not matter here");
87 IOProxy<Dptr>::restore(dummy, is, &br);
88 assert(br.name() == "dp");
89 assert(*rbd == 1.0);
90 IOProxy<Dptr>::restore(dummy, is, &br);
91 assert(*rbd == 3.0);
92 IOProxy<Dptr>::restore(dummy, is, &br);
93 assert(*rbd == 123.0);
94
95 auto rag = make_proxy_pack(
96 pack_member(ival),
97 pack_member(rbd)
98 );
99
100 cout << "ProxyPack class name is " << rag.classId().name() << endl;
101 assert(rag.restore(agw.classId(), is, &rag));
102 cout << "Var 0 enabled: " << std::get<0>(rag).isEnabled() << endl;
103 cout << "Var 1 enabled: " << std::get<1>(rag).isEnabled() << endl;
104 assert(ival == 30);
105 assert(*rbd == 345.0);
106
107 // Tuple-related ops
108 //
109 // tuple_cat -- concatenate two tuples
110 // tie
111 // swap
112 // tuple_size<tuple>::value
113 // tuple_element<N, tuple>::type
114 // get<N, tuple>x
115 // make_tuple
116
117 return 0;
118 }