File: test_buffer.c

package info (click to toggle)
dqlite 1.18.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,364 kB
  • sloc: ansic: 57,460; makefile: 336; sh: 243
file content (118 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download | duplicates (6)
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
#include "../../../src/lib/buffer.h"

#include "../../lib/runner.h"

TEST_MODULE(lib_buffer);

/******************************************************************************
 *
 * Fixture
 *
 ******************************************************************************/

struct fixture
{
	struct buffer buffer;
};

static void *setup(const MunitParameter params[], void *user_data)
{
	struct fixture *f = munit_malloc(sizeof *f);
	int rc;
	(void)params;
	(void)user_data;
	rc = buffer__init(&f->buffer);
	munit_assert_int(rc, ==, 0);
	return f;
}

static void tear_down(void *data)
{
	struct fixture *f = data;
	buffer__close(&f->buffer);
	free(f);
}

/******************************************************************************
 *
 * Helper macros.
 *
 ******************************************************************************/

#define ADVANCE(SIZE)                                       \
	{                                                   \
		cursor = buffer__advance(&f->buffer, SIZE); \
		munit_assert_ptr_not_null(cursor);          \
	}

/******************************************************************************
 *
 * Assertions.
 *
 ******************************************************************************/

#define ASSERT_N_PAGES(N) munit_assert_int(f->buffer.n_pages, ==, N)

/******************************************************************************
 *
 * buffer__init
 *
 ******************************************************************************/

TEST_SUITE(init);
TEST_SETUP(init, setup);
TEST_TEAR_DOWN(init, tear_down);

/* If n is 0, then the prefix is used to dermine the number of elements of the
 * tuple. */
TEST_CASE(init, n_pages, NULL)
{
	struct fixture *f = data;
	(void)params;
	ASSERT_N_PAGES(1);
	munit_assert_long(f->buffer.page_size, ==, sysconf(_SC_PAGESIZE));
	return MUNIT_OK;
}

/******************************************************************************
 *
 * buffer__advance
 *
 ******************************************************************************/

TEST_SUITE(advance);
TEST_SETUP(advance, setup);
TEST_TEAR_DOWN(advance, tear_down);

/* The buffer already has enough capacity. */
TEST_CASE(advance, enough, NULL)
{
	struct fixture *f = data;
	void *cursor;
	(void)params;
	ADVANCE(16);
	ASSERT_N_PAGES(1);
	return MUNIT_OK;
}

/* The buffer needs to double its size once. */
TEST_CASE(advance, double, NULL)
{
	struct fixture *f = data;
	void *cursor;
	(void)params;
	ADVANCE(16 + f->buffer.page_size);
	ASSERT_N_PAGES(2);
	return MUNIT_OK;
}

/* The buffer needs to double its sice twice. */
TEST_CASE(advance, double_twice, NULL)
{
	struct fixture *f = data;
	void *cursor;
	(void)params;
	ADVANCE(16 + 3 * f->buffer.page_size);
	ASSERT_N_PAGES(4);
	return MUNIT_OK;
}