tesseract/unittest/params_model_test.cc

76 lines
2.5 KiB
C++
Raw Permalink Normal View History

// (C) Copyright 2017, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-03-13 05:06:34 +08:00
#include <string> // std::string
#include <vector>
#include "include_gunit.h"
#include "params_model.h"
2021-03-13 05:06:34 +08:00
#include "serialis.h" // TFile
#include "tprintf.h" // tprintf
namespace tesseract {
// Test some basic I/O of params model files (automated learning of language
// model weights).
#ifndef DISABLED_LEGACY_ENGINE
2021-03-13 05:06:34 +08:00
static bool LoadFromFile(tesseract::ParamsModel &model, const char *lang, const char *full_path) {
tesseract::TFile fp;
if (!fp.Open(full_path, nullptr)) {
tprintf("Error opening file %s\n", full_path);
return false;
}
return model.LoadFromFp(lang, &fp);
}
#endif
class ParamsModelTest : public testing::Test {
#ifndef DISABLED_LEGACY_ENGINE
2021-03-13 05:06:34 +08:00
protected:
void SetUp() override {
std::locale::global(std::locale(""));
}
2021-03-13 05:06:34 +08:00
std::string TestDataNameToPath(const std::string &name) const {
return file::JoinPath(TESTDATA_DIR, name);
}
2021-03-13 05:06:34 +08:00
std::string OutputNameToPath(const std::string &name) const {
return file::JoinPath(FLAGS_test_tmpdir, name);
}
// Test that we are able to load a params model, save it, reload it,
// and verify that the re-serialized version is the same as the original.
2021-03-13 05:06:34 +08:00
void TestParamsModelRoundTrip(const std::string &params_model_filename) const {
tesseract::ParamsModel orig_model;
tesseract::ParamsModel duplicate_model;
file::MakeTmpdir();
std::string orig_file = TestDataNameToPath(params_model_filename);
std::string out_file = OutputNameToPath(params_model_filename);
EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
}
#endif
};
TEST_F(ParamsModelTest, TestEngParamsModelIO) {
#ifdef DISABLED_LEGACY_ENGINE
// Skip test because ParamsModel::LoadFromFp is missing.
GTEST_SKIP();
#else
TestParamsModelRoundTrip("eng.params_model");
#endif
}
2021-03-13 05:06:34 +08:00
} // namespace tesseract