19 class ScanutilsTest :
public ::testing::Test {
21 void SetUp()
override {
25 TEST_F(ScanutilsTest, DoesScanf) {
30 FILE* fp1 = fopen(filename.c_str(),
"r");
32 std::cout <<
"Failed to open file " << filename <<
'\n';
35 FILE* fp2 = fopen(filename.c_str(),
"r");
37 std::cout <<
"Failed to open file " << filename <<
'\n';
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);
56 for (
int i = 0; i < r1; ++i) {
57 EXPECT_FLOAT_EQ(f1[i], f2[i]);
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);
67 for (
int i = 0; i < kNumInts; ++i) {
68 EXPECT_EQ(i1[i], i2[i]);
71 const int kStrLen = 1024;
74 r1 = fscanf(fp1,
"%1023s", s1);
75 r2 =
tfscanf(fp2,
"%1023s", s2);
78 EXPECT_EQ(26, strlen(s2));
79 r1 = fscanf(fp1,
"%20s", s1);
83 EXPECT_EQ(20, strlen(s2));
85 r1 = fscanf(fp1,
"%1023s", s1);
86 r2 =
tfscanf(fp2,
"%1023s", s2);
89 EXPECT_EQ(6, strlen(s2));
90 r1 = fscanf(fp1,
"%1023s", s1);
91 r2 =
tfscanf(fp2,
"%1023s", 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]);
98 for (
int i = 0; i < kNumFloats; ++i) EXPECT_FLOAT_EQ(f1[i], f2[i]);
100 r1 = fscanf(fp1,
"%d %*s %*d %*f %*f", &i1[0]);
101 r2 =
tfscanf(fp2,
"%d %*s %*d %*f %*f", &i2[0]);
103 EXPECT_EQ(i1[0], i2[0]);
105 r1 = fscanf(fp1,
"%d %1023s", &i1[0], s1);
106 r2 =
tfscanf(fp2,
"%d %1023s", &i2[0], s2);
109 EXPECT_EQ(i1[0], i2[0]);