From 56f12b737cd39354f0f6d42c0ae7af275d2ad9a4 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 22 Nov 2013 07:45:43 -0800 Subject: [PATCH] Add support for long long and more tests for unsigned long long. https://github.com/vitaut/format/issues/9 --- format.cc | 8 ++++++ format.h | 6 ++++- format_test.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/format.cc b/format.cc index 4739d7bb..7bf98f48 100644 --- a/format.cc +++ b/format.cc @@ -537,6 +537,11 @@ void fmt::BasicFormatter::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; @@ -581,6 +586,9 @@ void fmt::BasicFormatter::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; diff --git a/format.h b/format.h index 5e041f4f..4f6ec947 100644 --- a/format.h +++ b/format.h @@ -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 @@ -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 }; @@ -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; @@ -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) {} diff --git a/format_test.cc b/format_test.cc index 98892ecc..bd457ee5 100644 --- a/format_test.cc +++ b/format_test.cc @@ -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) { @@ -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) { @@ -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) { @@ -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 { @@ -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 f; f.Clear(); @@ -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')); @@ -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')); @@ -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', @@ -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')); @@ -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')); @@ -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', @@ -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', @@ -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', @@ -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)); @@ -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', @@ -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', @@ -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", @@ -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)); @@ -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));