This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- LineBreakString
- MakeTestData
- TEST
- TEST
- TEST
- TEST
- TEST
#include "config.h"
#include "core/html/track/vtt/BufferedLineReader.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/CharacterNames.h"
#include <gtest/gtest.h>
using WebCore::BufferedLineReader;
namespace {
TEST(BufferedLineReader, Constructor)
{
BufferedLineReader reader;
ASSERT_FALSE(reader.isAtEndOfStream());
String line;
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, EOSNoInput)
{
BufferedLineReader reader;
String line;
ASSERT_FALSE(reader.getLine(line));
reader.setEndOfStream();
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, EOSInput)
{
BufferedLineReader reader;
reader.append("A");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "A");
}
TEST(BufferedLineReader, EOSMultipleReads_1)
{
BufferedLineReader reader;
reader.append("A");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "A");
ASSERT_FALSE(reader.getLine(line));
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, EOSMultipleReads_2)
{
BufferedLineReader reader;
reader.append("A\n");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "A");
ASSERT_FALSE(reader.getLine(line));
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, LineEndingCR)
{
BufferedLineReader reader;
reader.append("X\rY");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "Y");
}
TEST(BufferedLineReader, LineEndingCR_EOS)
{
BufferedLineReader reader;
reader.append("X\r");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, LineEndingLF)
{
BufferedLineReader reader;
reader.append("X\nY");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "Y");
}
TEST(BufferedLineReader, LineEndingLF_EOS)
{
BufferedLineReader reader;
reader.append("X\n");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, LineEndingCRLF)
{
BufferedLineReader reader;
reader.append("X\r\nY");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "Y");
}
TEST(BufferedLineReader, LineEndingCRLF_EOS)
{
BufferedLineReader reader;
reader.append("X\r\n");
reader.setEndOfStream();
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_FALSE(reader.getLine(line));
}
enum LineBreakType {
Cr,
Lf,
CrLf
};
String LineBreakString(LineBreakType type)
{
static const char breakStrings[] = "\r\n";
return String(type == Lf ? breakStrings + 1 : breakStrings, type == CrLf ? 2 : 1);
}
String MakeTestData(const char** lines, const LineBreakType* breaks, int count)
{
StringBuilder builder;
for (int i = 0; i < count; ++i) {
builder.append(lines[i]);
builder.append(LineBreakString(breaks[i]));
}
return builder.toString();
}
const size_t blockSizes[] = { 64, 32, 16, 8, 4, 2, 1, 3, 5, 7, 9, 11, 13, 17, 19, 23 };
TEST(BufferedLineReader, BufferSizes)
{
const char* lines[] = {
"aaaaaaaaaaaaaaaa",
"bbbbbbbbbb",
"ccccccccccccc",
"",
"dddddd",
"",
"eeeeeeeeee"
};
const LineBreakType breaks[] = { Lf, Lf, Lf, Lf, Lf, Lf, Lf };
const size_t numTestLines = WTF_ARRAY_LENGTH(lines);
COMPILE_ASSERT(numTestLines == WTF_ARRAY_LENGTH(breaks), DifferentLengths_lines_and_breaks);
String data = MakeTestData(lines, breaks, numTestLines);
for (size_t k = 0; k < WTF_ARRAY_LENGTH(blockSizes); ++k) {
size_t lineCount = 0;
BufferedLineReader reader;
size_t blockSize = blockSizes[k];
for (size_t i = 0; i < data.length(); i += blockSize) {
reader.append(data.substring(i, blockSize));
String line;
while (reader.getLine(line)) {
ASSERT_LT(lineCount, numTestLines);
ASSERT_EQ(line, lines[lineCount++]);
}
}
ASSERT_EQ(lineCount, numTestLines);
}
}
TEST(BufferedLineReader, BufferSizesMixedEndings)
{
const char* lines[] = {
"aaaaaaaaaaaaaaaa",
"bbbbbbbbbb",
"ccccccccccccc",
"",
"dddddd",
"eeeeeeeeee",
"fffffffffffffffffff"
};
const LineBreakType breaks[] = { Cr, Lf, CrLf, Cr, Lf, CrLf, Lf };
const size_t numTestLines = WTF_ARRAY_LENGTH(lines);
COMPILE_ASSERT(numTestLines == WTF_ARRAY_LENGTH(breaks), DifferentLengths_lines_and_breaks);
String data = MakeTestData(lines, breaks, numTestLines);
for (size_t k = 0; k < WTF_ARRAY_LENGTH(blockSizes); ++k) {
size_t lineCount = 0;
BufferedLineReader reader;
size_t blockSize = blockSizes[k];
for (size_t i = 0; i < data.length(); i += blockSize) {
reader.append(data.substring(i, blockSize));
String line;
while (reader.getLine(line)) {
ASSERT_LT(lineCount, numTestLines);
ASSERT_EQ(line, lines[lineCount++]);
}
}
ASSERT_EQ(lineCount, numTestLines);
}
}
TEST(BufferedLineReader, BufferBoundaryInCRLF_1)
{
BufferedLineReader reader;
reader.append("X\r");
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
reader.append("\n");
ASSERT_FALSE(reader.getLine(line));
}
TEST(BufferedLineReader, BufferBoundaryInCRLF_2)
{
BufferedLineReader reader;
reader.append("X\r");
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "X");
ASSERT_FALSE(reader.getLine(line));
reader.append("\n");
ASSERT_FALSE(reader.getLine(line));
reader.append("Y\n");
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line, "Y");
}
TEST(BufferedLineReader, NormalizedNUL)
{
BufferedLineReader reader;
reader.append(String("X\0Y\n", 4));
String line;
ASSERT_TRUE(reader.getLine(line));
ASSERT_EQ(line[1], WTF::Unicode::replacementCharacter);
}
}