#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(formatted_raw_ostreamTest, Test_Tell) {
SmallString<128> A;
raw_svector_ostream B(A);
formatted_raw_ostream C(B);
char tmp[100] = "";
for (unsigned i = 0; i != 3; ++i) {
C.write(tmp, 100);
EXPECT_EQ(100*(i+1), (unsigned) C.tell());
}
}
TEST(formatted_raw_ostreamTest, Test_LineColumn) {
SmallString<128> A;
raw_svector_ostream B(A);
formatted_raw_ostream C(B);
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(0U, C.getColumn());
C << "a";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(1U, C.getColumn());
C << "bcdef";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(6U, C.getColumn());
C << "\n";
EXPECT_EQ(1U, C.getLine());
EXPECT_EQ(0U, C.getColumn());
C << "foo\r";
EXPECT_EQ(1U, C.getLine());
EXPECT_EQ(0U, C.getColumn());
C << "1\t";
EXPECT_EQ(8U, C.getColumn());
C << "\t";
EXPECT_EQ(8U, C.getColumn());
C << "1234567\t";
EXPECT_EQ(16U, C.getColumn());
EXPECT_EQ(1U, C.getLine());
}
TEST(formatted_raw_ostreamTest, Test_Flush) {
SmallString<128> A;
raw_svector_ostream B(A);
B.SetBufferSize(32);
formatted_raw_ostream C(B);
C << "\nabc";
EXPECT_EQ(4U, C.GetNumBytesInBuffer());
C.flush();
EXPECT_EQ(1U, C.getLine());
EXPECT_EQ(3U, C.getColumn());
EXPECT_EQ(0U, C.GetNumBytesInBuffer());
}
TEST(formatted_raw_ostreamTest, Test_UTF8) {
SmallString<128> A;
raw_svector_ostream B(A);
B.SetBufferSize(32);
formatted_raw_ostream C(B);
C << u8"\u00a0";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(1U, C.getColumn());
EXPECT_EQ(2U, C.GetNumBytesInBuffer());
C << u8"\u2468";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(2U, C.getColumn());
EXPECT_EQ(5U, C.GetNumBytesInBuffer());
C << u8"\U00010000";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(3U, C.getColumn());
EXPECT_EQ(9U, C.GetNumBytesInBuffer());
C << u8"\u55b5";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(5U, C.getColumn());
EXPECT_EQ(12U, C.GetNumBytesInBuffer());
C << u8"\u200b";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(5U, C.getColumn());
EXPECT_EQ(15U, C.GetNumBytesInBuffer());
}
TEST(formatted_raw_ostreamTest, Test_UTF8Buffered) {
SmallString<128> A;
raw_svector_ostream B(A);
B.SetBufferSize(4);
formatted_raw_ostream C(B);
C << u8"123\u2468";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(4U, C.getColumn());
EXPECT_EQ(2U, C.GetNumBytesInBuffer());
C.flush();
EXPECT_EQ(6U, A.size());
C << u8"123\u55b5";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(9U, C.getColumn());
EXPECT_EQ(2U, C.GetNumBytesInBuffer());
C.flush();
EXPECT_EQ(12U, A.size());
}
TEST(formatted_raw_ostreamTest, Test_UTF8TinyBuffer) {
SmallString<128> A;
raw_svector_ostream B(A);
B.SetBufferSize(1);
formatted_raw_ostream C(B);
C << u8"\u2468";
EXPECT_EQ(0U, C.getLine());
EXPECT_EQ(1U, C.getColumn());
EXPECT_EQ(0U, C.GetNumBytesInBuffer());
C.flush();
EXPECT_EQ(3U, A.size());
}
}