OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_format.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2#include <iostream>
3
4
7
10
11using namespace openvpn;
12
13class MyObj
14{
15 public:
16 MyObj(int v)
17 : value(v)
18 {
19 }
20
21 std::string to_string() const
22 {
23 return std::to_string(value);
24 }
25
26 private:
27 int value;
28};
29
31
32const std::string expected = "7\n"
33 "foo\n"
34 "bar\n"
35 "3.141593\n"
36 "3\n"
37 "1\n"
38 "0\n"
39 "pi is not 3 nor is it 7 ; it is 3.14159 ...\n"
40 "pi is 'not' 3 nor is it 7 ; it is 3.141593... (and has 99% less fat!)\n"
41 "the year is 2015 and the weather is \"partly cloudy\"\n"
42 "where am I? is it still 2015?\n"
43 "no, it's 1666... bring out yer dedd?\n"
44 "save 20%!\n"
45 "no wait... save? 99.9999%!\n"
46 "extra argument is here\n"
47 "is the question true or false?\n"
48 "more extra arguments are here\n"
49 "null string ''\n"
50 "nullptr 'nullptr'\n"
51 "foo=bar non const\n"
52 "EX1: bad foo\n"
53 "EX2: this prog is done 4 U\n";
54
55TEST(misc, format)
56{
57 std::ostringstream os;
58
59 const MyObj seven(7);
60 const std::string foo = "foo";
61 const char *const bar = "bar";
62 const double pi = 3.14159265;
63 const int three = 3;
64 const std::string weather = "partly cloudy";
65 char *nc = const_cast<char *>("non const");
66
67 os << to_string(seven) << std::endl;
68 os << to_string(foo) << std::endl;
69 os << to_string(bar) << std::endl;
70 os << to_string(pi) << std::endl;
71 os << to_string(three) << std::endl;
72 os << to_string(true) << std::endl;
73 os << to_string(false) << std::endl;
74 os << prints("pi", "is", std::string("not"), 3, "nor is it", seven, ';', "it", "is", pi, "...") << std::endl;
75 os << printfmt("pi is %r %s nor is it %s ; it is %s... (and has %s%% less %s!)", "not", 3, seven, pi, 99, std::string("fat")) << std::endl;
76 os << printfmt("the year is %s and the weather is %R", 2015, weather) << std::endl;
77 os << printfmt("where am %s? is it still %s?", 'I', 2015) << std::endl;
78 os << printfmt("no, it's %s... bring out yer dedd%s", 1666) << std::endl;
79 os << printfmt("save 20%%!") << std::endl;
80 os << printfmt("no wait... save%s 99.9999%%!") << std::endl;
81 os << printfmt("extra argument is here", 1) << std::endl;
82 os << printfmt("is the question %s or %s?", true, false) << std::endl;
83 os << printfmt("more extra arguments are here", 1, 2, 3, 4) << std::endl;
84 os << printfmt("null string '%s'", static_cast<const char *>(nullptr)) << std::endl;
85 os << printfmt("nullptr '%s'", nullptr) << std::endl;
86 os << printfmt("%s=%s %s", foo, bar, nc) << std::endl;
87 try
88 {
89 const std::string exstr = "bad foo";
90 throw Exception(exstr);
91 }
92 catch (const std::exception &e)
93 {
94 os << prints("EX1:", e.what()) << std::endl;
95 }
96 try
97 {
98 throw Exception(prints("this", "prog", "is", "done", 4, 'U'));
99 }
100 catch (const std::exception &e)
101 {
102 os << prints("EX2:", e.what()) << std::endl;
103 }
104 const std::string actual = os.str();
105 ASSERT_EQ(expected, actual);
106}
107
108template <typename... Args>
109inline std::string pfmt(const std::string &fmt, Args... args)
110{
111#if 1
112 PrintFormatted<std::string> pf(fmt, 256);
113#else
115#endif
116 pf.process(args...);
117 return pf.str();
118}
119
120void perf()
121{
122 const MyObj seven(7);
123 // const double pi = 3.14159265;
124 size_t count = 0;
125 const std::string weather = "partly cloudy";
126 for (long i = 0; i < 1000000; ++i)
127 {
128 const std::string str = pfmt("the year is %s and the weather is %r", 2015, weather);
129 // const std::string str = pfmt("this program is brought to you by the number %s", seven);
130 // const std::string str = pfmt("foo %s", 69);
131 // const std::string str = pfmt("foo");
132 // const std::string str = pfmt("foo %s %s", 69, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
133 // const std::string str = pfmt("pi is %s %s nor is it %s ; it is %s... (and has %s%% less %s!)", "not", 3, seven, pi, 99, std::string("fat"));
134 count += str.length();
135 }
136 std::cout << count << std::endl;
137}
int value
std::string to_string() const
MyObj(int v)
std::string str()
Definition format.hpp:243
std::string prints(Args... args)
Definition format.hpp:64
std::string printfmt(const std::string &fmt, Args... args)
Definition format.hpp:314
#define OPENVPN_OSTREAM(TYPE, METH)
Definition ostream.hpp:21
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::ostringstream os
TEST(misc, format)
const std::string expected
std::string pfmt(const std::string &fmt, Args... args)
void perf()