tesseract  5.0.0-alpha-619-ge9db
params_model_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include <string> // std::string
13 #include <vector>
14 
15 #include "include_gunit.h"
16 #include "params_model.h"
17 #include <tesseract/serialis.h> // TFile
18 #include "tprintf.h" // tprintf
19 
20 namespace {
21 
22 // Test some basic I/O of params model files (automated learning of language
23 // model weights).
24 #ifndef DISABLED_LEGACY_ENGINE
25 static bool LoadFromFile(tesseract::ParamsModel& model, const char* lang, const char* full_path) {
27  if (!fp.Open(full_path, nullptr)) {
28  tprintf("Error opening file %s\n", full_path);
29  return false;
30  }
31  return model.LoadFromFp(lang, &fp);
32 }
33 #endif
34 
35 class ParamsModelTest : public testing::Test {
36 #ifndef DISABLED_LEGACY_ENGINE
37  protected:
38  void SetUp() override {
39  std::locale::global(std::locale(""));
40  }
41 
42  std::string TestDataNameToPath(const std::string& name) const {
43  return file::JoinPath(TESTDATA_DIR, name);
44  }
45  std::string OutputNameToPath(const std::string& name) const {
46  return file::JoinPath(FLAGS_test_tmpdir, name);
47  }
48  // Test that we are able to load a params model, save it, reload it,
49  // and verify that the re-serialized version is the same as the original.
50  void TestParamsModelRoundTrip(const std::string& params_model_filename) const {
51  tesseract::ParamsModel orig_model;
52  tesseract::ParamsModel duplicate_model;
53  std::string orig_file = TestDataNameToPath(params_model_filename);
54  std::string out_file = OutputNameToPath(params_model_filename);
55 
56  EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
57  EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
58 
59  EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
60  EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
61  }
62 #endif
63 };
64 
65 TEST_F(ParamsModelTest, TestEngParamsModelIO) {
66 #ifdef DISABLED_LEGACY_ENGINE
67  // Skip test because ParamsModel::LoadFromFp is missing.
68  GTEST_SKIP();
69 #else
70  TestParamsModelRoundTrip("eng.params_model");
71 #endif
72 }
73 
74 } // namespace
file::JoinPath
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:43
string
std::string string
Definition: equationdetect_test.cc:21
tesseract::ParamsModel::Equivalent
bool Equivalent(const ParamsModel &that) const
Definition: params_model.cpp:89
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
tesseract::TFile::Open
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:210
tesseract::ParamsModel::SaveToFile
bool SaveToFile(const char *full_path) const
Definition: params_model.cpp:140
FLAGS_test_tmpdir
const char * FLAGS_test_tmpdir
Definition: include_gunit.h:20
tesseract::TFile
Definition: serialis.h:75
tesseract::ParamsModel
Definition: params_model.h:31
tprintf.h
params_model.h
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
serialis.h
tesseract::ParamsModel::LoadFromFp
bool LoadFromFp(const char *lang, TFile *fp)
Definition: params_model.cpp:102