File: test_fileutil.c

package info (click to toggle)
pgbouncer 1.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,884 kB
  • sloc: ansic: 61,425; python: 5,703; sh: 4,527; makefile: 1,361; sed: 22
file content (74 lines) | stat: -rw-r--r-- 1,420 bytes parent folder | download
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 <usual/fileutil.h>

#include <usual/string.h>
#include <usual/mbuf.h>

#include "test_common.h"

/*
 * LN1 = 4*8
 * LN2 = 8*4*8
 * LN3 = 8*8*4*8
 */

#define LN1 "11112222333344445555666677778888"
#define LN2 LN1 LN1 LN1 LN1 LN1 LN1 LN1 LN1
#define LN3 LN2 LN2 LN2 LN2 LN2 LN2 LN2 LN2

static const char fdata[] = "1\n"
			    "line 2\n"
			    "\n"
			    LN3
			    "noln";
static const char filename[] = "test_fileutil.tmp";

static bool createfile(void)
{
	FILE *f = fopen(filename, "wb+");
	if (!f) return false;
	fwrite(fdata, 1, strlen(fdata), f);
	fclose(f);
	return true;
}

static void test_fsize(void *p)
{
	int_check(createfile(), 1);

	tt_assert(file_size(filename) == (int)strlen(fdata));
	tt_assert(file_size(filename) == (int)sizeof(fdata) - 1);
	tt_assert(file_size("nonexist") == -1);
end:    ;
}

static bool addln(void *arg, const char *ln, ssize_t len)
{
	struct MBuf *buf = arg;
	int xlen = len;
	if (len < 0)
		return false;
	if (len > 0 && ln[len - 1] == '\n')
		xlen--;
	if (memchr(ln, '\n', xlen))
		return false;
	return mbuf_write(buf, ln, len);
}

static void test_getline(void *p)
{
	struct MBuf buf;

	mbuf_init_dynamic(&buf);

	tt_assert(foreach_line(filename, addln, &buf));
	tt_assert(mbuf_write_byte(&buf, 0));
end:
	unlink(filename);
	mbuf_free(&buf);
}

struct testcase_t fileutil_tests[] = {
	{ "file_size", test_fsize },
	{ "getline", test_getline },
	END_OF_TESTCASES
};