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.
2
11
3
- #include " tesseract/ccutil/scanutils.h"
12
+ #include < iostream> // for cout
13
+
14
+ #include " include_gunit.h"
15
+ #include " scanutils.h"
4
16
5
17
namespace {
6
18
7
19
class ScanutilsTest : public ::testing::Test {
8
20
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 {
15
22
}
16
23
};
17
24
18
25
TEST_F (ScanutilsTest, DoesScanf) {
19
26
// This test verifies that tfscanf does Scanf the same as stdio fscanf.
20
27
// There are probably a gazillion more test cases that could be added, but
21
28
// 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" );
23
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
+ return ;
35
+ }
24
36
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
+ }
25
43
// The file contains this:
26
44
// 42.5 17 0.001000 -0.001000
27
45
// 0 1 123 -123 0x100
@@ -34,14 +52,24 @@ TEST_F(ScanutilsTest, DoesScanf) {
34
52
float f1[kNumFloats ], f2[kNumFloats ];
35
53
int r1 = fscanf (fp1, " %f %f %f %f" , &f1[0 ], &f1[1 ], &f1[2 ], &f1[3 ]);
36
54
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
+ }
39
62
const int kNumInts = 5 ;
40
63
int i1[kNumInts ], i2[kNumInts ];
41
64
r1 = fscanf (fp1, " %d %d %d %d %i" , &i1[0 ], &i1[1 ], &i1[2 ], &i1[3 ], &i1[4 ]);
42
65
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
+ }
45
73
const int kStrLen = 1024 ;
46
74
char s1[kStrLen ];
47
75
char s2[kStrLen ];
@@ -81,6 +109,8 @@ TEST_F(ScanutilsTest, DoesScanf) {
81
109
EXPECT_EQ (r1, r2);
82
110
EXPECT_EQ (1 , r2);
83
111
EXPECT_EQ (i1[0 ], i2[0 ]);
112
+ fclose (fp2);
113
+ fclose (fp1);
84
114
}
85
115
86
116
} // namespace
0 commit comments