Skip to content

Commit ddbe199

Browse files
committed
test: Add ensure() macro
Signed-off-by: Diogo Behrens <[email protected]>
1 parent f89a840 commit ddbe199

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
add_subdirectory(checkers)
5-
5+
include_directories(include)
66
add_compile_options(-DVATOMIC_BUILTINS)
77
set(TSAN_mp on)
88
set(TSAN_simple-tsan on)

test/include/dice/ensure.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#ifndef DICE_ENSURE_H
6+
#define DICE_ENSURE_H
7+
8+
#ifdef NDEBUG
9+
#include <dice/log.h>
10+
#define ensure(COND) \
11+
{ \
12+
if (!(COND)) { \
13+
log_fatal("error: %s", #COND); \
14+
} \
15+
}
16+
#else
17+
#include <assert.h>
18+
#define ensure assert
19+
#endif
20+
#endif /* DICE_ENSURE_H */

test/log/log_levels.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,48 @@
33
* SPDX-License-Identifier: MIT
44
*/
55

6+
#include <stdbool.h>
7+
#include <string.h>
8+
9+
#include <dice/ensure.h>
610
#include <dice/interpose.h>
711
#include <dice/log.h>
8-
#include <string.h>
9-
#include <assert.h>
10-
#include <stdbool.h>
1112

1213
#define MAX_EXP 16
1314
static char *strings[MAX_EXP] = {0};
14-
static char **head = &strings[0];
15-
static char **tail = &strings[0];
15+
static char **head = &strings[0];
16+
static char **tail = &strings[0];
1617

17-
INTERPOSE(ssize_t, write, int fd, const void *buf, size_t count) {
18+
INTERPOSE(ssize_t, write, int fd, const void *buf, size_t count)
19+
{
1820
static int nest = 0;
1921
if (nest == 1) {
2022
return REAL(write, fd, buf, count);
2123
}
22-
assert(nest == 0);
24+
ensure(nest == 0);
2325
nest++;
24-
assert(*tail);
25-
assert(tail < head);
26-
if (strncmp((char*) buf, *tail, count) != 0) {
26+
ensure(*tail);
27+
ensure(tail < head);
28+
if (strncmp((char *)buf, *tail, count) != 0) {
2729
log_printf("exp: %s\n", *tail);
28-
log_printf("buf: %s\n", (char*)buf);
30+
log_printf("buf: %s\n", (char *)buf);
2931
abort();
3032
}
3133
tail++;
3234
nest--;
3335
return count;
3436
}
3537

36-
static void expect(char *e) {
37-
assert(head < strings + MAX_EXP);
38+
static void
39+
expect(char *e)
40+
{
41+
ensure(head < strings + MAX_EXP);
3842
*head = e;
3943
head++;
4044
}
41-
static bool empty(void) {
45+
static bool
46+
empty(void)
47+
{
4248
return head == tail;
4349
}
4450

@@ -49,7 +55,7 @@ main()
4955
// this should always work
5056
expect("print");
5157
log_printf("print");
52-
assert(empty());
58+
ensure(empty());
5359

5460
// this should always work, but we remove the abort to about actually
5561
// aborting
@@ -60,15 +66,15 @@ main()
6066
#define abort()
6167
log_fatal("fatal");
6268
#undef abort
63-
assert(empty());
69+
ensure(empty());
6470

6571
#if LOG_LEVEL_ >= LOG_LEVEL_INFO
6672
printf("level >= info\n");
6773
expect("dice: ");
6874
expect("info");
6975
expect("\n");
7076
log_info("info");
71-
assert(empty());
77+
ensure(empty());
7278
#endif
7379

7480
#if LOG_LEVEL_ >= LOG_LEVEL_DEBUG
@@ -77,9 +83,8 @@ main()
7783
expect("debug");
7884
expect("\n");
7985
log_debug("debug");
80-
assert(empty());
86+
ensure(empty());
8187
#endif
8288

83-
8489
return 0;
8590
}

0 commit comments

Comments
 (0)