-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtinyformat-test.cc
182 lines (171 loc) · 5.28 KB
/
tinyformat-test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#if defined(__linux__) && defined(__clang__)
// Workaround for bug in gcc 4.4 standard library headers when compling with
// clang in C++11 mode.
namespace std { class type_info; }
#endif
#include <stdexcept>
#include <climits>
#include <cfloat>
#include <cstddef>
#ifdef SPEED_TEST
#ifdef HAVE_FORMAT
# include "fmt/format.h"
# include "fmt/compile.h"
#endif
#if __has_include(<boost/format.hpp>)
# include <boost/format.hpp>
# define HAVE_BOOST
#endif
#if __has_include(<folly/Format.h>)
# include <folly/Format.h>
# define HAVE_FOLLY
#endif
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#include <iomanip>
#include <stdio.h>
#endif
// Throw instead of abort() so we can test error conditions.
#define TINYFORMAT_ERROR(reason) \
throw std::runtime_error(reason);
#include "tinyformat.h"
#include <cassert>
#if 0
// Compare result of tfm::format() to C's sprintf().
template<typename... Args>
void compareSprintf(const Args&... args)
{
std::string tfmResult = tfm::format(args...);
char sprintfResult[200];
sprintf(sprintfResult, args...);
if(tfmResult != sprintfResult)
{
std::cout << tfmResult << std::endl;
std::cout << sprintfResult << std::endl;
assert(0 && "results didn't match, see above.");
}
}
#endif
#define EXPECT_ERROR(expression) \
{ \
try { expression; assert(0 && "expected exception in " \
#expression); } \
catch(std::runtime_error&) {} \
}
#define CHECK_EQUAL(a, b) \
if(!((a) == (b))) \
{ \
std::cout << "test failed, line " << __LINE__ << "\n"; \
std::cout << (a) << " != " << (b) << "\n"; \
std::cout << "[" #a ", " #b "]\n"; \
++nfailed; \
}
#ifdef FMT_PROFILE
# include <gperftools/profiler.h>
// Make sure the profiler library is linked in.
static int profiling_enabled = ProfilingIsEnabledForAllThreads();
#endif
#ifdef SPEED_TEST
void speedTest(const std::string& which)
{
// Following is required so that we're not limited by per-character
// buffering.
std::ios_base::sync_with_stdio(false);
const long maxIter = 2000000L;
if(which == "printf")
{
// libc version
for(long i = 0; i < maxIter; ++i)
printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
}
else if(which == "iostreams")
{
// Std iostreams version. What a mess!!
for(long i = 0; i < maxIter; ++i)
std::cout << std::setprecision(10) << std::fixed << 1.234 << ':'
<< std::resetiosflags(std::ios::floatfield)
<< std::setw(4) << std::setfill('0') << 42 << std::setfill(' ') << ':'
<< std::setiosflags(std::ios::showpos) << 3.13 << std::resetiosflags(std::ios::showpos) << ':'
<< "str" << ':'
<< (void*)1000 << ':'
<< 'X' << ":%\n";
}
else if(which == "tinyformat")
{
// tinyformat version.
for(long i = 0; i < maxIter; ++i)
tfm::printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n",
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
}
#ifdef HAVE_FORMAT
else if(which == "format")
{
// fmt version.
for(long i = 0; i < maxIter; ++i)
fmt::print("{:.10f}:{:04}:{:+}:{}:{}:{}:%\n",
1.234, 42, 3.13, "str", (void*)1000, 'X');
}
else if(which == "fmt::compile")
{
// fmt version (compiled).
for(long i = 0; i < maxIter; ++i)
{
char buf[100];
//fmt::memory_buffer buf;
auto finished_at = fmt::format_to(
buf, FMT_COMPILE("{:.10f}:{:04}:{:+}:{}:{}:{}:%\n"),
1.234, 42, 3.13, "str", (void*)1000, 'X');
*finished_at = '\0';
std::puts(buf);
}
}
#endif
else if(which == "folly")
{
#ifdef HAVE_FOLLY
// folly::format version
for(long i = 0; i < maxIter; ++i)
std::cout << folly::format("{:.10f}:{:04}:{:+}:{}:{}:{}:%\n",
1.234, 42, 3.13, "str", (void*)1000, 'X');
#else
fprintf(stderr, "folly is not available\n");
#endif
}
else if(which == "boost")
{
#ifdef HAVE_BOOST
// boost::format version
for(long i = 0; i < maxIter; ++i)
std::cout << boost::format("%0.10f:%04d:%+g:%s:%p:%c:%%\n")
% 1.234 % 42 % 3.13 % "str" % (void*)1000 % (int)'X';
#else
fprintf(stderr, "boost is not available\n");
#endif
}
else if(which == "stb_sprintf")
{
char buf[100];
// stb_sprintf version
for(long i = 0; i < maxIter; ++i) {
stbsp_sprintf(buf, "%0.10f:%04d:%+g:%s:%p:%c:%%\n",
1.234, 42, 3.13, "str", (void*)1000, (int)'X');
fputs(buf, stdout);
}
}
else
{
assert(0 && "speed test for which version?");
}
}
#endif
int main(int argc, char* argv[])
{
#ifdef SPEED_TEST
if(argc >= 2)
speedTest(argv[1]);
return 0;
#else
return unitTests();
#endif
}