tesseract v5.3.3.20231005
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 "serialis.h" // TFile
18#include "tprintf.h" // tprintf
19
20namespace tesseract {
21
22// Test some basic I/O of params model files (automated learning of language
23// model weights).
24#ifndef DISABLED_LEGACY_ENGINE
25static 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
36#ifndef DISABLED_LEGACY_ENGINE
37protected:
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;
54 std::string orig_file = TestDataNameToPath(params_model_filename);
55 std::string out_file = OutputNameToPath(params_model_filename);
56
57 EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
58 EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
59
60 EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
61 EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
62 }
63#endif
64};
65
66TEST_F(ParamsModelTest, TestEngParamsModelIO) {
67#ifdef DISABLED_LEGACY_ENGINE
68 // Skip test because ParamsModel::LoadFromFp is missing.
69 GTEST_SKIP();
70#else
71 TestParamsModelRoundTrip("eng.params_model");
72#endif
73}
74
75} // namespace tesseract
#define GTEST_SKIP()
Definition: gtest.h:1889
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
TEST_F(EuroText, FastLatinOCR)
bool Open(const char *filename, FileReader reader)
Definition: serialis.cpp:140
bool SaveToFile(const char *full_path) const
bool LoadFromFp(const char *lang, TFile *fp)
bool Equivalent(const ParamsModel &that) const
static void MakeTmpdir()
Definition: include_gunit.h:38
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:65
std::string OutputNameToPath(const std::string &name) const
std::string TestDataNameToPath(const std::string &name) const
void TestParamsModelRoundTrip(const std::string &params_model_filename) const