This source file includes following definitions.
- TEST
- determineParagraphDirectionality
- testDirectionality
- TEST
- m_orderFailures
- skipTestsWith
- isNonRenderedCodePoint
- diffString
- runTest
- TEST
#include "config.h"
#include "platform/text/BidiResolver.h"
#include "platform/text/BidiTestHarness.h"
#include "platform/text/TextRunIterator.h"
#include "wtf/OwnPtr.h"
#include <fstream>
#include <gtest/gtest.h>
namespace {
using namespace WTF;
using namespace WebCore;
TEST(BidiResolver, Basic)
{
bool hasStrongDirectionality;
String value("foo");
TextRun run(value);
BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
bidiResolver.setStatus(BidiStatus(run.direction(), run.directionalOverride()));
bidiResolver.setPositionIgnoringNestedIsolates(TextRunIterator(&run, 0));
TextDirection direction = bidiResolver.determineParagraphDirectionality(&hasStrongDirectionality);
EXPECT_TRUE(hasStrongDirectionality);
EXPECT_EQ(LTR, direction);
}
TextDirection determineParagraphDirectionality(const TextRun& textRun, bool* hasStrongDirectionality = 0)
{
BidiResolver<TextRunIterator, BidiCharacterRun> resolver;
resolver.setStatus(BidiStatus(LTR, false));
resolver.setPositionIgnoringNestedIsolates(TextRunIterator(&textRun, 0));
return resolver.determineParagraphDirectionality(hasStrongDirectionality);
}
struct TestData {
UChar text[3];
size_t length;
TextDirection expectedDirection;
bool expectedStrong;
};
void testDirectionality(const TestData& entry)
{
bool hasStrongDirectionality;
String data(entry.text, entry.length);
TextRun run(data);
TextDirection direction = determineParagraphDirectionality(run, &hasStrongDirectionality);
EXPECT_EQ(entry.expectedStrong, hasStrongDirectionality);
EXPECT_EQ(entry.expectedDirection, direction);
}
TEST(BidiResolver, ParagraphDirectionSurrogates)
{
const TestData testData[] = {
{ { 0xD802, 0xDC58 }, 2, RTL, true },
{ { 0xD834, 0xDD5F }, 2, LTR, true },
{ { 0xD802, ' ' }, 2, LTR, false },
{ { 0xDC58, 0x05D0 }, 2, RTL, true },
{ { 0xD802, 0xD802, 0xDC58 }, 3, RTL, true },
{ { 0xD802, 0xDC58 }, 1, LTR, false },
{ { 0xDC58, 0xD802 }, 2, LTR, false }
};
for (size_t i = 0; i < WTF_ARRAY_LENGTH(testData); ++i)
testDirectionality(testData[i]);
}
class BidiTestRunner {
public:
BidiTestRunner()
: m_testsRun(0)
, m_testsSkipped(0)
, m_ignoredCharFailures(0)
, m_levelFailures(0)
, m_orderFailures(0)
{
}
void skipTestsWith(UChar codepoint)
{
m_skippedCodePoints.insert(codepoint);
}
void runTest(const std::basic_string<UChar>& input, const std::vector<int>& reorder,
const std::vector<int>& levels, bidi_test::ParagraphDirection,
const std::string& line, size_t lineNumber);
size_t m_testsRun;
size_t m_testsSkipped;
std::set<UChar> m_skippedCodePoints;
size_t m_ignoredCharFailures;
size_t m_levelFailures;
size_t m_orderFailures;
};
static bool isNonRenderedCodePoint(UChar c)
{
if (c == 0xAD)
return true;
return c >= 0x202A && c <= 0x202E;
}
std::string diffString(const std::vector<int>& actual, const std::vector<int>& expected)
{
std::ostringstream diff;
diff << "actual: ";
std::copy(actual.begin(), actual.end(), std::ostream_iterator<int>(diff, " "));
diff << " expected: ";
std::copy(expected.begin(), expected.end(), std::ostream_iterator<int>(diff, " "));
return diff.str();
}
void BidiTestRunner::runTest(const std::basic_string<UChar>& input, const std::vector<int>& expectedOrder,
const std::vector<int>& expectedLevels, bidi_test::ParagraphDirection paragraphDirection,
const std::string& line, size_t lineNumber)
{
if (!m_skippedCodePoints.empty()) {
for (size_t i = 0; i < input.size(); i++) {
if (m_skippedCodePoints.count(input[i])) {
m_testsSkipped++;
return;
}
}
}
m_testsRun++;
TextRun textRun(input.data(), input.size());
switch (paragraphDirection) {
case bidi_test::DirectionAutoLTR:
textRun.setDirection(determineParagraphDirectionality(textRun));
break;
case bidi_test::DirectionLTR:
textRun.setDirection(LTR);
break;
case bidi_test::DirectionRTL:
textRun.setDirection(RTL);
break;
}
BidiResolver<TextRunIterator, BidiCharacterRun> resolver;
resolver.setStatus(BidiStatus(textRun.direction(), textRun.directionalOverride()));
resolver.setPositionIgnoringNestedIsolates(TextRunIterator(&textRun, 0));
BidiRunList<BidiCharacterRun>& runs = resolver.runs();
resolver.createBidiRunsForLine(TextRunIterator(&textRun, textRun.length()));
std::ostringstream errorContext;
errorContext << ", line " << lineNumber << " \"" << line << "\"";
errorContext << " context: " << bidi_test::nameFromParagraphDirection(paragraphDirection);
std::vector<int> actualOrder;
std::vector<int> actualLevels;
actualLevels.assign(input.size(), -1);
BidiCharacterRun* run = runs.firstRun();
while (run) {
bool reversed = run->reversed(false);
ASSERT(run->stop() >= run->start());
size_t length = run->stop() - run->start();
for (size_t i = 0; i < length; i++) {
int inputIndex = reversed ? run->stop() - i - 1 : run->start() + i;
if (!isNonRenderedCodePoint(input[inputIndex]))
actualOrder.push_back(inputIndex);
actualLevels[inputIndex] = run->level();
}
run = run->next();
}
if (expectedOrder.size() != actualOrder.size()) {
m_ignoredCharFailures++;
EXPECT_EQ(expectedOrder.size(), actualOrder.size()) << errorContext.str();
} else if (expectedOrder != actualOrder) {
m_orderFailures++;
printf("ORDER %s%s\n", diffString(actualOrder, expectedOrder).c_str(), errorContext.str().c_str());
}
if (expectedLevels.size() != actualLevels.size()) {
m_ignoredCharFailures++;
EXPECT_EQ(expectedLevels.size(), actualLevels.size()) << errorContext.str();
} else {
for (size_t i = 0; i < expectedLevels.size(); i++) {
if (expectedLevels[i] == actualLevels[i] || expectedLevels[i] == -1)
continue;
printf("LEVELS %s%s\n", diffString(actualLevels, expectedLevels).c_str(), errorContext.str().c_str());
m_levelFailures++;
break;
}
}
}
TEST(BidiResolver, BidiTest_txt)
{
BidiTestRunner runner;
runner.skipTestsWith(0x2066);
runner.skipTestsWith(0x2067);
runner.skipTestsWith(0x2068);
runner.skipTestsWith(0x2069);
std::string bidiTestPath = "BidiTest.txt";
std::ifstream bidiTestFile(bidiTestPath.c_str());
if (!bidiTestFile.is_open()) {
printf("ERROR: Failed to open BidiTest.txt, cannot run tests.\n");
return;
}
bidi_test::Harness<BidiTestRunner> harness(runner);
harness.parse(bidiTestFile);
bidiTestFile.close();
if (runner.m_testsSkipped)
printf("WARNING: Skipped %zu tests.\n", runner.m_testsSkipped);
printf("Ran %zu tests: %zu level failures %zu order failures.\n",
runner.m_testsRun, runner.m_levelFailures, runner.m_orderFailures);
EXPECT_EQ(352098u, runner.m_testsRun);
EXPECT_EQ(418143u, runner.m_testsSkipped);
EXPECT_EQ(0u, runner.m_ignoredCharFailures);
EXPECT_EQ(44882u, runner.m_levelFailures);
EXPECT_EQ(19151u, runner.m_orderFailures);
}
}