summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-06-04 10:33:12 -0300
committerEuAndreh <eu@euandre.org>2024-06-04 10:33:12 -0300
commit160a9d47882544aea32191349429dfe5c7f3ca12 (patch)
tree1276cd6917eb931f2d2f1065840715eeb7bdbaea
parentsrc/vector.h: Implement vector_assign() (diff)
downloadpindaiba-160a9d47882544aea32191349429dfe5c7f3ca12.tar.gz
pindaiba-160a9d47882544aea32191349429dfe5c7f3ca12.tar.xz
src/set.c: Implement set_new() and set_free()
-rw-r--r--deps.mk14
-rw-r--r--src/set.c26
-rw-r--r--src/set.h1
-rw-r--r--tests/set.c76
4 files changed, 108 insertions, 9 deletions
diff --git a/deps.mk b/deps.mk
index 12678b6..43ef4d6 100644
--- a/deps.mk
+++ b/deps.mk
@@ -96,7 +96,7 @@ src/lib.o: src/logerr.h
src/logerr.o:
src/math.o:
src/random.o: src/logerr.h src/util.h
-src/set.o: src/logerr.h src/util.h
+src/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h
src/string.o: src/logerr.h src/math.h src/util.h
src/testing.o:
src/tree.o: src/logerr.h src/util.h
@@ -110,7 +110,7 @@ tests/lib.o: src/logerr.h
tests/logerr.o: src/testing.h src/util.h tests/slurp.h
tests/math.o: src/testing.h
tests/random.o: src/logerr.h src/util.h src/testing.h
-tests/set.o: src/logerr.h src/util.h src/testing.h
+tests/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h src/testing.h
tests/string.o: src/logerr.h src/math.h src/util.h src/testing.h
tests/testing.o:
tests/tree.o: src/logerr.h src/util.h src/testing.h
@@ -123,10 +123,10 @@ tests/i18n.a: src/catalog.o src/logerr.o
tests/lib.a: src/logerr.o
tests/logerr.a: src/testing.o src/util.o tests/slurp.o
tests/math.a: src/testing.o
-tests/random.a: src/logerr.o src/util.o src/testing.o
-tests/set.a: src/logerr.o src/util.o src/testing.o
-tests/string.a: src/logerr.o src/math.o src/util.o src/testing.o
+tests/random.a: src/logerr.o src/testing.o src/util.o
+tests/set.a: src/catalog.o src/hash.o src/i18n.o src/logerr.o src/math.o src/random.o src/testing.o src/tree.o src/util.o src/vector.o
+tests/string.a: src/logerr.o src/math.o src/testing.o src/util.o
tests/testing.a:
-tests/tree.a: src/logerr.o src/util.o src/testing.o
+tests/tree.a: src/logerr.o src/testing.o src/util.o
tests/util.a: src/logerr.o src/testing.o tests/slurp.o
-tests/vector.a: src/catalog.o src/i18n.o src/logerr.o src/math.o src/util.o src/testing.o
+tests/vector.a: src/catalog.o src/i18n.o src/logerr.o src/math.o src/testing.o src/util.o
diff --git a/src/set.c b/src/set.c
index 3c03231..465a141 100644
--- a/src/set.c
+++ b/src/set.c
@@ -5,26 +5,35 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
+#include <siphashbs.h>
+
+#include "hash.h"
#include "logerr.h"
+#include "tree.h"
#include "util.h"
+#include "vector.h"
#include "set.h"
struct Set {
+ const struct Vector *table;
+ const size_t value_size;
size_t count;
};
int
-set_new(const struct Set **const out) {
+set_new(const size_t value_size, const struct Set **const out) {
int rc = -1;
const struct Set *ret = NULL;
+ const struct Vector *table = NULL;
ret = malloc(sizeof(*ret));
if (ret == NULL) {
@@ -32,13 +41,23 @@ set_new(const struct Set **const out) {
goto out;
}
+ if (vector_new(sizeof(struct Tree *), &table)) {
+ logerr("vector_new()");
+ goto out;
+ }
+
memcpy((void *)ret, &(struct Set) {
- .count = 0U,
+ .table = table,
+ .value_size = value_size,
+ .count = 0U,
}, sizeof(*ret));
*out = ret;
rc = 0;
out:
if (rc) {
+ if (table != NULL) {
+ vector_free(&table);
+ }
if (ret != NULL) {
freeit((void *)&ret);
}
@@ -49,5 +68,8 @@ out:
void
set_free(const struct Set **const s) {
assert((*s) != NULL);
+ const struct Vector *table = (*s)->table;
+ vector_free(&table);
free((void *)*s);
+ *s = NULL;
}
diff --git a/src/set.h b/src/set.h
index e69de29..5a7377f 100644
--- a/src/set.h
+++ b/src/set.h
@@ -0,0 +1 @@
+struct Set;
diff --git a/tests/set.c b/tests/set.c
index 66d0472..e00a253 100644
--- a/tests/set.c
+++ b/tests/set.c
@@ -3,9 +3,85 @@
#include "../src/testing.h"
+
+static int
+test_set_new(void) {
+ int rc = -1;
+
+ const struct Set *s = NULL;
+
+ test_start("set_new()");
+ {
+ testing("simple allocation");
+
+ const size_t size = sizeof(long long);
+ if (set_new(size, &s)) {
+ logerr("set_new()");
+ goto out;
+ }
+
+ assert(s->value_size == size);
+
+ set_free(&s);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (s != NULL) {
+ set_free(&s);
+ }
+ return rc;
+}
+
+static int
+test_set_free(void) {
+ int rc = -1;
+
+ const struct Set *s = NULL;
+
+ test_start("set_free()");
+ {
+ testing("*s becomes NULL again after set_free(&s)");
+
+ assert(s == NULL);
+ if (set_new(1U, &s)) {
+ logerr("set_new()");
+ goto out;
+ }
+
+ assert(s != NULL);
+ set_free(&s);
+ assert(s == NULL);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (s != NULL) {
+ set_free(&s);
+ }
+ return rc;
+}
+
+
int
main(void) {
int rc = EXIT_FAILURE;
+
+ if (test_set_new()) {
+ logerr("test_set_new()");
+ goto out;
+ }
+
+ if (test_set_free()) {
+ logerr("test_set_free()");
+ goto out;
+ }
+
rc = EXIT_SUCCESS;
+out:
return rc;
}