Skip to content

Commit e0e2912

Browse files
committed
unittest: Fix and enable scanutils_test
Signed-off-by: Stefan Weil <[email protected]>
1 parent 3afc946 commit e0e2912

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

test

unittest/Makefile.am

+4-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ check_PROGRAMS += qrsequence_test
140140
check_PROGRAMS += recodebeam_test
141141
check_PROGRAMS += rect_test
142142
check_PROGRAMS += resultiterator_test
143-
# check_PROGRAMS += scanutils_test
143+
check_PROGRAMS += scanutils_test
144144
check_PROGRAMS += shapetable_test
145145
# check_PROGRAMS += stridemap_test
146146
check_PROGRAMS += stats_test
@@ -302,6 +302,9 @@ rect_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
302302
resultiterator_test_SOURCES = resultiterator_test.cc
303303
resultiterator_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TRAINING_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS)
304304

305+
scanutils_test_SOURCES = scanutils_test.cc
306+
scanutils_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
307+
305308
shapetable_test_SOURCES = shapetable_test.cc
306309
shapetable_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)
307310

unittest/scanutils_test.cc

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
#include <stdio.h>
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.
211

3-
#include "tesseract/ccutil/scanutils.h"
12+
#include <iostream> // for cout
13+
14+
#include "include_gunit.h"
15+
#include "scanutils.h"
416

517
namespace {
618

719
class ScanutilsTest : public ::testing::Test {
820
protected:
9-
void SetUp() {
10-
std::locale::global(std::locale(""));
11-
}
12-
13-
string TestDataNameToPath(const string& name) {
14-
return file::JoinPath(FLAGS_test_srcdir, "testdata/" + name);
21+
void SetUp() override {
1522
}
1623
};
1724

1825
TEST_F(ScanutilsTest, DoesScanf) {
1926
// This test verifies that tfscanf does Scanf the same as stdio fscanf.
2027
// There are probably a gazillion more test cases that could be added, but
2128
// these brought the tesseract and unittest test results in line.
22-
string filename = TestDataNameToPath("scanftest.txt");
29+
std::string filename = file::JoinPath(TESTDATA_DIR, "scanftest.txt");
2330
FILE* fp1 = fopen(filename.c_str(), "r");
31+
if (fp1 == nullptr) {
32+
std::cout << "Failed to open file " << filename << '\n';
33+
GTEST_SKIP();
34+
return;
35+
}
2436
FILE* fp2 = fopen(filename.c_str(), "r");
37+
if (fp2 == nullptr) {
38+
std::cout << "Failed to open file " << filename << '\n';
39+
GTEST_SKIP();
40+
fclose(fp1);
41+
return;
42+
}
2543
// The file contains this:
2644
// 42.5 17 0.001000 -0.001000
2745
// 0 1 123 -123 0x100
@@ -34,14 +52,24 @@ TEST_F(ScanutilsTest, DoesScanf) {
3452
float f1[kNumFloats], f2[kNumFloats];
3553
int r1 = fscanf(fp1, "%f %f %f %f", &f1[0], &f1[1], &f1[2], &f1[3]);
3654
int r2 = tfscanf(fp2, "%f %f %f %f", &f2[0], &f2[1], &f2[2], &f2[3]);
37-
EXPECT_EQ(r1, r2);
38-
for (int i = 0; i < kNumFloats; ++i) EXPECT_FLOAT_EQ(f1[i], f2[i]);
55+
EXPECT_EQ(r1, kNumFloats);
56+
EXPECT_EQ(r2, kNumFloats);
57+
if (r1 == r2) {
58+
for (int i = 0; i < r1; ++i) {
59+
EXPECT_FLOAT_EQ(f1[i], f2[i]);
60+
}
61+
}
3962
const int kNumInts = 5;
4063
int i1[kNumInts], i2[kNumInts];
4164
r1 = fscanf(fp1, "%d %d %d %d %i", &i1[0], &i1[1], &i1[2], &i1[3], &i1[4]);
4265
r2 = tfscanf(fp2, "%d %d %d %d %i", &i2[0], &i2[1], &i2[2], &i2[3], &i2[4]);
43-
EXPECT_EQ(r1, r2);
44-
for (int i = 0; i < kNumInts; ++i) EXPECT_EQ(i1[i], i2[i]);
66+
EXPECT_EQ(r1, kNumInts);
67+
EXPECT_EQ(r2, kNumInts);
68+
if (r1 == r2) {
69+
for (int i = 0; i < kNumInts; ++i) {
70+
EXPECT_EQ(i1[i], i2[i]);
71+
}
72+
}
4573
const int kStrLen = 1024;
4674
char s1[kStrLen];
4775
char s2[kStrLen];
@@ -81,6 +109,8 @@ TEST_F(ScanutilsTest, DoesScanf) {
81109
EXPECT_EQ(r1, r2);
82110
EXPECT_EQ(1, r2);
83111
EXPECT_EQ(i1[0], i2[0]);
112+
fclose(fp2);
113+
fclose(fp1);
84114
}
85115

86116
} // namespace

0 commit comments

Comments
 (0)