mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-12 15:39:09 +00:00
Simplify tests. Add support for long long in Writer.
This commit is contained in:
parent
ad1be3c17a
commit
aefb3bb918
18
format.h
18
format.h
@ -558,13 +558,20 @@ class BasicWriter {
|
||||
BasicWriter &operator<<(long value) {
|
||||
return *this << IntFormatter<long, TypeSpec<0> >(value, TypeSpec<0>());
|
||||
}
|
||||
BasicWriter &operator<<(unsigned long value) {
|
||||
return *this <<
|
||||
IntFormatter<unsigned long, TypeSpec<0> >(value, TypeSpec<0>());
|
||||
}
|
||||
BasicWriter &operator<<(long long value) {
|
||||
return *this << IntFormatter<long long, TypeSpec<0> >(value, TypeSpec<0>());
|
||||
}
|
||||
|
||||
/**
|
||||
Formats *value* and writes it to the stream.
|
||||
*/
|
||||
BasicWriter &operator<<(unsigned long value) {
|
||||
BasicWriter &operator<<(unsigned long long value) {
|
||||
return *this <<
|
||||
IntFormatter<unsigned long, TypeSpec<0> >(value, TypeSpec<0>());
|
||||
IntFormatter<unsigned long long, TypeSpec<0> >(value, TypeSpec<0>());
|
||||
}
|
||||
|
||||
BasicWriter &operator<<(double value) {
|
||||
@ -586,9 +593,10 @@ class BasicWriter {
|
||||
return *this;
|
||||
}
|
||||
|
||||
BasicWriter &operator<<(const Char *value) {
|
||||
std::size_t size = std::strlen(value);
|
||||
std::copy(value, value + size, GrowBuffer(size));
|
||||
BasicWriter &operator<<(const fmt::StringRef value) {
|
||||
const char *str = value.c_str();
|
||||
std::size_t size = value.size();
|
||||
std::copy(str, str + size, GrowBuffer(size));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
// Check if format.h compiles with windows.h included.
|
||||
@ -85,6 +86,23 @@ using fmt::pad;
|
||||
FORMAT_TEST_THROW_(statement, expected_exception, expected_message, \
|
||||
GTEST_NONFATAL_FAILURE_)
|
||||
|
||||
struct WriteChecker {
|
||||
template <typename T>
|
||||
::testing::AssertionResult operator()(const char *, const T &value) const {
|
||||
std::ostringstream os;
|
||||
os << value;
|
||||
std::string expected = os.str(), actual = str(Writer() << value);
|
||||
if (expected == actual)
|
||||
return ::testing::AssertionSuccess();
|
||||
return ::testing::AssertionFailure()
|
||||
<< "Value of: str(Writer() << value)\n"
|
||||
<< " Actual: " << actual << "\n"
|
||||
<< "Expected: " << expected << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
#define CHECK_WRITE(value) ASSERT_PRED_FORMAT1(WriteChecker(), value)
|
||||
|
||||
// Increment a number in a string.
|
||||
void Increment(char *s) {
|
||||
for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
|
||||
@ -227,37 +245,39 @@ TEST(WriterTest, Data) {
|
||||
}
|
||||
|
||||
TEST(WriterTest, WriteInt) {
|
||||
EXPECT_EQ("42", str(Writer() << 42));
|
||||
EXPECT_EQ("-42", str(Writer() << -42));
|
||||
EXPECT_EQ("12", str(Writer() << static_cast<short>(12)));
|
||||
EXPECT_EQ("34", str(Writer() << 34u));
|
||||
EXPECT_EQ("56", str(Writer() << 56l));
|
||||
EXPECT_EQ("78", str(Writer() << 78ul));
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
SPrintf(buffer, "%d", INT_MIN);
|
||||
EXPECT_EQ(buffer, str(Writer() << INT_MIN));
|
||||
SPrintf(buffer, "%d", INT_MAX);
|
||||
EXPECT_EQ(buffer, str(Writer() << INT_MAX));
|
||||
SPrintf(buffer, "%u", UINT_MAX);
|
||||
EXPECT_EQ(buffer, str(Writer() << UINT_MAX));
|
||||
SPrintf(buffer, "%ld", 0 - static_cast<unsigned long>(LONG_MIN));
|
||||
EXPECT_EQ(buffer, str(Writer() << LONG_MIN));
|
||||
SPrintf(buffer, "%ld", LONG_MAX);
|
||||
EXPECT_EQ(buffer, str(Writer() << LONG_MAX));
|
||||
SPrintf(buffer, "%lu", ULONG_MAX);
|
||||
EXPECT_EQ(buffer, str(Writer() << ULONG_MAX));
|
||||
CHECK_WRITE(42);
|
||||
CHECK_WRITE(-42);
|
||||
CHECK_WRITE(static_cast<short>(12));
|
||||
CHECK_WRITE(34u);
|
||||
CHECK_WRITE(std::numeric_limits<int>::min());
|
||||
CHECK_WRITE(std::numeric_limits<int>::max());
|
||||
CHECK_WRITE(std::numeric_limits<unsigned>::max());
|
||||
}
|
||||
|
||||
TEST(WriterTest, WriteLong) {
|
||||
CHECK_WRITE(56l);
|
||||
CHECK_WRITE(78ul);
|
||||
CHECK_WRITE(std::numeric_limits<long>::min());
|
||||
CHECK_WRITE(std::numeric_limits<long>::max());
|
||||
CHECK_WRITE(std::numeric_limits<unsigned long>::max());
|
||||
}
|
||||
|
||||
TEST(WriterTest, WriteLongLong) {
|
||||
CHECK_WRITE(56ll);
|
||||
CHECK_WRITE(78ull);
|
||||
CHECK_WRITE(std::numeric_limits<long long>::min());
|
||||
CHECK_WRITE(std::numeric_limits<long long>::max());
|
||||
CHECK_WRITE(std::numeric_limits<unsigned long long>::max());
|
||||
}
|
||||
|
||||
TEST(WriterTest, WriteDouble) {
|
||||
EXPECT_EQ("4.2", str(Writer() << 4.2));
|
||||
EXPECT_EQ("-4.2", str(Writer() << -4.2));
|
||||
EXPECT_EQ("4.2", str(Writer() << 4.2l));
|
||||
CHECK_WRITE(4.2);
|
||||
CHECK_WRITE(-4.2);
|
||||
CHECK_WRITE(4.2l);
|
||||
}
|
||||
|
||||
TEST(WriterTest, WriteString) {
|
||||
EXPECT_EQ("abc", str(Writer() << "abc"));
|
||||
CHECK_WRITE("abc");
|
||||
}
|
||||
|
||||
TEST(WriterTest, oct) {
|
||||
@ -1222,6 +1242,10 @@ TEST(FormatterTest, Examples) {
|
||||
ReportError("File not found: {0}") << path;
|
||||
}
|
||||
|
||||
TEST(FormatIntTest, FormatInt) {
|
||||
EXPECT_EQ("42", fmt::FormatInt(42).str());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string str(const T &value) {
|
||||
return fmt::str(fmt::Format("{0}") << value);
|
||||
|
Loading…
Reference in New Issue
Block a user