2014-06-06 17:38:37 +04:00
|
|
|
/*
|
|
|
|
Utility tests.
|
|
|
|
|
|
|
|
Copyright (c) 2012-2014, Victor Zverovich
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-07-02 19:38:31 +04:00
|
|
|
#include <cfloat>
|
|
|
|
#include <climits>
|
2014-06-06 17:38:37 +04:00
|
|
|
#include <cstring>
|
2014-06-06 19:07:05 +04:00
|
|
|
#include <limits>
|
2014-06-06 17:38:37 +04:00
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-06-06 18:29:57 +04:00
|
|
|
// Check if format.h compiles with windows.h included.
|
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "format.h"
|
|
|
|
|
2014-06-06 19:18:53 +04:00
|
|
|
#undef max
|
|
|
|
|
2014-06-06 17:38:37 +04:00
|
|
|
using fmt::StringRef;
|
2014-07-14 17:55:29 +04:00
|
|
|
using fmt::internal::Arg;
|
2014-07-16 18:55:31 +04:00
|
|
|
using fmt::internal::MakeArg;
|
2014-06-06 17:38:37 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
std::string GetSystemErrorMessage(int error_code) {
|
2014-06-11 02:38:57 +04:00
|
|
|
#if defined(__MINGW32__) || !defined(_WIN32)
|
2014-06-06 17:38:37 +04:00
|
|
|
return strerror(error_code);
|
|
|
|
#else
|
|
|
|
enum { BUFFER_SIZE = 200 };
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
EXPECT_EQ(0, strerror_s(buffer, BUFFER_SIZE, error_code));
|
|
|
|
std::size_t max_len = BUFFER_SIZE - 1;
|
|
|
|
EXPECT_LT(std::strlen(buffer), max_len);
|
|
|
|
return buffer;
|
|
|
|
#endif
|
|
|
|
}
|
2014-07-04 18:18:44 +04:00
|
|
|
|
|
|
|
struct Test {};
|
2014-07-16 18:55:31 +04:00
|
|
|
template <typename Char>
|
|
|
|
std::basic_ostream<Char> &operator<<(std::basic_ostream<Char> &os, Test) {
|
|
|
|
return os << "test";
|
|
|
|
}
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, Increment) {
|
|
|
|
char s[10] = "123";
|
|
|
|
Increment(s);
|
|
|
|
EXPECT_STREQ("124", s);
|
|
|
|
s[2] = '8';
|
|
|
|
Increment(s);
|
|
|
|
EXPECT_STREQ("129", s);
|
|
|
|
Increment(s);
|
|
|
|
EXPECT_STREQ("130", s);
|
|
|
|
s[1] = s[2] = '9';
|
|
|
|
Increment(s);
|
|
|
|
EXPECT_STREQ("200", s);
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:55:29 +04:00
|
|
|
template <Arg::Type>
|
|
|
|
struct ArgInfo;
|
|
|
|
|
|
|
|
#define ARG_INFO(type_code, Type, field) \
|
|
|
|
template <> \
|
|
|
|
struct ArgInfo<Arg::type_code> { \
|
|
|
|
static Type get(const Arg &arg) { return arg.field; } \
|
|
|
|
};
|
|
|
|
|
|
|
|
ARG_INFO(INT, int, int_value);
|
|
|
|
ARG_INFO(UINT, unsigned, uint_value);
|
|
|
|
ARG_INFO(LONG_LONG, fmt::LongLong, long_long_value);
|
|
|
|
ARG_INFO(ULONG_LONG, fmt::ULongLong, ulong_long_value);
|
|
|
|
ARG_INFO(DOUBLE, double, double_value);
|
|
|
|
ARG_INFO(LONG_DOUBLE, long double, long_double_value);
|
|
|
|
ARG_INFO(CHAR, int, int_value);
|
|
|
|
ARG_INFO(STRING, const char *, string.value);
|
|
|
|
ARG_INFO(WSTRING, const wchar_t *, wstring.value);
|
|
|
|
ARG_INFO(POINTER, const void *, pointer_value);
|
|
|
|
ARG_INFO(CUSTOM, Arg::CustomValue, custom);
|
|
|
|
|
|
|
|
#define CHECK_ARG_INFO(Type, field, value) { \
|
|
|
|
Arg arg = {Arg::Type}; \
|
|
|
|
arg.field = value; \
|
|
|
|
EXPECT_EQ(value, ArgInfo<Arg::Type>::get(arg)); \
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:55:31 +04:00
|
|
|
TEST(ArgTest, ArgInfo) {
|
2014-07-14 17:55:29 +04:00
|
|
|
CHECK_ARG_INFO(INT, int_value, 42);
|
|
|
|
CHECK_ARG_INFO(UINT, uint_value, 42);
|
|
|
|
CHECK_ARG_INFO(LONG_LONG, long_long_value, 42);
|
|
|
|
CHECK_ARG_INFO(ULONG_LONG, ulong_long_value, 42);
|
|
|
|
CHECK_ARG_INFO(DOUBLE, double_value, 4.2);
|
|
|
|
CHECK_ARG_INFO(LONG_DOUBLE, long_double_value, 4.2);
|
|
|
|
CHECK_ARG_INFO(CHAR, int_value, 'x');
|
2014-07-14 18:17:58 +04:00
|
|
|
const char STR[] = "abc";
|
|
|
|
CHECK_ARG_INFO(STRING, string.value, STR);
|
|
|
|
const wchar_t WSTR[] = L"abc";
|
|
|
|
CHECK_ARG_INFO(WSTRING, wstring.value, WSTR);
|
2014-07-14 17:55:29 +04:00
|
|
|
int p = 0;
|
|
|
|
CHECK_ARG_INFO(POINTER, pointer_value, &p);
|
|
|
|
Arg arg = {Arg::CUSTOM};
|
|
|
|
arg.custom.value = &p;
|
|
|
|
EXPECT_EQ(&p, ArgInfo<Arg::CUSTOM>::get(arg).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EXPECT_ARG_(Char, type_code, Type, value) { \
|
2014-07-03 19:50:57 +04:00
|
|
|
Type expected_value = static_cast<Type>(value); \
|
2014-07-16 18:55:31 +04:00
|
|
|
Arg arg = MakeArg<Char>(expected_value); \
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_EQ(Arg::type_code, arg.type); \
|
|
|
|
EXPECT_EQ(expected_value, ArgInfo<Arg::type_code>::get(arg)); \
|
2014-07-02 19:38:31 +04:00
|
|
|
}
|
|
|
|
|
2014-07-14 17:55:29 +04:00
|
|
|
#define EXPECT_ARG(type_code, Type, value) \
|
|
|
|
EXPECT_ARG_(char, type_code, Type, value)
|
|
|
|
|
|
|
|
#define EXPECT_ARGW(type_code, Type, value) \
|
|
|
|
EXPECT_ARG_(wchar_t, type_code, Type, value)
|
2014-07-02 19:38:31 +04:00
|
|
|
|
2014-07-16 18:55:31 +04:00
|
|
|
TEST(ArgTest, MakeArg) {
|
2014-07-02 19:38:31 +04:00
|
|
|
// Test bool.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(INT, bool, true);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test char.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(CHAR, signed char, 'a');
|
|
|
|
EXPECT_ARG(CHAR, signed char, SCHAR_MIN);
|
|
|
|
EXPECT_ARG(CHAR, signed char, SCHAR_MAX);
|
|
|
|
EXPECT_ARG(CHAR, unsigned char, 'a');
|
|
|
|
EXPECT_ARG(CHAR, unsigned char, UCHAR_MAX );
|
|
|
|
EXPECT_ARG(CHAR, char, 'a');
|
|
|
|
EXPECT_ARG(CHAR, char, CHAR_MIN);
|
|
|
|
EXPECT_ARG(CHAR, char, CHAR_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test wchar_t.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARGW(CHAR, wchar_t, L'a');
|
|
|
|
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MIN);
|
|
|
|
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test short.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(INT, short, 42);
|
|
|
|
EXPECT_ARG(INT, short, SHRT_MIN);
|
|
|
|
EXPECT_ARG(INT, short, SHRT_MAX);
|
|
|
|
EXPECT_ARG(UINT, unsigned short, 42);
|
|
|
|
EXPECT_ARG(UINT, unsigned short, USHRT_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test int.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(INT, int, 42);
|
|
|
|
EXPECT_ARG(INT, int, INT_MIN);
|
|
|
|
EXPECT_ARG(INT, int, INT_MAX);
|
|
|
|
EXPECT_ARG(UINT, unsigned, 42);
|
|
|
|
EXPECT_ARG(UINT, unsigned, UINT_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test long.
|
|
|
|
#if LONG_MAX == INT_MAX
|
|
|
|
# define LONG INT
|
|
|
|
# define ULONG UINT
|
|
|
|
# define long_value int_value
|
2014-07-02 20:12:25 +04:00
|
|
|
# define ulong_value uint_value
|
2014-07-02 19:38:31 +04:00
|
|
|
#else
|
|
|
|
# define LONG LONG_LONG
|
|
|
|
# define ULONG ULONG_LONG
|
|
|
|
# define long_value long_long_value
|
|
|
|
# define ulong_value ulong_long_value
|
|
|
|
#endif
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(LONG, long, 42);
|
|
|
|
EXPECT_ARG(LONG, long, LONG_MIN);
|
|
|
|
EXPECT_ARG(LONG, long, LONG_MAX);
|
|
|
|
EXPECT_ARG(ULONG, unsigned long, 42);
|
|
|
|
EXPECT_ARG(ULONG, unsigned long, ULONG_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test long long.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, 42);
|
|
|
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MIN);
|
|
|
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MAX);
|
|
|
|
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, 42);
|
|
|
|
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ULLONG_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test float.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(DOUBLE, float, 4.2);
|
|
|
|
EXPECT_ARG(DOUBLE, float, FLT_MIN);
|
|
|
|
EXPECT_ARG(DOUBLE, float, FLT_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test double.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(DOUBLE, double, 4.2);
|
|
|
|
EXPECT_ARG(DOUBLE, double, DBL_MIN);
|
|
|
|
EXPECT_ARG(DOUBLE, double, DBL_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test long double.
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(LONG_DOUBLE, long double, 4.2);
|
|
|
|
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MIN);
|
|
|
|
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MAX);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
|
|
|
// Test string.
|
|
|
|
char STR[] = "test";
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(STRING, char*, STR);
|
|
|
|
EXPECT_ARG(STRING, const char*, STR);
|
|
|
|
EXPECT_ARG(STRING, std::string, STR);
|
|
|
|
EXPECT_ARG(STRING, fmt::StringRef, STR);
|
2014-07-02 19:38:31 +04:00
|
|
|
|
2014-07-03 19:50:57 +04:00
|
|
|
// Test wide string.
|
|
|
|
wchar_t WSTR[] = L"test";
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARGW(WSTRING, wchar_t*, WSTR);
|
|
|
|
EXPECT_ARGW(WSTRING, const wchar_t*, WSTR);
|
|
|
|
EXPECT_ARGW(WSTRING, std::wstring, WSTR);
|
|
|
|
EXPECT_ARGW(WSTRING, fmt::WStringRef, WSTR);
|
2014-07-03 19:50:57 +04:00
|
|
|
|
2014-07-04 18:18:44 +04:00
|
|
|
int n = 42;
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_ARG(POINTER, void*, &n);
|
|
|
|
EXPECT_ARG(POINTER, const void*, &n);
|
2014-07-04 18:18:44 +04:00
|
|
|
|
|
|
|
::Test t;
|
2014-07-16 18:55:31 +04:00
|
|
|
fmt::internal::Arg arg = MakeArg<char>(t);
|
2014-07-04 18:18:44 +04:00
|
|
|
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_EQ(&t, arg.custom.value);
|
2014-07-04 18:18:44 +04:00
|
|
|
fmt::Writer w;
|
2014-07-09 03:38:50 +04:00
|
|
|
fmt::BasicFormatter<char> formatter(w);
|
2014-07-09 03:20:33 +04:00
|
|
|
arg.custom.format(&formatter, &t, "}");
|
2014-07-04 18:18:44 +04:00
|
|
|
EXPECT_EQ("test", w.str());
|
2014-07-02 19:38:31 +04:00
|
|
|
}
|
|
|
|
|
2014-07-14 17:55:29 +04:00
|
|
|
struct Result {
|
|
|
|
fmt::internal::Arg arg;
|
|
|
|
|
2014-07-16 18:55:31 +04:00
|
|
|
Result() : arg(MakeArg<char>(0xdeadbeef)) {}
|
2014-07-14 17:55:29 +04:00
|
|
|
|
|
|
|
template <typename T>
|
2014-07-16 18:55:31 +04:00
|
|
|
Result(const T& value) : arg(MakeArg<char>(value)) {}
|
|
|
|
Result(const wchar_t *s) : arg(MakeArg<wchar_t>(s)) {}
|
2014-07-14 17:55:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TestVisitor : fmt::internal::ArgVisitor<TestVisitor, Result> {
|
|
|
|
Result visit_int(int value) { return value; }
|
|
|
|
Result visit_uint(unsigned value) { return value; }
|
|
|
|
Result visit_long_long(fmt::LongLong value) { return value; }
|
|
|
|
Result visit_ulong_long(fmt::ULongLong value) { return value; }
|
|
|
|
Result visit_double(double value) { return value; }
|
|
|
|
Result visit_long_double(long double value) { return value; }
|
2014-07-14 18:17:58 +04:00
|
|
|
Result visit_char(int value) { return static_cast<char>(value); }
|
2014-07-14 19:04:17 +04:00
|
|
|
Result visit_string(Arg::StringValue<char> s) { return s.value; }
|
|
|
|
Result visit_wstring(Arg::StringValue<wchar_t> s) { return s.value; }
|
2014-07-14 18:27:07 +04:00
|
|
|
Result visit_pointer(const void *p) { return p; }
|
2014-07-14 19:04:17 +04:00
|
|
|
Result visit_custom(Arg::CustomValue c) {
|
2014-07-14 18:27:07 +04:00
|
|
|
return *static_cast<const ::Test*>(c.value);
|
|
|
|
}
|
2014-07-14 17:55:29 +04:00
|
|
|
};
|
|
|
|
|
2014-07-14 18:17:58 +04:00
|
|
|
#define EXPECT_RESULT_(Char, type_code, value) { \
|
|
|
|
Result result = TestVisitor().visit(MakeArg<Char>(value)); \
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_EQ(Arg::type_code, result.arg.type); \
|
|
|
|
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \
|
|
|
|
}
|
|
|
|
|
2014-07-14 18:17:58 +04:00
|
|
|
#define EXPECT_RESULT(type_code, value) \
|
|
|
|
EXPECT_RESULT_(char, type_code, value)
|
|
|
|
#define EXPECT_RESULTW(type_code, value) \
|
|
|
|
EXPECT_RESULT_(wchar_t, type_code, value)
|
|
|
|
|
2014-07-16 18:55:31 +04:00
|
|
|
TEST(ArgVisitorTest, VisitAll) {
|
2014-07-14 17:55:29 +04:00
|
|
|
EXPECT_RESULT(INT, 42);
|
|
|
|
EXPECT_RESULT(UINT, 42u);
|
|
|
|
EXPECT_RESULT(LONG_LONG, 42ll);
|
|
|
|
EXPECT_RESULT(ULONG_LONG, 42ull);
|
|
|
|
EXPECT_RESULT(DOUBLE, 4.2);
|
2014-07-14 18:17:58 +04:00
|
|
|
EXPECT_RESULT(LONG_DOUBLE, 4.2l);
|
|
|
|
EXPECT_RESULT(CHAR, 'x');
|
2014-07-14 18:27:07 +04:00
|
|
|
const char STR[] = "abc";
|
|
|
|
EXPECT_RESULT(STRING, STR);
|
|
|
|
const wchar_t WSTR[] = L"abc";
|
|
|
|
EXPECT_RESULTW(WSTRING, WSTR);
|
|
|
|
const void *p = STR;
|
|
|
|
EXPECT_RESULT(POINTER, p);
|
|
|
|
::Test t;
|
|
|
|
Result result = TestVisitor().visit(MakeArg<char>(t));
|
|
|
|
EXPECT_EQ(Arg::CUSTOM, result.arg.type);
|
|
|
|
EXPECT_EQ(&t, result.arg.custom.value);
|
2014-07-14 17:55:29 +04:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:55:31 +04:00
|
|
|
struct TestAnyVisitor : fmt::internal::ArgVisitor<TestAnyVisitor, Result> {
|
|
|
|
template <typename T>
|
|
|
|
Result visit_any_int(T value) { return value; }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Result visit_any_double(T value) { return value; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef EXPECT_RESULT
|
|
|
|
#define EXPECT_RESULT(type_code, value) { \
|
|
|
|
Result result = TestAnyVisitor().visit(MakeArg<char>(value)); \
|
|
|
|
EXPECT_EQ(Arg::type_code, result.arg.type); \
|
|
|
|
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ArgVisitorTest, VisitAny) {
|
|
|
|
EXPECT_RESULT(INT, 42);
|
|
|
|
EXPECT_RESULT(UINT, 42u);
|
|
|
|
EXPECT_RESULT(LONG_LONG, 42ll);
|
|
|
|
EXPECT_RESULT(ULONG_LONG, 42ull);
|
|
|
|
EXPECT_RESULT(DOUBLE, 4.2);
|
|
|
|
EXPECT_RESULT(LONG_DOUBLE, 4.2l);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestUnhandledVisitor :
|
|
|
|
fmt::internal::ArgVisitor<TestUnhandledVisitor, const char *> {
|
|
|
|
const char *visit_unhandled_arg() { return "test"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define EXPECT_UNHANDLED(value) \
|
|
|
|
EXPECT_STREQ("test", TestUnhandledVisitor().visit(MakeArg<wchar_t>(value)));
|
|
|
|
|
|
|
|
TEST(ArgVisitorTest, VisitUnhandledArg) {
|
|
|
|
EXPECT_UNHANDLED(42);
|
|
|
|
EXPECT_UNHANDLED(42u);
|
|
|
|
EXPECT_UNHANDLED(42ll);
|
|
|
|
EXPECT_UNHANDLED(42ull);
|
|
|
|
EXPECT_UNHANDLED(4.2);
|
|
|
|
EXPECT_UNHANDLED(4.2l);
|
|
|
|
EXPECT_UNHANDLED('x');
|
|
|
|
const char STR[] = "abc";
|
|
|
|
EXPECT_UNHANDLED(STR);
|
|
|
|
const wchar_t WSTR[] = L"abc";
|
|
|
|
EXPECT_UNHANDLED(WSTR);
|
|
|
|
const void *p = STR;
|
|
|
|
EXPECT_UNHANDLED(p);
|
|
|
|
EXPECT_UNHANDLED(::Test());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ArgVisitorTest, VisitInvalidArg) {
|
|
|
|
Arg arg = {static_cast<Arg::Type>(Arg::CUSTOM + 1)};
|
|
|
|
EXPECT_DEBUG_DEATH(TestVisitor().visit(arg), "Assertion");
|
|
|
|
}
|
|
|
|
|
2014-07-24 20:07:43 +04:00
|
|
|
// Tests fmt::internal::count_digits for integer type Int.
|
2014-06-06 17:38:37 +04:00
|
|
|
template <typename Int>
|
|
|
|
void TestCountDigits(Int) {
|
|
|
|
for (Int i = 0; i < 10; ++i)
|
2014-07-24 20:07:43 +04:00
|
|
|
EXPECT_EQ(1u, fmt::internal::count_digits(i));
|
2014-06-06 17:38:37 +04:00
|
|
|
for (Int i = 1, n = 1,
|
2014-07-24 20:07:43 +04:00
|
|
|
end = std::numeric_limits<Int>::max() / 10; n <= end; ++i) {
|
2014-06-06 17:38:37 +04:00
|
|
|
n *= 10;
|
2014-07-24 20:07:43 +04:00
|
|
|
EXPECT_EQ(i, fmt::internal::count_digits(n - 1));
|
|
|
|
EXPECT_EQ(i + 1, fmt::internal::count_digits(n));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, CountDigits) {
|
|
|
|
TestCountDigits(uint32_t());
|
|
|
|
TestCountDigits(uint64_t());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
TEST(UtilTest, UTF16ToUTF8) {
|
|
|
|
std::string s = "ёжик";
|
|
|
|
fmt::internal::UTF16ToUTF8 u(L"\x0451\x0436\x0438\x043A");
|
2014-07-01 05:12:57 +04:00
|
|
|
EXPECT_EQ(s, u.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
EXPECT_EQ(s.size(), u.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF8ToUTF16) {
|
|
|
|
std::string s = "лошадка";
|
|
|
|
fmt::internal::UTF8ToUTF16 u(s.c_str());
|
2014-07-01 05:12:57 +04:00
|
|
|
EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
EXPECT_EQ(7, u.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Converter>
|
|
|
|
void CheckUTFConversionError(const char *message) {
|
|
|
|
fmt::Writer out;
|
2014-07-28 02:09:05 +04:00
|
|
|
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
|
2014-07-01 04:01:31 +04:00
|
|
|
fmt::SystemError error(0, "");
|
2014-06-06 17:38:37 +04:00
|
|
|
try {
|
|
|
|
Converter(0);
|
|
|
|
} catch (const fmt::SystemError &e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(ERROR_INVALID_PARAMETER, error.error_code());
|
|
|
|
EXPECT_EQ(out.str(), error.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF16ToUTF8Error) {
|
|
|
|
CheckUTFConversionError<fmt::internal::UTF16ToUTF8>(
|
|
|
|
"cannot convert string from UTF-16 to UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF8ToUTF16Error) {
|
|
|
|
CheckUTFConversionError<fmt::internal::UTF8ToUTF16>(
|
|
|
|
"cannot convert string from UTF-8 to UTF-16");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, UTF16ToUTF8Convert) {
|
|
|
|
fmt::internal::UTF16ToUTF8 u;
|
2014-07-27 23:53:42 +04:00
|
|
|
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(0));
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
TEST(UtilTest, StrError) {
|
2014-07-27 23:53:42 +04:00
|
|
|
using fmt::internal::safe_strerror;
|
2014-06-06 17:38:37 +04:00
|
|
|
char *message = 0;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
#ifndef NDEBUG
|
2014-07-27 23:53:42 +04:00
|
|
|
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = 0, 0), "Assertion");
|
|
|
|
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = buffer, 0), "Assertion");
|
2014-06-06 17:38:37 +04:00
|
|
|
#endif
|
|
|
|
buffer[0] = 'x';
|
|
|
|
#ifdef _GNU_SOURCE
|
2014-07-27 23:53:42 +04:00
|
|
|
// Use invalid error code to make sure that safe_strerror returns an error
|
2014-06-06 17:38:37 +04:00
|
|
|
// message in the buffer rather than a pointer to a static string.
|
|
|
|
int error_code = -1;
|
|
|
|
#else
|
|
|
|
int error_code = EDOM;
|
|
|
|
#endif
|
2014-07-09 21:11:10 +04:00
|
|
|
|
2014-07-27 23:53:42 +04:00
|
|
|
int result = safe_strerror(error_code, message = buffer, BUFFER_SIZE);
|
2014-07-09 21:13:44 +04:00
|
|
|
EXPECT_EQ(0, result);
|
|
|
|
std::size_t message_size = std::strlen(message);
|
|
|
|
EXPECT_GE(BUFFER_SIZE - 1u, message_size);
|
|
|
|
EXPECT_EQ(GetSystemErrorMessage(error_code), message);
|
|
|
|
|
2014-07-27 23:53:42 +04:00
|
|
|
// safe_strerror never uses buffer on MinGW.
|
2014-07-09 21:11:10 +04:00
|
|
|
#ifndef __MINGW32__
|
2014-07-27 23:53:42 +04:00
|
|
|
result = safe_strerror(error_code, message = buffer, message_size);
|
2014-07-09 21:13:44 +04:00
|
|
|
EXPECT_EQ(ERANGE, result);
|
2014-07-27 23:53:42 +04:00
|
|
|
result = safe_strerror(error_code, message = buffer, 1);
|
2014-06-06 17:38:37 +04:00
|
|
|
EXPECT_EQ(buffer, message); // Message should point to buffer.
|
|
|
|
EXPECT_EQ(ERANGE, result);
|
|
|
|
EXPECT_STREQ("", message);
|
2014-07-09 21:11:10 +04:00
|
|
|
#endif
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*FormatErrorMessage)(
|
|
|
|
fmt::Writer &out, int error_code, StringRef message);
|
|
|
|
|
2014-07-01 01:26:29 +04:00
|
|
|
template <typename Error>
|
|
|
|
void CheckThrowError(int error_code, FormatErrorMessage format) {
|
|
|
|
fmt::SystemError error(0, "");
|
2014-06-06 17:38:37 +04:00
|
|
|
try {
|
2014-07-01 01:26:29 +04:00
|
|
|
throw Error(error_code, "test {}", "error");
|
2014-06-06 17:38:37 +04:00
|
|
|
} catch (const fmt::SystemError &e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
fmt::Writer message;
|
|
|
|
format(message, error_code, "test error");
|
2014-06-30 17:43:53 +04:00
|
|
|
EXPECT_EQ(message.str(), error.what());
|
2014-06-06 17:38:37 +04:00
|
|
|
EXPECT_EQ(error_code, error.error_code());
|
|
|
|
}
|
|
|
|
|
2014-07-28 02:09:05 +04:00
|
|
|
TEST(UtilTest, FormatSystemError) {
|
2014-06-06 17:38:37 +04:00
|
|
|
fmt::Writer message;
|
2014-07-28 02:09:05 +04:00
|
|
|
fmt::internal::format_system_error(message, EDOM, "test");
|
2014-06-29 22:51:10 +04:00
|
|
|
EXPECT_EQ(fmt::format("test: {}",
|
2014-06-30 17:43:53 +04:00
|
|
|
GetSystemErrorMessage(EDOM)), message.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 19:41:50 +04:00
|
|
|
TEST(UtilTest, SystemError) {
|
|
|
|
fmt::SystemError e(EDOM, "test");
|
|
|
|
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
|
|
|
|
EXPECT_EQ(EDOM, e.error_code());
|
2014-07-28 02:09:05 +04:00
|
|
|
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(UtilTest, ReportSystemError) {
|
|
|
|
fmt::Writer out;
|
2014-07-28 02:09:05 +04:00
|
|
|
fmt::internal::format_system_error(out, EDOM, "test error");
|
2014-06-06 17:38:37 +04:00
|
|
|
out << '\n';
|
2014-07-28 19:41:50 +04:00
|
|
|
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2014-07-28 19:41:50 +04:00
|
|
|
TEST(UtilTest, FormatWindowsError) {
|
2014-06-06 17:38:37 +04:00
|
|
|
LPWSTR message = 0;
|
|
|
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
|
|
|
ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
|
|
|
fmt::internal::UTF16ToUTF8 utf8_message(message);
|
|
|
|
LocalFree(message);
|
|
|
|
fmt::Writer actual_message;
|
2014-07-28 02:09:05 +04:00
|
|
|
fmt::internal::format_windows_error(
|
2014-06-06 17:38:37 +04:00
|
|
|
actual_message, ERROR_FILE_EXISTS, "test");
|
2014-07-01 06:11:20 +04:00
|
|
|
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
2014-07-01 05:38:45 +04:00
|
|
|
actual_message.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 19:41:50 +04:00
|
|
|
TEST(UtilTest, WindowsError) {
|
2014-07-01 03:56:22 +04:00
|
|
|
CheckThrowError<fmt::WindowsError>(
|
2014-07-28 02:09:05 +04:00
|
|
|
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 19:41:50 +04:00
|
|
|
TEST(UtilTest, ReportWindowsError) {
|
2014-06-06 17:38:37 +04:00
|
|
|
fmt::Writer out;
|
2014-07-28 02:09:05 +04:00
|
|
|
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
2014-06-06 17:38:37 +04:00
|
|
|
out << '\n';
|
|
|
|
EXPECT_WRITE(stderr,
|
2014-07-28 19:41:50 +04:00
|
|
|
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"), out.str());
|
2014-06-06 17:38:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _WIN32
|