Skip to content

Commit dd150f3

Browse files
committed
Add libs/event/test.c
1 parent 1a50bac commit dd150f3

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

libs/event/meson.build

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ libevent_dep = declare_dependency(
4040
)
4141

4242
libraries += libevent
43+
44+
if unit_test
45+
e = executable(
46+
'event',
47+
['event.c', 'test.c', slist_src],
48+
c_args: ['-DAM_EVENT_POOLS_NUM_MAX=32'],
49+
dependencies : [libonesize_dep, libqueue_dep, libassert_dep],
50+
include_directories: inc)
51+
test('event', e)
52+
endif

libs/event/test.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) Adel Mamin
5+
*
6+
* Source: https://github.com/adel-mamin/amast
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
/**
28+
* @file
29+
* Event allocation unit tests.
30+
*/
31+
32+
#include <stdint.h>
33+
34+
#include "common/macros.h"
35+
#include "common/alignment.h"
36+
37+
#include "event.h"
38+
39+
struct buf1 {
40+
struct am_event e;
41+
int64_t _;
42+
} buf1;
43+
44+
struct buf2 {
45+
struct am_event e;
46+
int64_t _[2];
47+
} buf2;
48+
49+
struct buf3 {
50+
struct am_event e;
51+
int64_t _[3];
52+
} buf3;
53+
54+
struct buf4 {
55+
struct am_event e;
56+
int64_t _[4];
57+
} buf4;
58+
59+
struct buf5 {
60+
struct am_event e;
61+
int64_t _[5];
62+
} buf5;
63+
64+
static void crit_stub(void) {}
65+
66+
static void test_allocate(int size, int pool_index_plus_one) {
67+
const struct am_event *e = am_event_allocate(AM_EVT_USER, size);
68+
AM_ASSERT(e->pool_index_plus_one == pool_index_plus_one);
69+
am_event_free(e);
70+
}
71+
72+
int main(void) {
73+
const int align = AM_ALIGNOF(am_event_t);
74+
static const struct am_event_state_cfg cfg = {
75+
.crit_enter = crit_stub, .crit_exit = crit_stub
76+
};
77+
{
78+
am_event_state_ctor(&cfg);
79+
am_event_add_pool(&buf1, sizeof(buf1), sizeof(buf1), align);
80+
81+
test_allocate(sizeof(buf1), /*pool_index_plus_one=*/1);
82+
test_allocate(sizeof(buf1) - 1, /*pool_index_plus_one=*/1);
83+
}
84+
{
85+
am_event_state_ctor(&cfg);
86+
am_event_add_pool(&buf1, sizeof(buf1), sizeof(buf1), align);
87+
am_event_add_pool(&buf2, sizeof(buf2), sizeof(buf2), align);
88+
89+
test_allocate(sizeof(buf1), /*pool_index_plus_one=*/1);
90+
test_allocate(sizeof(buf1) + 1, /*pool_index_plus_one=*/2);
91+
test_allocate(sizeof(buf2), /*pool_index_plus_one=*/2);
92+
test_allocate(sizeof(buf2) - 1, /*pool_index_plus_one=*/2);
93+
}
94+
{
95+
am_event_state_ctor(&cfg);
96+
am_event_add_pool(&buf1, sizeof(buf1), sizeof(buf1), align);
97+
am_event_add_pool(&buf2, sizeof(buf2), sizeof(buf2), align);
98+
am_event_add_pool(&buf3, sizeof(buf3), sizeof(buf3), align);
99+
100+
test_allocate(sizeof(buf1), /*pool_index_plus_one=*/1);
101+
test_allocate(sizeof(buf2), /*pool_index_plus_one=*/2);
102+
test_allocate(sizeof(buf1) + 1, /*pool_index_plus_one=*/2);
103+
test_allocate(sizeof(buf2) - 1, /*pool_index_plus_one=*/2);
104+
test_allocate(sizeof(buf2) + 1, /*pool_index_plus_one=*/3);
105+
test_allocate(sizeof(buf3) - 1, /*pool_index_plus_one=*/3);
106+
test_allocate(sizeof(buf3), /*pool_index_plus_one=*/3);
107+
}
108+
{
109+
am_event_state_ctor(&cfg);
110+
am_event_add_pool(&buf1, sizeof(buf1), sizeof(buf1), align);
111+
am_event_add_pool(&buf2, sizeof(buf2), sizeof(buf2), align);
112+
am_event_add_pool(&buf3, sizeof(buf3), sizeof(buf3), align);
113+
am_event_add_pool(&buf4, sizeof(buf4), sizeof(buf4), align);
114+
115+
test_allocate(sizeof(buf1), /*pool_index_plus_one=*/1);
116+
test_allocate(sizeof(buf1) + 1, /*pool_index_plus_one=*/2);
117+
test_allocate(sizeof(buf2), /*pool_index_plus_one=*/2);
118+
test_allocate(sizeof(buf2) - 1, /*pool_index_plus_one=*/2);
119+
test_allocate(sizeof(buf2) + 1, /*pool_index_plus_one=*/3);
120+
test_allocate(sizeof(buf3) - 1, /*pool_index_plus_one=*/3);
121+
test_allocate(sizeof(buf3), /*pool_index_plus_one=*/3);
122+
test_allocate(sizeof(buf3) + 1, /*pool_index_plus_one=*/4);
123+
test_allocate(sizeof(buf4) - 1, /*pool_index_plus_one=*/4);
124+
test_allocate(sizeof(buf4), /*pool_index_plus_one=*/4);
125+
}
126+
{
127+
am_event_state_ctor(&cfg);
128+
am_event_add_pool(&buf1, sizeof(buf1), sizeof(buf1), align);
129+
am_event_add_pool(&buf2, sizeof(buf2), sizeof(buf2), align);
130+
am_event_add_pool(&buf3, sizeof(buf3), sizeof(buf3), align);
131+
am_event_add_pool(&buf4, sizeof(buf4), sizeof(buf4), align);
132+
am_event_add_pool(&buf5, sizeof(buf5), sizeof(buf5), align);
133+
134+
test_allocate(sizeof(buf1), /*pool_index_plus_one=*/1);
135+
test_allocate(sizeof(buf1) + 1, /*pool_index_plus_one=*/2);
136+
test_allocate(sizeof(buf2), /*pool_index_plus_one=*/2);
137+
test_allocate(sizeof(buf2) - 1, /*pool_index_plus_one=*/2);
138+
test_allocate(sizeof(buf2) + 1, /*pool_index_plus_one=*/3);
139+
test_allocate(sizeof(buf3) - 1, /*pool_index_plus_one=*/3);
140+
test_allocate(sizeof(buf3), /*pool_index_plus_one=*/3);
141+
test_allocate(sizeof(buf3) + 1, /*pool_index_plus_one=*/4);
142+
test_allocate(sizeof(buf4) - 1, /*pool_index_plus_one=*/4);
143+
test_allocate(sizeof(buf4), /*pool_index_plus_one=*/4);
144+
test_allocate(sizeof(buf4) + 1, /*pool_index_plus_one=*/5);
145+
test_allocate(sizeof(buf5) - 1, /*pool_index_plus_one=*/5);
146+
test_allocate(sizeof(buf5), /*pool_index_plus_one=*/5);
147+
}
148+
149+
return 0;
150+
}

0 commit comments

Comments
 (0)