jairee/test/test_jairee.c
2019-10-29 19:22:18 -04:00

58 lines
1.3 KiB
C

#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);
}
static void test_basic_re()
{
jairee_string_t * s = jairee_string_from_asciiz("abcd");
jairee_re_t * re = jairee_compile(s);
TEST(re != NULL);
TEST(re->error == 0u);
jairee_match_t * match = jairee_matchc(s, 0u, re);
TEST(match != NULL);
TEST(match->start == 0u);
TEST(match->end == 4u);
TEST(match->next == NULL);
jairee_string_free(s);
}
int main(int argc, char * argv[])
{
test_asciiz_string();
test_basic_re();
printf("%d/%d tests passed\n", n_tests_passed, n_tests_passed + n_tests_failed);
return n_tests_failed == 0 ? 0 : 1;
}