2019-06-14 22:50:58 +08:00
|
|
|
// (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.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <iostream> // for cout
|
2019-06-14 22:50:58 +08:00
|
|
|
|
|
|
|
#include "include_gunit.h"
|
|
|
|
#include "scanutils.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
class ScanutilsTest : public ::testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
|
|
|
void SetUp() override {}
|
2018-08-24 21:07:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
2019-06-14 22:50:58 +08:00
|
|
|
std::string filename = file::JoinPath(TESTDATA_DIR, "scanftest.txt");
|
2021-03-13 05:06:34 +08:00
|
|
|
FILE *fp1 = fopen(filename.c_str(), "r");
|
2019-06-14 22:50:58 +08:00
|
|
|
if (fp1 == nullptr) {
|
|
|
|
std::cout << "Failed to open file " << filename << '\n';
|
|
|
|
GTEST_SKIP();
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
FILE *fp2 = fopen(filename.c_str(), "r");
|
2019-06-14 22:50:58 +08:00
|
|
|
if (fp2 == nullptr) {
|
|
|
|
std::cout << "Failed to open file " << filename << '\n';
|
|
|
|
fclose(fp1);
|
2019-07-10 22:38:30 +08:00
|
|
|
GTEST_SKIP();
|
2019-06-14 22:50:58 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
// The file contains this:
|
|
|
|
// 42.5 17 0.001000 -0.001000
|
|
|
|
// 0 1 123 -123 0x100
|
|
|
|
// abcdefghijklmnopqrstuvwxyz
|
|
|
|
// abcdefghijklmnopqrstuvwxyz
|
|
|
|
// MF 25 6.25e-2 0.5e5 -1e+4
|
|
|
|
// 42 MF 25 6.25e-2 0.5
|
|
|
|
// 24
|
|
|
|
const int kNumFloats = 4;
|
|
|
|
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]);
|
2019-06-14 22:50:58 +08:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
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]);
|
2019-06-14 22:50:58 +08:00
|
|
|
EXPECT_EQ(r1, kNumInts);
|
|
|
|
EXPECT_EQ(r2, kNumInts);
|
|
|
|
if (r1 == r2) {
|
|
|
|
for (int i = 0; i < kNumInts; ++i) {
|
|
|
|
EXPECT_EQ(i1[i], i2[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
const int kStrLen = 1024;
|
|
|
|
char s1[kStrLen];
|
|
|
|
char s2[kStrLen];
|
|
|
|
r1 = fscanf(fp1, "%1023s", s1);
|
|
|
|
r2 = tfscanf(fp2, "%1023s", s2);
|
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_STREQ(s1, s2);
|
|
|
|
EXPECT_EQ(26, strlen(s2));
|
|
|
|
r1 = fscanf(fp1, "%20s", s1);
|
|
|
|
r2 = tfscanf(fp2, "%20s", s2);
|
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_STREQ(s1, s2);
|
|
|
|
EXPECT_EQ(20, strlen(s2));
|
|
|
|
// Now read the rest of the alphabet.
|
2019-07-10 22:33:08 +08:00
|
|
|
r1 = fscanf(fp1, "%1023s", s1);
|
|
|
|
r2 = tfscanf(fp2, "%1023s", s2);
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_STREQ(s1, s2);
|
|
|
|
EXPECT_EQ(6, strlen(s2));
|
2019-07-10 22:33:08 +08:00
|
|
|
r1 = fscanf(fp1, "%1023s", s1);
|
|
|
|
r2 = tfscanf(fp2, "%1023s", s2);
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_STREQ(s1, s2);
|
|
|
|
EXPECT_EQ(2, strlen(s2));
|
|
|
|
r1 = fscanf(fp1, "%f %f %f %f", &f1[0], &f1[1], &f1[2], &f1[3]);
|
|
|
|
r2 = tfscanf(fp2, "%f %f %f %f", &f2[0], &f2[1], &f2[2], &f2[3]);
|
|
|
|
EXPECT_EQ(r1, r2);
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int i = 0; i < kNumFloats; ++i) {
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_FLOAT_EQ(f1[i], f2[i]);
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
// Test the * for field suppression.
|
|
|
|
r1 = fscanf(fp1, "%d %*s %*d %*f %*f", &i1[0]);
|
2018-09-29 15:19:13 +08:00
|
|
|
r2 = tfscanf(fp2, "%d %*s %*d %*f %*f", &i2[0]);
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_EQ(i1[0], i2[0]);
|
|
|
|
// We should still see the next value and no phantoms.
|
2019-07-10 22:33:08 +08:00
|
|
|
r1 = fscanf(fp1, "%d %1023s", &i1[0], s1);
|
|
|
|
r2 = tfscanf(fp2, "%d %1023s", &i2[0], s2);
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_EQ(r1, r2);
|
|
|
|
EXPECT_EQ(1, r2);
|
|
|
|
EXPECT_EQ(i1[0], i2[0]);
|
2019-06-14 22:50:58 +08:00
|
|
|
fclose(fp2);
|
|
|
|
fclose(fp1);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|