aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/file_ut.cpp
blob: ac0f09796ee1ba4e05c606a8b8ee0f91d2ac5991 (plain) (blame)
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
#include "file.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/system/tempfile.h>

static const char* TmpFileName = "./fileio";
static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan";
static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous");

Y_UNIT_TEST_SUITE(TFileTest) {
    Y_UNIT_TEST(InputTest) {
        TTempFile tmp(TmpFileName);

        {
            TUnbufferedFileOutput output(TmpFileName);
            output.Write(TmpFileContents, strlen(TmpFileContents));
        }

        {
            TUnbufferedFileInput input(TmpFileName);
            TString s = input.ReadAll();
            UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents);
        }

        {
            TUnbufferedFileInput input(TmpFileName);
            input.Skip(TmpFileSubstring - TmpFileContents);
            TString s = input.ReadAll();
            UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan");
        }

        {
            TUnbufferedFileOutput output(TFile::ForAppend(TmpFileName));
            output.Write(TmpFileContents, strlen(TmpFileContents));
        }

        {
            TUnbufferedFileInput input(TmpFileName);
            TString s = input.ReadAll();
            UNIT_ASSERT_VALUES_EQUAL(s, TString::Join(TmpFileContents, TmpFileContents));
        }
    }

    Y_UNIT_TEST(EmptyMapTest) {
        TTempFile tmp(TmpFileName);

        {
            TUnbufferedFileOutput output(TmpFileName);
            /* Write nothing. */
        }

        {
            TMappedFileInput input(TmpFileName);
            TString s = input.ReadAll();
            UNIT_ASSERT(s.empty());
        }
    }

#ifdef _unix_
    Y_UNIT_TEST(PipeReadLineTest) {
        int fds[2];
        UNIT_ASSERT(pipe(fds) == 0);
        TFile readEnd(fds[0]);
        TFileInput fileInput(readEnd);
        UNIT_ASSERT_VALUES_EQUAL(write(fds[1], "hello\n", 6), 6);

        TString line;
        UNIT_ASSERT(fileInput.ReadLine(line));
        UNIT_ASSERT_STRINGS_EQUAL(line, "hello");
        close(fds[1]);
    }
#endif
}