scsl/dictionaryTest.cc

106 lines
2.7 KiB
C++
Raw Normal View History

2023-10-06 06:17:09 +00:00
#include <cassert>
#include <iostream>
#include "Arena.h"
#include "Dictionary.h"
#include "testFixtures.h"
using namespace klib;
2023-10-06 06:17:09 +00:00
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;
2023-10-10 23:44:29 +00:00
std::cout << "test Set " << k << "->" << v << "\n";
2023-10-06 06:17:09 +00:00
ok = pb.Set(k, kl, v, vl) == 0;
std::cout << "\tSet complete\n";
return ok;
}
int
main(int argc, const char *argv[])
{
2023-10-09 20:23:03 +00:00
(void)argc; (void)argv;
2023-10-06 06:17:09 +00:00
Arena arena;
TLV::Record value;
TLV::Record expect;
2023-10-10 23:44:29 +00:00
std::cout << "TESTPROG: " << argv[0] << "\n";
2023-10-06 06:17:09 +00:00
#if defined(__linux__)
if (arena.Create(ARENA_FILE, ARENA_SIZE) == -1) {
2023-10-06 06:17:09 +00:00
abort();
}
#else
2023-10-10 02:59:21 +00:00
if (arena.SetAlloc(ARENA_SIZE) == -1) {
2023-10-06 06:17:09 +00:00
abort();
}
#endif
2023-10-10 23:44:29 +00:00
std::cout << arena << "\n";
2023-10-06 06:17:09 +00:00
TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN3, TEST_KVSTR3);
Dictionary dict(arena);
assert(!dict.Contains(TEST_KVSTR2, TEST_KVSTRLEN2));
2023-10-06 06:17:09 +00:00
assert(testSetKV(dict, TEST_KVSTR1, TEST_KVSTRLEN1, TEST_KVSTR3,
TEST_KVSTRLEN3));
std::cout << dict;
2023-10-06 06:17:09 +00:00
assert(testSetKV(dict, TEST_KVSTR2, TEST_KVSTRLEN2, TEST_KVSTR3,
TEST_KVSTRLEN3));
std::cout << dict;
assert(dict.Contains(TEST_KVSTR2, TEST_KVSTRLEN2));
2023-10-06 06:17:09 +00:00
assert(testSetKV(dict, TEST_KVSTR4, TEST_KVSTRLEN4, TEST_KVSTR5,
TEST_KVSTRLEN5));
std::cout << dict;
2023-10-06 06:17:09 +00:00
assert(dict.Lookup(TEST_KVSTR2, TEST_KVSTRLEN2, value));
assert(cmpRecord(value, expect));
2023-10-10 23:44:29 +00:00
std::cout << "test overwriting key" << "\n";
2023-10-06 06:17:09 +00:00
assert(testSetKV(dict, TEST_KVSTR2, TEST_KVSTRLEN2, TEST_KVSTR6,
TEST_KVSTRLEN6));
std::cout << dict;
2023-10-06 06:17:09 +00:00
TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN6, TEST_KVSTR6);
2023-10-10 23:44:29 +00:00
std::cout << "\tlookup" << "\n";
2023-10-06 06:17:09 +00:00
assert(dict.Lookup(TEST_KVSTR2, TEST_KVSTRLEN2, value));
2023-10-10 23:44:29 +00:00
std::cout << "\tcompare records" << "\n";
2023-10-06 06:17:09 +00:00
assert(cmpRecord(value, expect));
2023-10-10 23:44:29 +00:00
std::cout << "\tadd new key to dictionary" << "\n";
2023-10-06 06:17:09 +00:00
assert(testSetKV(dict, TEST_KVSTR3, TEST_KVSTRLEN3, TEST_KVSTR5,
TEST_KVSTRLEN5));
std::cout << dict;
2023-10-06 06:17:09 +00:00
TLV::SetRecord(expect, DICTIONARY_TAG_VAL, TEST_KVSTRLEN5, TEST_KVSTR5);
assert(dict.Lookup(TEST_KVSTR4, TEST_KVSTRLEN4, value));
assert(cmpRecord(value, expect));
2023-10-10 23:44:29 +00:00
std::cout << "OK" <<"\n";
2023-10-06 06:17:09 +00:00
// Dump the generated arena for inspection later.
#if defined(__linux__)
#else
dict.DumpToFile(ARENA_FILE);
#endif
arena.Clear();
std::cout << dict;
2023-10-06 06:17:09 +00:00
}