Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <lxsameer@lxsameer.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <serene/rt/context.h>
22#include <serene/rt/engine.h>
24#include <serene/utils.h>
25
26#include "acutest.h"
27
28#define ENTRY(name, test) {name, test}
29
30#define ASSERT_NOT_NULL(x) TEST_ASSERT(x != nullptr)
31#define ASSERT_NULL(x) TEST_ASSERT(x == nullptr)
32#define TEST_NO_ERROR(x) TEST_CHECK((x).maybe_error == nullptr)
33
34#define MAKE_ENGINE(mm, engine) \
35 srn_mm_t *mm = srn_mm_init(nullptr); \
36 TEST_ASSERT(mm != nullptr); \
37 srn_engine_t *engine = srn_engine_make(mm, nullptr); \
38 TEST_ASSERT(engine != nullptr)
39
40#define SHUTDOWN_ENGINE(mm, engine) \
41 srn_engine_shutdown(engine); \
42 srn_mm_shutdown(mm)
43
44#define MAKE_CONTEXT(engine, x) \
45 srn_context_t *x = srn_context_make(engine); \
46 TEST_ASSERT(x != nullptr)
47
48#define RELEASE_CONTEXT(x) srn_context_release(x);
49
50#define TEST_IN_FIBER_WITH(name, arg_expr, body) \
51 static srn_fiber_result_t test_##name##_fiber(srn_context_t *ctx, void *arg) { \
52 { \
53 body \
54 } \
55 return nullptr; \
56 } \
57 static void test_##name() { \
58 MAKE_ENGINE(mm, engine); \
59 MAKE_CONTEXT(engine, ctx); \
60 srn_scheduler_t *sched = engine->scheduler; \
61 srn_fiber_spawn(ctx, test_##name##_fiber, (arg_expr)); \
62 srn_sched_run(sched, 1); \
63 RELEASE_CONTEXT(ctx); \
64 SHUTDOWN_ENGINE(mm, engine); \
65 }
66
67#define TEST_IN_FIBER(name, body) \
68 static srn_fiber_result_t test_##name##_fiber(srn_context_t *ctx, void *arg) { \
69 UNUSED(arg); \
70 { \
71 body \
72 } \
73 return nullptr; \
74 } \
75 static void test_##name() { \
76 MAKE_ENGINE(mm, engine); \
77 MAKE_CONTEXT(engine, ctx); \
78 srn_scheduler_t *sched = engine->scheduler; \
79 srn_fiber_spawn(ctx, test_##name##_fiber, nullptr); \
80 srn_sched_run(sched, 1); \
81 RELEASE_CONTEXT(ctx); \
82 SHUTDOWN_ENGINE(mm, engine); \
83 }