Skip to content

Commit 5529a5d

Browse files
committed
unittest: Fix and enable params_model_test
This needs the latest test submodule. The test uses LoadFromFile which is not used otherwise, so remove that function from class ParamsModel. Signed-off-by: Stefan Weil <[email protected]>
1 parent b1078dd commit 5529a5d

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

src/wordrec/params_model.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// File: params_model.cpp
33
// Description: Trained language model parameters.
44
// Author: David Eger
5-
// Created: Mon Jun 11 11:26:42 PDT 2012
65
//
76
// (C) Copyright 2012, Google Inc.
87
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -100,17 +99,6 @@ bool ParamsModel::Equivalent(const ParamsModel &that) const {
10099
return true;
101100
}
102101

103-
bool ParamsModel::LoadFromFile(
104-
const char *lang,
105-
const char *full_path) {
106-
TFile fp;
107-
if (!fp.Open(full_path, nullptr)) {
108-
tprintf("Error opening file %s\n", full_path);
109-
return false;
110-
}
111-
return LoadFromFp(lang, &fp);
112-
}
113-
114102
bool ParamsModel::LoadFromFp(const char *lang, TFile *fp) {
115103
const int kMaxLineSize = 100;
116104
char line[kMaxLineSize];

src/wordrec/params_model.h

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// File: params_model.h
33
// Description: Trained feature serialization for language parameter training.
44
// Author: David Eger
5-
// Created: Mon Jun 11 11:26:42 PDT 2012
65
//
76
// (C) Copyright 2011, Google Inc.
87
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -62,7 +61,6 @@ class ParamsModel {
6261
bool SaveToFile(const char *full_path) const;
6362

6463
// Returns true on success.
65-
bool LoadFromFile(const char *lang, const char *full_path);
6664
bool LoadFromFp(const char *lang, TFile *fp);
6765

6866
const GenericVector<float>& weights() const {

test

unittest/Makefile.am

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ check_PROGRAMS = \
123123
matrix_test \
124124
nthitem_test \
125125
osd_test \
126-
paragraphs_test \
126+
paragraphs_test
127+
check_PROGRAMS += params_model_test
128+
check_PROGRAMS += \
127129
progress_test \
128130
qrsequence_test \
129131
recodebeam_test \
@@ -259,6 +261,9 @@ nthitem_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
259261
paragraphs_test_SOURCES = paragraphs_test.cc
260262
paragraphs_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)
261263

264+
params_model_test_SOURCES = params_model_test.cc
265+
params_model_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
266+
262267
osd_test_SOURCES = osd_test.cc
263268
osd_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
264269

unittest/params_model_test.cc

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
1-
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
213
#include <vector>
314

4-
#include "tesseract/wordrec/params_model.h"
15+
#include "include_gunit.h"
16+
#include "params_model.h"
17+
#include "serialis.h" // TFile
18+
#include "tprintf.h" // tprintf
519

620
namespace {
721

822
// Test some basic I/O of params model files (automated learning of language
923
// model weights).
24+
static bool LoadFromFile(tesseract::ParamsModel& model, const char* lang, const char* full_path) {
25+
tesseract::TFile fp;
26+
if (!fp.Open(full_path, nullptr)) {
27+
tprintf("Error opening file %s\n", full_path);
28+
return false;
29+
}
30+
return model.LoadFromFp(lang, &fp);
31+
}
32+
1033
class ParamsModelTest : public testing::Test {
1134
protected:
12-
string TestDataNameToPath(const string& name) const {
13-
return file::JoinPath(FLAGS_test_srcdir, "testdata/" + name);
35+
std::string TestDataNameToPath(const std::string& name) const {
36+
return file::JoinPath(TESTDATA_DIR, name);
1437
}
15-
string OutputNameToPath(const string& name) const {
38+
std::string OutputNameToPath(const std::string& name) const {
1639
return file::JoinPath(FLAGS_test_tmpdir, name);
1740
}
1841
// Test that we are able to load a params model, save it, reload it,
1942
// and verify that the re-serialized version is the same as the original.
20-
void TestParamsModelRoundTrip(const string& params_model_filename) const {
43+
void TestParamsModelRoundTrip(const std::string& params_model_filename) const {
2144
tesseract::ParamsModel orig_model;
2245
tesseract::ParamsModel duplicate_model;
23-
string orig_file = TestDataNameToPath(params_model_filename);
24-
string out_file = OutputNameToPath(params_model_filename);
46+
std::string orig_file = TestDataNameToPath(params_model_filename);
47+
std::string out_file = OutputNameToPath(params_model_filename);
2548

26-
EXPECT_TRUE(orig_model.LoadFromFile("eng", orig_file.c_str()));
49+
EXPECT_TRUE(LoadFromFile(orig_model, "eng", orig_file.c_str()));
2750
EXPECT_TRUE(orig_model.SaveToFile(out_file.c_str()));
2851

29-
EXPECT_TRUE(duplicate_model.LoadFromFile("eng", out_file.c_str()));
52+
EXPECT_TRUE(LoadFromFile(duplicate_model, "eng", out_file.c_str()));
3053
EXPECT_TRUE(orig_model.Equivalent(duplicate_model));
3154
}
3255
};

0 commit comments

Comments
 (0)