summaryrefslogtreecommitdiffstats
path: root/src/test/test-memstream-util.c
blob: b8cc8568f04a6b2f5ebf1c590154e76dffa9f245 (plain)
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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "memstream-util.h"
#include "string-util.h"
#include "tests.h"

TEST(memstream_done) {
        _cleanup_(memstream_done) MemStream m = {};

        assert_se(memstream_init(&m));
}

TEST(memstream_empty) {
        _cleanup_(memstream_done) MemStream m = {};
        _cleanup_free_ char *buf = NULL;
        size_t sz;

        assert_se(memstream_init(&m));
        assert_se(memstream_finalize(&m, &buf, &sz) >= 0);
        ASSERT_STREQ(buf, "");
        assert_se(sz == 0);
}

TEST(memstream) {
        _cleanup_(memstream_done) MemStream m = {};
        _cleanup_free_ char *buf = NULL;
        size_t sz;
        FILE *f;

        assert_se(f = memstream_init(&m));
        fputs("hoge", f);
        fputs("おはよう!", f);
        fputs(u8"😀😀😀", f);
        assert_se(memstream_finalize(&m, &buf, &sz) >= 0);
        ASSERT_STREQ(buf, u8"hogeおはよう!😀😀😀");
        assert_se(sz == strlen(u8"hogeおはよう!😀😀😀"));

        buf = mfree(buf);

        assert_se(f = memstream_init(&m));
        fputs("second", f);
        assert_se(memstream_finalize(&m, &buf, &sz) >= 0);
        ASSERT_STREQ(buf, "second");
        assert_se(sz == strlen("second"));
}

TEST(memstream_dump) {
        _cleanup_(memstream_done) MemStream m = {};
        FILE *f;

        assert_se(f = memstream_init(&m));
        fputs("first", f);
        assert_se(memstream_dump(LOG_DEBUG, &m) >= 0);

        assert_se(f = memstream_init(&m));
        fputs("second", f);
        assert_se(memstream_dump(LOG_DEBUG, &m) >= 0);
}

DEFINE_TEST_MAIN(LOG_DEBUG);