Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
ns_tests.h
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
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 "base.h"
22#include "serene/rt/core.h"
23#include "serene/rt/fiber.h"
25#include "serene/rt/protocols.h"
26#include "serene/rt/strings.h"
27#include "serene/rt/symbols.h"
28#include "serene/runtime.h"
29
30#define NS_TESTS(X) \
31 X("namespace::make", test_ns_make), X("config::wiring", test_config_wiring), \
32 X("config::runtime_macros", test_runtime_macros)
33
34static void test_ns_make() {
35 MAKE_ENGINE(mm, engine);
36 MAKE_CONTEXT(engine, ctx);
37
38 srn_value_t *name = srn_string_make(ctx, &absurd_metadata, "serene.tests");
40
42 // ns should has it's own context
43 TEST_CHECK(AS_NS(ns)->root_context != ctx);
44 TEST_CHECK(AS_NS(ns)->sym_table.len == 0);
45
46 TEST_CHECK(srn_string_eq(AS_NS(ns)->name, AS_STRING(name)));
47
48 RELEASE_CONTEXT(ctx);
49 SHUTDOWN_ENGINE(mm, engine);
50}
51
53 UNUSED(ctx);
54 UNUSED(arg);
55 return nullptr;
56}
57
58// Every configuration knob is read from the configuration, the MM block size
59// at srn_mm_init, the per fiber stack default, and the string and namespace
60// length limits.
61static void test_config_wiring() {
63 cfg.mm.block_size_magnitude = 18; // 256 KiB
64 cfg.fiber.stack_size = 256U * 1024U;
65 cfg.limits.string_max_len = 8;
66 cfg.limits.ns_name_max_len = 4;
67
68 srn_mm_t *mm = srn_mm_init(&cfg);
69 TEST_ASSERT(mm != nullptr);
70 TEST_CHECK(mm->block_size == ((size_t)1 << 18));
71
72 srn_engine_t *engine = srn_engine_make(mm, &cfg);
73 TEST_ASSERT(engine != nullptr);
74 MAKE_CONTEXT(engine, ctx);
75
76 // A keyword name reaching the configured string limit fails as an error
77 // value; a short one interns.
78 srn_value_t *too_long = srn_engine_intern_keyword(ctx, &absurd_metadata, "waytoolongforthis");
79 TEST_CHECK(TYPE(too_long) == VError);
80 srn_value_t *ok = srn_engine_intern_keyword(ctx, &absurd_metadata, "ok");
82
83 // A namespace name over the configured limit errors.
84 srn_value_t *long_name = srn_string_make(ctx, &absurd_metadata, "toolong");
85 TEST_ASSERT(TYPE(long_name) == VString);
87 TEST_CHECK(TYPE(ns) == VError);
88
89 // A zero per fiber stack size falls back to the configured default.
91 TEST_ASSERT(f != nullptr);
92 TEST_CHECK(srn_fiber_stack_size(f->stack) >= 256U * 1024U);
93
94 // Reap the never-run fiber while its struct (in the context block) is
95 // still alive, before the context that owns it is released.
97
98 RELEASE_CONTEXT(ctx);
99 SHUTDOWN_ENGINE(mm, engine);
100}
101
102// The bootstrap macros bring the whole runtime up and down, mm plus engine
103// from one config, the scheduler bound, and a teardown that reclaims every
104// context chain through the manager.
105static void test_runtime_macros() {
106 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
107 MAKE_CONTEXT(engine, ctx);
108
109 (void)srn_fiber_spawn(ctx, config_wiring_fiber_entry, nullptr);
110 srn_sched_run(sched, 1);
111
112 RELEASE_CONTEXT(ctx);
114}
#define TEST_CHECK(cond)
Definition acutest.h:95
#define TEST_ASSERT(cond)
Definition acutest.h:117
#define RELEASE_CONTEXT(x)
Definition base.h:46
#define SHUTDOWN_ENGINE(mm, engine)
Definition base.h:38
#define MAKE_ENGINE(mm, engine)
Definition base.h:32
#define MAKE_CONTEXT(engine, x)
Definition base.h:42
static int ok
Definition hello.c:28
#define SRN_CONFIG_DEFAULTS
srn_metadata_t absurd_metadata
We use this for testing. Defined once in core.c.
Definition core.c:26
#define TYPE(value_ref)
Definition core.h:165
@ VNamespace
Definition core.h:122
@ VError
VError should be last.
Definition core.h:128
@ VKeyword
Definition core.h:125
@ VString
Definition core.h:123
#define AS_STRING(value_ref)
Definition core.h:176
#define AS_NS(value_ref)
Definition core.h:175
#define IS_A(value_ref, field)
Definition core.h:167
srn_mm_t * srn_mm_init(const srn_configuration_t *config)
Initialize the memory manager, this function will panic on error.
Definition default.c:322
srn_engine_t * srn_engine_make(srn_mm_t *mm, const srn_configuration_t *config)
Create the engine over mm, copying config (the runtime's knobs) into it.
Definition engine.c:101
srn_fiber_t * srn_fiber_spawn(srn_context_t *ctx, srn_fiber_entry_t entry, void *arg)
Make and schedule a fiber with every default, the engine's scheduler, the configured stack size,...
Definition fiber.c:247
AI Generated (🤦) Fiber subsystem overview.
static size_t srn_fiber_stack_size(srn_fiber_stack_t s)
Definition fiber.h:611
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
Definition fiber.h:157
srn_value_t * srn_namespace_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_string_t *name)
Creates a new namespace in the give context.
Definition namespaces.c:28
static void test_config_wiring()
Definition ns_tests.h:61
static void test_ns_make()
Definition ns_tests.h:34
static srn_fiber_result_t config_wiring_fiber_entry(srn_context_t *ctx, void *arg)
Definition ns_tests.h:52
static void test_runtime_macros()
Definition ns_tests.h:105
#define SERENE_RUNTIME_SHUTDOWN(engine)
Tear down what SERENE_RUNTIME_INIT brought up, in reverse order, the engine (reactor,...
Definition runtime.h:58
#define SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, config)
SERENE_RUNTIME_INIT plus a sched variable bound to the engine's scheduler, which every run and fiber ...
Definition runtime.h:46
void srn_sched_shutdown(srn_scheduler_t *sched)
The one stop tear down of the fiber subsystem, should be called once srn_sched_run has returned.
Definition scheduler.c:327
void srn_sched_run(srn_scheduler_t *sched, size_t nworkers)
Run the scheduler with nworkers os threads draining it, returning once the pool goes quiescent (every...
Definition scheduler.c:843
srn_value_t * srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src)
Create a string from a null terminated C string.
Definition strings.c:51
bool srn_string_eq(const srn_string_t *a, const srn_string_t *b)
Definition strings.c:79
Every runtime knob, in one place.
srn_fiber_config_t fiber
srn_limits_config_t limits
srn_mm_config_t mm
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:51
srn_scheduler_t * scheduler
The fiber scheduler, that is the entry point of the fiber subsystem.
Definition engine.h:75
size_t stack_size
Size of every fiber stack, in bytes.
srn_fiber_stack_t stack
Definition fiber.h:267
size_t ns_name_max_len
Longest namespace name accepted, in bytes.
size_t string_max_len
Largest string accepted, in bytes.
size_t block_size_magnitude
Magnitude of one block the arena hands out from.
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:107
size_t block_size
Definition interface.h:122
#define UNUSED(x)
Definition utils.h:45