Add some very basic tests for sets.

They test creation, empty, everything and inversion. Already found a bug
(typo: "everythign" :P)
This commit is contained in:
Bill Currie 2012-12-07 19:34:34 +09:00
parent e0c92b6089
commit c76231ca30
2 changed files with 97 additions and 1 deletions

View file

@ -3,7 +3,8 @@ AUTOMAKE_OPTIONS= foreign
INCLUDES= -I$(top_srcdir)/include
check_PROGRAMS= \
test-dq test-half test-mat3 test-mat4 test-qfs test-quat test-vrect
test-dq test-half test-mat3 test-mat4 test-qfs test-quat test-set \
test-vrect
test_dq_SOURCES=test-dq.c
test_dq_LDADD=$(top_builddir)/libs/util/libQFutil.la
@ -29,6 +30,10 @@ test_quat_SOURCES=test-quat.c
test_quat_LDADD=$(top_builddir)/libs/util/libQFutil.la
test_quat_DEPENDENCIES=$(top_builddir)/libs/util/libQFutil.la
test_set_SOURCES=test-set.c
test_set_LDADD=$(top_builddir)/libs/util/libQFutil.la
test_set_DEPENDENCIES=$(top_builddir)/libs/util/libQFutil.la
test_vrect_SOURCES=test-vrect.c
test_vrect_LDADD=$(top_builddir)/libs/util/libQFutil.la
test_vrect_DEPENDENCIES=$(top_builddir)/libs/util/libQFutil.la

91
libs/util/test/test-set.c Normal file
View file

@ -0,0 +1,91 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include "QF/set.h"
typedef set_t *(*setup_func) (void);
typedef set_t *(*test_func) (set_t *s1, set_t *s2);
static set_t *
make_empty1 (void)
{
set_t *set = set_new ();
return set;
}
static set_t *
make_empty2 (void)
{
set_t *set = set_new ();
return set_empty (set);
}
static set_t *
make_everything (void)
{
set_t *set = set_new ();
return set_everything (set);
}
static set_t *
make_empty_invert (void)
{
set_t *set = set_new ();
return set_invert (set);
}
static set_t *
make_everything_invert (void)
{
set_t *set = set_new ();
set_everything (set);
return set_invert (set);
}
struct {
setup_func set1;
setup_func set2;
test_func test;
const char *expect;
} tests[] = {
{make_empty1, 0, 0, "[empty]"},
{make_empty2, 0, 0, "[empty]"},
{make_everything, 0, 0, "[everything]"},
{make_empty_invert, 0, 0, "[everything]"},
{make_everything_invert, 0, 0, "[empty]"},
};
#define num_tests (sizeof (tests) / sizeof (tests[0]))
int
main (int argc, const char **argv)
{
size_t i;
int res = 0;
for (i = 0; i < num_tests; i++) {
set_t *s1, *s2 = 0;
const char *set_str;
s1 = tests[i].set1 ();
if (tests[i].set2)
s2 = tests[i].set2 ();
if (tests[i].test) {
if (tests[i].test (s1, s2) != s1) {
res |= 1;
printf ("test %d didn't return s1\n", (int) i);
continue;
}
}
set_str = set_as_string (s1);
if (strcmp (set_str, tests[i].expect)) {
res |= 1;
printf ("test %d failed\n", (int) i);
printf ("expect: %s\n", tests[i].expect);
printf ("got : %s\n", set_str);
}
}
return res;
}