scsl/test/dictionary.cc

110 lines
2.7 KiB
C++
Raw Normal View History

2023-10-06 06:17:09 +00:00
#include <iostream>
#include <scsl/Arena.h>
#include <scsl/Dictionary.h>
#include <sctest/Assert.h>
#include "test_fixtures.h"
2023-10-11 01:57:43 +00:00
2023-10-15 01:38:01 +00:00
using namespace scsl;
2023-10-19 07:37:56 +00:00
using namespace sctest;
2023-10-06 06:17:09 +00:00
2023-10-11 01:57:43 +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;
2023-10-06 06:17:09 +00:00
static bool
testSetKV(Dictionary &pb, const char *k, uint8_t kl, const char *v,
uint8_t vl)
{
2023-10-11 01:57:43 +00:00
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-11 01:57:43 +00:00
(void) argc;
(void) argv;
2023-10-09 20:23:03 +00:00
2023-10-11 01:57:43 +00:00
Arena arena;
TLV::Record value;
TLV::Record expect;
2023-10-06 06:17:09 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "TESTPROG: " << argv[0] << "\n";
2023-10-06 06:17:09 +00:00
2023-10-11 01:57:43 +00:00
#if defined(__linux__)
if (arena.Create(ARENA_FILE, ARENA_SIZE) == -1) {
2023-10-06 06:17:09 +00:00
abort();
}
2023-10-11 01:57:43 +00:00
#else
2023-10-10 02:59:21 +00:00
if (arena.SetAlloc(ARENA_SIZE) == -1) {
2023-10-06 06:17:09 +00:00
abort();
}
2023-10-11 01:57:43 +00:00
#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);
2023-10-11 01:57:43 +00:00
Dictionary dict(arena);
2023-10-19 07:37:56 +00:00
Assert(!dict.Contains(TEST_KVSTR2, TEST_KVSTRLEN2));
2023-10-06 06:17:09 +00:00
2023-10-19 07:37:56 +00:00
Assert(testSetKV(dict, TEST_KVSTR1, TEST_KVSTRLEN1, TEST_KVSTR3,
TEST_KVSTRLEN3));
std::cout << dict;
2023-10-19 07:37:56 +00:00
Assert(testSetKV(dict, TEST_KVSTR2, TEST_KVSTRLEN2, TEST_KVSTR3,
TEST_KVSTRLEN3));
std::cout << dict;
2023-10-19 07:37:56 +00:00
Assert(dict.Contains(TEST_KVSTR2, TEST_KVSTRLEN2));
Assert(testSetKV(dict, TEST_KVSTR4, TEST_KVSTRLEN4, TEST_KVSTR5,
TEST_KVSTRLEN5));
std::cout << dict;
2023-10-19 07:37:56 +00:00
Assert(dict.Lookup(TEST_KVSTR2, TEST_KVSTRLEN2, value));
2023-10-06 06:17:09 +00:00
2023-10-19 07:37:56 +00:00
Assert(cmpRecord(value, expect));
2023-10-06 06:17:09 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "test overwriting key" << "\n";
2023-10-19 07:37:56 +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-19 07:37:56 +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-19 07:37:56 +00:00
Assert(cmpRecord(value, expect));
2023-10-06 06:17:09 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "\tadd new key to dictionary" << "\n";
2023-10-19 07:37:56 +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);
2023-10-19 07:37:56 +00:00
Assert(dict.Lookup(TEST_KVSTR4, TEST_KVSTRLEN4, value));
Assert(cmpRecord(value, expect));
2023-10-06 06:17:09 +00:00
2023-10-11 01:57:43 +00:00
std::cout << "OK" << "\n";
2023-10-06 06:17:09 +00:00
// Dump the generated arena for inspection later.
2023-10-11 01:57:43 +00:00
#if defined(__linux__)
#else
2023-10-06 06:17:09 +00:00
dict.DumpToFile(ARENA_FILE);
2023-10-11 01:57:43 +00:00
#endif
2023-10-06 06:17:09 +00:00
arena.Clear();
std::cout << dict;
2023-10-06 06:17:09 +00:00
}