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