Skip to content

Commit

Permalink
unittest: Fix and enable scanutils_test
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Jun 14, 2019
1 parent 3afc946 commit e0e2912
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion test
Submodule test updated 1 files
+7 −0 testdata/scanftest.txt
5 changes: 4 additions & 1 deletion unittest/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ check_PROGRAMS += qrsequence_test
check_PROGRAMS += recodebeam_test
check_PROGRAMS += rect_test
check_PROGRAMS += resultiterator_test
# check_PROGRAMS += scanutils_test
check_PROGRAMS += scanutils_test
check_PROGRAMS += shapetable_test
# check_PROGRAMS += stridemap_test
check_PROGRAMS += stats_test
Expand Down Expand Up @@ -302,6 +302,9 @@ rect_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
resultiterator_test_SOURCES = resultiterator_test.cc
resultiterator_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TRAINING_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS)

scanutils_test_SOURCES = scanutils_test.cc
scanutils_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)

shapetable_test_SOURCES = shapetable_test.cc
shapetable_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)

Expand Down
56 changes: 43 additions & 13 deletions unittest/scanutils_test.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
#include <stdio.h>
// (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.

#include "tesseract/ccutil/scanutils.h"
#include <iostream> // for cout

#include "include_gunit.h"
#include "scanutils.h"

namespace {

class ScanutilsTest : public ::testing::Test {
protected:
void SetUp() {
std::locale::global(std::locale(""));
}

string TestDataNameToPath(const string& name) {
return file::JoinPath(FLAGS_test_srcdir, "testdata/" + name);
void SetUp() override {
}
};

TEST_F(ScanutilsTest, DoesScanf) {
// This test verifies that tfscanf does Scanf the same as stdio fscanf.
// There are probably a gazillion more test cases that could be added, but
// these brought the tesseract and unittest test results in line.
string filename = TestDataNameToPath("scanftest.txt");
std::string filename = file::JoinPath(TESTDATA_DIR, "scanftest.txt");
FILE* fp1 = fopen(filename.c_str(), "r");
if (fp1 == nullptr) {
std::cout << "Failed to open file " << filename << '\n';
GTEST_SKIP();
return;
}
FILE* fp2 = fopen(filename.c_str(), "r");
if (fp2 == nullptr) {
std::cout << "Failed to open file " << filename << '\n';
GTEST_SKIP();
fclose(fp1);
return;
}
// The file contains this:
// 42.5 17 0.001000 -0.001000
// 0 1 123 -123 0x100
Expand All @@ -34,14 +52,24 @@ TEST_F(ScanutilsTest, DoesScanf) {
float f1[kNumFloats], f2[kNumFloats];
int r1 = fscanf(fp1, "%f %f %f %f", &f1[0], &f1[1], &f1[2], &f1[3]);
int r2 = tfscanf(fp2, "%f %f %f %f", &f2[0], &f2[1], &f2[2], &f2[3]);
EXPECT_EQ(r1, r2);
for (int i = 0; i < kNumFloats; ++i) EXPECT_FLOAT_EQ(f1[i], f2[i]);
EXPECT_EQ(r1, kNumFloats);
EXPECT_EQ(r2, kNumFloats);
if (r1 == r2) {
for (int i = 0; i < r1; ++i) {
EXPECT_FLOAT_EQ(f1[i], f2[i]);
}
}
const int kNumInts = 5;
int i1[kNumInts], i2[kNumInts];
r1 = fscanf(fp1, "%d %d %d %d %i", &i1[0], &i1[1], &i1[2], &i1[3], &i1[4]);
r2 = tfscanf(fp2, "%d %d %d %d %i", &i2[0], &i2[1], &i2[2], &i2[3], &i2[4]);
EXPECT_EQ(r1, r2);
for (int i = 0; i < kNumInts; ++i) EXPECT_EQ(i1[i], i2[i]);
EXPECT_EQ(r1, kNumInts);
EXPECT_EQ(r2, kNumInts);
if (r1 == r2) {
for (int i = 0; i < kNumInts; ++i) {
EXPECT_EQ(i1[i], i2[i]);
}
}
const int kStrLen = 1024;
char s1[kStrLen];
char s2[kStrLen];
Expand Down Expand Up @@ -81,6 +109,8 @@ TEST_F(ScanutilsTest, DoesScanf) {
EXPECT_EQ(r1, r2);
EXPECT_EQ(1, r2);
EXPECT_EQ(i1[0], i2[0]);
fclose(fp2);
fclose(fp1);
}

} // namespace

0 comments on commit e0e2912

Please sign in to comment.