Split out tests into its own file
Also adjust the build system to do some more (cleaning, building, testing, running).
This commit is contained in:
20
build.sh
20
build.sh
@@ -3,20 +3,36 @@
|
|||||||
set -xe
|
set -xe
|
||||||
|
|
||||||
CFLAGS="-Wall -Wextra -std=c11 -ggdb -fsanitize=address -fsanitize=undefined"
|
CFLAGS="-Wall -Wextra -std=c11 -ggdb -fsanitize=address -fsanitize=undefined"
|
||||||
SRC="vec.c symtable.c tag.c constructor.c sys.c main.c"
|
LINK=""
|
||||||
|
LIB="sv.c vec.c symtable.c tag.c constructor.c sys.c"
|
||||||
OUT="alisp.out"
|
OUT="alisp.out"
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
cc $CFLAGS -o $OUT $SRC;
|
cc $LINK $CFLAGS -o $OUT $LIB main.c;
|
||||||
|
cc $LINK $CFLAGS -o test.out $LIB test.c;
|
||||||
|
}
|
||||||
|
|
||||||
|
clean() {
|
||||||
|
rm -v $OUT test.out;
|
||||||
}
|
}
|
||||||
|
|
||||||
run() {
|
run() {
|
||||||
./$OUT;
|
./$OUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test() {
|
||||||
|
./test.out
|
||||||
|
}
|
||||||
|
|
||||||
build
|
build
|
||||||
|
|
||||||
if [ "$1" = "run" ]
|
if [ "$1" = "run" ]
|
||||||
then
|
then
|
||||||
run
|
run
|
||||||
|
elif [ "$1" = "test" ]
|
||||||
|
then
|
||||||
|
test
|
||||||
|
elif [ "$1" = "clean" ]
|
||||||
|
then
|
||||||
|
clean
|
||||||
fi
|
fi
|
||||||
|
|||||||
168
main.c
168
main.c
@@ -18,174 +18,8 @@
|
|||||||
|
|
||||||
#include "./alisp.h"
|
#include "./alisp.h"
|
||||||
|
|
||||||
sv_t sv_copy(sv_t old)
|
|
||||||
{
|
|
||||||
char *newstr = calloc(1, (old.size + 1) * sizeof(*newstr));
|
|
||||||
memcpy(newstr, old.data, old.size);
|
|
||||||
newstr[old.size] = '\0';
|
|
||||||
return SV(newstr, old.size);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *words[] = {
|
|
||||||
"aliquam", "erat", "volutpat", "nunc", "eleifend",
|
|
||||||
"leo", "vitae", "magna", "in", "id",
|
|
||||||
"erat", "non", "orci", "commodo", "lobortis",
|
|
||||||
"proin", "neque", "massa", "cursus", "ut",
|
|
||||||
"gravida", "ut", "lobortis", "eget", "lacus",
|
|
||||||
"sed", "diam", "praesent", "fermentum", "tempor",
|
|
||||||
"tellus", "nullam", "tempus", "mauris", "ac",
|
|
||||||
"felis", "vel", "velit", "tristique", "imperdiet",
|
|
||||||
"donec", "at", "pede", "etiam", "vel",
|
|
||||||
"neque", "nec", "dui", "dignissim", "bibendum",
|
|
||||||
"vivamus", "id", "enim", "phasellus", "neque",
|
|
||||||
"orci", "porta", "a", "aliquet", "quis",
|
|
||||||
"semper", "a", "massa", "phasellus", "purus",
|
|
||||||
"pellentesque", "tristique", "imperdiet", "tortor", "nam",
|
|
||||||
"euismod", "tellus", "id", "erat",
|
|
||||||
};
|
|
||||||
|
|
||||||
char text[] =
|
|
||||||
"Pellentesque dapibus suscipit ligula. Donec posuere augue in quam. "
|
|
||||||
"Etiam vel tortor sodales tellus ultricies commodo. Suspendisse potenti. "
|
|
||||||
"Aenean in sem ac leo mollis blandit. Donec neque quam, dignissim in, "
|
|
||||||
"mollis nec, sagittis eu, wisi. Phasellus lacus. Etiam laoreet quam sed "
|
|
||||||
"arcu. Phasellus at dui in ligula mollis ultricies. Integer placerat "
|
|
||||||
"tristique nisl. Praesent augue. Fusce commodo. Vestibulum convallis, "
|
|
||||||
"lorem a tempus semper, dui dui euismod elit, vitae placerat urna tortor "
|
|
||||||
"vitae lacus. Nullam libero mauris, consequat quis, varius et, dictum id, "
|
|
||||||
"arcu. Mauris mollis tincidunt felis. Aliquam feugiat tellus ut neque. "
|
|
||||||
"Nulla facilisis, risus a rhoncus fermentum, tellus tellus lacinia purus, "
|
|
||||||
"et dictum nunc justo sit amet elit.";
|
|
||||||
|
|
||||||
void symtable_test(void)
|
|
||||||
{
|
|
||||||
sym_table_t table = {0};
|
|
||||||
sym_table_init(&table);
|
|
||||||
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
|
||||||
{
|
|
||||||
char *ptr = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
|
||||||
printf("[symtable_test]: %s => %p\n", words[i], ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf(
|
|
||||||
"[symtable_test]: |words|=%lu, |table|= %lu => Unique word ratio: %lf\n",
|
|
||||||
ARRSIZE(words), table.count, table.count / (double)ARRSIZE(words));
|
|
||||||
|
|
||||||
sym_table_cleanup(&table);
|
|
||||||
}
|
|
||||||
|
|
||||||
void make_int_test(void)
|
|
||||||
{
|
|
||||||
i64 ints[] = {
|
|
||||||
1, -1, (1 << 10) - 1, (-1) * ((1 << 10) - 1),
|
|
||||||
INT_MIN, INT_MAX, INT64_MAX, INT64_MIN,
|
|
||||||
};
|
|
||||||
|
|
||||||
for (u64 i = 0; i < ARRSIZE(ints); ++i)
|
|
||||||
{
|
|
||||||
i64 in = ints[i];
|
|
||||||
lisp_t *lisp = make_int(in);
|
|
||||||
i64 out = as_int(lisp);
|
|
||||||
|
|
||||||
printf("[make_int_test]: %#16lx => %#16lx => %#16lx\n", in, (u64)lisp, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void intern_test(void)
|
|
||||||
{
|
|
||||||
sys_t system = {0};
|
|
||||||
sys_init(&system);
|
|
||||||
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
|
||||||
{
|
|
||||||
char *in = words[i];
|
|
||||||
lisp_t *lisp = intern(&system, SV(in, strlen(in)));
|
|
||||||
char *out = as_sym(lisp);
|
|
||||||
printf("[intern test]: `%s` -> %p -> `%s`\n", in, lisp, out);
|
|
||||||
}
|
|
||||||
sys_cleanup(&system);
|
|
||||||
}
|
|
||||||
|
|
||||||
void make_vec_test(void)
|
|
||||||
{
|
|
||||||
sys_t system = {0};
|
|
||||||
sys_init(&system);
|
|
||||||
|
|
||||||
// Generating a vector word by word
|
|
||||||
lisp_t *vec = make_vec(&system, 0);
|
|
||||||
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
|
||||||
{
|
|
||||||
char *word = words[i];
|
|
||||||
vec_append(as_vec(vec), word, strlen(word));
|
|
||||||
vec_append(as_vec(vec), " ", 1);
|
|
||||||
}
|
|
||||||
printf("[make_vec_test]: %lu/%lu, inlined?: %s, text=%.*s\n",
|
|
||||||
as_vec(vec)->size, as_vec(vec)->capacity,
|
|
||||||
as_vec(vec)->is_inlined ? "yes" : "no", (int)as_vec(vec)->size,
|
|
||||||
vec_data(as_vec(vec)));
|
|
||||||
|
|
||||||
// Generating substrings
|
|
||||||
struct Test
|
|
||||||
{
|
|
||||||
u64 start, size;
|
|
||||||
} tests[] = {
|
|
||||||
{0, 16},
|
|
||||||
{0, 32},
|
|
||||||
{32, 64},
|
|
||||||
{0, ARRSIZE(text)},
|
|
||||||
};
|
|
||||||
for (u64 i = 0; i < ARRSIZE(tests); ++i)
|
|
||||||
{
|
|
||||||
struct Test test = tests[i];
|
|
||||||
// Always initialise below what we expect
|
|
||||||
lisp_t *lvec = make_vec(&system, test.size / 2);
|
|
||||||
vec_t *vec = as_vec(lvec);
|
|
||||||
printf("[make_vec_test]: %p -> %p[%lu/%lu] -> ", lvec, vec, vec->size,
|
|
||||||
vec->capacity);
|
|
||||||
vec_append(vec, text + test.start, test.size);
|
|
||||||
printf("[%lu/%lu] -> ", vec->size, vec->capacity);
|
|
||||||
printf("`%.*s`\n", (int)vec->size, (char *)vec_data(vec));
|
|
||||||
}
|
|
||||||
|
|
||||||
sys_cleanup(&system);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cons_test(void)
|
|
||||||
{
|
|
||||||
sys_t system = {0};
|
|
||||||
sys_init(&system);
|
|
||||||
|
|
||||||
// Iterate through each "word" in words, make a large contiguous list which is
|
|
||||||
// its reverse
|
|
||||||
lisp_t *lisp = NIL;
|
|
||||||
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
|
||||||
{
|
|
||||||
char *word = words[i];
|
|
||||||
lisp_t *lword = intern(&system, SV(word, strlen(word)));
|
|
||||||
lisp = cons(&system, lword, lisp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then iterate through it
|
|
||||||
printf("[cons_test]: ");
|
|
||||||
for (lisp_t *iter = lisp; iter; iter = cdr(iter))
|
|
||||||
{
|
|
||||||
lisp_t *item = car(iter);
|
|
||||||
printf("%s ", as_sym(item));
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
sys_cleanup(&system);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
symtable_test();
|
puts("Watch this space\n");
|
||||||
printf("\n");
|
|
||||||
make_int_test();
|
|
||||||
printf("\n");
|
|
||||||
intern_test();
|
|
||||||
printf("\n");
|
|
||||||
make_vec_test();
|
|
||||||
printf("\n");
|
|
||||||
cons_test();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
27
sv.c
Normal file
27
sv.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* Copyright (C) 2025 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 Unlicense
|
||||||
|
* for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the
|
||||||
|
* Unlicense, which you should have received a copy of along with this
|
||||||
|
* program. If not, please go to <https://unlicense.org/>.
|
||||||
|
|
||||||
|
* Created: 2025-08-21
|
||||||
|
* Description: String views
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "./alisp.h"
|
||||||
|
|
||||||
|
sv_t sv_copy(sv_t old)
|
||||||
|
{
|
||||||
|
char *newstr = calloc(1, (old.size + 1) * sizeof(*newstr));
|
||||||
|
memcpy(newstr, old.data, old.size);
|
||||||
|
newstr[old.size] = '\0';
|
||||||
|
return SV(newstr, old.size);
|
||||||
|
}
|
||||||
184
test.c
Normal file
184
test.c
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
/* Copyright (C) 2025 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 Unlicense
|
||||||
|
* for details.
|
||||||
|
|
||||||
|
* You may distribute and modify this code under the terms of the
|
||||||
|
* Unlicense, which you should have received a copy of along with this
|
||||||
|
* program. If not, please go to <https://unlicense.org/>.
|
||||||
|
|
||||||
|
* Created: 2025-08-21
|
||||||
|
* Description: Tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "./alisp.h"
|
||||||
|
|
||||||
|
// Sample data
|
||||||
|
char *words[] = {
|
||||||
|
"aliquam", "erat", "volutpat", "nunc", "eleifend",
|
||||||
|
"leo", "vitae", "magna", "in", "id",
|
||||||
|
"erat", "non", "orci", "commodo", "lobortis",
|
||||||
|
"proin", "neque", "massa", "cursus", "ut",
|
||||||
|
"gravida", "ut", "lobortis", "eget", "lacus",
|
||||||
|
"sed", "diam", "praesent", "fermentum", "tempor",
|
||||||
|
"tellus", "nullam", "tempus", "mauris", "ac",
|
||||||
|
"felis", "vel", "velit", "tristique", "imperdiet",
|
||||||
|
"donec", "at", "pede", "etiam", "vel",
|
||||||
|
"neque", "nec", "dui", "dignissim", "bibendum",
|
||||||
|
"vivamus", "id", "enim", "phasellus", "neque",
|
||||||
|
"orci", "porta", "a", "aliquet", "quis",
|
||||||
|
"semper", "a", "massa", "phasellus", "purus",
|
||||||
|
"pellentesque", "tristique", "imperdiet", "tortor", "nam",
|
||||||
|
"euismod", "tellus", "id", "erat",
|
||||||
|
};
|
||||||
|
|
||||||
|
char text[] =
|
||||||
|
"Pellentesque dapibus suscipit ligula. Donec posuere augue in quam. "
|
||||||
|
"Etiam vel tortor sodales tellus ultricies commodo. Suspendisse potenti. "
|
||||||
|
"Aenean in sem ac leo mollis blandit. Donec neque quam, dignissim in, "
|
||||||
|
"mollis nec, sagittis eu, wisi. Phasellus lacus. Etiam laoreet quam sed "
|
||||||
|
"arcu. Phasellus at dui in ligula mollis ultricies. Integer placerat "
|
||||||
|
"tristique nisl. Praesent augue. Fusce commodo. Vestibulum convallis, "
|
||||||
|
"lorem a tempus semper, dui dui euismod elit, vitae placerat urna tortor "
|
||||||
|
"vitae lacus. Nullam libero mauris, consequat quis, varius et, dictum id, "
|
||||||
|
"arcu. Mauris mollis tincidunt felis. Aliquam feugiat tellus ut neque. "
|
||||||
|
"Nulla facilisis, risus a rhoncus fermentum, tellus tellus lacinia purus, "
|
||||||
|
"et dictum nunc justo sit amet elit.";
|
||||||
|
|
||||||
|
void symtable_test(void)
|
||||||
|
{
|
||||||
|
sym_table_t table = {0};
|
||||||
|
sym_table_init(&table);
|
||||||
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
|
{
|
||||||
|
char *ptr = sym_table_find(&table, SV(words[i], strlen(words[i])));
|
||||||
|
printf("[symtable_test]: %s => %p\n", words[i], ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(
|
||||||
|
"[symtable_test]: |words|=%lu, |table|= %lu => Unique word ratio: %lf\n",
|
||||||
|
ARRSIZE(words), table.count, table.count / (double)ARRSIZE(words));
|
||||||
|
|
||||||
|
sym_table_cleanup(&table);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_int_test(void)
|
||||||
|
{
|
||||||
|
i64 ints[] = {
|
||||||
|
1, -1, (1 << 10) - 1, (-1) * ((1 << 10) - 1),
|
||||||
|
INT_MIN, INT_MAX, INT64_MAX, INT64_MIN,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (u64 i = 0; i < ARRSIZE(ints); ++i)
|
||||||
|
{
|
||||||
|
i64 in = ints[i];
|
||||||
|
lisp_t *lisp = make_int(in);
|
||||||
|
i64 out = as_int(lisp);
|
||||||
|
|
||||||
|
printf("[make_int_test]: %#16lx => %#16lx => %#16lx\n", in, (u64)lisp, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void intern_test(void)
|
||||||
|
{
|
||||||
|
sys_t system = {0};
|
||||||
|
sys_init(&system);
|
||||||
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
|
{
|
||||||
|
char *in = words[i];
|
||||||
|
lisp_t *lisp = intern(&system, SV(in, strlen(in)));
|
||||||
|
char *out = as_sym(lisp);
|
||||||
|
printf("[intern test]: `%s` -> %p -> `%s`\n", in, lisp, out);
|
||||||
|
}
|
||||||
|
sys_cleanup(&system);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_vec_test(void)
|
||||||
|
{
|
||||||
|
sys_t system = {0};
|
||||||
|
sys_init(&system);
|
||||||
|
|
||||||
|
// Generating a vector word by word
|
||||||
|
lisp_t *vec = make_vec(&system, 0);
|
||||||
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
|
{
|
||||||
|
char *word = words[i];
|
||||||
|
vec_append(as_vec(vec), word, strlen(word));
|
||||||
|
vec_append(as_vec(vec), " ", 1);
|
||||||
|
}
|
||||||
|
printf("[make_vec_test]: %lu/%lu, inlined?: %s, text=%.*s\n",
|
||||||
|
as_vec(vec)->size, as_vec(vec)->capacity,
|
||||||
|
as_vec(vec)->is_inlined ? "yes" : "no", (int)as_vec(vec)->size,
|
||||||
|
(char *)vec_data(as_vec(vec)));
|
||||||
|
|
||||||
|
// Generating substrings
|
||||||
|
struct Test
|
||||||
|
{
|
||||||
|
u64 start, size;
|
||||||
|
} tests[] = {
|
||||||
|
{0, 16},
|
||||||
|
{0, 32},
|
||||||
|
{32, 64},
|
||||||
|
{0, ARRSIZE(text)},
|
||||||
|
};
|
||||||
|
for (u64 i = 0; i < ARRSIZE(tests); ++i)
|
||||||
|
{
|
||||||
|
struct Test test = tests[i];
|
||||||
|
// Always initialise below what we expect
|
||||||
|
lisp_t *lvec = make_vec(&system, test.size / 2);
|
||||||
|
vec_t *vec = as_vec(lvec);
|
||||||
|
printf("[make_vec_test]: %p -> %p[%lu/%lu] -> ", lvec, vec, vec->size,
|
||||||
|
vec->capacity);
|
||||||
|
vec_append(vec, text + test.start, test.size);
|
||||||
|
printf("[%lu/%lu] -> ", vec->size, vec->capacity);
|
||||||
|
printf("`%.*s`\n", (int)vec->size, (char *)vec_data(vec));
|
||||||
|
}
|
||||||
|
|
||||||
|
sys_cleanup(&system);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cons_test(void)
|
||||||
|
{
|
||||||
|
sys_t system = {0};
|
||||||
|
sys_init(&system);
|
||||||
|
|
||||||
|
// Iterate through each "word" in words, make a large contiguous list which is
|
||||||
|
// its reverse
|
||||||
|
lisp_t *lisp = NIL;
|
||||||
|
for (u64 i = 0; i < ARRSIZE(words); ++i)
|
||||||
|
{
|
||||||
|
char *word = words[i];
|
||||||
|
lisp_t *lword = intern(&system, SV(word, strlen(word)));
|
||||||
|
lisp = cons(&system, lword, lisp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then iterate through it
|
||||||
|
printf("[cons_test]: ");
|
||||||
|
for (lisp_t *iter = lisp; iter; iter = cdr(iter))
|
||||||
|
{
|
||||||
|
lisp_t *item = car(iter);
|
||||||
|
printf("%s ", as_sym(item));
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
sys_cleanup(&system);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
symtable_test();
|
||||||
|
printf("\n");
|
||||||
|
make_int_test();
|
||||||
|
printf("\n");
|
||||||
|
intern_test();
|
||||||
|
printf("\n");
|
||||||
|
make_vec_test();
|
||||||
|
printf("\n");
|
||||||
|
cons_test();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user