24 class TfileTest :
public ::testing::Test {
27 std::locale::global(std::locale(
""));
35 MathData() : num_squares_(0), num_triangles_(0) {}
38 for (
int s = 0; s < 42; ++s) squares_.push_back(s * s);
39 num_squares_ = squares_.size();
40 for (
int t = 0; t < 52; ++t) triangles_.push_back(t * (t + 1) / 2);
41 num_triangles_ = triangles_.size();
43 void ExpectEq(
const MathData& other) {
45 EXPECT_EQ(num_squares_, other.num_squares_);
46 for (
int s = 0; s < squares_.size(); ++s)
47 EXPECT_EQ(squares_[s], other.squares_[s]);
48 EXPECT_EQ(num_triangles_, other.num_triangles_);
49 for (
int s = 0; s < triangles_.size(); ++s)
50 EXPECT_EQ(triangles_[s], other.triangles_[s]);
53 if (fp->
FWrite(&num_squares_,
sizeof(num_squares_), 1) != 1)
return false;
54 if (!squares_.Serialize(fp))
return false;
55 if (fp->
FWrite(&num_triangles_,
sizeof(num_triangles_), 1) != 1)
57 if (!triangles_.Serialize(fp))
return false;
61 if (fp->
FReadEndian(&num_squares_,
sizeof(num_squares_), 1) != 1)
63 if (!squares_.DeSerialize(fp))
return false;
64 if (fp->
FReadEndian(&num_triangles_,
sizeof(num_triangles_), 1) != 1)
66 if (!triangles_.DeSerialize(fp))
return false;
69 bool SerializeBigEndian(
TFile* fp) {
70 ReverseN(&num_squares_,
sizeof(num_squares_));
71 if (fp->
FWrite(&num_squares_,
sizeof(num_squares_), 1) != 1)
return false;
74 if (fp->
FWrite(&num_squares_,
sizeof(num_squares_), 1) != 1)
return false;
75 for (
int i = 0; i < squares_.size(); ++i)
76 ReverseN(&squares_[i],
sizeof(squares_[i]));
77 if (!squares_.Serialize(fp))
return false;
78 ReverseN(&num_triangles_,
sizeof(num_triangles_));
79 if (fp->
FWrite(&num_triangles_,
sizeof(num_triangles_), 1) != 1)
81 if (fp->
FWrite(&num_triangles_,
sizeof(num_triangles_), 1) != 1)
83 for (
int i = 0; i < triangles_.size(); ++i)
84 ReverseN(&triangles_[i],
sizeof(triangles_[i]));
85 return triangles_.Serialize(fp);
87 bool DeSerializeBigEndian(
TFile* fp) {
88 if (fp->
FReadEndian(&num_squares_,
sizeof(num_squares_), 1) != 1)
90 if (!squares_.DeSerialize(fp))
return false;
94 if (fp->
FReadEndian(&last_element,
sizeof(last_element), 1) != 1)
97 squares_.push_back(last_element);
98 if (fp->
FReadEndian(&num_triangles_,
sizeof(num_triangles_), 1) != 1)
100 if (!triangles_.DeSerialize(fp))
return false;
101 if (fp->
FReadEndian(&last_element,
sizeof(last_element), 1) != 1)
103 triangles_.remove(0);
104 triangles_.push_back(last_element);
123 EXPECT_TRUE(m1.Serialize(&fpw));
125 EXPECT_TRUE(fpr.Open(&data[0], data.
size()));
127 EXPECT_TRUE(m2.DeSerialize(&fpr));
130 EXPECT_FALSE(m3.DeSerialize(&fpr));
132 EXPECT_TRUE(m3.DeSerialize(&fpr));
136 TEST_F(TfileTest, FGets) {
139 std::string line_str =
"This is a textline with a newline\n";
144 EXPECT_TRUE(m1.Serialize(&fpw));
145 EXPECT_EQ(1, fpw.
FWrite(line_str.data(), line_str.size(), 1));
146 EXPECT_TRUE(m1.Serialize(&fpw));
149 EXPECT_TRUE(fpr.Open(&data[0], data.
size()));
151 EXPECT_TRUE(m2.DeSerialize(&fpr));
153 const int kBufsize = 1024;
154 char buffer[kBufsize + 1];
155 EXPECT_EQ(buffer, fpr.FGets(buffer, kBufsize));
156 EXPECT_STREQ(line_str.c_str(), buffer);
158 EXPECT_TRUE(m3.DeSerialize(&fpr));
162 TEST_F(TfileTest, BigEndian) {
169 EXPECT_TRUE(m1.SerializeBigEndian(&fpw));
171 EXPECT_TRUE(fpr.Open(&data[0], data.
size()));
174 EXPECT_TRUE(m2.DeSerializeBigEndian(&fpr));