From fdd5de0c84cbddcc6b31d579d7006773682059ac Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Mon, 18 Nov 2013 20:01:05 +0100 Subject: Finished implementation of sets with linked lists. --- set_test.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 set_test.c (limited to 'set_test.c') diff --git a/set_test.c b/set_test.c new file mode 100644 index 0000000..eca2663 --- /dev/null +++ b/set_test.c @@ -0,0 +1,97 @@ +#include +#include "sets.h" + +int fails = 0; + +#define ASSERT(x) if (!(x)) { fprintf(stderr, "Assert fail: %s\n", #x); fails++; } + +int main() { + set empty = empty_set(1000); + + set s = empty_set(1000); + dump_set(s); + + set_add_ip(42, s); + dump_set(s); + + set k = singleton(1000, 12); + dump_set(k); + + set_add_ip(126, k); + dump_set(k); + + set j = set_add(76, k); + dump_set(j); + + ASSERT(set_mem(76, j)); + ASSERT(set_mem(126, j)); + ASSERT(!set_mem(42, k)); + ASSERT(set_mem(42, s)); + + set r = set_remove(42, s); + ASSERT(!set_mem(42, r)); + ASSERT(is_set_empty(r)); + set_remove_ip(42, s); + ASSERT(!set_mem(42, s)); + ASSERT(is_set_empty(s)); + ASSERT(sets_equal(s, r)); + + set_add_ip(4, r); + set_add_ip(12, r); + set_add_ip(3, r); + set_add_ip(6, r); + printf("A: "); dump_set(r); + + set x = empty_set(1000); + set_union_ip(x, r); + printf("A(copy): "); dump_set(x); + + set_add_ip(3, s); + set_add_ip(7, s); + set_add_ip(4, s); + set_add_ip(1, s); + set_add_ip(2, s); + set_add_ip(4, s); + set_add_ip(7, s); + printf("B: "); dump_set(s); + + set u = set_union(r, s); + printf("AuB: "); dump_set(u); + set d = set_diff(r, s); + printf("A\\B: "); dump_set(d); + set n = set_inter(r, s); + printf("AnB: "); dump_set(n); + + set A = set_inter(empty, r); + set B = set_inter(r, empty); + printf("@nA: "); dump_set(A); + printf("An@: "); dump_set(B); + ASSERT(is_set_empty(A)); + ASSERT(is_set_empty(B)); + set_union_ip(A, r); + printf("@uA: "); dump_set(A); + set C = set_union(r, empty); + printf("Au@: "); dump_set(C); + ASSERT(sets_equal(A, r)); + ASSERT(sets_equal(C, r)); + set_diff_ip(A, empty); + printf("A\\@: "); dump_set(A); + ASSERT(sets_equal(A, r)); + set_diff_ip(C, u); + printf("A\\(AuB): "); dump_set(C); + ASSERT(is_set_empty(C)); + + delete_set(s); + delete_set(k); + delete_set(j); + delete_set(r); + delete_set(u); + delete_set(d); + delete_set(n); + delete_set(empty); + delete_set(A); + delete_set(B); + delete_set(C); + + return fails; +} -- cgit v1.2.3