mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-27 03:21:34 +00:00
Test MakeArg and handle bool, signed char and unsigned char.
This commit is contained in:
parent
6e5551e77a
commit
9fbdc32adc
8
format.h
8
format.h
@ -351,7 +351,7 @@ class CharTraits;
|
|||||||
template <>
|
template <>
|
||||||
class CharTraits<char> : public BasicCharTraits<char> {
|
class CharTraits<char> : public BasicCharTraits<char> {
|
||||||
private:
|
private:
|
||||||
// Conversion from wchar_t to char is not supported.
|
// Conversion from wchar_t to char is not allowed.
|
||||||
static char ConvertChar(wchar_t);
|
static char ConvertChar(wchar_t);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -596,9 +596,9 @@ struct Arg {
|
|||||||
union {
|
union {
|
||||||
int int_value;
|
int int_value;
|
||||||
unsigned uint_value;
|
unsigned uint_value;
|
||||||
double double_value;
|
|
||||||
LongLong long_long_value;
|
LongLong long_long_value;
|
||||||
ULongLong ulong_long_value;
|
ULongLong ulong_long_value;
|
||||||
|
double double_value;
|
||||||
long double long_double_value;
|
long double long_double_value;
|
||||||
const void *pointer_value;
|
const void *pointer_value;
|
||||||
StringValue<char> string;
|
StringValue<char> string;
|
||||||
@ -623,7 +623,7 @@ class MakeArg : public Arg {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MakeArg() {}
|
MakeArg() {}
|
||||||
// TODO: unsigned char & signed char
|
MakeArg(bool value) { type = INT; int_value = value; }
|
||||||
MakeArg(short value) { type = INT; int_value = value; }
|
MakeArg(short value) { type = INT; int_value = value; }
|
||||||
MakeArg(unsigned short value) { type = UINT; uint_value = value; }
|
MakeArg(unsigned short value) { type = UINT; uint_value = value; }
|
||||||
MakeArg(int value) { type = INT; int_value = value; }
|
MakeArg(int value) { type = INT; int_value = value; }
|
||||||
@ -651,6 +651,8 @@ class MakeArg : public Arg {
|
|||||||
MakeArg(float value) { type = DOUBLE; double_value = value; }
|
MakeArg(float value) { type = DOUBLE; double_value = value; }
|
||||||
MakeArg(double value) { type = DOUBLE; double_value = value; }
|
MakeArg(double value) { type = DOUBLE; double_value = value; }
|
||||||
MakeArg(long double value) { type = LONG_DOUBLE; long_double_value = value; }
|
MakeArg(long double value) { type = LONG_DOUBLE; long_double_value = value; }
|
||||||
|
MakeArg(signed char value) { type = CHAR; int_value = value; }
|
||||||
|
MakeArg(unsigned char value) { type = CHAR; int_value = value; }
|
||||||
MakeArg(char value) { type = CHAR; int_value = value; }
|
MakeArg(char value) { type = CHAR; int_value = value; }
|
||||||
MakeArg(wchar_t value) {
|
MakeArg(wchar_t value) {
|
||||||
type = CHAR;
|
type = CHAR;
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cfloat>
|
||||||
|
#include <climits>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include "gtest-extra.h"
|
#include "gtest-extra.h"
|
||||||
@ -70,6 +72,102 @@ TEST(UtilTest, Increment) {
|
|||||||
EXPECT_STREQ("200", s);
|
EXPECT_STREQ("200", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define EXPECT_ARG_(Char, type_code, Type, field, value) { \
|
||||||
|
Type expected_value = value; \
|
||||||
|
fmt::internal::Arg arg = \
|
||||||
|
fmt::internal::MakeArg<Char>(expected_value); \
|
||||||
|
EXPECT_EQ(fmt::internal::Arg::type_code, arg.type); \
|
||||||
|
EXPECT_EQ(expected_value, arg.field); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define EXPECT_ARG(type_code, Type, field, value) \
|
||||||
|
EXPECT_ARG_(char, type_code, Type, field, value)
|
||||||
|
|
||||||
|
#define EXPECT_ARGW(type_code, Type, field, value) \
|
||||||
|
EXPECT_ARG_(wchar_t, type_code, Type, field, value)
|
||||||
|
|
||||||
|
TEST(UtilTest, MakeArg) {
|
||||||
|
// Test bool.
|
||||||
|
EXPECT_ARG(INT, bool, int_value, true);
|
||||||
|
|
||||||
|
// Test char.
|
||||||
|
EXPECT_ARG(CHAR, signed char, int_value, 42);
|
||||||
|
EXPECT_ARG(CHAR, signed char, int_value, SCHAR_MIN);
|
||||||
|
EXPECT_ARG(CHAR, signed char, int_value, SCHAR_MAX);
|
||||||
|
EXPECT_ARG(CHAR, unsigned char, int_value, 42);
|
||||||
|
EXPECT_ARG(CHAR, unsigned char, int_value, UCHAR_MAX );
|
||||||
|
EXPECT_ARG(CHAR, char, int_value, 'a');
|
||||||
|
EXPECT_ARG(CHAR, char, int_value, CHAR_MIN);
|
||||||
|
EXPECT_ARG(CHAR, char, int_value, CHAR_MAX);
|
||||||
|
|
||||||
|
// Test wchar_t.
|
||||||
|
EXPECT_ARGW(CHAR, wchar_t, int_value, 42);
|
||||||
|
EXPECT_ARGW(CHAR, wchar_t, int_value, WCHAR_MIN);
|
||||||
|
EXPECT_ARGW(CHAR, wchar_t, int_value, WCHAR_MAX);
|
||||||
|
|
||||||
|
// Test short.
|
||||||
|
EXPECT_ARG(INT, short, int_value, 42);
|
||||||
|
EXPECT_ARG(INT, short, int_value, SHRT_MIN);
|
||||||
|
EXPECT_ARG(INT, short, int_value, SHRT_MAX);
|
||||||
|
EXPECT_ARG(UINT, unsigned short, uint_value, 42);
|
||||||
|
EXPECT_ARG(UINT, unsigned short, uint_value, USHRT_MAX);
|
||||||
|
|
||||||
|
// Test int.
|
||||||
|
EXPECT_ARG(INT, int, int_value, 42);
|
||||||
|
EXPECT_ARG(INT, int, int_value, INT_MIN);
|
||||||
|
EXPECT_ARG(INT, int, int_value, INT_MAX);
|
||||||
|
EXPECT_ARG(UINT, unsigned, uint_value, 42);
|
||||||
|
EXPECT_ARG(UINT, unsigned, uint_value, UINT_MAX);
|
||||||
|
|
||||||
|
// Test long.
|
||||||
|
#if LONG_MAX == INT_MAX
|
||||||
|
# define LONG INT
|
||||||
|
# define ULONG UINT
|
||||||
|
# define long_value int_value
|
||||||
|
# define ulong_value iint_value
|
||||||
|
#else
|
||||||
|
# define LONG LONG_LONG
|
||||||
|
# define ULONG ULONG_LONG
|
||||||
|
# define long_value long_long_value
|
||||||
|
# define ulong_value ulong_long_value
|
||||||
|
#endif
|
||||||
|
EXPECT_ARG(LONG, long, long_value, 42);
|
||||||
|
EXPECT_ARG(LONG, long, long_value, LONG_MIN);
|
||||||
|
EXPECT_ARG(LONG, long, long_value, LONG_MAX);
|
||||||
|
EXPECT_ARG(ULONG, unsigned long, ulong_value, 42);
|
||||||
|
EXPECT_ARG(ULONG, unsigned long, ulong_value, ULONG_MAX);
|
||||||
|
|
||||||
|
// Test long long.
|
||||||
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, 42);
|
||||||
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, LLONG_MIN);
|
||||||
|
EXPECT_ARG(LONG_LONG, fmt::LongLong, long_long_value, LLONG_MAX);
|
||||||
|
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ulong_long_value, 42);
|
||||||
|
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ulong_long_value, ULLONG_MAX);
|
||||||
|
|
||||||
|
// Test float.
|
||||||
|
EXPECT_ARG(DOUBLE, float, double_value, 4.2);
|
||||||
|
EXPECT_ARG(DOUBLE, float, double_value, FLT_MIN);
|
||||||
|
EXPECT_ARG(DOUBLE, float, double_value, FLT_MAX);
|
||||||
|
|
||||||
|
// Test double.
|
||||||
|
EXPECT_ARG(DOUBLE, double, double_value, 4.2);
|
||||||
|
EXPECT_ARG(DOUBLE, double, double_value, DBL_MIN);
|
||||||
|
EXPECT_ARG(DOUBLE, double, double_value, DBL_MAX);
|
||||||
|
|
||||||
|
// Test long double.
|
||||||
|
EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, 4.2);
|
||||||
|
EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, LDBL_MIN);
|
||||||
|
EXPECT_ARG(LONG_DOUBLE, long double, long_double_value, LDBL_MAX);
|
||||||
|
|
||||||
|
// Test string.
|
||||||
|
char STR[] = "test";
|
||||||
|
EXPECT_ARG(STRING, char*, string.value, STR);
|
||||||
|
EXPECT_ARG(STRING, const char*, string.value, STR);
|
||||||
|
//EXPECT_ARG(STRING, volatile char*, string.value, STR);
|
||||||
|
|
||||||
|
// TODO: test pointers
|
||||||
|
}
|
||||||
|
|
||||||
// Tests fmt::internal::CountDigits for integer type Int.
|
// Tests fmt::internal::CountDigits for integer type Int.
|
||||||
template <typename Int>
|
template <typename Int>
|
||||||
void TestCountDigits(Int) {
|
void TestCountDigits(Int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user