Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
seq_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 <stdio.h>
22
23#include <serene/rt/context.h>
24#include <serene/rt/impl/seq.h>
25
26#include "base.h"
27
28#define SEQ_TESTS(X) \
29 X("seq::empty", test_seq_empty), \
30 X("seq::push_less_than_32", test_seq_push_less_than_32), \
31 X("seq::push_more_than_32", test_seq_push_more_than_32)
32
33#if defined(SEQ_DEBUG)
34# define SEQ_TEST_LOG(...) DBG("SEQ_TEST", __VA_ARGS__)
35#else
36# define SEQ_TEST_LOG(...)
37#endif
38
39typedef struct seq_dummy {
40 int foo;
41 int bar;
43
44static void test_seq_empty() {
45 MAKE_ENGINE(mm, engine);
46 MAKE_CONTEXT(engine, ctx);
47 auto s = seq_empty(ctx);
48
49 TEST_CHECK(s.len == 0);
50 TEST_CHECK(s.tail_len == 0);
51 TEST_CHECK(s.root == nullptr);
52 TEST_CHECK(s.maybe_error == nullptr);
53
54 RELEASE_CONTEXT(ctx);
55 SHUTDOWN_ENGINE(mm, engine);
56}
57
59 MAKE_ENGINE(mm, engine);
60 MAKE_CONTEXT(engine, ctx);
61
62 seq_dummy d = {.foo = 10, .bar = 20}; // NOLINT
63 auto s = seq_empty(ctx);
64 auto s1 = seq_push(ctx, &s, (seq_elem_t)&d);
65 TEST_CHECK(s.len == 0);
66 TEST_CHECK(s.tail_len == 0);
67 TEST_CHECK(s.root == nullptr);
68 TEST_CHECK(s.maybe_error == nullptr);
69
70 TEST_CHECK(s1.len == 1);
71 TEST_CHECK(s1.tail_len == 1);
72 TEST_CHECK(s1.root == nullptr);
73 TEST_CHECK(s1.maybe_error == nullptr);
74 seq_dummy *d1 = (seq_dummy *)s1.tail[0];
75 TEST_CHECK(d1->foo == 10);
76 TEST_CHECK(d1->bar == 20);
77
78 RELEASE_CONTEXT(ctx);
79 SHUTDOWN_ENGINE(mm, engine);
80}
81
83 MAKE_ENGINE(mm, engine);
84 MAKE_CONTEXT(engine, ctx);
85
86 seq_t s = seq_empty(ctx);
87
88 for (int i = 0; i < 2000; i++) {
89
90 seq_dummy *d = ALLOC(ctx, seq_dummy);
91 d->foo = i;
92 d->bar = 3000 - i;
93 s = seq_push(ctx, &s, (seq_elem_t)d);
94 }
95
96 TEST_CHECK(s.len == 2000);
97 TEST_CHECK(s.tail_len == 16);
98 TEST_CHECK(s.root != nullptr);
99 TEST_CHECK(s.maybe_error == nullptr);
100
101 seq_dummy *d1 = (seq_dummy *)s.tail[0];
102
103 ASSERT_NOT_NULL(d1);
104 TEST_CHECK(d1->foo == 1984);
105 TEST_CHECK(d1->bar == 3000 - d1->foo);
106
107 seq_dummy *d2 = (seq_dummy *)s.tail[s.tail_len - 1];
108 ASSERT_NOT_NULL(d2);
109 TEST_CHECK(d2->foo == 1999);
110 TEST_CHECK(d2->bar == 3000 - d2->foo);
111
112 for (int i = 0; i < 2000; i++) {
113 seq_lookup_result_t res = seq_get(ctx, &s, (size_t)i);
114 TEST_CHECK(res.maybe_error == nullptr);
116 seq_dummy *d = (seq_dummy *)res.data;
117
118 SEQ_TEST_LOG("%d %d %d", i, d->foo, d->bar);
119 TEST_CHECK(d->foo == i);
120 TEST_CHECK(d->bar == 3000 - i);
121 };
122
123 RELEASE_CONTEXT(ctx);
124 SHUTDOWN_ENGINE(mm, engine);
125}
#define TEST_CHECK(cond)
Definition acutest.h:96
#define RELEASE_CONTEXT(x)
Definition base.h:46
#define ASSERT_NOT_NULL(x)
Definition base.h:30
#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
#define ALLOC(ctx, T)
Definition context.h:82
seq_t seq_push(const srn_context_t *ctx, const seq_t *seq, seq_elem_t x)
Definition seq.c:87
seq_t seq_empty(const srn_context_t *ctx)
Definition seq.c:76
seq_lookup_result_t seq_get(const srn_context_t *ctx, const seq_t *seq, size_t n)
Negative index is not supported.
Definition seq.c:173
This is an implementation of bit - partitioned, persistent, immutable sequence For more information,...
void * seq_elem_t
We use generic pointers to refer to internal nodes, leaf nodes and even elements.
Definition seq.h:131
void test_seq_push_more_than_32()
Definition seq_tests.h:82
void test_seq_push_less_than_32()
Definition seq_tests.h:58
static void test_seq_empty()
Definition seq_tests.h:44
#define SEQ_TEST_LOG(...)
Definition seq_tests.h:36
seq_elem_t data
Definition seq.h:135
Definition seq.h:155
size_t len
logical length.
Definition seq.h:161
seq_node_t * root
NULL means “all data is in tail”
Definition seq.h:167
seq_elem_t * tail
small tail array for fast push/pop.
Definition seq.h:170
uint16_t tail_len
0..SEQ_BR
Definition seq.h:163