mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Test writing to ostream
This commit is contained in:
@ -364,7 +364,7 @@ class CharConverter : public fmt::internal::ArgVisitor<CharConverter, void> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Write the content of w to os.
|
// Write the content of w to os.
|
||||||
void write(std::ostream &os, fmt::MemoryWriter &w) {
|
void write(std::ostream &os, fmt::Writer &w) {
|
||||||
const char *data = w.data();
|
const char *data = w.data();
|
||||||
typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
|
typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
|
||||||
UnsignedStreamSize size = w.size();
|
UnsignedStreamSize size = w.size();
|
||||||
|
@ -31,11 +31,14 @@
|
|||||||
// Include format.cc instead of format.h to test implementation-specific stuff.
|
// Include format.cc instead of format.h to test implementation-specific stuff.
|
||||||
#include "cppformat/format.cc"
|
#include "cppformat/format.cc"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "gmock/gmock.h"
|
||||||
#include "gtest-extra.h"
|
#include "gtest-extra.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#undef min
|
||||||
#undef max
|
#undef max
|
||||||
|
|
||||||
TEST(FormatTest, ArgConverter) {
|
TEST(FormatTest, ArgConverter) {
|
||||||
@ -119,3 +122,55 @@ TEST(FormatTest, FormatErrorCode) {
|
|||||||
EXPECT_EQ(msg, w.str());
|
EXPECT_EQ(msg, w.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(FormatTest, WriteToOStream) {
|
||||||
|
std::ostringstream os;
|
||||||
|
fmt::MemoryWriter w;
|
||||||
|
w << "foo";
|
||||||
|
fmt::write(os, w);
|
||||||
|
EXPECT_EQ("foo", os.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(FormatTest, WriteToOStreamMaxSize) {
|
||||||
|
std::size_t max_size = std::numeric_limits<std::size_t>::max();
|
||||||
|
std::streamsize max_streamsize = std::numeric_limits<std::streamsize>::max();
|
||||||
|
if (max_size <= fmt::internal::to_unsigned(max_streamsize))
|
||||||
|
return;
|
||||||
|
|
||||||
|
class TestWriter : public fmt::BasicWriter<char> {
|
||||||
|
private:
|
||||||
|
struct TestBuffer : fmt::Buffer<char> {
|
||||||
|
explicit TestBuffer(std::size_t size) { size_ = size; }
|
||||||
|
void grow(std::size_t) {}
|
||||||
|
} buffer_;
|
||||||
|
public:
|
||||||
|
explicit TestWriter(std::size_t size)
|
||||||
|
: fmt::BasicWriter<char>(buffer_), buffer_(size) {}
|
||||||
|
} w(max_size);
|
||||||
|
|
||||||
|
struct MockStreamBuf : std::streambuf {
|
||||||
|
MOCK_METHOD2(xsputn, std::streamsize (const void *s, std::streamsize n));
|
||||||
|
std::streamsize xsputn(const char *s, std::streamsize n) {
|
||||||
|
const void *v = s;
|
||||||
|
return xsputn(v, n);
|
||||||
|
}
|
||||||
|
} buffer;
|
||||||
|
|
||||||
|
struct TestOStream : std::ostream {
|
||||||
|
explicit TestOStream(MockStreamBuf &buffer) : std::ostream(&buffer) {}
|
||||||
|
} os(buffer);
|
||||||
|
|
||||||
|
testing::InSequence sequence;
|
||||||
|
const char *data = 0;
|
||||||
|
std::size_t size = max_size;
|
||||||
|
do {
|
||||||
|
typedef fmt::internal::MakeUnsigned<std::streamsize>::Type UStreamSize;
|
||||||
|
UStreamSize n = std::min<UStreamSize>(
|
||||||
|
size, fmt::internal::to_unsigned(max_streamsize));
|
||||||
|
EXPECT_CALL(buffer, xsputn(data, static_cast<std::streamsize>(n)))
|
||||||
|
.WillOnce(testing::Return(max_streamsize));
|
||||||
|
data += n;
|
||||||
|
size -= n;
|
||||||
|
} while (size != 0);
|
||||||
|
fmt::write(os, w);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user