Compare commits

...

6 Commits

Author SHA1 Message Date
Aryadev Chavali
91264d96e4 test: Added definition to make default testing less verbose
TEST_VERBOSE is a preprocesser directive which TEST is dependent on.
By default it is 0, in which case TEST simply fails if the condition
is not true.  Otherwise, a full log (as done previously) is made.
2026-02-05 05:16:11 +00:00
Aryadev Chavali
d88d7f7f23 tests: c23 allows you to inline stack allocated arrays in struct decls 2026-02-05 05:10:19 +00:00
Aryadev Chavali
16be3392b0 tests: slight cleanliness 2026-02-05 05:10:12 +00:00
Aryadev Chavali
2705ef9bb7 tests: Meaningful and pretty logging for tests 2026-02-05 04:56:11 +00:00
Aryadev Chavali
45ef1fa01a tests: Added string view suite
sv_copy is the only function, but we may have others later.
2026-02-05 04:48:42 +00:00
Aryadev Chavali
9095b118fd tests: split of symtable testing into its own suite
makes sense to be there, not in the lisp API
2026-02-05 04:48:42 +00:00
6 changed files with 165 additions and 69 deletions

View File

@@ -12,11 +12,15 @@
#include "./test.h"
#include "./test_lisp_api.c"
#include "./test_sv.c"
#include "./test_symtable.c"
#include "./test_vec.c"
test_suite_t SUITES[] = {
LISP_API_SUITE,
VEC_SUITE,
SV_SUITE,
SYMTABLE_SUITE,
LISP_API_SUITE,
};
int main(void)
@@ -27,7 +31,7 @@ int main(void)
printf("Suite [%s]\n", suite.name);
for (u64 j = 0; j < suite.size; ++j)
{
printf("[%s]: Running...\n", suite.tests[j].name);
printf("\t[%s]: Running...\n", suite.tests[j].name);
suite.tests[j].fn();
}
}

View File

@@ -10,27 +10,41 @@
#include <alisp/alisp.h>
#define TEST_PASSED() printf("[%s]: Passed\n", __func__)
#define TEST(COND, ...) \
do \
{ \
bool cond = (COND); \
if (!cond) \
{ \
printf("\tFAIL: "); \
} \
else \
{ \
printf("\tPASS: "); \
} \
printf("%s => ", #COND); \
printf(__VA_ARGS__); \
printf("\n"); \
if (!cond) \
{ \
assert(0); \
} \
#ifndef TEST_VERBOSE
#define TEST_VERBOSE 0
#endif
#define TEST_PASSED() printf("\t[%s]: Passed\n", __func__)
#if TEST_VERBOSE
#define TEST(COND, ...) \
do \
{ \
bool cond = (COND); \
if (!cond) \
{ \
printf("\t\tFAIL: "); \
} \
else \
{ \
printf("\t\tPASS: "); \
} \
printf(__VA_ARGS__); \
printf("\n\t\t [%s]\n", #COND); \
if (!cond) \
{ \
assert(0); \
} \
} while (0)
#else
#define TEST(COND, ...) \
do \
{ \
if (!(COND)) \
{ \
assert(0); \
} \
} while (0)
#endif
typedef struct TestFn
{

View File

@@ -28,28 +28,11 @@ void int_test(void)
TEST_PASSED();
}
void symtable_test(void)
{
sym_table_t table = {0};
sym_table_init(&table);
for (u64 i = 0; i < ARRSIZE(words); ++i)
sym_table_find(&table, SV((char *)words[i], strlen(words[i])));
TEST(table.count == ARRSIZE(unique_words), "%lu == %lu", table.count,
ARRSIZE(unique_words));
TEST(table.count < ARRSIZE(unique_words), "%lu < %lu", table.count,
ARRSIZE(unique_words));
TEST_PASSED();
sym_table_cleanup(&table);
}
void sym_test(void)
{
sys_t system = {0};
sys_init(&system);
for (u64 i = 0; i < ARRSIZE(words); ++i)
{
const char *in = words[i];
@@ -57,10 +40,11 @@ void sym_test(void)
char *out = as_sym(lisp);
TEST(in != out, "%p != %p", in, out);
TEST(strlen(in) == strlen(out), "%zu == %zu", strlen(in), strlen(out));
TEST(strncmp(in, out, strlen(in)) == 0, "%d", strncmp(in, out, strlen(in)));
TEST(strncmp(in, out, strlen(in)) == 0, "`%s` == `%s`", in, out);
}
TEST_PASSED();
sys_free(&system);
TEST_PASSED();
}
void cons_test(void)
@@ -89,21 +73,19 @@ void cons_test(void)
TEST(strncmp(expected, got, size) == 0, "%s == %s", expected, got);
}
TEST_PASSED();
sys_free(&system);
TEST_PASSED();
}
const test_fn TESTS_LISP_API[] = {
MAKE_TEST_FN(int_test),
MAKE_TEST_FN(sym_test),
MAKE_TEST_FN(cons_test),
};
const test_suite_t LISP_API_SUITE = {
.name = "Lisp API Tests",
.tests = TESTS_LISP_API,
.size = ARRSIZE(TESTS_LISP_API),
.name = "Lisp API Tests",
.tests =
(test_fn[]){
MAKE_TEST_FN(int_test),
MAKE_TEST_FN(sym_test),
MAKE_TEST_FN(cons_test),
},
.size = 3,
};
/* Copyright (C) 2026 Aryadev Chavali

51
test/test_sv.c Normal file
View File

@@ -0,0 +1,51 @@
/* test_sv.c: String View tests
* Created: 2026-02-05
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
*/
#include <assert.h>
#include <malloc.h>
#include "./data.h"
#include "./test.h"
void sv_copy_test(void)
{
static_assert(ARRSIZE(unique_words) > 3, "Expected at least 3 unique words");
for (u64 i = 0; i < 3; ++i)
{
sv_t word = SV((char *)unique_words[i], strlen(unique_words[i]));
sv_t copy = sv_copy(word);
TEST(word.data != copy.data, "%p != %p", word.data, copy.data);
TEST(word.size == copy.size, "%lu == %lu", word.size, copy.size);
TEST(strncmp(word.data, copy.data, copy.size) == 0, "`%s` == `%s`",
word.data, copy.data);
// Obviously we can't just have this lying around.
free(copy.data);
}
}
const test_suite_t SV_SUITE = {
.name = "String View Tests",
.tests =
(test_fn[]){
MAKE_TEST_FN(sv_copy_test),
},
.size = 1,
};
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
*/

45
test/test_symtable.c Normal file
View File

@@ -0,0 +1,45 @@
/* test_symtable.c: Symbol table tests
* Created: 2026-02-05
* Author: Aryadev Chavali
* License: See end of file
* Commentary:
*/
#include "./data.h"
#include "./test.h"
void symtable_test(void)
{
sym_table_t table = {0};
sym_table_init(&table);
for (u64 i = 0; i < ARRSIZE(words); ++i)
sym_table_find(&table, SV((char *)words[i], strlen(words[i])));
TEST(table.count == ARRSIZE(unique_words), "%lu == %lu", table.count,
ARRSIZE(unique_words));
sym_table_cleanup(&table);
TEST_PASSED();
}
const test_suite_t SYMTABLE_SUITE = {
.name = "Symbol Table Tests",
.tests =
(test_fn[]){
MAKE_TEST_FN(symtable_test),
},
.size = 1,
};
/* Copyright (C) 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
*/

View File

@@ -8,7 +8,7 @@
#include "./data.h"
#include "./test.h"
void vec_test1(void)
void vec_test_concat(void)
{
sys_t system = {0};
sys_init(&system);
@@ -28,14 +28,15 @@ void vec_test1(void)
TEST(vec->size == ARRSIZE(words_text), "%lu == %lu", vec->size,
ARRSIZE(words_text));
TEST(strncmp((char *)vec_data(vec), words_text, vec->size) == 0, "%d",
strncmp((char *)vec_data(vec), words_text, vec->size));
TEST(strncmp((char *)vec_data(vec), words_text, vec->size) == 0,
"%p@%lu == %p@%lu", (char *)vec_data(vec), vec->size, words_text,
strlen(words_text));
TEST_PASSED();
sys_free(&system);
TEST_PASSED();
}
void vec_test2(void)
void vec_test_substr(void)
{
sys_t system = {0};
sys_init(&system);
@@ -60,23 +61,22 @@ void vec_test2(void)
vec_append(as_vec(lvec), text + test.start, test.size);
TEST(as_vec(lvec)->size > size, "%lu > %lu", as_vec(lvec)->size, size);
TEST(strncmp((char *)vec_data(as_vec(lvec)), substr.data, substr.size) == 0,
"%d",
strncmp((char *)vec_data(as_vec(lvec)), substr.data, substr.size));
"%p@%lu == %p@%lu", (char *)vec_data(as_vec(lvec)), as_vec(lvec)->size,
substr.data, substr.size);
}
TEST_PASSED();
sys_free(&system);
TEST_PASSED();
}
const test_fn TESTS_VEC[] = {
MAKE_TEST_FN(vec_test1),
MAKE_TEST_FN(vec_test2),
};
const test_suite_t VEC_SUITE = {
.name = "Vector Tests",
.tests = TESTS_VEC,
.size = ARRSIZE(TESTS_VEC),
.name = "Vector Tests",
.tests =
(test_fn[]){
MAKE_TEST_FN(vec_test_concat),
MAKE_TEST_FN(vec_test_substr),
},
.size = 2,
};
/* Copyright (C) 2026 Aryadev Chavali