| 1 |
#include "../../../src/lib/buffer.h" |
| 2 |
|
| 3 |
#include "../../lib/runner.h" |
| 4 |
|
| 5 |
TEST_MODULE(lib_buffer); |
| 6 |
|
| 7 |
/****************************************************************************** |
| 8 |
* |
| 9 |
* Fixture |
| 10 |
* |
| 11 |
******************************************************************************/ |
| 12 |
|
| 13 |
struct fixture |
| 14 |
{ |
| 15 |
struct buffer buffer; |
| 16 |
}; |
| 17 |
|
| 18 |
static void *setup(const MunitParameter params[], void *user_data) |
| 19 |
{ |
| 20 |
struct fixture *f = munit_malloc(sizeof *f); |
| 21 |
int rc; |
| 22 |
(void)params; |
| 23 |
(void)user_data; |
| 24 |
rc = buffer__init(&f->buffer); |
| 25 |
munit_assert_int(rc, ==, 0); |
| 26 |
return f; |
| 27 |
} |
| 28 |
|
| 29 |
static void tear_down(void *data) |
| 30 |
{ |
| 31 |
struct fixture *f = data; |
| 32 |
buffer__close(&f->buffer); |
| 33 |
free(f); |
| 34 |
} |
| 35 |
|
| 36 |
/****************************************************************************** |
| 37 |
* |
| 38 |
* Helper macros. |
| 39 |
* |
| 40 |
******************************************************************************/ |
| 41 |
|
| 42 |
#define ADVANCE(SIZE) \ |
| 43 |
{ \ |
| 44 |
cursor = buffer__advance(&f->buffer, SIZE); \ |
| 45 |
munit_assert_ptr_not_null(cursor); \ |
| 46 |
} |
| 47 |
|
| 48 |
/****************************************************************************** |
| 49 |
* |
| 50 |
* Assertions. |
| 51 |
* |
| 52 |
******************************************************************************/ |
| 53 |
|
| 54 |
#define ASSERT_N_PAGES(N) munit_assert_int(f->buffer.n_pages, ==, N) |
| 55 |
|
| 56 |
/****************************************************************************** |
| 57 |
* |
| 58 |
* buffer__init |
| 59 |
* |
| 60 |
******************************************************************************/ |
| 61 |
|
| 62 |
TEST_SUITE(init); |
| 63 |
TEST_SETUP(init, setup); |
| 64 |
TEST_TEAR_DOWN(init, tear_down); |
| 65 |
|
| 66 |
/* If n is 0, then the prefix is used to dermine the number of elements of the |
| 67 |
* tuple. */ |
| 68 |
TEST_CASE(init, n_pages, NULL) |
| 69 |
{ |
| 70 |
struct fixture *f = data; |
| 71 |
(void)params; |
| 72 |
ASSERT_N_PAGES(1); |
| 73 |
munit_assert_long(f->buffer.page_size, ==, sysconf(_SC_PAGESIZE)); |
| 74 |
return MUNIT_OK; |
| 75 |
} |
| 76 |
|
| 77 |
/****************************************************************************** |
| 78 |
* |
| 79 |
* buffer__advance |
| 80 |
* |
| 81 |
******************************************************************************/ |
| 82 |
|
| 83 |
TEST_SUITE(advance); |
| 84 |
TEST_SETUP(advance, setup); |
| 85 |
TEST_TEAR_DOWN(advance, tear_down); |
| 86 |
|
| 87 |
/* The buffer already has enough capacity. */ |
| 88 |
TEST_CASE(advance, enough, NULL) |
| 89 |
{ |
| 90 |
struct fixture *f = data; |
| 91 |
void *cursor; |
| 92 |
(void)params; |
| 93 |
ADVANCE(16); |
| 94 |
ASSERT_N_PAGES(1); |
| 95 |
return MUNIT_OK; |
| 96 |
} |
| 97 |
|
| 98 |
/* The buffer needs to double its size once. */ |
| 99 |
TEST_CASE(advance, double, NULL) |
| 100 |
{ |
| 101 |
struct fixture *f = data; |
| 102 |
void *cursor; |
| 103 |
(void)params; |
| 104 |
ADVANCE(16 + f->buffer.page_size); |
| 105 |
ASSERT_N_PAGES(2); |
| 106 |
return MUNIT_OK; |
| 107 |
} |
| 108 |
|
| 109 |
/* The buffer needs to double its sice twice. */ |
| 110 |
TEST_CASE(advance, double_twice, NULL) |
| 111 |
{ |
| 112 |
struct fixture *f = data; |
| 113 |
void *cursor; |
| 114 |
(void)params; |
| 115 |
ADVANCE(16 + 3 * f->buffer.page_size); |
| 116 |
ASSERT_N_PAGES(4); |
| 117 |
return MUNIT_OK; |
| 118 |
} |