|
1 | 1 |
|
| 2 | +#include <cstdlib> // for system |
| 3 | +#include <fstream> // for ifstream |
2 | 4 | #include <set>
|
3 | 5 | #include <string>
|
4 | 6 | #include <vector>
|
|
8 | 10 | #include "trie.h"
|
9 | 11 |
|
10 | 12 | #include "include_gunit.h"
|
11 |
| -#include "base/filelinereader.h" |
12 |
| -#include "util/process/subprocess.h" |
13 | 13 |
|
14 | 14 | namespace {
|
15 | 15 |
|
16 |
| -void RemoveTrailingLineTerminators(char* line) { |
17 |
| - char* end = line + strlen(line) - 1; |
18 |
| - while (end >= line && ('\n' == *end || '\r' == *end)) { |
19 |
| - *end-- = 0; |
20 |
| - } |
21 |
| -} |
22 |
| - |
23 |
| -void AddLineToSet(std::set<std::string>* words, char* line) { |
24 |
| - RemoveTrailingLineTerminators(line); |
25 |
| - words->insert(line); |
26 |
| -} |
27 |
| - |
28 | 16 | // Test some basic functionality dealing with Dawgs (compressed dictionaries,
|
29 | 17 | // aka Directed Acyclic Word Graphs).
|
30 | 18 | class DawgTest : public testing::Test {
|
31 | 19 | protected:
|
32 | 20 | void LoadWordlist(const std::string& filename, std::set<std::string>* words) const {
|
33 |
| - FileLineReader::Options options; |
34 |
| - options.set_comment_char(0); |
35 |
| - FileLineReader flr(filename.c_str(), options); |
36 |
| - flr.set_line_callback(NewPermanentCallback(AddLineToSet, words)); |
37 |
| - flr.Reload(); |
| 21 | + std::ifstream file(filename); |
| 22 | + if (file.is_open()) { |
| 23 | + std::string line; |
| 24 | + while (getline(file, line)) { |
| 25 | + // Remove trailing line terminators from line. |
| 26 | + while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) { |
| 27 | + line.resize(line.size() - 1); |
| 28 | + } |
| 29 | + // Add line to set. |
| 30 | + words->insert(line.c_str()); |
| 31 | + } |
| 32 | + file.close(); |
| 33 | + } |
38 | 34 | }
|
39 | 35 | std::string TestDataNameToPath(const std::string& name) const {
|
40 |
| - return file::JoinPath(TESTDATA_DIR, "/" + name); |
| 36 | + return file::JoinPath(TESTDATA_DIR, name); |
41 | 37 | }
|
42 |
| - std::string TessBinaryPath(const std::string& binary_name) const { |
43 |
| - return file::JoinPath(TESS_SRC_DIR, |
| 38 | + std::string TessBinaryPath(const std::string& name) const { |
| 39 | + return file::JoinPath(TESSBIN_DIR, "src/training/" + name); |
44 | 40 | }
|
45 | 41 | std::string OutputNameToPath(const std::string& name) const {
|
46 | 42 | return file::JoinPath(FLAGS_test_tmpdir, name);
|
47 | 43 | }
|
48 |
| - int RunCommand(const std::string& program, const std::string& arg1, const std::string& arg2, |
49 |
| - const std::string& arg3) const { |
50 |
| - SubProcess p; |
51 |
| - std::vector<std::string> argv; |
52 |
| - argv.push_back(program); |
53 |
| - argv.push_back(arg1); |
54 |
| - argv.push_back(arg2); |
55 |
| - argv.push_back(arg3); |
56 |
| - p.SetProgram(TessBinaryPath(program), argv); |
57 |
| - p.Start(); |
58 |
| - p.Wait(); |
59 |
| - return p.exit_code(); |
| 44 | + int RunCommand(const std::string& program, const std::string& arg1, |
| 45 | + const std::string& arg2, const std::string& arg3) const { |
| 46 | + std::string cmdline = |
| 47 | + TessBinaryPath(program) + " " + arg1 + " " + arg2 + " " + arg3; |
| 48 | + return system(cmdline.c_str()); |
60 | 49 | }
|
61 | 50 | // Test that we are able to convert a wordlist file (one "word" per line) to
|
62 | 51 | // a dawg (a compressed format) and then extract the original wordlist back
|
|
0 commit comments