2012-12-07 10:34:34 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-07-03 09:50:36 +00:00
|
|
|
#include "QF/dstring.h"
|
2012-12-07 10:34:34 +00:00
|
|
|
#include "QF/set.h"
|
2015-07-03 09:50:36 +00:00
|
|
|
#include "QF/va.h"
|
2012-12-07 10:34:34 +00:00
|
|
|
|
2013-03-16 12:26:49 +00:00
|
|
|
#define SIZE (SET_DEFMAP_SIZE * sizeof (set_bits_t) * 8)
|
2012-12-07 11:43:11 +00:00
|
|
|
|
2012-12-07 10:34:34 +00:00
|
|
|
typedef set_t *(*setup_func) (void);
|
2012-12-07 11:43:11 +00:00
|
|
|
typedef set_t *(*op_func) (set_t *s1, const set_t *s2);
|
|
|
|
typedef int (*test_func) (const set_t *s1, const set_t *s2);
|
2012-12-07 10:34:34 +00:00
|
|
|
|
|
|
|
static set_t *
|
2012-12-07 11:43:11 +00:00
|
|
|
make_empty (void)
|
2012-12-07 10:34:34 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-12-07 11:43:11 +00:00
|
|
|
static set_t *
|
|
|
|
make_SIZE (void)
|
|
|
|
{
|
|
|
|
set_t *set = set_new ();
|
|
|
|
set_add (set, SIZE);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2012-12-09 04:52:48 +00:00
|
|
|
static set_t *
|
|
|
|
make_not_SIZE (void)
|
|
|
|
{
|
|
|
|
set_t *set = set_new ();
|
|
|
|
set_add (set, SIZE);
|
|
|
|
return set_invert (set);
|
|
|
|
}
|
|
|
|
|
2012-12-07 11:43:11 +00:00
|
|
|
static set_t *
|
|
|
|
make_0_to_SIZEm1 (void)
|
|
|
|
{
|
|
|
|
set_t *set = set_new ();
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < SIZE; i++)
|
|
|
|
set_add (set, i);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_size (const set_t *set, const set_t *unused)
|
|
|
|
{
|
|
|
|
return set->size;
|
|
|
|
}
|
|
|
|
|
2021-07-27 04:54:22 +00:00
|
|
|
static int
|
|
|
|
check_count (const set_t *set, const set_t *unused)
|
|
|
|
{
|
|
|
|
return set_count (set);
|
|
|
|
}
|
|
|
|
|
2012-12-07 13:16:07 +00:00
|
|
|
static set_t *
|
|
|
|
make_5 (void)
|
|
|
|
{
|
|
|
|
set_t *set = set_new ();
|
|
|
|
return set_add (set, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static set_t *
|
|
|
|
make_55 (void)
|
|
|
|
{
|
|
|
|
set_t *set = set_new ();
|
|
|
|
return set_add (set, 55);
|
|
|
|
}
|
|
|
|
|
|
|
|
static set_t *
|
|
|
|
make_not_5 (void)
|
|
|
|
{
|
|
|
|
return set_invert (make_5 ());
|
|
|
|
}
|
|
|
|
|
|
|
|
static set_t *
|
|
|
|
make_not_55 (void)
|
|
|
|
{
|
|
|
|
return set_invert (make_55 ());
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:54:03 +00:00
|
|
|
static set_t *
|
|
|
|
expand_3xSIZEm1 (set_t *set, const set_t *x)
|
|
|
|
{
|
|
|
|
set_expand (set, 3 * SIZE - 1);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
static set_t *
|
|
|
|
expand_3xSIZEp1 (set_t *set, const set_t *x)
|
|
|
|
{
|
|
|
|
set_expand (set, 3 * SIZE + 1);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
static set_t *
|
|
|
|
expand_3xSIZE (set_t *set, const set_t *x)
|
|
|
|
{
|
|
|
|
set_expand (set, 3 * SIZE);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2012-12-07 10:34:34 +00:00
|
|
|
struct {
|
2012-12-07 11:43:11 +00:00
|
|
|
setup_func set1;
|
|
|
|
setup_func set2;
|
|
|
|
op_func op;
|
|
|
|
test_func test;
|
|
|
|
int test_expect;
|
|
|
|
const char *str_expect;
|
2012-12-07 10:34:34 +00:00
|
|
|
} tests[] = {
|
2012-12-09 13:17:55 +00:00
|
|
|
{make_empty, 0, 0, check_size, SIZE, "{}"},
|
|
|
|
{make_empty2, 0, 0, check_size, SIZE, "{}"},
|
|
|
|
{make_everything, 0, 0, check_size, SIZE, "{...}"},
|
|
|
|
{make_empty_invert, 0, 0, check_size, SIZE, "{...}"},
|
|
|
|
{make_everything_invert, 0, 0, check_size, SIZE, "{}"},
|
2013-03-16 12:26:49 +00:00
|
|
|
{make_SIZE, 0, 0, check_size, SIZE + SET_BITS, "{64}"},
|
2012-12-07 11:43:11 +00:00
|
|
|
{make_0_to_SIZEm1, 0, 0, check_size, SIZE,
|
2012-12-09 13:17:55 +00:00
|
|
|
"{0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
|
2012-12-07 11:43:11 +00:00
|
|
|
" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
|
|
|
|
" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
|
2012-12-09 13:17:55 +00:00
|
|
|
" 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63}"
|
2012-12-07 11:43:11 +00:00
|
|
|
},
|
|
|
|
{make_everything, make_0_to_SIZEm1, set_difference, check_size,
|
2012-12-09 13:17:55 +00:00
|
|
|
SIZE, "{64 ...}"
|
2012-12-07 11:43:11 +00:00
|
|
|
},
|
|
|
|
{make_0_to_SIZEm1, make_everything, set_reverse_difference,
|
2012-12-09 13:17:55 +00:00
|
|
|
check_size, SIZE, "{64 ...}"
|
2012-12-07 11:43:11 +00:00
|
|
|
},
|
2021-07-26 00:54:03 +00:00
|
|
|
{make_SIZE, 0, expand_3xSIZEm1, check_size, 3 * SIZE,
|
|
|
|
"{64}"},
|
|
|
|
{make_SIZE, 0, expand_3xSIZE, check_size, 3 * SIZE, "{64}"},
|
|
|
|
{make_SIZE, 0, expand_3xSIZEp1, check_size,
|
|
|
|
3 * SIZE + SET_BITS, "{64}"},
|
2012-12-09 04:52:48 +00:00
|
|
|
{make_everything, make_empty, 0, set_is_subset, 1, 0},
|
|
|
|
{make_everything, make_empty, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_everything, make_empty, 0, set_is_intersecting, 0, 0},
|
|
|
|
{make_everything, make_empty, 0, set_is_disjoint, 1, 0},
|
|
|
|
{make_empty, make_everything, 0, set_is_subset, 0, 0},
|
|
|
|
{make_empty, make_everything, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_empty, make_everything, 0, set_is_intersecting, 0, 0},
|
|
|
|
{make_empty, make_everything, 0, set_is_disjoint, 1, 0},
|
|
|
|
{make_everything, make_everything, 0, set_is_subset, 1, 0},
|
|
|
|
{make_everything, make_everything, 0, set_is_equivalent, 1, 0},
|
|
|
|
{make_everything, make_everything, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_everything, make_everything, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_empty, make_empty, 0, set_is_subset, 1, 0},
|
|
|
|
{make_empty, make_empty, 0, set_is_equivalent, 1, 0},
|
|
|
|
{make_empty, make_empty, 0, set_is_intersecting, 0, 0},
|
|
|
|
{make_empty, make_empty, 0, set_is_disjoint, 1, 0},
|
2012-12-07 13:16:22 +00:00
|
|
|
{make_5, make_5, 0, set_is_equivalent, 1, 0},
|
2012-12-09 04:52:48 +00:00
|
|
|
{make_5, make_5, 0, set_is_intersecting, 1, 0},
|
2012-12-07 13:16:22 +00:00
|
|
|
{make_5, make_5, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_5, make_55, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_5, make_55, 0, set_is_intersecting, 0, 0},
|
|
|
|
{make_5, make_55, 0, set_is_disjoint, 1, 0},
|
|
|
|
{make_not_5, make_55, 0, set_is_equivalent, 0, 0},
|
2012-12-07 13:16:07 +00:00
|
|
|
{make_not_5, make_55, 0, set_is_intersecting, 1, 0},
|
2012-12-07 13:16:22 +00:00
|
|
|
{make_not_5, make_55, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_5, make_not_55, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_5, make_not_55, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_5, make_not_55, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_not_5, make_not_55, 0, set_is_equivalent, 0, 0},
|
2012-12-07 13:16:07 +00:00
|
|
|
{make_not_5, make_not_55, 0, set_is_intersecting, 1, 0},
|
2012-12-07 13:16:22 +00:00
|
|
|
{make_not_5, make_not_55, 0, set_is_disjoint, 0, 0},
|
2012-12-09 13:17:55 +00:00
|
|
|
{make_5, make_55, set_union, set_is_equivalent, 0, "{5 55}"},
|
|
|
|
{make_5, make_55, set_union, set_is_intersecting, 1, "{5 55}"},
|
|
|
|
{make_5, make_55, set_union, set_is_disjoint, 0, "{5 55}"},
|
|
|
|
{make_55, make_5, set_union, set_is_equivalent, 0, "{5 55}"},
|
|
|
|
{make_55, make_5, set_union, set_is_intersecting, 1, "{5 55}"},
|
|
|
|
{make_55, make_5, set_union, set_is_disjoint, 0, "{5 55}"},
|
2021-07-27 04:54:22 +00:00
|
|
|
{make_55, make_5, set_union, check_count, 2, "{5 55}"},
|
2012-12-09 04:52:48 +00:00
|
|
|
{make_not_SIZE, make_everything, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_not_SIZE, make_everything, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_not_SIZE, make_everything, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_SIZE, make_everything, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_SIZE, make_everything, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_SIZE, make_everything, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_not_5, make_everything, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_not_5, make_everything, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_not_5, make_everything, 0, set_is_disjoint, 0, 0},
|
|
|
|
{make_5, make_everything, 0, set_is_equivalent, 0, 0},
|
|
|
|
{make_5, make_everything, 0, set_is_intersecting, 1, 0},
|
|
|
|
{make_5, make_everything, 0, set_is_disjoint, 0, 0},
|
2021-07-27 04:54:22 +00:00
|
|
|
{make_empty, 0, 0, check_count, 0, 0},
|
|
|
|
{make_everything, 0, 0, check_count, 0, 0},
|
|
|
|
{make_5, 0, 0, check_count, 1, 0},
|
|
|
|
{make_not_5, 0, 0, check_count, 1, 0},
|
|
|
|
{make_0_to_SIZEm1, 0, 0, check_size, SIZE, 0},
|
2012-12-07 10:34:34 +00:00
|
|
|
};
|
|
|
|
#define num_tests (sizeof (tests) / sizeof (tests[0]))
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, const char **argv)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int res = 0;
|
2015-07-03 09:50:36 +00:00
|
|
|
dstring_t *str;
|
|
|
|
|
|
|
|
//printf ("set_bits_t: %d, SET_DEFMAP_SIZE: %d, SIZE: %d\n",
|
|
|
|
// sizeof (set_bits_t), SET_DEFMAP_SIZE, SIZE);
|
|
|
|
|
2021-06-01 10:11:51 +00:00
|
|
|
tests[5].str_expect = nva ("{%zd}", SIZE);
|
|
|
|
tests[7].str_expect = nva ("{%zd ...}", SIZE);
|
2021-07-29 05:10:18 +00:00
|
|
|
tests[8].str_expect = tests[7].str_expect;
|
|
|
|
tests[9].str_expect = tests[5].str_expect;
|
|
|
|
tests[10].str_expect = tests[5].str_expect;
|
|
|
|
tests[11].str_expect = tests[5].str_expect;
|
2015-07-03 09:50:36 +00:00
|
|
|
|
|
|
|
str = dstring_new ();
|
|
|
|
for (i = 0; i < SIZE; i++) {
|
2021-06-01 10:11:51 +00:00
|
|
|
dasprintf (str, "%c%zd", i ? ' ' : '{', i);
|
2015-07-03 09:50:36 +00:00
|
|
|
}
|
|
|
|
dstring_appendstr (str, "}");
|
|
|
|
tests[6].str_expect = dstring_freeze (str);
|
2012-12-07 10:34:34 +00:00
|
|
|
|
|
|
|
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 ();
|
2012-12-07 11:43:11 +00:00
|
|
|
if (tests[i].op) {
|
|
|
|
if (tests[i].op (s1, s2) != s1) {
|
|
|
|
res |= 1;
|
|
|
|
printf ("test op %d didn't return s1\n", (int) i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-12-07 10:34:34 +00:00
|
|
|
if (tests[i].test) {
|
2012-12-07 11:43:11 +00:00
|
|
|
int test_res;
|
|
|
|
test_res = tests[i].test (s1, s2);
|
|
|
|
if (test_res != tests[i].test_expect) {
|
2012-12-07 10:34:34 +00:00
|
|
|
res |= 1;
|
2012-12-07 11:43:11 +00:00
|
|
|
printf ("test %d failed\n", (int) i);
|
|
|
|
printf ("expect: %d\n", tests[i].test_expect);
|
|
|
|
printf ("got : %d\n", test_res);
|
2012-12-07 10:34:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_str = set_as_string (s1);
|
2012-12-07 11:43:11 +00:00
|
|
|
if (tests[i].str_expect && strcmp (set_str, tests[i].str_expect)) {
|
2012-12-07 10:34:34 +00:00
|
|
|
res |= 1;
|
|
|
|
printf ("test %d failed\n", (int) i);
|
2012-12-07 11:43:11 +00:00
|
|
|
printf ("expect: %s\n", tests[i].str_expect);
|
2012-12-07 10:34:34 +00:00
|
|
|
printf ("got : %s\n", set_str);
|
2012-12-07 11:43:11 +00:00
|
|
|
continue;
|
2012-12-07 10:34:34 +00:00
|
|
|
}
|
2012-12-07 11:43:11 +00:00
|
|
|
set_delete (s1);
|
|
|
|
if (s2)
|
|
|
|
set_delete (s2);
|
2012-12-07 10:34:34 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|