params_model_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <string> // std::string
  12. #include <vector>
  13. #include "include_gunit.h"
  14. #include "params_model.h"
  15. #include "serialis.h" // TFile
  16. #include "tprintf.h" // tprintf
  17. namespace tesseract {
  18. // Test some basic I/O of params model files (automated learning of language
  19. // model weights).
  20. #ifndef DISABLED_LEGACY_ENGINE
  21. static bool LoadFromFile(tesseract::ParamsModel &model, const char *lang, const char *full_path) {
  22. tesseract::TFile fp;
  23. if (!fp.Open(full_path, nullptr)) {
  24. tprintf("Error opening file %s\n", full_path);
  25. return false;
  26. }
  27. return model.LoadFromFp(lang, &fp);
  28. }
  29. #endif
  30. class ParamsModelTest : public testing::Test {
  31. #ifndef DISABLED_LEGACY_ENGINE
  32. protected:
  33. void SetUp() override {
  34. std::locale::global(std::locale(""));
  35. }
  36. std::string TestDataNameToPath(const std::string &name) const {
  37. return file::JoinPath(TESTDATA_DIR, name);
  38. }
  39. std::string OutputNameToPath(const std::string &name) const {
  40. return file::JoinPath(FLAGS_test_tmpdir, name);
  41. }
  42. // Test that we are able to load a params model, save it, reload it,
  43. // and verify that the re-serialized version is the same as the original.
  44. void TestParamsModelRoundTrip(const std::string &params_model_filename) const {
  45. tesseract::ParamsModel orig_model;
  46. tesseract::ParamsModel duplicate_model;
  47. file::MakeTmpdir();
  48. std::string orig_file = TestDataNameToPath(params_model_filename);
  49. std::string out_file = OutputNameToPath(params_model_filename);
  50. EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
  51. EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
  52. EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
  53. EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
  54. }
  55. #endif
  56. };
  57. TEST_F(ParamsModelTest, TestEngParamsModelIO) {
  58. #ifdef DISABLED_LEGACY_ENGINE
  59. // Skip test because ParamsModel::LoadFromFp is missing.
  60. GTEST_SKIP();
  61. #else
  62. TestParamsModelRoundTrip("eng.params_model");
  63. #endif
  64. }
  65. } // namespace tesseract