Skip to content

Commit

Permalink
Add support for long long and more tests for unsigned long long. #9
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 22, 2013
1 parent a4e72b4 commit 56f12b7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
8 changes: 8 additions & 0 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ void fmt::BasicFormatter<Char>::DoFormat() {
case ULONG:
value = precision_arg.ulong_value;
break;
case LONG_LONG:
if (precision_arg.long_long_value < 0)
ReportError(s, "negative precision in format");
value = precision_arg.long_long_value;
break;
case ULONG_LONG:
value = precision_arg.ulong_long_value;
break;
Expand Down Expand Up @@ -581,6 +586,9 @@ void fmt::BasicFormatter<Char>::DoFormat() {
case ULONG:
writer.FormatInt(arg.ulong_value, spec);
break;
case LONG_LONG:
writer.FormatInt(arg.long_long_value, spec);
break;
case ULONG_LONG:
writer.FormatInt(arg.ulong_long_value, spec);
break;
Expand Down
6 changes: 5 additions & 1 deletion format.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ DEFINE_INT_FORMATTERS(int)
DEFINE_INT_FORMATTERS(long)
DEFINE_INT_FORMATTERS(unsigned)
DEFINE_INT_FORMATTERS(unsigned long)
DEFINE_INT_FORMATTERS(long long)
DEFINE_INT_FORMATTERS(unsigned long long)

template <typename Char>
Expand Down Expand Up @@ -812,7 +813,7 @@ class BasicFormatter {

enum Type {
// Numeric types should go first.
INT, UINT, LONG, ULONG, ULONG_LONG, DOUBLE, LONG_DOUBLE,
INT, UINT, LONG, ULONG, LONG_LONG, ULONG_LONG, DOUBLE, LONG_DOUBLE,
LAST_NUMERIC_TYPE = LONG_DOUBLE,
CHAR, STRING, WSTRING, POINTER, CUSTOM
};
Expand Down Expand Up @@ -847,6 +848,7 @@ class BasicFormatter {
double double_value;
long long_value;
unsigned long ulong_value;
long long long_long_value;
unsigned long long ulong_long_value;
long double long_double_value;
const void *pointer_value;
Expand All @@ -867,6 +869,8 @@ class BasicFormatter {
Arg(unsigned value) : type(UINT), uint_value(value), formatter(0) {}
Arg(long value) : type(LONG), long_value(value), formatter(0) {}
Arg(unsigned long value) : type(ULONG), ulong_value(value), formatter(0) {}
Arg(long long value)
: type(LONG_LONG), long_long_value(value), formatter(0) {}
Arg(unsigned long long value)
: type(ULONG_LONG), ulong_long_value(value), formatter(0) {}
Arg(float value) : type(DOUBLE), double_value(value), formatter(0) {}
Expand Down
66 changes: 65 additions & 1 deletion format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ TEST(WriterTest, bin) {
EXPECT_EQ("1011101010111110", str(Writer() << bin(0xbabeu)));
EXPECT_EQ("1101111010101101", str(Writer() << bin(0xdeadl)));
EXPECT_EQ("1011111011101111", str(Writer() << bin(0xbeeful)));
EXPECT_EQ("11001010111111101011101010111110",
str(Writer() << bin(0xcafebabell)));
EXPECT_EQ("11011110101011011011111011101111",
str(Writer() << bin(0xdeadbeefull)));
}

TEST(WriterTest, oct) {
Expand All @@ -331,6 +335,8 @@ TEST(WriterTest, oct) {
EXPECT_EQ("34", str(Writer() << oct(034u)));
EXPECT_EQ("56", str(Writer() << oct(056l)));
EXPECT_EQ("70", str(Writer() << oct(070ul)));
EXPECT_EQ("1234", str(Writer() << oct(01234ll)));
EXPECT_EQ("5670", str(Writer() << oct(05670ull)));
}

TEST(WriterTest, hex) {
Expand All @@ -344,6 +350,8 @@ TEST(WriterTest, hex) {
EXPECT_EQ("babe", str(Writer() << hex(0xbabeu)));
EXPECT_EQ("dead", str(Writer() << hex(0xdeadl)));
EXPECT_EQ("beef", str(Writer() << hex(0xbeeful)));
EXPECT_EQ("cafebabe", str(Writer() << hex(0xcafebabell)));
EXPECT_EQ("deadbeef", str(Writer() << hex(0xdeadbeefull)));
}

TEST(WriterTest, hexu) {
Expand All @@ -352,6 +360,8 @@ TEST(WriterTest, hexu) {
EXPECT_EQ("BABE", str(Writer() << hexu(0xbabeu)));
EXPECT_EQ("DEAD", str(Writer() << hexu(0xdeadl)));
EXPECT_EQ("BEEF", str(Writer() << hexu(0xbeeful)));
EXPECT_EQ("CAFEBABE", str(Writer() << hexu(0xcafebabell)));
EXPECT_EQ("DEADBEEF", str(Writer() << hexu(0xdeadbeefull)));
}

class Date {
Expand Down Expand Up @@ -396,11 +406,15 @@ TEST(WriterTest, pad) {
EXPECT_EQ(" babe", str(Writer() << pad(hex(0xbabeu), 8)));
EXPECT_EQ(" dead", str(Writer() << pad(hex(0xdeadl), 8)));
EXPECT_EQ(" beef", str(Writer() << pad(hex(0xbeeful), 8)));
EXPECT_EQ(" dead", str(Writer() << pad(hex(0xdeadll), 8)));
EXPECT_EQ(" beef", str(Writer() << pad(hex(0xbeefull), 8)));

EXPECT_EQ(" 11", str(Writer() << pad(11, 7)));
EXPECT_EQ(" 22", str(Writer() << pad(22u, 7)));
EXPECT_EQ(" 33", str(Writer() << pad(33l, 7)));
EXPECT_EQ(" 44", str(Writer() << pad(44lu, 7)));
EXPECT_EQ(" 44", str(Writer() << pad(44ul, 7)));
EXPECT_EQ(" 33", str(Writer() << pad(33ll, 7)));
EXPECT_EQ(" 44", str(Writer() << pad(44ull, 7)));

BasicWriter<char> f;
f.Clear();
Expand Down Expand Up @@ -526,6 +540,8 @@ TEST(FormatterTest, LeftAlign) {
EXPECT_EQ("42 ", str(Format("{0:<5}") << 42u));
EXPECT_EQ("-42 ", str(Format("{0:<5}") << -42l));
EXPECT_EQ("42 ", str(Format("{0:<5}") << 42ul));
EXPECT_EQ("-42 ", str(Format("{0:<5}") << -42ll));
EXPECT_EQ("42 ", str(Format("{0:<5}") << 42ull));
EXPECT_EQ("-42 ", str(Format("{0:<5}") << -42.0));
EXPECT_EQ("-42 ", str(Format("{0:<5}") << -42.0l));
EXPECT_EQ("c ", str(Format("{0:<5}") << 'c'));
Expand All @@ -543,6 +559,8 @@ TEST(FormatterTest, RightAlign) {
EXPECT_EQ(" 42", str(Format("{0:>5}") << 42u));
EXPECT_EQ(" -42", str(Format("{0:>5}") << -42l));
EXPECT_EQ(" 42", str(Format("{0:>5}") << 42ul));
EXPECT_EQ(" -42", str(Format("{0:>5}") << -42ll));
EXPECT_EQ(" 42", str(Format("{0:>5}") << 42ull));
EXPECT_EQ(" -42", str(Format("{0:>5}") << -42.0));
EXPECT_EQ(" -42", str(Format("{0:>5}") << -42.0l));
EXPECT_EQ(" c", str(Format("{0:>5}") << 'c'));
Expand All @@ -563,6 +581,8 @@ TEST(FormatterTest, NumericAlign) {
EXPECT_EQ(" 42", str(Format("{0:=5}") << 42u));
EXPECT_EQ("- 42", str(Format("{0:=5}") << -42l));
EXPECT_EQ(" 42", str(Format("{0:=5}") << 42ul));
EXPECT_EQ("- 42", str(Format("{0:=5}") << -42ll));
EXPECT_EQ(" 42", str(Format("{0:=5}") << 42ull));
EXPECT_EQ("- 42", str(Format("{0:=5}") << -42.0));
EXPECT_EQ("- 42", str(Format("{0:=5}") << -42.0l));
EXPECT_THROW_MSG(Format("{0:=5") << 'c',
Expand All @@ -585,6 +605,8 @@ TEST(FormatterTest, CenterAlign) {
EXPECT_EQ(" 42 ", str(Format("{0:^5}") << 42u));
EXPECT_EQ(" -42 ", str(Format("{0:^5}") << -42l));
EXPECT_EQ(" 42 ", str(Format("{0:^5}") << 42ul));
EXPECT_EQ(" -42 ", str(Format("{0:^5}") << -42ll));
EXPECT_EQ(" 42 ", str(Format("{0:^5}") << 42ull));
EXPECT_EQ(" -42 ", str(Format("{0:^6}") << -42.0));
EXPECT_EQ(" -42 ", str(Format("{0:^5}") << -42.0l));
EXPECT_EQ(" c ", str(Format("{0:^5}") << 'c'));
Expand All @@ -604,6 +626,8 @@ TEST(FormatterTest, Fill) {
EXPECT_EQ("***42", str(Format("{0:*>5}") << 42u));
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42l));
EXPECT_EQ("***42", str(Format("{0:*>5}") << 42ul));
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42ll));
EXPECT_EQ("***42", str(Format("{0:*>5}") << 42ull));
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42.0));
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42.0l));
EXPECT_EQ("c****", str(Format("{0:*<5}") << 'c'));
Expand All @@ -622,6 +646,9 @@ TEST(FormatterTest, PlusSign) {
EXPECT_EQ("+42", str(Format("{0:+}") << 42l));
EXPECT_THROW_MSG(Format("{0:+}") << 42ul,
FormatError, "format specifier '+' requires signed argument");
EXPECT_EQ("+42", str(Format("{0:+}") << 42ll));
EXPECT_THROW_MSG(Format("{0:+}") << 42ull,
FormatError, "format specifier '+' requires signed argument");
EXPECT_EQ("+42", str(Format("{0:+}") << 42.0));
EXPECT_EQ("+42", str(Format("{0:+}") << 42.0l));
EXPECT_THROW_MSG(Format("{0:+") << 'c',
Expand All @@ -645,6 +672,9 @@ TEST(FormatterTest, MinusSign) {
EXPECT_EQ("42", str(Format("{0:-}") << 42l));
EXPECT_THROW_MSG(Format("{0:-}") << 42ul,
FormatError, "format specifier '-' requires signed argument");
EXPECT_EQ("42", str(Format("{0:-}") << 42ll));
EXPECT_THROW_MSG(Format("{0:-}") << 42ull,
FormatError, "format specifier '-' requires signed argument");
EXPECT_EQ("42", str(Format("{0:-}") << 42.0));
EXPECT_EQ("42", str(Format("{0:-}") << 42.0l));
EXPECT_THROW_MSG(Format("{0:-") << 'c',
Expand All @@ -668,6 +698,9 @@ TEST(FormatterTest, SpaceSign) {
EXPECT_EQ(" 42", str(Format("{0: }") << 42l));
EXPECT_THROW_MSG(Format("{0: }") << 42ul,
FormatError, "format specifier ' ' requires signed argument");
EXPECT_EQ(" 42", str(Format("{0: }") << 42ll));
EXPECT_THROW_MSG(Format("{0: }") << 42ull,
FormatError, "format specifier ' ' requires signed argument");
EXPECT_EQ(" 42", str(Format("{0: }") << 42.0));
EXPECT_EQ(" 42", str(Format("{0: }") << 42.0l));
EXPECT_THROW_MSG(Format("{0: ") << 'c',
Expand Down Expand Up @@ -696,6 +729,7 @@ TEST(FormatterTest, HashFlag) {
EXPECT_EQ("42", str(Format("{0:#}") << 42u));
EXPECT_EQ("0x42", str(Format("{0:#x}") << 0x42u));
EXPECT_EQ("042", str(Format("{0:#o}") << 042u));

EXPECT_EQ("-42", str(Format("{0:#}") << -42l));
EXPECT_EQ("0x42", str(Format("{0:#x}") << 0x42l));
EXPECT_EQ("-0x42", str(Format("{0:#x}") << -0x42l));
Expand All @@ -704,6 +738,16 @@ TEST(FormatterTest, HashFlag) {
EXPECT_EQ("42", str(Format("{0:#}") << 42ul));
EXPECT_EQ("0x42", str(Format("{0:#x}") << 0x42ul));
EXPECT_EQ("042", str(Format("{0:#o}") << 042ul));

EXPECT_EQ("-42", str(Format("{0:#}") << -42ll));
EXPECT_EQ("0x42", str(Format("{0:#x}") << 0x42ll));
EXPECT_EQ("-0x42", str(Format("{0:#x}") << -0x42ll));
EXPECT_EQ("042", str(Format("{0:#o}") << 042ll));
EXPECT_EQ("-042", str(Format("{0:#o}") << -042ll));
EXPECT_EQ("42", str(Format("{0:#}") << 42ull));
EXPECT_EQ("0x42", str(Format("{0:#x}") << 0x42ull));
EXPECT_EQ("042", str(Format("{0:#o}") << 042ull));

EXPECT_EQ("-42.0000", str(Format("{0:#}") << -42.0));
EXPECT_EQ("-42.0000", str(Format("{0:#}") << -42.0l));
EXPECT_THROW_MSG(Format("{0:#") << 'c',
Expand All @@ -724,6 +768,8 @@ TEST(FormatterTest, ZeroFlag) {
EXPECT_EQ("00042", str(Format("{0:05}") << 42u));
EXPECT_EQ("-0042", str(Format("{0:05}") << -42l));
EXPECT_EQ("00042", str(Format("{0:05}") << 42ul));
EXPECT_EQ("-0042", str(Format("{0:05}") << -42ll));
EXPECT_EQ("00042", str(Format("{0:05}") << 42ull));
EXPECT_EQ("-0042", str(Format("{0:05}") << -42.0));
EXPECT_EQ("-0042", str(Format("{0:05}") << -42.0l));
EXPECT_THROW_MSG(Format("{0:0") << 'c',
Expand Down Expand Up @@ -758,6 +804,8 @@ TEST(FormatterTest, Width) {
EXPECT_EQ(" 42", str(Format("{0:5}") << 42u));
EXPECT_EQ(" -42", str(Format("{0:6}") << -42l));
EXPECT_EQ(" 42", str(Format("{0:7}") << 42ul));
EXPECT_EQ(" -42", str(Format("{0:6}") << -42ll));
EXPECT_EQ(" 42", str(Format("{0:7}") << 42ull));
EXPECT_EQ(" -1.23", str(Format("{0:8}") << -1.23));
EXPECT_EQ(" -1.23", str(Format("{0:9}") << -1.23l));
EXPECT_EQ(" 0xcafe",
Expand Down Expand Up @@ -807,6 +855,14 @@ TEST(FormatterTest, Precision) {
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.2f}") << 42ul,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.2}") << 42ll,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.2f}") << 42ll,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.2}") << 42ull,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.2f}") << 42ull,
FormatError, "precision specifier requires floating-point argument");
EXPECT_EQ("1.2", str(Format("{0:.2}") << 1.2345));
EXPECT_EQ("1.2", str(Format("{0:.2}") << 1.2345l));

Expand Down Expand Up @@ -891,6 +947,14 @@ TEST(FormatterTest, RuntimePrecision) {
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42ul << 2,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42ll << 2,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42ll << 2,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.{1}}") << 42ull << 2,
FormatError, "precision specifier requires floating-point argument");
EXPECT_THROW_MSG(Format("{0:.{1}f}") << 42ull << 2,
FormatError, "precision specifier requires floating-point argument");
EXPECT_EQ("1.2", str(Format("{0:.{1}}") << 1.2345 << 2));
EXPECT_EQ("1.2", str(Format("{1:.{0}}") << 2 << 1.2345l));

Expand Down

0 comments on commit 56f12b7

Please sign in to comment.