From d5cedc6f28a60301bad21a16fdb047fdb82ff883 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Thu, 9 Apr 2020 20:37:12 -0400 Subject: nserver: Add db.h * nserver/.gitignore: Add nserver.db * nserver/Makefile ($(PROGRAMS)) (tests): Link gdbm. (clean): Remove nserver.db * nserver/src/db.c (db_init): New function. * nserver/src/db.h (db_init): New function declaration. * nserver/tests/db_tests.c: Tests for db.h --- nserver/.gitignore | 1 + nserver/Makefile | 5 +++-- nserver/src/db.c | 20 ++++++++++++++++++++ nserver/src/db.h | 11 +++++++++++ nserver/tests/db_tests.c | 21 +++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 nserver/src/db.c create mode 100644 nserver/src/db.h create mode 100644 nserver/tests/db_tests.c diff --git a/nserver/.gitignore b/nserver/.gitignore index 7a18cd1..7a6be0c 100644 --- a/nserver/.gitignore +++ b/nserver/.gitignore @@ -1 +1,2 @@ tests/*_tests +nserver.db \ No newline at end of file diff --git a/nserver/Makefile b/nserver/Makefile index 5c6bc9c..3ecfd8d 100644 --- a/nserver/Makefile +++ b/nserver/Makefile @@ -28,7 +28,7 @@ $(TARGET): build $(OBJECTS) $(SO_TARGET): $(TARGET) $(OBJECTS) $(CC) -shared -o $@ $(OBJECTS) -$(PROGRAMS): LDLIBS += $(TARGET) -lm +$(PROGRAMS): LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm build: @mkdir -p build @@ -36,7 +36,7 @@ build: # The Unit Tests .PHONY: tests -tests: LDLIBS += $(TARGET) -lm +tests: LDLIBS += $(TARGET) -lm -L/usr/local/lib -lgdbm tests: $(TESTS) sh ./tests/runtests.sh @@ -46,6 +46,7 @@ clean: rm -f tests/tests.log find . -name "*.gc" -exec rm {} \; rm -rf `find . -name "*.dSYM" -print` + rm -rf nserver.db # The Install install: all diff --git a/nserver/src/db.c b/nserver/src/db.c new file mode 100644 index 0000000..315bf7b --- /dev/null +++ b/nserver/src/db.c @@ -0,0 +1,20 @@ +#include + + +static const char *DB_FILE = "nserver.db"; + +int db_init() +{ + // Create DB if it's not already created. + GDBM_FILE gf = gdbm_open(DB_FILE, 0, GDBM_WRCREAT, + S_IRUSR|S_IWUSR, NULL); + check(gf != NULL, "unable to init db"); + + // Close the DB. + int rc = gdbm_close(gf); + check(rc == 0, "error closing db after init"); + + return 0; + error: + return -1; +} diff --git a/nserver/src/db.h b/nserver/src/db.h new file mode 100644 index 0000000..433f812 --- /dev/null +++ b/nserver/src/db.h @@ -0,0 +1,11 @@ +#ifndef _db_h +#define _db_h + +#include +#include +#include + +int db_init(); + +#endif + diff --git a/nserver/tests/db_tests.c b/nserver/tests/db_tests.c new file mode 100644 index 0000000..f97f114 --- /dev/null +++ b/nserver/tests/db_tests.c @@ -0,0 +1,21 @@ +#include "minunit.h" +#include + +char *test_db_init() +{ + int rc = db_init(); + mu_assert(rc == 0, "db init failed"); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_db_init); + + return NULL; +} + +RUN_TESTS(all_tests); -- cgit v1.2.3