diff --git a/.gitignore b/.gitignore index 833c481..289c1e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,5 @@ build core core.* cmake-build-* -phonebook_test -tlv_test +dictionaryTest +tlvTest diff --git a/CMakeLists.txt b/CMakeLists.txt index ff3840e..412b972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,11 +17,11 @@ add_compile_options("-DDESKTOP_BUILD") add_library(klib STATIC Arena.cc TLV.cc - Phonebook.cc) + Dictionary.cc) -add_executable(tlv_test tlv_test.cc) +add_executable(tlv_test tlvTest.cc) target_link_libraries(tlv_test klib) -add_executable(phonebook_test phonebook_test.cc) -target_link_libraries(phonebook_test klib) +add_executable(dictionary_test dictionaryTest.cc) +target_link_libraries(dictionary_test klib) diff --git a/Phonebook.cc b/Dictionary.cc similarity index 83% rename from Phonebook.cc rename to Dictionary.cc index e11acf9..6d50552 100644 --- a/Phonebook.cc +++ b/Dictionary.cc @@ -1,13 +1,13 @@ #include #include -#include "Phonebook.h" +#include "Dictionary.h" #if defined(DESKTOP_BUILD) #include #endif bool -Phonebook::Lookup(const char *key, uint8_t klen, TLV::Record &res) +Dictionary::Lookup(const char *key, uint8_t klen, TLV::Record &res) { res.Tag = this->kTag; uint8_t *cursor = TLV::FindTag(this->arena, NULL, res); @@ -30,7 +30,7 @@ Phonebook::Lookup(const char *key, uint8_t klen, TLV::Record &res) int -Phonebook::Set(const char *key, uint8_t klen, const char *val, uint8_t vlen) +Dictionary::Set(const char *key, uint8_t klen, const char *val, uint8_t vlen) { TLV::Record rec; uint8_t *cursor = NULL; @@ -62,7 +62,7 @@ Phonebook::Set(const char *key, uint8_t klen, const char *val, uint8_t vlen) uint8_t * -Phonebook::seek(const char *key, uint8_t klen) +Dictionary::seek(const char *key, uint8_t klen) { TLV::Record rec; @@ -84,14 +84,14 @@ Phonebook::seek(const char *key, uint8_t klen) bool -Phonebook::Has(const char *key, uint8_t klen) +Dictionary::Has(const char *key, uint8_t klen) { return this->seek(key, klen) != NULL; } bool -Phonebook::spaceAvailable(uint8_t klen, uint8_t vlen) +Dictionary::spaceAvailable(uint8_t klen, uint8_t vlen) { size_t required = 0; uintptr_t remaining = 0; @@ -113,13 +113,13 @@ Phonebook::spaceAvailable(uint8_t klen, uint8_t vlen) #if defined(DESKTOP_BUILD) void -Phonebook::DumpKVPairs() +Dictionary::DumpKVPairs() { uint8_t *cursor = (this->arena).Store; TLV::Record rec; TLV::ReadFromMemory(rec, cursor); - std::cout << "Phonebook KV pairs" << std::endl; + std::cout << "Dictionary KV pairs" << std::endl; if (rec.Tag == TAG_EMPTY) { std::cout << "\t(NONE)" << std::endl; return; @@ -137,14 +137,14 @@ Phonebook::DumpKVPairs() } void -Phonebook::DumpToFile(const char *path) +Dictionary::DumpToFile(const char *path) { WriteArena(this->arena, path); } #else void -Phonebook::dump_kvpairs() +Dictionary::dump_kvpairs() { } diff --git a/Phonebook.h b/Dictionary.h similarity index 56% rename from Phonebook.h rename to Dictionary.h index 1f14172..5dec825 100644 --- a/Phonebook.h +++ b/Dictionary.h @@ -1,23 +1,26 @@ -#ifndef KLIB_PHONEBOOK_H -#define KLIB_PHONEBOOK_H +#ifndef KLIB_DICTIONARY_H +#define KLIB_DICTIONARY_H #include "Arena.h" #include "TLV.h" -#define PHONEBOOK_KEY_TAG 1 -#define PHONEBOOK_VAL_TAG 2 +#define DICTIONARY_TAG_KEY 1 +#define DICTIONARY_TAG_VAL 2 - -class Phonebook { +/* + * A Dictionary is a collection of key-value pairs, similar to how + * a dictionary is a mapping of names to definitions. + */ +class Dictionary { public: - Phonebook(Arena &arena) : + Dictionary(Arena &arena) : arena(arena), - kTag(PHONEBOOK_KEY_TAG), - vTag(PHONEBOOK_VAL_TAG) {} ; - Phonebook(Arena &arena, uint8_t kt, uint8_t vt) : + kTag(DICTIONARY_TAG_KEY), + vTag(DICTIONARY_TAG_VAL) {} ; + Dictionary(Arena &arena, uint8_t kt, uint8_t vt) : arena(arena), kTag(kt), vTag(vt) {}; diff --git a/Makefile b/Makefile index 8b58c6c..cb720d5 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ TARGET := klib.a -TESTS := tlv_test phonebook_test +TESTS := tlv_test dictionary_test HEADERS := $(wildcard *.h) SOURCES := $(wildcard *.cc) -OBJS := Arena.o Phonebook.o TLV.o +OBJS := Arena.o Dictionary.o TLV.o CXX := clang++ CXXFLAGS := -g -std=c++14 -Werror -Wall @@ -16,10 +16,10 @@ tags: $(HEADERS) $(SOURCES) $(TARGET): $(OBJS) $(AR) rcs $@ $(OBJS) -tlv_test: tlv_test.o $(TARGET) +tlv_test: tlvTest.o $(TARGET) $(CXX) -o $@ $(CXXFLAGS) $@.o $(TARGET) -phonebook_test: phonebook_test.o $(TARGET) +dictionary_test: dictionaryTest.o $(TARGET) $(CXX) -o $@ $(CXXFLAGS) $@.o $(TARGET) .PHONY: print-% diff --git a/dictionaryTest.cc b/dictionaryTest.cc new file mode 100644 index 0000000..bb6a3a5 --- /dev/null +++ b/dictionaryTest.cc @@ -0,0 +1,104 @@ +#include +#include + +#include "Arena.h" +#include "Dictionary.h" +#include "testFixtures.h" + + +constexpr char TEST_KVSTR1[] = "foo"; +constexpr uint8_t TEST_KVSTRLEN1 = 3; +constexpr char TEST_KVSTR2[] = "baz"; +constexpr uint8_t TEST_KVSTRLEN2 = 3; +constexpr char TEST_KVSTR3[] = "quux"; +constexpr uint8_t TEST_KVSTRLEN3 = 4; +constexpr char TEST_KVSTR4[] = "spam"; +constexpr uint8_t TEST_KVSTRLEN4 = 4; +constexpr char TEST_KVSTR5[] = "xyzzx"; +constexpr uint8_t TEST_KVSTRLEN5 = 5; +constexpr char TEST_KVSTR6[] = "corvid"; +constexpr uint8_t TEST_KVSTRLEN6 = 6; + + +static bool +testSetKV(Dictionary &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; + InitializeArena(arena); + + #if defined(__linux__) + if (CreateArena(arena, ARENA_FILE, ARENA_SIZE, 0644) == -1) { + abort(); + } + #else + if (AllocNewArena(arena, ARENA_SIZE) == -1) { + abort(); + } + #endif + DisplayArena(arena); + + TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN3, TEST_KVSTR3); + + Dictionary dict(arena); + assert(!dict.Has(TEST_KVSTR2, TEST_KVSTRLEN2)); + + assert(testSetKV(dict, TEST_KVSTR1, TEST_KVSTRLEN1, TEST_KVSTR3, + TEST_KVSTRLEN3)); + dict.DumpKVPairs(); + assert(testSetKV(dict, TEST_KVSTR2, TEST_KVSTRLEN2, TEST_KVSTR3, + TEST_KVSTRLEN3)); + dict.DumpKVPairs(); + assert(dict.Has(TEST_KVSTR2, TEST_KVSTRLEN2)); + assert(testSetKV(dict, TEST_KVSTR4, TEST_KVSTRLEN4, TEST_KVSTR5, + TEST_KVSTRLEN5)); + dict.DumpKVPairs(); + assert(dict.Lookup(TEST_KVSTR2, TEST_KVSTRLEN2, value)); + + assert(cmpRecord(value, expect)); + + std::cout << "test overwriting key" << std::endl; + assert(testSetKV(dict, TEST_KVSTR2, TEST_KVSTRLEN2, TEST_KVSTR6, + TEST_KVSTRLEN6)); + dict.DumpKVPairs(); + TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN6, TEST_KVSTR6); + std::cout << "\tlookup" << std::endl; + assert(dict.Lookup(TEST_KVSTR2, TEST_KVSTRLEN2, value)); + std::cout << "\tcompare records" << std::endl; + assert(cmpRecord(value, expect)); + + std::cout << "\tadd new key to dictionary" << std::endl; + assert(testSetKV(dict, TEST_KVSTR3, TEST_KVSTRLEN3, TEST_KVSTR5, + TEST_KVSTRLEN5)); + dict.DumpKVPairs(); + + TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN5, TEST_KVSTR5); + assert(dict.Lookup(TEST_KVSTR4, TEST_KVSTRLEN4, value)); + assert(cmpRecord(value, expect)); + + std::cout << "OK" < -#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; - InitializeArena(arena); - - #if defined(__linux__) - if (CreateArena(arena, ARENA_FILE, ARENA_SIZE, 0644) == -1) { - abort(); - } - #else - if (AllocNewArena(arena, ARENA_SIZE) == -1) { - abort(); - } - #endif - DisplayArena(arena); - - TLV::SetRecord(expect, PHONEBOOK_VAL_TAG, TEST_PBSTRLEN3, TEST_PBSTR3); - - Phonebook pb(arena); - assert(!pb.Has(TEST_PBSTR2, TEST_PBSTRLEN2)); - - assert(test_setpb(pb, TEST_PBSTR1, TEST_PBSTRLEN1, TEST_PBSTR3, - TEST_PBSTRLEN3)); - pb.DumpKVPairs(); - assert(test_setpb(pb, TEST_PBSTR2, TEST_PBSTRLEN2, TEST_PBSTR3, - TEST_PBSTRLEN3)); - pb.DumpKVPairs(); - assert(pb.Has(TEST_PBSTR2, TEST_PBSTRLEN2)); - assert(test_setpb(pb, TEST_PBSTR4, TEST_PBSTRLEN4, TEST_PBSTR5, - TEST_PBSTRLEN5)); - pb.DumpKVPairs(); - assert(pb.Lookup(TEST_PBSTR2, TEST_PBSTRLEN2, value)); - - assert(cmp_record(value, expect)); - - std::cout << "test overwriting key" << std::endl; - assert(test_setpb(pb, TEST_PBSTR2, TEST_PBSTRLEN2, TEST_PBSTR6, - TEST_PBSTRLEN6)); - pb.DumpKVPairs(); - TLV::SetRecord(expect, PHONEBOOK_VAL_TAG, TEST_PBSTRLEN6, TEST_PBSTR6); - std::cout << "\tlookup" << std::endl; - assert(pb.Lookup(TEST_PBSTR2, TEST_PBSTRLEN2, value)); - std::cout << "\tcompare records" << std::endl; - assert(cmp_record(value, expect)); - - std::cout << "\tadd new key to phonebook" << std::endl; - assert(test_setpb(pb, TEST_PBSTR3, TEST_PBSTRLEN3, TEST_PBSTR5, - TEST_PBSTRLEN5)); - pb.DumpKVPairs(); - - TLV::SetRecord(expect, PHONEBOOK_VAL_TAG, TEST_PBSTRLEN5, TEST_PBSTR5); - assert(pb.Lookup(TEST_PBSTR4, TEST_PBSTRLEN4, value)); - assert(cmp_record(value, expect)); - - std::cout << "OK" <