forked from youtube/cobalt_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_write_test.cc
169 lines (134 loc) · 4.91 KB
/
file_write_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// 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.
// Writing is partially tested by some of the file helpers that create files for
// the tests to operate on.
#include <string>
#include "starboard/file.h"
#include "starboard/nplb/file_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
// Sets up an empty test fixture, required for typed tests.
template <class SbFileWriteType>
class SbFileWriteTest : public testing::Test {};
class SbFileWriter {
public:
static int Write(SbFile file, char* data, int size) {
return SbFileWrite(file, data, size);
}
};
class SbFileWriterAll {
public:
static int Write(SbFile file, char* data, int size) {
return SbFileWriteAll(file, data, size);
}
};
typedef testing::Types<SbFileWriter, SbFileWriterAll> SbFileWriteTestTypes;
TYPED_TEST_CASE(SbFileWriteTest, SbFileWriteTestTypes);
const int kBufferLength = 16 * 1024;
TYPED_TEST(SbFileWriteTest, InvalidFileErrors) {
char buffer[kBufferLength] = {0};
int result = TypeParam::Write(kSbFileInvalid, buffer, kBufferLength);
EXPECT_EQ(-1, result);
}
TYPED_TEST(SbFileWriteTest, BasicWriting) {
// Choose a file size that is not an even multiple of the buffer size, but
// is over several times the size of the buffer.
const int kFileSize = kBufferLength * 16 / 3;
ScopedRandomFile random_file(0, ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();
SbFile file = SbFileOpen(filename.c_str(),
kSbFileCreateAlways | kSbFileWrite | kSbFileRead,
NULL, NULL);
ASSERT_TRUE(SbFileIsValid(file));
// Create a bigger buffer than necessary, so we can test the memory around
// the portion given to SbFileRead.
char buffer[kBufferLength] = {0};
// Initialize to some arbitrary pattern so we can verify it later.
for (int i = 0; i < kBufferLength; ++i) {
buffer[i] = static_cast<char>(i & 0xFF);
}
// Read and check the whole file.
int total = 0;
while (true) {
if (total == kFileSize) {
break;
}
int remaining = kFileSize - total;
int to_write = remaining < kBufferLength ? remaining : kBufferLength;
int bytes_written = TypeParam::Write(
file, buffer + (total % kBufferLength), to_write);
// Check that we didn't write more than the buffer size.
EXPECT_GE(to_write, bytes_written);
// Check that we didn't get an error.
ASSERT_LT(0, bytes_written);
total += bytes_written;
EXPECT_EQ(total, SbFileSeek(file, kSbFileFromCurrent, 0));
}
// Tests reading and writing from same opened file.
bool result = SbFileFlush(file);
ASSERT_TRUE(result);
int position = static_cast<int>(SbFileSeek(file, kSbFileFromBegin, 0));
ASSERT_EQ(0, position);
// Read and check the whole file.
total = 0;
int previous_total = 0;
while (true) {
int bytes_read = SbFileReadAll(file, buffer, kBufferLength);
if (bytes_read == 0) {
break;
}
// Check that we didn't read more than the buffer size.
EXPECT_GE(kBufferLength, bytes_read);
// Check that we didn't get an error.
ASSERT_LT(0, bytes_read);
// Do some accounting to check later.
previous_total = total;
total += bytes_read;
ScopedRandomFile::ExpectPattern(previous_total, buffer, bytes_read,
__LINE__);
}
// Check that we read the whole file.
EXPECT_EQ(kFileSize, total);
result = SbFileClose(file);
EXPECT_TRUE(result);
}
TYPED_TEST(SbFileWriteTest, WriteZeroBytes) {
ScopedRandomFile random_file(0, ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();
SbFile file = SbFileOpen(filename.c_str(), kSbFileCreateOnly | kSbFileWrite,
NULL, NULL);
ASSERT_TRUE(SbFileIsValid(file));
char buffer[kBufferLength] = {0};
// Write zero bytes.
for (int i = 0; i < 10; ++i) {
int bytes_written = TypeParam::Write(file, buffer, 0);
EXPECT_EQ(0, bytes_written);
}
bool result = SbFileClose(file);
EXPECT_TRUE(result);
file =
SbFileOpen(filename.c_str(), kSbFileOpenOnly | kSbFileRead, NULL, NULL);
ASSERT_TRUE(SbFileIsValid(file));
SbFileInfo info;
result = SbFileGetInfo(file, &info);
EXPECT_TRUE(result);
EXPECT_EQ(0, info.size);
result = SbFileClose(file);
EXPECT_TRUE(result);
}
} // namespace
} // namespace nplb
} // namespace starboard