Skip to content

Commit 6a27e9a

Browse files
committed
Apply X-macro trick to reduce duplicated code
X macros are a technique for reliable maintenance of parallel lists of code and/or data, whose corresponding items must be declared or executed in the same order. The operations in DUT are defined and implemented by means of X-macro technique, meaning that the duplicated code can then be eliminated. Reference: https://en.wikipedia.org/wiki/X_Macro
1 parent d42971d commit 6a27e9a

File tree

5 files changed

+55
-58
lines changed

5 files changed

+55
-58
lines changed

dudect/constant.c

+30-13
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,33 @@
1212
*/
1313
static struct list_head *l = NULL;
1414

15+
#define dut_new() ((void) (l = q_new()))
16+
17+
#define dut_size(n) \
18+
do { \
19+
for (int __iter = 0; __iter < n; ++__iter) \
20+
q_size(l); \
21+
} while (0)
22+
23+
#define dut_insert_head(s, n) \
24+
do { \
25+
int j = n; \
26+
while (j--) \
27+
q_insert_head(l, s); \
28+
} while (0)
29+
30+
#define dut_insert_tail(s, n) \
31+
do { \
32+
int j = n; \
33+
while (j--) \
34+
q_insert_tail(l, s); \
35+
} while (0)
36+
37+
#define dut_free() ((void) (q_free(l)))
38+
1539
static char random_string[N_MEASURES][8];
1640
static int random_string_iter = 0;
1741

18-
enum {
19-
test_insert_head,
20-
test_insert_tail,
21-
test_remove_head,
22-
test_remove_tail,
23-
};
24-
2542
/* Implement the necessary queue interface to simulation */
2643
void init_dut(void)
2744
{
@@ -55,11 +72,11 @@ void measure(int64_t *before_ticks,
5572
uint8_t *input_data,
5673
int mode)
5774
{
58-
assert(mode == test_insert_head || mode == test_insert_tail ||
59-
mode == test_remove_head || mode == test_remove_tail);
75+
assert(mode == DUT(insert_head) || mode == DUT(insert_tail) ||
76+
mode == DUT(remove_head) || mode == DUT(remove_tail));
6077

6178
switch (mode) {
62-
case test_insert_head:
79+
case DUT(insert_head):
6380
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
6481
char *s = get_random_string();
6582
dut_new();
@@ -72,7 +89,7 @@ void measure(int64_t *before_ticks,
7289
dut_free();
7390
}
7491
break;
75-
case test_insert_tail:
92+
case DUT(insert_tail):
7693
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
7794
char *s = get_random_string();
7895
dut_new();
@@ -85,7 +102,7 @@ void measure(int64_t *before_ticks,
85102
dut_free();
86103
}
87104
break;
88-
case test_remove_head:
105+
case DUT(remove_head):
89106
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
90107
dut_new();
91108
dut_insert_head(
@@ -99,7 +116,7 @@ void measure(int64_t *before_ticks,
99116
dut_free();
100117
}
101118
break;
102-
case test_remove_tail:
119+
case DUT(remove_tail):
103120
for (size_t i = DROP_SIZE; i < N_MEASURES - DROP_SIZE; i++) {
104121
dut_new();
105122
dut_insert_head(

dudect/constant.h

+13-23
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,19 @@
1111

1212
#define DROP_SIZE 20
1313

14-
#define dut_new() ((void) (l = q_new()))
15-
16-
#define dut_size(n) \
17-
do { \
18-
for (int __iter = 0; __iter < n; ++__iter) \
19-
q_size(l); \
20-
} while (0)
21-
22-
#define dut_insert_head(s, n) \
23-
do { \
24-
int j = n; \
25-
while (j--) \
26-
q_insert_head(l, s); \
27-
} while (0)
28-
29-
#define dut_insert_tail(s, n) \
30-
do { \
31-
int j = n; \
32-
while (j--) \
33-
q_insert_tail(l, s); \
34-
} while (0)
35-
36-
#define dut_free() ((void) (q_free(l)))
14+
#define DUT_FUNCS \
15+
_(insert_head) \
16+
_(insert_tail) \
17+
_(remove_head) \
18+
_(remove_tail)
19+
20+
#define DUT(x) DUT_##x
21+
22+
enum {
23+
#define _(x) DUT(x),
24+
DUT_FUNCS
25+
#undef _
26+
};
3727

3828
void init_dut();
3929
void prepare_inputs(uint8_t *input_data, uint8_t *classes);

dudect/fixture.c

+8-18
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,12 @@ static bool test_const(char *text, int mode)
170170
return result;
171171
}
172172

173-
bool is_insert_head_const(void)
174-
{
175-
return test_const("insert_head", 0);
176-
}
177-
178-
bool is_insert_tail_const(void)
179-
{
180-
return test_const("insert_tail", 1);
181-
}
182-
183-
bool is_remove_head_const(void)
184-
{
185-
return test_const("remove_head", 2);
186-
}
173+
#define DUT_FUNC_IMPL(op) \
174+
bool is_##op##_const(void) \
175+
{ \
176+
return test_const(#op, DUT(op)); \
177+
}
187178

188-
bool is_remove_tail_const(void)
189-
{
190-
return test_const("remove_tail", 3);
191-
}
179+
#define _(x) DUT_FUNC_IMPL(x)
180+
DUT_FUNCS
181+
#undef _

dudect/fixture.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
#include "constant.h"
66

77
/* Interface to test if function is constant */
8-
bool is_insert_head_const(void);
9-
bool is_insert_tail_const(void);
10-
bool is_remove_head_const(void);
11-
bool is_remove_tail_const(void);
8+
#define _(x) bool is_##x##_const(void);
9+
DUT_FUNCS
10+
#undef _
1211

1312
#endif

scripts/aspell-pws

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ bash
77
etc
88
var
99
dudect
10+
dut
1011
runtime
1112
todo
1213
fixme

0 commit comments

Comments
 (0)