Skip to content

Instantly share code, notes, and snippets.

@rioki
Created Feb 26, 2018
Embed
What would you like to do?
#include <stdio.h>
#include "test.h"
int register_parse_tests();
int main(int argc, char*argv[])
{
int r = 0;
r = register_parse_tests();
if (r < 0)
{
printf("Registering parse tests failed.\n");
return -1;
}
return tst_run();
}
/* example usage from ezscript */
#include "test.h"
#include "ezscript.h"
int test_empty()
{
char* code = "";
int r = ez_compile(code);
return r == EZ_OK;
}
int test_variable()
{
char* code = "var a = 1;";
int r = ez_compile(code);
return r == EZ_OK;
}
int register_parse_tests()
{
TST_REGISTER(test_empty);
TST_REGISTER(test_variable);
return 0;
}
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "test.h"
typedef struct Test {
const char* name;
test_fun fun;
struct Test* next;
} Test;
Test* test_root = NULL;
int tst_register(const char* name, test_fun fun)
{
Test* test = malloc(sizeof(Test));
if (test == NULL)
{
printf("Failed to alocate structures for %s.\n", name);
// this will probably do nothing sensible, because this code is executed premain
return -1;
}
test->name = name; // safe if used with TEST macro
test->fun = fun;
test->next = NULL;
if (test_root == NULL) {
test_root = test;
}
else
{
Test* tmp = test_root;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = test;
}
return 0;
}
int tst_run()
{
int success = 0;
int fail = 0;
int sum;
Test* tmp = test_root;
while (tmp != NULL)
{
int r = tmp->fun();
if (r)
{
success++;
}
else
{
fail++;
printf("Test %s failed.\n", tmp->name);
}
tmp = tmp->next;
}
sum = success + fail;
printf("Ran %d tests: %d success %d fail\n", sum, success, fail);
return 0;
}
#include <stdio.h>
typedef int (*test_fun)();
int tst_register(const char* name, test_fun fun);
int tst_run();
#define TST_REGISTER(NAME) \
int result ## NAME = tst_register(#NAME, NAME); \
if (result ## NAME < 0) \
{ \
printf("Failed to register %s test.", #NAME); \
return result ## NAME; \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment