OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_ostream_containers.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
4#include <list>
5#include <complex>
6#include <deque>
7
8using namespace openvpn;
9
10// simple use case example; cases will mostly look like this
11TEST(ostream_container, simple_vector_int)
12{
13 std::vector<int> vi{2, 4, 6, 8};
14 std::ostringstream oss;
15
16 oss << C2os::cast(vi);
17
18 ASSERT_EQ(oss.str(), std::string("[2, 4, 6, 8]"));
19}
20
21// TestItem and generic_test() allow short implementation of tests for arbitrary
22// conforming containers that hold arbitrary conforming types
23template <typename Container>
25{
26 TestItem(Container &&c, std::string &&s)
27 : container(std::move(c)), expected(std::move(s))
28 {
29 }
30
31 const Container container;
32 const std::string expected;
33};
34
35template <typename Tests>
36void generic_test(const Tests &tests)
37{
38 for (auto &test : tests)
39 {
40 std::ostringstream oss;
41
42 oss << C2os::cast(test.container);
43
44 EXPECT_EQ(oss.str(), test.expected);
45 }
46}
47
48template <template <typename...> typename Coll_T, typename Val_T>
49auto container_of_pointers(const Coll_T<Val_T> &colln)
50{
51 Coll_T<Val_T *> transformed;
52 typename Coll_T<Val_T>::const_iterator inIt = colln.begin(), inEndIt = colln.end();
53 // transform here
54 std::transform(inIt, inEndIt, std::back_inserter(transformed), [](const Val_T &in) -> Val_T *
55 { return const_cast<Val_T *>(&in); });
56
57 return transformed;
58}
59
60template <typename Tests>
61void generic_ptr_test(const Tests &tests)
62{
63 for (auto &test : tests)
64 {
65 auto ptr_colln = container_of_pointers(test.container);
66
67 std::ostringstream oss;
68
69 oss << C2os::cast_deref(ptr_colln);
70
71 EXPECT_EQ(oss.str(), test.expected);
72 }
73}
74
75// tests for int/set, string/list, complex/vector, and custom/deque
76using ssi = std::set<int>;
78 TestItem<ssi>({3, 5, 7, 11}, "[3, 5, 7, 11]"),
79 TestItem<ssi>({-3, 5, -7, 0}, "[-7, -3, 0, 5]"),
80 TestItem<ssi>({}, "[]"),
81};
82
83TEST(ostream_container, set_int)
84{
86
87 // the test harness, as is, does not work for sets of pointers. The first reason is
88 // that, in translating the non-pointer test data, transform uses a back_inserter;
89 // not supported by set. The second reason, since we could work around the first
90 // with a special set specific transform'er, is that the order of the \c int* 's
91 // would not match the order of the \c int 's; so we could not use the same
92 // "expected" value.
93 // generic_ptr_test(set_int_tests);
94}
95
96using sls = std::list<std::string>;
98 TestItem<sls>({"Alfred", "E.", "Neuman"}, "[Alfred, E., Neuman]"),
99 TestItem<sls>({"Institute has", "the finest", "professors"}, "[Institute has, the finest, professors]"),
100};
101
102TEST(ostream_container, list_string)
103{
106}
107
108using svc = std::vector<std::complex<double>>;
110 TestItem<svc>({{1.5, 2.0}, {6.4, 7.2}, {8.9, 0.4}}, "[(1.5,2), (6.4,7.2), (8.9,0.4)]"),
111 TestItem<svc>({{-5, 0}, {18, 500}, {1e6, 32}}, "[(-5,0), (18,500), (1e+06,32)]"),
112};
113
114TEST(ostream_container, vector_complex)
115{
118}
119
120struct MyComplex : public std::complex<double>
121{
122 MyComplex(double re, double im)
123 : std::complex<double>(re, im)
124 {
125 }
126};
127
128std::ostream &operator<<(std::ostream &os, const MyComplex &mc)
129{
130 os << mc.real() << "+i" << mc.imag();
131 return os;
132}
133
134using sdm = std::deque<MyComplex>;
136 TestItem<sdm>({{1.5, 2.0}, {6.4, 7.2}, {8.9, 0.4}}, "[1.5+i2, 6.4+i7.2, 8.9+i0.4]"),
137 TestItem<sdm>({{-5, 0}, {18, 500}, {1e6, 32}}, "[-5+i0, 18+i500, 1e+06+i32]"),
138};
139
140TEST(ostream_container, deque_custom)
141{
144}
145// end: tests for int/set, string/list, complex/vector, and custom/deque
const auto cast(const C &container)
Provide an instance of C2os::Container<C> from the underlying container.
const auto cast_deref(const C &container)
Provide an instance of C2os::PtrContainer<C> from the underlying container.
std::basic_ostream< Elem, Traits > & operator<<(std::basic_ostream< Elem, Traits > &os, const SafeString &ss)
Definition safestr.hpp:180
Coercion safe method to insert a container into an ostream.
MyComplex(double re, double im)
TestItem(Container &&c, std::string &&s)
const std::string expected
const Container container
std::ostringstream os
std::vector< std::complex< double > > svc
void generic_ptr_test(const Tests &tests)
std::deque< MyComplex > sdm
const TestItem< ssi > set_int_tests[]
const TestItem< svc > vector_complex_tests[]
const TestItem< sdm > deque_custom_tests[]
TEST(ostream_container, simple_vector_int)
const TestItem< sls > list_string_tests[]
std::set< int > ssi
auto container_of_pointers(const Coll_T< Val_T > &colln)
void generic_test(const Tests &tests)
std::list< std::string > sls
void test()
Definition test_rc.cpp:80