File: BidiTestHarness.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (268 lines) | stat: -rw-r--r-- 10,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BidiTestHarness_h
#define BidiTestHarness_h

#include <istream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

// FIXME: We don't have any business owning this code. We should try to
// upstream this to unicode.org if possible (for other implementations to use).
// Unicode.org provides a reference implmentation, including parser:
// http://www.unicode.org/Public/PROGRAMS/BidiReferenceC/6.3.0/source/brtest.c
// But it, like the other implementations I've found, is rather tied to
// the algorithms it is testing. This file seeks to only implement the parser
// bits.

// Other C/C++ implementations of this parser:
// https://github.com/googlei18n/fribidi-vs-unicode/blob/master/test.c
// http://source.icu-project.org/repos/icu/icu/trunk/source/test/intltest/bidiconf.cpp
// Both of those are too tied to their respective projects to be use to Blink.

// There are non-C implmentations to parse BidiTest.txt as well, including:
// https://github.com/twitter/twitter-cldr-rb/blob/master/spec/bidi/bidi_spec.rb

// NOTE: None of this file is currently written to be thread-safe.

namespace bidi_test {

enum ParagraphDirection {
  DirectionAutoLTR = 1,
  DirectionLTR = 2,
  DirectionRTL = 4,
};
const int kMaxParagraphDirection =
    DirectionAutoLTR | DirectionLTR | DirectionRTL;

// For error printing:
std::string nameFromParagraphDirection(ParagraphDirection paragraphDirection) {
  switch (paragraphDirection) {
    case bidi_test::DirectionAutoLTR:
      return "Auto-LTR";
    case bidi_test::DirectionLTR:
      return "LTR";
    case bidi_test::DirectionRTL:
      return "RTL";
  }
  // This should never be reached.
  return "";
}

template <class Runner>
class Harness {
 public:
  Harness(Runner& runner) : m_runner(runner) {}
  void parse(std::istream& bidiTestFile);

 private:
  Runner& m_runner;
};

// We could use boost::trim, but no other part of Blink uses boost yet.
inline void ltrim(std::string& s) {
  static const std::string separators(" \t");
  s.erase(0, s.find_first_not_of(separators));
}

inline void rtrim(std::string& s) {
  static const std::string separators(" \t");
  size_t lastNonSpace = s.find_last_not_of(separators);
  if (lastNonSpace == std::string::npos) {
    s.erase();
    return;
  }
  size_t firstSpaceAtEndOfString = lastNonSpace + 1;
  if (firstSpaceAtEndOfString >= s.size())
    return;  // lastNonSpace was the last char.
  s.erase(firstSpaceAtEndOfString,
          std::string::npos);  // erase to the end of the string.
}

inline void trim(std::string& s) {
  rtrim(s);
  ltrim(s);
}

static std::vector<std::string> parseStringList(const std::string& str) {
  std::vector<std::string> strings;
  static const std::string separators(" \t");
  size_t lastPos = str.find_first_not_of(separators);   // skip leading spaces
  size_t pos = str.find_first_of(separators, lastPos);  // find next space

  while (std::string::npos != pos || std::string::npos != lastPos) {
    strings.push_back(str.substr(lastPos, pos - lastPos));
    lastPos = str.find_first_not_of(separators, pos);
    pos = str.find_first_of(separators, lastPos);
  }
  return strings;
}

static std::vector<int> parseIntList(const std::string& str) {
  std::vector<int> ints;
  std::vector<std::string> strings = parseStringList(str);
  for (size_t x = 0; x < strings.size(); x++) {
    int i = atoi(strings[x].c_str());
    ints.push_back(i);
  }
  return ints;
}

static std::vector<int> parseLevels(const std::string& line) {
  std::vector<int> levels;
  std::vector<std::string> strings = parseStringList(line);
  for (size_t x = 0; x < strings.size(); x++) {
    const std::string& levelString = strings[x];
    int i;
    if (levelString == "x")
      i = -1;
    else
      i = atoi(levelString.c_str());
    levels.push_back(i);
  }
  return levels;
}

// This is not thread-safe as written.
static std::basic_string<UChar> parseTestString(const std::string& line) {
  std::basic_string<UChar> testString;
  static std::map<std::string, UChar> charClassExamples;
  if (charClassExamples.empty()) {
    // FIXME: Explicit make_pair is ugly, but required for C++98 compat.
    charClassExamples.insert(std::make_pair("L", 0x6c));     // 'l' for L
    charClassExamples.insert(std::make_pair("R", 0x05D0));   // HEBREW ALEF
    charClassExamples.insert(std::make_pair("EN", 0x33));    // '3' for EN
    charClassExamples.insert(std::make_pair("ES", 0x2d));    // '-' for ES
    charClassExamples.insert(std::make_pair("ET", 0x25));    // '%' for ET
    charClassExamples.insert(std::make_pair("AN", 0x0660));  // arabic 0
    charClassExamples.insert(std::make_pair("CS", 0x2c));    // ',' for CS
    charClassExamples.insert(std::make_pair("B", 0x0A));     // <control-000A>
    charClassExamples.insert(std::make_pair("S", 0x09));     // <control-0009>
    charClassExamples.insert(std::make_pair("WS", 0x20));    // ' ' for WS
    charClassExamples.insert(std::make_pair("ON", 0x3d));    // '=' for ON
    charClassExamples.insert(
        std::make_pair("NSM", 0x05BF));  // HEBREW POINT RAFE
    charClassExamples.insert(std::make_pair("AL", 0x0608));  // ARABIC RAY
    charClassExamples.insert(std::make_pair("BN", 0x00AD));  // SOFT HYPHEN
    charClassExamples.insert(std::make_pair("LRE", 0x202A));
    charClassExamples.insert(std::make_pair("RLE", 0x202B));
    charClassExamples.insert(std::make_pair("PDF", 0x202C));
    charClassExamples.insert(std::make_pair("LRO", 0x202D));
    charClassExamples.insert(std::make_pair("RLO", 0x202E));
    charClassExamples.insert(std::make_pair("LRI", 0x2066));
    charClassExamples.insert(std::make_pair("RLI", 0x2067));
    charClassExamples.insert(std::make_pair("FSI", 0x2068));
    charClassExamples.insert(std::make_pair("PDI", 0x2069));
  }

  std::vector<std::string> charClasses = parseStringList(line);
  for (size_t i = 0; i < charClasses.size(); i++) {
    // FIXME: If the lookup failed we could return false for a parse error.
    testString.push_back(charClassExamples.find(charClasses[i])->second);
  }
  return testString;
}

static bool parseParagraphDirectionMask(const std::string& line,
                                        int& modeMask) {
  modeMask = atoi(line.c_str());
  return modeMask >= 1 && modeMask <= kMaxParagraphDirection;
}

static void parseError(const std::string& line, size_t lineNumber) {
  // Use printf to avoid the expense of std::cout.
  printf("Parse error, line %zu : %s\n", lineNumber, line.c_str());
}

template <class Runner>
void Harness<Runner>::parse(std::istream& bidiTestFile) {
  static const std::string levelsPrefix("@Levels");
  static const std::string reorderPrefix("@Reorder");

  // FIXME: UChar is an ICU type and cheating a bit to use here.
  // uint16_t might be more portable.
  std::basic_string<UChar> testString;
  std::vector<int> levels;
  std::vector<int> reorder;
  int paragraphDirectionMask;

  std::string line;
  size_t lineNumber = 0;
  while (std::getline(bidiTestFile, line)) {
    lineNumber++;
    const std::string originalLine = line;
    size_t commentStart = line.find_first_of('#');
    if (commentStart != std::string::npos)
      line = line.substr(0, commentStart);
    trim(line);
    if (line.empty())
      continue;
    if (line[0] == '@') {
      if (!line.find(levelsPrefix)) {
        levels = parseLevels(line.substr(levelsPrefix.length() + 1));
        continue;
      }
      if (!line.find(reorderPrefix)) {
        reorder = parseIntList(line.substr(reorderPrefix.length() + 1));
        continue;
      }
    } else {
      // Assume it's a data line.
      size_t seperatorIndex = line.find_first_of(';');
      if (seperatorIndex == std::string::npos) {
        parseError(originalLine, lineNumber);
        continue;
      }
      testString = parseTestString(line.substr(0, seperatorIndex));
      if (!parseParagraphDirectionMask(line.substr(seperatorIndex + 1),
                                       paragraphDirectionMask)) {
        parseError(originalLine, lineNumber);
        continue;
      }

      if (paragraphDirectionMask & DirectionAutoLTR)
        m_runner.runTest(testString, reorder, levels, DirectionAutoLTR,
                         originalLine, lineNumber);
      if (paragraphDirectionMask & DirectionLTR)
        m_runner.runTest(testString, reorder, levels, DirectionLTR,
                         originalLine, lineNumber);
      if (paragraphDirectionMask & DirectionRTL)
        m_runner.runTest(testString, reorder, levels, DirectionRTL,
                         originalLine, lineNumber);
    }
  }
}

}  // namespace bidi_test

#endif  // BidiTestHarness_h