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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
#include "file.h"
#include "fs.h"
#include "tempfile.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/stream/file.h>
#include <util/generic/yexception.h>
class TFileTest: public TTestBase {
UNIT_TEST_SUITE(TFileTest);
UNIT_TEST(TestOpen);
UNIT_TEST(TestOpenSync);
UNIT_TEST(TestRW);
UNIT_TEST(TestReWrite);
UNIT_TEST(TestAppend);
UNIT_TEST(TestLinkTo);
UNIT_TEST(TestResize);
UNIT_TEST(TestLocale);
UNIT_TEST(TestFlush);
UNIT_TEST(TestFlushSpecialFile);
UNIT_TEST(TestRawRead);
UNIT_TEST(TestRead);
UNIT_TEST(TestRawPread);
UNIT_TEST(TestPread);
UNIT_TEST(TestCache);
UNIT_TEST_SUITE_END();
public:
void TestOpen();
void TestOpenSync();
void TestRW();
void TestLocale();
void TestFlush();
void TestFlushSpecialFile();
void TestRawRead();
void TestRead();
void TestRawPread();
void TestPread();
void TestCache();
inline void TestLinkTo() {
TTempFile tmp1("tmp1");
TTempFile tmp2("tmp2");
{
TFile f1(tmp1.Name(), OpenAlways | WrOnly);
TFile f2(tmp2.Name(), OpenAlways | WrOnly);
f1.LinkTo(f2);
f1.Write("12345", 5);
f2.Write("67890", 5);
}
UNIT_ASSERT_EQUAL(TUnbufferedFileInput(tmp2.Name()).ReadAll(), "1234567890");
}
inline void TestAppend() {
TTempFile tmp("tmp");
{
TFile f(tmp.Name(), OpenAlways | WrOnly);
f.Write("12345678", 8);
}
{
TFile f(tmp.Name(), OpenAlways | WrOnly | ForAppend);
f.Write("67", 2);
f.Write("89", 2);
}
UNIT_ASSERT_EQUAL(TUnbufferedFileInput(tmp.Name()).ReadAll(), "123456786789");
}
inline void TestReWrite() {
TTempFile tmp("tmp");
{
TFile f(tmp.Name(), OpenAlways | WrOnly);
f.Write("12345678", 8);
}
{
TFile f(tmp.Name(), OpenAlways | WrOnly);
f.Write("6789", 4);
}
UNIT_ASSERT_EQUAL(TUnbufferedFileInput(tmp.Name()).ReadAll(), "67895678");
}
inline void TestResize() {
TTempFile tmp("tmp");
{
TFile file(tmp.Name(), OpenAlways | WrOnly);
file.Write("1234567", 7);
file.Seek(3, sSet);
file.Resize(5);
UNIT_ASSERT_EQUAL(file.GetLength(), 5);
UNIT_ASSERT_EQUAL(file.GetPosition(), 3);
file.Resize(12);
UNIT_ASSERT_EQUAL(file.GetLength(), 12);
UNIT_ASSERT_EQUAL(file.GetPosition(), 3);
}
const TString data = TUnbufferedFileInput(tmp.Name()).ReadAll();
UNIT_ASSERT_EQUAL(data.length(), 12);
UNIT_ASSERT(data.StartsWith("12345"));
}
};
UNIT_TEST_SUITE_REGISTRATION(TFileTest);
void TFileTest::TestOpen() {
TString res;
TFile f1;
try {
TFile f2("f1.txt", OpenExisting);
} catch (const yexception& e) {
res = e.what();
}
UNIT_ASSERT(!res.empty());
res.remove();
try {
TFile f2("f1.txt", OpenAlways);
f1 = f2;
} catch (const yexception& e) {
res = e.what();
}
UNIT_ASSERT(res.empty());
UNIT_ASSERT(f1.IsOpen());
UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt");
UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0);
try {
TFile f2("f1.txt", CreateNew);
} catch (const yexception& e) {
res = e.what();
}
UNIT_ASSERT(!res.empty());
res.remove();
f1.Close();
UNIT_ASSERT(unlink("f1.txt") == 0);
}
void TFileTest::TestOpenSync() {
TFile f1("f1.txt", CreateNew | Sync);
UNIT_ASSERT(f1.IsOpen());
f1.Close();
UNIT_ASSERT(!f1.IsOpen());
UNIT_ASSERT(unlink("f1.txt") == 0);
}
void TFileTest::TestRW() {
TFile f1("f1.txt", CreateNew);
UNIT_ASSERT(f1.IsOpen());
UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt");
UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0);
ui32 d[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
f1.Write(&d, sizeof(ui32) * 10);
UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 40);
UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 40);
UNIT_ASSERT_VALUES_EQUAL(f1.Seek(12, sSet), 12);
f1.Flush();
ui32 v;
f1.Load(&v, sizeof(v));
UNIT_ASSERT_VALUES_EQUAL(v, 3u);
UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 16);
TFile f2 = f1;
UNIT_ASSERT(f2.IsOpen());
UNIT_ASSERT_VALUES_EQUAL(f2.GetName(), "f1.txt");
UNIT_ASSERT_VALUES_EQUAL(f2.GetPosition(), 16);
UNIT_ASSERT_VALUES_EQUAL(f2.GetLength(), 40);
f2.Write(&v, sizeof(v));
UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 20);
UNIT_ASSERT_VALUES_EQUAL(f1.Seek(-4, sCur), 16);
v = 0;
f1.Load(&v, sizeof(v));
UNIT_ASSERT_VALUES_EQUAL(v, 3u);
f1.Close();
UNIT_ASSERT(!f1.IsOpen());
UNIT_ASSERT(!f2.IsOpen());
UNIT_ASSERT(unlink("f1.txt") == 0);
}
#ifdef _unix_
#include <locale.h>
#endif
void TFileTest::TestLocale() {
#ifdef _unix_
const char* loc = setlocale(LC_CTYPE, nullptr);
setlocale(LC_CTYPE, "ru_RU.UTF-8");
#endif
TFile f("Имя.txt", CreateNew);
UNIT_ASSERT(f.IsOpen());
UNIT_ASSERT_VALUES_EQUAL(f.GetName(), "Имя.txt");
UNIT_ASSERT_VALUES_EQUAL(f.GetLength(), 0);
f.Close();
UNIT_ASSERT(NFs::Remove("Имя.txt"));
#ifdef _unix_
setlocale(LC_CTYPE, loc);
#endif
}
void TFileTest::TestFlush() {
TTempFile tmp("tmp");
{
TFile f(tmp.Name(), OpenAlways | WrOnly);
f.Flush();
f.FlushData();
f.Close();
UNIT_ASSERT_EXCEPTION(f.Flush(), TFileError);
UNIT_ASSERT_EXCEPTION(f.FlushData(), TFileError);
}
}
void TFileTest::TestFlushSpecialFile() {
#ifdef _unix_
TFile devNull("/dev/null", WrOnly);
devNull.FlushData();
devNull.Flush();
devNull.Close();
#endif
}
void TFileTest::TestRawRead() {
TTempFile tmp("tmp");
{
TFile file(tmp.Name(), OpenAlways | WrOnly);
file.Write("1234567", 7);
file.Flush();
file.Close();
}
{
TFile file(tmp.Name(), OpenExisting | RdOnly);
char buf[7];
i32 reallyRead = file.RawRead(buf, 7);
Y_ENSURE(0 <= reallyRead && reallyRead <= 7);
Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("1234567").Head(reallyRead));
}
}
void TFileTest::TestRead() {
TTempFile tmp("tmp");
{
TFile file(tmp.Name(), OpenAlways | WrOnly);
file.Write("1234567", 7);
file.Flush();
file.Close();
}
{
TFile file(tmp.Name(), OpenExisting | RdOnly);
char buf[7];
Y_ENSURE(file.Read(buf, 7) == 7);
Y_ENSURE(TStringBuf(buf, 7) == "1234567");
memset(buf, 0, sizeof(buf));
file.Seek(0, sSet);
Y_ENSURE(file.Read(buf, 123) == 7);
Y_ENSURE(TStringBuf(buf, 7) == "1234567");
}
}
void TFileTest::TestRawPread() {
TTempFile tmp("tmp");
{
TFile file(tmp.Name(), OpenAlways | WrOnly);
file.Write("1234567", 7);
file.Flush();
file.Close();
}
{
TFile file(tmp.Name(), OpenExisting | RdOnly);
char buf[7];
i32 reallyRead = file.RawPread(buf, 3, 1);
Y_ENSURE(0 <= reallyRead && reallyRead <= 3);
Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("234").Head(reallyRead));
memset(buf, 0, sizeof(buf));
reallyRead = file.RawPread(buf, 2, 5);
Y_ENSURE(0 <= reallyRead && reallyRead <= 2);
Y_ENSURE(TStringBuf(buf, reallyRead) == TStringBuf("67").Head(reallyRead));
}
}
void TFileTest::TestPread() {
TTempFile tmp("tmp");
{
TFile file(tmp.Name(), OpenAlways | WrOnly);
file.Write("1234567", 7);
file.Flush();
file.Close();
}
{
TFile file(tmp.Name(), OpenExisting | RdOnly);
char buf[7];
Y_ENSURE(file.Pread(buf, 3, 1) == 3);
Y_ENSURE(TStringBuf(buf, 3) == "234");
memset(buf, 0, sizeof(buf));
Y_ENSURE(file.Pread(buf, 2, 5) == 2);
Y_ENSURE(TStringBuf(buf, 2) == "67");
}
}
#ifdef _linux_
#include <sys/statfs.h>
#endif
#ifndef TMPFS_MAGIC
#define TMPFS_MAGIC 0x01021994
#endif
void TFileTest::TestCache(){
#ifdef _linux_
{// create file in /tmp, current dir could be tmpfs which does not support fadvise
TFile file(MakeTempName("/tmp"), OpenAlways | Transient | RdWr | NoReadAhead);
struct statfs fs;
if (!fstatfs(file.GetHandle(), &fs) && fs.f_type == TMPFS_MAGIC) {
return;
}
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 0);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(0, 0), 0);
file.Resize(7);
file.PrefetchCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 7);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(3, 2), 2);
file.FlushCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 7);
file.EvictCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 0);
file.PrefetchCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 7);
file.Resize(12345);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 4096);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(4096, 0), 0);
file.PrefetchCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 12345);
file.FlushCache();
file.EvictCache();
UNIT_ASSERT_LE(file.CountCache(), 0);
file.Resize(33333333);
file.PrefetchCache(11111111, 11111111);
UNIT_ASSERT_GE(file.CountCache(), 11111111);
UNIT_ASSERT_LE(file.CountCache(0, 11111111), 1111111);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(11111111, 11111111), 11111111);
UNIT_ASSERT_LE(file.CountCache(22222222, 11111111), 1111111);
file.FlushCache(11111111, 11111111);
UNIT_ASSERT_GE(file.CountCache(), 11111111);
// first and last incomplete pages could stay in cache
file.EvictCache(11111111, 11111111);
UNIT_ASSERT_LT(file.CountCache(11111111, 11111111), 4096 * 2);
file.EvictCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), 0);
}
#else
{TFile file(MakeTempName(), OpenAlways | Transient | RdWr);
file.Resize(12345);
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(), -1);
file.PrefetchCache();
file.FlushCache();
file.EvictCache();
UNIT_ASSERT_VALUES_EQUAL(file.CountCache(0, 12345), -1);
}
#endif
}
Y_UNIT_TEST_SUITE(TTestFileHandle) {
Y_UNIT_TEST(MoveAssignment) {
TTempFile tmp("tmp");
{
TFileHandle file1(tmp.Name(), OpenAlways | WrOnly);
file1.Write("1", 1);
TFileHandle file2;
file2 = std::move(file1);
Y_ENSURE(!file1.IsOpen());
Y_ENSURE(file2.IsOpen());
file2.Write("2", 1);
}
{
TFileHandle file(tmp.Name(), OpenExisting | RdOnly);
char buf[2];
Y_ENSURE(file.Read(buf, 2) == 2);
Y_ENSURE(TStringBuf(buf, 2) == "12");
}
}
} // Y_UNIT_TEST_SUITE(TTestFileHandle)
Y_UNIT_TEST_SUITE(TTestDecodeOpenMode) {
Y_UNIT_TEST(It) {
UNIT_ASSERT_VALUES_EQUAL("0", DecodeOpenMode(0));
UNIT_ASSERT_VALUES_EQUAL("RdOnly", DecodeOpenMode(RdOnly));
UNIT_ASSERT_VALUES_EQUAL("RdWr", DecodeOpenMode(RdWr));
UNIT_ASSERT_VALUES_EQUAL("WrOnly|ForAppend", DecodeOpenMode(WrOnly | ForAppend));
UNIT_ASSERT_VALUES_EQUAL("RdWr|CreateAlways|CreateNew|ForAppend|Transient|CloseOnExec|Temp|Sync|Direct|DirectAligned|Seq|NoReuse|NoReadAhead|AX|AR|AW|AWOther|0xF8888000", DecodeOpenMode(0xFFFFFFFF));
}
} // Y_UNIT_TEST_SUITE(TTestDecodeOpenMode)
|