Rename phonebook -> dictionary.

This commit is contained in:
Kyle Isom 2023-10-05 23:17:09 -07:00
parent 0d28baef0e
commit f727ce4b5a
9 changed files with 161 additions and 157 deletions

4
.gitignore vendored
View File

@ -5,5 +5,5 @@ build
core
core.*
cmake-build-*
phonebook_test
tlv_test
dictionaryTest
tlvTest

View File

@ -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)

View File

@ -1,13 +1,13 @@
#include <cstring>
#include <cstdlib>
#include "Phonebook.h"
#include "Dictionary.h"
#if defined(DESKTOP_BUILD)
#include <iostream>
#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()
{
}

View File

@ -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) {};

View File

@ -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-%

104
dictionaryTest.cc Normal file
View File

@ -0,0 +1,104 @@
#include <cassert>
#include <iostream>
#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" <<std::endl;
// Dump the generated arena for inspection later.
#if defined(__linux__)
#else
dict.DumpToFile(ARENA_FILE);
#endif
ClearArena(arena);
dict.DumpKVPairs();
}

View File

@ -1,104 +0,0 @@
#include <cassert>
#include <iostream>
#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" <<std::endl;
// Dump the generated arena for inspection later.
#if defined(__linux__)
#else
pb.DumpToFile(ARENA_FILE);
#endif
ClearArena(arena);
pb.DumpKVPairs();
}

View File

@ -1,5 +1,5 @@
#ifndef KLIB_TEST_FIXTURES_H
#define KLIB_TEST_FIXTURES_H
#ifndef KLIB_TESTFIXTURES_H
#define KLIB_TESTFIXTURES_H
#define ARENA_SIZE 128
@ -18,7 +18,7 @@
#define TEST_STRLEN4 35
static bool
cmp_record(TLV::Record &a, TLV::Record &b)
cmpRecord(TLV::Record &a, TLV::Record &b)
{
if (a.Tag != b .Tag) {
return false;

View File

@ -5,14 +5,14 @@
#include "Arena.h"
#include "TLV.h"
#include "test_fixtures.h"
#include "testFixtures.h"
static uint8_t arena_buffer[ARENA_SIZE];
static uint8_t arenaBuffer[ARENA_SIZE];
bool
tlv_test_suite(Arena &backend)
tlvTestSuite(Arena &backend)
{
TLV::Record rec1, rec2, rec3, rec4;
uint8_t *cursor = NULL;
@ -32,11 +32,11 @@ tlv_test_suite(Arena &backend)
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != NULL);
assert(cursor != backend.Store);
assert(cmp_record(rec1, rec4));
assert(cmpRecord(rec1, rec4));
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != NULL);
assert(cmp_record(rec3, rec4));
assert(cmpRecord(rec3, rec4));
TLV::SetRecord(rec4, 3, TEST_STRLEN3, TEST_STR3);
assert(TLV::WriteToMemory(backend, NULL, rec4));
@ -52,12 +52,12 @@ tlv_test_suite(Arena &backend)
}
bool
run_suite(Arena &backend, const char *label)
runSuite(Arena &backend, const char *label)
{
DisplayArena(backend);
std::cout << "running test suite " << label << ": ";
if (!tlv_test_suite(backend)) {
if (!tlvTestSuite(backend)) {
std::cout << "FAILED" << std::endl;
return false;
}
@ -77,33 +77,34 @@ run_suite(Arena &backend, const char *label)
int
main(int argc, const char *argv[])
{
Arena arena_static;
Arena arena_mem;
Arena arena_file;
Arena arenaStatic;
Arena arenaMem;
std::cout << "TESTPROG: " << argv[0] << std::endl;
InitializeArena(arena_static);
InitializeArena(arena_mem);
InitializeArena(arena_file);
InitializeArena(arenaStatic);
InitializeArena(arenaMem);
if (-1 == NewStaticArena(arena_static, arena_buffer, ARENA_SIZE)) {
if (-1 == NewStaticArena(arenaStatic, arenaBuffer, ARENA_SIZE)) {
abort();
} else if (!run_suite(arena_static, "arena_static")) {
} else if (!runSuite(arenaStatic, "arenaStatic")) {
abort();
}
ClearArena(arena_static);
ClearArena(arenaStatic);
#if defined(__linux__)
if (-1 == CreateArena(arena_file, ARENA_FILE, ARENA_SIZE, 0644)) {
Arena arenaFile;
InitializeArena(arenaFile);
if (-1 == CreateArena(arenaFile, ARENA_FILE, ARENA_SIZE, 0644)) {
abort();
} else if (!run_suite(arena_file, "arena_file")) {
} else if (!run_suite(arenaFile, "arenaFile")) {
abort();
}
#endif
if (-1 == AllocNewArena(arena_mem, ARENA_SIZE)) {
if (-1 == AllocNewArena(arenaMem, ARENA_SIZE)) {
abort();
} else if (!run_suite(arena_mem, "arena_mem")) {
} else if (!runSuite(arenaMem, "arenaMem")) {
abort();
}