aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/guard/guard_test.cpp
blob: 2efe66201f9a410ba8c32fb5ed456d527e868b71 (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
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/guard.h>
#include <fcntl.h>
#include <unistd.h>

using namespace vespalib;

TEST("testFilePointer")
{
    {
        FilePointer file(fopen("bogus", "r"));
        EXPECT_TRUE(!file.valid());
    }
    {
        FilePointer file(fopen("filept.txt", "w"));
        EXPECT_TRUE(file.valid());
        fprintf(file, "Hello");
    }
    {
        FilePointer file(fopen("filept.txt", "r"));
        EXPECT_TRUE(file.valid());
        char tmp[128];
        char *fgetsres = fgets(tmp, sizeof(tmp), file);
        ASSERT_EQUAL(tmp, fgetsres);
        EXPECT_TRUE(strcmp(tmp, "Hello") == 0);
    }
    {
        FILE *pt = NULL;
        {
            FilePointer file(fopen("filept.txt", "r"));
            EXPECT_TRUE(file.valid());
            pt = file;
        }
        EXPECT_TRUE(pt != NULL);
        // char tmp[128];
        // EXPECT_TRUE(fgets(tmp, sizeof(tmp), pt) == NULL);
    }
    {
        FilePointer file(fopen("filept.txt", "w"));
        EXPECT_TRUE(file.valid());
        fprintf(file, "World");

        file.reset(fopen("filept.txt", "r"));
        EXPECT_TRUE(file.valid());
        char tmp[128];
        char *fgetsres = fgets(tmp, sizeof(tmp), file.fp());
        ASSERT_EQUAL(tmp, fgetsres);
        EXPECT_TRUE(strcmp(tmp, "World") == 0);

        FILE *ref = file.fp();
        FILE *fp = file.release();
        EXPECT_TRUE(fp != NULL);
        EXPECT_TRUE(fp == ref);
        EXPECT_TRUE(!file.valid());
        EXPECT_TRUE(file.fp() == NULL);
        fclose(fp);
    }
}

TEST("testFileDescriptor")
{
    {
        FileDescriptor file(open("bogus", O_RDONLY));
        EXPECT_TRUE(!file.valid());
    }
    {
        FileDescriptor file(open("filedesc.txt", O_WRONLY | O_CREAT, 0644));
        EXPECT_TRUE(file.valid());
        EXPECT_TRUE((size_t)write(file.fd(), "Hello", strlen("Hello")) == strlen("Hello"));
    }
    {
        FileDescriptor file(open("filedesc.txt", O_RDONLY));
        EXPECT_TRUE(file.valid());
        char tmp[128];
        size_t res = read(file.fd(), tmp, sizeof(tmp));
        EXPECT_TRUE(res == strlen("Hello"));
        tmp[res] = '\0';
        EXPECT_TRUE(strcmp(tmp, "Hello") == 0);
    }
    {
        int fd = -1;
        {
            FileDescriptor file(open("filedesc.txt", O_RDONLY));
            EXPECT_TRUE(file.valid());
            fd = file.fd();
        }
        char tmp[128];
        EXPECT_TRUE(read(fd, tmp, sizeof(tmp)) == -1);
    }
    {
        FileDescriptor file(open("filedesc.txt", O_WRONLY | O_CREAT, 0644));
        EXPECT_TRUE(file.valid());
        EXPECT_TRUE((size_t)write(file.fd(), "World", strlen("World")) == strlen("World"));

        file.reset(open("filedesc.txt", O_RDONLY));
        EXPECT_TRUE(file.valid());
        char tmp[128];
        size_t res = read(file.fd(), tmp, sizeof(tmp));
        EXPECT_TRUE(res == strlen("World"));
        tmp[res] = '\0';
        EXPECT_TRUE(strcmp(tmp, "World") == 0);

        int ref = file.fd();
        int fd = file.release();
        EXPECT_TRUE(fd >= 0);
        EXPECT_TRUE(fd == ref);
        EXPECT_TRUE(!file.valid());
        EXPECT_TRUE(file.fd() == -1);
        close(fd);
    }
}

TEST("testCounterGuard")
{
    int cnt = 10;
    {
        EXPECT_TRUE(cnt == 10);
        CounterGuard guard(cnt);
        EXPECT_TRUE(cnt == 11);
    }
    EXPECT_TRUE(cnt == 10);
}

TEST_MAIN() { TEST_RUN_ALL(); }