From 82f5e32ddb3a847da659efa9081dffb0ef45e961 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 5 Oct 2023 21:18:21 -0700 Subject: [PATCH] working on testing the phonebook --- .gitignore | 3 +++ Makefile | 5 +++- Phonebook.cc | 26 ++++++++++++++++++++- Phonebook.h | 4 +++- phonebook_test.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 phonebook_test.cc diff --git a/.gitignore b/.gitignore index a75a318..833c481 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,8 @@ *.o *.bin build +core +core.* cmake-build-* +phonebook_test tlv_test diff --git a/Makefile b/Makefile index 1596009..8b58c6c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGET := klib.a -TESTS := tlv_test +TESTS := tlv_test phonebook_test HEADERS := $(wildcard *.h) SOURCES := $(wildcard *.cc) OBJS := Arena.o Phonebook.o TLV.o @@ -19,6 +19,9 @@ $(TARGET): $(OBJS) tlv_test: tlv_test.o $(TARGET) $(CXX) -o $@ $(CXXFLAGS) $@.o $(TARGET) +phonebook_test: phonebook_test.o $(TARGET) + $(CXX) -o $@ $(CXXFLAGS) $@.o $(TARGET) + .PHONY: print-% print-%: ; @echo '$(subst ','\'',$*=$($*))' diff --git a/Phonebook.cc b/Phonebook.cc index 56b16da..386fdfc 100644 --- a/Phonebook.cc +++ b/Phonebook.cc @@ -26,7 +26,7 @@ Phonebook::lookup(const char *key, uint8_t klen, TLV::Record &res) int -Phonebook::set(const char *key, uint8_t klen, char *val, uint8_t vlen) +Phonebook::set(const char *key, uint8_t klen, const char *val, uint8_t vlen) { TLV::Record rec; uint8_t *cursor = NULL; @@ -38,6 +38,10 @@ Phonebook::set(const char *key, uint8_t klen, char *val, uint8_t vlen) TLV::delete_record(this->arena, cursor); } + if (!space_available(klen, vlen)) { + return -1; + } + cursor = TLV::write_to_memory(this->arena, NULL, rec); if (cursor == NULL) { return -1; @@ -80,3 +84,23 @@ Phonebook::has(const char *key, uint8_t klen) } +bool +Phonebook::space_available(uint8_t kl, uint8_t vl) +{ + size_t required = 0; + uintptr_t remaining = 0; + uint8_t *cursor = NULL; + + + cursor = TLV::find_empty(this->arena, NULL); + if (cursor == NULL) { + return false; + } + + required += kl + 2; + required += vl + 2; + + remaining = (uintptr_t)cursor - (uintptr_t)arena.store; + remaining = arena.size - remaining; + return ((size_t)remaining >= required); +} diff --git a/Phonebook.h b/Phonebook.h index 55282ed..1eee678 100644 --- a/Phonebook.h +++ b/Phonebook.h @@ -23,11 +23,13 @@ public: vtag(vt) {}; bool lookup(const char *key, uint8_t klen, TLV::Record &res); - int set(const char *key, uint8_t klen, char *val, uint8_t vlen); + int set(const char *key, uint8_t klen, const char *val, + uint8_t vlen); bool has(const char *key, uint8_t klen); private: uint8_t *seek(const char *key, uint8_t klen); + bool space_available(uint8_t klen, uint8_t vlen); Arena &arena; uint8_t ktag; diff --git a/phonebook_test.cc b/phonebook_test.cc new file mode 100644 index 0000000..eabe089 --- /dev/null +++ b/phonebook_test.cc @@ -0,0 +1,58 @@ +#include +#include + +#include "Arena.h" +#include "Phonebook.h" +#include "test_fixtures.h" + + +constexpr char TEST_PBSTR1[] = "foo"; +constexpr uint8_t TEST_PBSTRLEN1 = 3; +constexpr char TEST_PBSTR2[] = "baz"; +constexpr uint8_t TEST_PBSTRLEN2 = 3; +constexpr char TEST_PBSTR3[] = "quux"; +constexpr uint8_t TEST_PBSTRLEN3 = 4; +/* +constexpr char TEST_PBSTR4[] = "spam"; +constexpr uint8_t TEST_PBSTRLEN4 = 4; +constexpr char TEST_PBSTR5[] = "xyzzx"; +constexpr uint8_t TEST_PBSTRLEN5 = 5; +constexpr char TEST_PBSTR6[] = "corvid"; +constexpr uint8_t TEST_PBSTRLEN6 = 6; + */ + + +static bool +test_setpb(Phonebook &pb, const char *k, uint8_t kl, const char *v, + uint8_t vl) +{ + bool ok; + std::cout << "test set " << k << "->" << v << std::endl; + ok = pb.set(k, kl, v, vl) == 0; + std::cout << "\tset complete\n"; + return ok; +} + + +int +main(int argc, const char *argv[]) +{ + Arena arena; + //TLV::Record value; + //TLV::Record expect; + + std::cout << "TESTPROG: " << argv[0] << std::endl; + if (create_arena(arena, ARENA_FILE, ARENA_SIZE, 0644) == -1) { + abort(); + } + + Phonebook pb(arena); + assert(!pb.has(TEST_PBSTR2, TEST_PBSTRLEN2)); + + assert(test_setpb(pb, TEST_PBSTR1, TEST_PBSTRLEN1, TEST_PBSTR3, + TEST_PBSTRLEN3)); + assert(test_setpb(pb, TEST_PBSTR2, TEST_PBSTRLEN2, TEST_PBSTR3, + TEST_PBSTRLEN3)); + + std::cout << "OK" <