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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/Log/ILog.h"
#include "System/TimeProfiler.h"
#include "lib/streflop/streflop_cond.h"
#include "lib/lua/include/LuaUser.h"
#define __USE_MINGW_ANSI_STDIO 1 // use __mingw_sprintf() for sprintf(), instead of __builtin_sprintf()
#include <stdio.h>
#include <vector>
#include <limits>
#include <cmath>
#define BOOST_TEST_MODULE Printf
#include <boost/test/unit_test.hpp>
static char s[128];
constexpr const char* FMT_STRING = "%.14g";
static inline float randf() {
return rand() / float(RAND_MAX);
}
static inline float RandFloat(const float min, const float max) {
// we work with large floats here (1e22 etc.) we cannot blend them without overflows, so do it a bit differently
if (rand() & 1) {
return max * randf();
}
return min * randf();
}
static inline float RandInt() {
return rand() - rand();
}
static const std::vector<std::pair<float, const char*>> testNumbers = {
// precision
{1200.12345, "1200.12341"},
{4266.38345015625, "4266.3833"},
{1.6, "1.60000002"},
{0.016, "0.016"},
{-2, "-2"},
{-1.0000003, "-1.0000004"},
{math::acos(-1.f), "3.14159274" /*3.141592653589793*/}, // PI
// large/extreme numbers
{7.5e-08, "0.00000008"},
{7e-16, "7.0e-16"},
{1e-16, "1.0e-16"},
{1e16, "1.0e+16"},
{1000000000, "1000000000"},
{10000000000, "1.0e+10"},
{1e22, "1.0e+22"}, // larger than max int64!
{std::pow(2, 24), "16777216"},
{std::numeric_limits<int>::max(), "2147483648"},
{std::numeric_limits<float>::max(), "3.4028e+38"},
{std::numeric_limits<float>::min(), "1.1755e-38"},
// infinite numbers
{std::numeric_limits<float>::quiet_NaN(), "nan"},
// some clang versions have difficulties with nan
// https://llvm.org/bugs/show_bug.cgi?id=16666
// https://stackoverflow.com/a/32224172
#ifndef __clang__
{-std::numeric_limits<float>::quiet_NaN(), "-nan"},
#endif
{std::numeric_limits<float>::infinity(), "inf"},
{-std::numeric_limits<float>::infinity(), "-inf"}
};
BOOST_AUTO_TEST_CASE( SpringFormat )
{
streflop::streflop_init<streflop::Simple>();
// printf & spring output should be equal when a precision is specified
LOG("");
LOG("--------------------");
LOG("-- spring_lua_format");
LOG("%40s %40s", "[spring]", "[printf]");
const std::vector<const char*> fmts = {
"11.3", "+7.1" , " 3.5", ".0", ".15"
};
char s1[128];
char s2[128];
auto CHECK_FORMAT = [&](const float f, const char* fmt) {
spring_lua_format(f, fmt, s1);
sprintf(s2, (std::string("%") + fmt + "f").c_str(), f);
LOG("%40s %40s", s1, s2);
#ifdef WIN32
if (!std::isnan(f)) // window's printf doesn't support signed NaNs
#endif
BOOST_CHECK(strcmp(s1, s2) == 0);
};
// test #1
for (const auto& fmt: fmts) {
LOG("format: %%%sf", fmt);
for (const auto& p: testNumbers) {
CHECK_FORMAT(p.first, fmt);
}
}
// test #2
srand( 0 );
for (int j=25000; j>0; --j) {
const float f = RandFloat(1e-22, 1e22);
sprintf(s1, "%+.5f", f);
spring_lua_format(f, "+.5", s2);
BOOST_CHECK(strcmp(s1, s2) == 0);
}
}
BOOST_AUTO_TEST_CASE( Printf )
{
LOG("");
LOG("----------------------");
LOG("-- printf comparisions");
#ifdef _WIN32
LOG("\n__mingw_sprintf:");
_set_output_format(_TWO_DIGIT_EXPONENT);
for (const auto& p: testNumbers) {
__mingw_sprintf(s, FMT_STRING, p.first);
LOG("%20s [%s]", s, p.second);
BOOST_WARN(strcmp(s, p.second) == 0);
}
LOG("\n__builtin_sprintf:");
for (const auto& p: testNumbers) {
__builtin_sprintf(s, FMT_STRING, p.first);
LOG("%20s [%s]", s, p.second);
BOOST_WARN(strcmp(s, p.second) == 0); // it's known to not sync `Mingw vs. Linux`
}
#endif
LOG("\nsprintf:");
for (const auto& p: testNumbers) {
sprintf(s, FMT_STRING, p.first);
LOG("%20s [%s]", s, p.second);
BOOST_WARN(strcmp(s, p.second) == 0); // not sync Mingw vs Linux
}
LOG("\nspring_lua_ftoa:");
for (const auto& p: testNumbers) {
spring_lua_ftoa(p.first, s);
LOG("%20s [%s]", s, p.second);
BOOST_CHECK(strcmp(s, p.second) == 0);
}
}
BOOST_AUTO_TEST_CASE( Roundings )
{
LOG("");
LOG("----------------");
LOG("-- roundings");
static const std::vector<std::tuple<float, const char*, const char*>> testNumbers = {
std::make_tuple(0.9996f, "1.000", ".3"),
std::make_tuple(0.9995f, "1.000", ".3"),
std::make_tuple(0.99945f, "0.999", ".3"),
std::make_tuple(1999999.0f, "1999999", ".0"),
std::make_tuple(1999999.7f, "2000000", ".0"),
std::make_tuple(-1999999.0f, "-1999999", ".0"),
std::make_tuple(-1999999.7f, "-2000000", ".0")
};
for (const auto& p: testNumbers) {
spring_lua_format(std::get<0>(p), std::get<2>(p), s);
LOG("%f -\"%%%sf\"-> %s [%s]", std::get<0>(p), std::get<2>(p), s, std::get<1>(p));
BOOST_CHECK(strcmp(s, std::get<1>(p)) == 0);
}
}
BOOST_AUTO_TEST_CASE( Precision )
{
LOG("");
LOG("----------------");
LOG("-- precision");
char s1[128];
char s2[128];
char s3[128];
auto CHECK_PRECISION = [&](const float f) {
sprintf(s1, FMT_STRING, f);
const double floatPrintf = atof(s1);
sprintf(s3, "%.8f", f);
const double floatPrintf2 = atof(s3);
spring_lua_ftoa(f, s2);
const double floatSpring = atof(s2);
const double diffPrintf = std::abs(f - floatPrintf);
const double diffPrintf2 = std::abs(f - floatPrintf2);
const double diffSpring = std::abs(f - floatSpring);
const double maxDiff = std::abs(f * 1e-4);
bool inPrecisionRange = false;
inPrecisionRange |= (diffSpring <= maxDiff);
inPrecisionRange |= (diffSpring <= (diffPrintf * 1.5f));
// spring_lua_ftoa hasn't used scientific notation,
// but sprintf might have (check the non-scientific sprintf one then)
if (strchr(s2, 'e') == nullptr)
inPrecisionRange |= (diffSpring <= (diffPrintf2 * 1.5f));
if (!inPrecisionRange) {
LOG_L(L_ERROR, "number=%e maxError=%e printf=%s [error=%e] spring=%s [error=%e]", f, maxDiff, s1, diffPrintf, s2, diffSpring);
}
BOOST_CHECK(inPrecisionRange);
};
for (const auto& p: testNumbers) {
if (!std::isfinite(p.first))
continue;
CHECK_PRECISION(p.first);
}
srand( 0 );
for (int j=25000; j>0; --j) {
CHECK_PRECISION(RandFloat(1e-5, 1e5));
}
}
BOOST_AUTO_TEST_CASE( Performance )
{
LOG("");
LOG("----------------");
LOG("-- performance");
const int iterations = 1000000;
// small floats
{
srand( 0 );
ScopedOnceTimer foo("printf() - floats");
for (int j=iterations; j>0; --j) {
sprintf(s, FMT_STRING, randf() * 10000);
}
}
{
srand( 0 );
ScopedOnceTimer foo("spring_lua_ftoa() - floats");
for (int j=iterations; j>0; --j) {
spring_lua_ftoa(randf() * 10000, s);
}
}
// large floats
{
srand( 0 );
ScopedOnceTimer foo("printf() - large floats");
for (int j=iterations; j>0; --j) {
sprintf(s, FMT_STRING, RandFloat(1e-22, 1e22));
}
}
{
srand( 0 );
ScopedOnceTimer foo("spring_lua_ftoa() - large floats");
for (int j=iterations; j>0; --j) {
spring_lua_ftoa(RandFloat(1e-22, 1e22), s);
}
}
// integers
{
srand( 0 );
ScopedOnceTimer foo("printf() - ints");
for (int j=iterations; j>0; --j) {
sprintf(s, FMT_STRING, RandInt());
}
}
{
srand( 0 );
ScopedOnceTimer foo("spring_lua_ftoa() - ints");
for (int j=iterations; j>0; --j) {
spring_lua_ftoa(RandInt(), s);
}
}
}
|