add some unit tests

This commit is contained in:
Josh Holtrop 2019-10-29 17:44:16 -04:00
parent 0f9cee10b4
commit bd541e581a
3 changed files with 56 additions and 0 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
.PHONY: all
all:
./rscons
.PHONY: test
test: all
./build/e.1/tests

7
Rsconscript Normal file
View File

@ -0,0 +1,7 @@
build do
Environment.new do |env|
sources = glob("src/**/*.c")
env["CPPPATH"] += glob("src/**")
env.Program("^/tests", sources + glob("test/**/*.c"))
end
end

42
test/test_jairee.c Normal file
View File

@ -0,0 +1,42 @@
#include "jairee.h"
#include <stdio.h>
#define TEST(x) Test(#x, x, __FILE__, __LINE__)
static int n_tests_passed;
static int n_tests_failed;
static void Test(const char * message, int result, const char * file, int line)
{
if (result)
{
n_tests_passed++;
}
else
{
fprintf(stderr, "Test failure at %s:%d: \"%s\"\n", file, line, message);
n_tests_failed++;
}
}
static void test_asciiz_string()
{
jairee_string_t * s = jairee_string_from_asciiz("Test String");
size_t offset = 0;
TEST(s->len == 11u);
TEST(s->decode(s->str, &offset) == 'T');
TEST(offset == 1u);
offset = 10u;
TEST(s->decode(s->str, &offset) == 'g');
TEST(offset == 11u);
jairee_string_free(s);
}
int main(int argc, char * argv[])
{
test_asciiz_string();
printf("%d/%d tests passed\n", n_tests_passed, n_tests_passed + n_tests_failed);
return n_tests_failed == 0 ? 0 : 1;
}