32
32
#include "base/file_stream.h"
33
33
#include "base/mmap.h"
34
34
#include "base/util.h"
35
#include "converter/connector.h"
36
#include "converter/converter_compiler.h"
35
#include "converter/connector_interface.h"
36
#include "converter/sparse_connector.h"
37
37
#include "testing/base/public/gunit.h"
39
39
DECLARE_string(test_tmpdir);
40
DECLARE_bool(use_sparse_connector);
44
class ConnectorTest : public testing::Test {
46
void ConnectorOpenTest() {
47
const string input_filename
48
= mozc::Util::JoinPath(FLAGS_test_tmpdir, "connector.txt");
50
const string output_filename
51
= mozc::Util::JoinPath(FLAGS_test_tmpdir, "connector.db");
54
mozc::OutputFileStream ofs(input_filename.c_str());
57
ofs << "3 3" << endl; // 3x3 matrix
58
for (int i = 0; i < 3; ++i) {
59
for (int j = 0; j < 3; ++j) {
61
ofs << i << " " << j << " " << 3 * i + j << endl;
66
ConverterCompiler::CompileConnectionTable(input_filename,
70
CHECK(cmmap.Open(output_filename.c_str()))
71
<< "Failed to open matrix image" << output_filename;
72
CHECK_GE(cmmap.Size(), 4) << "Malformed matrix image";
73
scoped_ptr<ConnectorInterface> connector(
74
ConnectorInterface::OpenFromArray(cmmap.begin(), cmmap.GetFileSize()));
43
TEST(SparseConnectorTest, SparseConnecterOpenTest) {
44
const string input_filename
45
= mozc::Util::JoinPath(FLAGS_test_tmpdir, "connector.txt");
47
const string output_filename
48
= mozc::Util::JoinPath(FLAGS_test_tmpdir, "connector.db");
51
mozc::OutputFileStream ofs(input_filename.c_str());
54
ofs << "3 3" << endl; // 3x3 matrix
76
55
for (int i = 0; i < 3; ++i) {
77
56
for (int j = 0; j < 3; ++j) {
78
EXPECT_EQ(3 * i + j, connector->GetTransitionCost(i, j));
58
ofs << i << " " << j << " " << 3 * i + j << endl;
84
// This code is a kind of kludge to use 2 implementations of transition
85
// cost matrix. We might remove this later.
86
TEST_F(ConnectorTest, DenseConnecterOpenTest) {
87
FLAGS_use_sparse_connector = false;
91
TEST_F(ConnectorTest, SparseConnecterOpenTest) {
92
FLAGS_use_sparse_connector = true;
63
SparseConnectorBuilder::Compile(input_filename.c_str(),
64
output_filename.c_str());
67
CHECK(cmmap.Open(output_filename.c_str()))
68
<< "Failed to open matrix image" << output_filename;
69
CHECK_GE(cmmap.Size(), 4) << "Malformed matrix image";
70
scoped_ptr<SparseConnector> connector(
71
new SparseConnector(cmmap.begin(), cmmap.GetFileSize()));
73
for (int i = 0; i < 3; ++i) {
74
for (int j = 0; j < 3; ++j) {
75
EXPECT_EQ(3 * i + j, connector->GetTransitionCost(i, j));
96
80
TEST(SparseConnectorTest, key_coding) {