working on testing the phonebook

This commit is contained in:
Kyle Isom 2023-10-05 21:18:21 -07:00
parent 5d2e255bea
commit 82f5e32ddb
5 changed files with 93 additions and 3 deletions

3
.gitignore vendored
View File

@ -2,5 +2,8 @@
*.o
*.bin
build
core
core.*
cmake-build-*
phonebook_test
tlv_test

View File

@ -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 ','\'',$*=$($*))'

View File

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

View File

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

58
phonebook_test.cc Normal file
View File

@ -0,0 +1,58 @@
#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;
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" <<std::endl;
}