tesseract  5.0.0-alpha-619-ge9db
scanutils_test.cc
Go to the documentation of this file.
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 <iostream> // for cout
13 
14 #include "include_gunit.h"
15 #include "scanutils.h"
16 
17 namespace {
18 
19 class ScanutilsTest : public ::testing::Test {
20  protected:
21  void SetUp() override {
22  }
23 };
24 
25 TEST_F(ScanutilsTest, DoesScanf) {
26  // This test verifies that tfscanf does Scanf the same as stdio fscanf.
27  // There are probably a gazillion more test cases that could be added, but
28  // these brought the tesseract and unittest test results in line.
29  std::string filename = file::JoinPath(TESTDATA_DIR, "scanftest.txt");
30  FILE* fp1 = fopen(filename.c_str(), "r");
31  if (fp1 == nullptr) {
32  std::cout << "Failed to open file " << filename << '\n';
33  GTEST_SKIP();
34  }
35  FILE* fp2 = fopen(filename.c_str(), "r");
36  if (fp2 == nullptr) {
37  std::cout << "Failed to open file " << filename << '\n';
38  fclose(fp1);
39  GTEST_SKIP();
40  }
41  // The file contains this:
42  // 42.5 17 0.001000 -0.001000
43  // 0 1 123 -123 0x100
44  // abcdefghijklmnopqrstuvwxyz
45  // abcdefghijklmnopqrstuvwxyz
46  // MF 25 6.25e-2 0.5e5 -1e+4
47  // 42 MF 25 6.25e-2 0.5
48  // 24
49  const int kNumFloats = 4;
50  float f1[kNumFloats], f2[kNumFloats];
51  int r1 = fscanf(fp1, "%f %f %f %f", &f1[0], &f1[1], &f1[2], &f1[3]);
52  int r2 = tfscanf(fp2, "%f %f %f %f", &f2[0], &f2[1], &f2[2], &f2[3]);
53  EXPECT_EQ(r1, kNumFloats);
54  EXPECT_EQ(r2, kNumFloats);
55  if (r1 == r2) {
56  for (int i = 0; i < r1; ++i) {
57  EXPECT_FLOAT_EQ(f1[i], f2[i]);
58  }
59  }
60  const int kNumInts = 5;
61  int i1[kNumInts], i2[kNumInts];
62  r1 = fscanf(fp1, "%d %d %d %d %i", &i1[0], &i1[1], &i1[2], &i1[3], &i1[4]);
63  r2 = tfscanf(fp2, "%d %d %d %d %i", &i2[0], &i2[1], &i2[2], &i2[3], &i2[4]);
64  EXPECT_EQ(r1, kNumInts);
65  EXPECT_EQ(r2, kNumInts);
66  if (r1 == r2) {
67  for (int i = 0; i < kNumInts; ++i) {
68  EXPECT_EQ(i1[i], i2[i]);
69  }
70  }
71  const int kStrLen = 1024;
72  char s1[kStrLen];
73  char s2[kStrLen];
74  r1 = fscanf(fp1, "%1023s", s1);
75  r2 = tfscanf(fp2, "%1023s", s2);
76  EXPECT_EQ(r1, r2);
77  EXPECT_STREQ(s1, s2);
78  EXPECT_EQ(26, strlen(s2));
79  r1 = fscanf(fp1, "%20s", s1);
80  r2 = tfscanf(fp2, "%20s", s2);
81  EXPECT_EQ(r1, r2);
82  EXPECT_STREQ(s1, s2);
83  EXPECT_EQ(20, strlen(s2));
84  // Now read the rest of the alphabet.
85  r1 = fscanf(fp1, "%1023s", s1);
86  r2 = tfscanf(fp2, "%1023s", s2);
87  EXPECT_EQ(r1, r2);
88  EXPECT_STREQ(s1, s2);
89  EXPECT_EQ(6, strlen(s2));
90  r1 = fscanf(fp1, "%1023s", s1);
91  r2 = tfscanf(fp2, "%1023s", s2);
92  EXPECT_EQ(r1, r2);
93  EXPECT_STREQ(s1, s2);
94  EXPECT_EQ(2, strlen(s2));
95  r1 = fscanf(fp1, "%f %f %f %f", &f1[0], &f1[1], &f1[2], &f1[3]);
96  r2 = tfscanf(fp2, "%f %f %f %f", &f2[0], &f2[1], &f2[2], &f2[3]);
97  EXPECT_EQ(r1, r2);
98  for (int i = 0; i < kNumFloats; ++i) EXPECT_FLOAT_EQ(f1[i], f2[i]);
99  // Test the * for field suppression.
100  r1 = fscanf(fp1, "%d %*s %*d %*f %*f", &i1[0]);
101  r2 = tfscanf(fp2, "%d %*s %*d %*f %*f", &i2[0]);
102  EXPECT_EQ(r1, r2);
103  EXPECT_EQ(i1[0], i2[0]);
104  // We should still see the next value and no phantoms.
105  r1 = fscanf(fp1, "%d %1023s", &i1[0], s1);
106  r2 = tfscanf(fp2, "%d %1023s", &i2[0], s2);
107  EXPECT_EQ(r1, r2);
108  EXPECT_EQ(1, r2);
109  EXPECT_EQ(i1[0], i2[0]);
110  fclose(fp2);
111  fclose(fp1);
112 }
113 
114 } // namespace
file::JoinPath
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:43
string
std::string string
Definition: equationdetect_test.cc:21
tfscanf
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
scanutils.h