scsl/tlvTest.cc

125 lines
2.8 KiB
C++
Raw Permalink Normal View History

2023-10-06 03:13:46 +00:00
#include <cassert>
#include <cstring>
#include <exception>
2023-10-06 03:13:46 +00:00
#include <iostream>
#include "Arena.h"
2023-10-10 02:59:21 +00:00
#include "Test.h"
2023-10-06 03:13:46 +00:00
#include "TLV.h"
2023-10-06 06:17:09 +00:00
#include "testFixtures.h"
2023-10-06 03:13:46 +00:00
2023-10-15 01:38:01 +00:00
using namespace scsl;
2023-10-10 02:59:21 +00:00
2023-10-06 03:13:46 +00:00
2023-10-06 06:17:09 +00:00
static uint8_t arenaBuffer[ARENA_SIZE];
2023-10-06 03:13:46 +00:00
void
2023-10-06 06:17:09 +00:00
tlvTestSuite(Arena &backend)
2023-10-06 03:13:46 +00:00
{
TLV::Record rec1, rec2, rec3, rec4;
uint8_t *cursor = nullptr;
2023-10-06 03:13:46 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "\tSetting first three records." << "\n";
2023-10-06 06:08:35 +00:00
TLV::SetRecord(rec1, 1, TEST_STRLEN1, TEST_STR1);
TLV::SetRecord(rec2, 2, TEST_STRLEN2, TEST_STR2);
TLV::SetRecord(rec3, 1, TEST_STRLEN4, TEST_STR4);
2023-10-06 03:13:46 +00:00
rec4.Tag = 1;
2023-10-10 23:44:29 +00:00
std::cout << "\twriting new rec1" << "\n";
assert(TLV::WriteToMemory(backend, cursor, rec1) != nullptr);
2023-10-10 23:44:29 +00:00
std::cout << "\twriting new rec2" << "\n";
assert((cursor = TLV::WriteToMemory(backend, cursor, rec2)) != nullptr);
2023-10-10 23:44:29 +00:00
std::cout << "\twriting new rec3" << "\n";
assert(TLV::WriteToMemory(backend, cursor, rec3) != nullptr);
cursor = nullptr;
2023-10-06 03:13:46 +00:00
// the cursor should point at the next record,
// and rec4 should contain the same data as rec1.
2023-10-10 23:44:29 +00:00
std::cout << "\tFindTag 1" << "\n";
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != nullptr);
2023-10-15 01:38:01 +00:00
assert(cursor != backend.Start());
2023-10-06 06:17:09 +00:00
assert(cmpRecord(rec1, rec4));
2023-10-06 03:13:46 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "\tFindTag 2" << "\n";
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != nullptr);
2023-10-06 06:17:09 +00:00
assert(cmpRecord(rec3, rec4));
2023-10-06 03:13:46 +00:00
std::cout << "\tSetRecord 1\n";
2023-10-06 06:08:35 +00:00
TLV::SetRecord(rec4, 3, TEST_STRLEN3, TEST_STR3);
assert(TLV::WriteToMemory(backend, nullptr, rec4));
2023-10-06 03:13:46 +00:00
std::cout << "FindTag 3\n";
rec4.Tag = 2;
cursor = TLV::FindTag(backend, nullptr, rec4);
assert(cursor != nullptr);
std::cout << "DeleteRecord\n";
2023-10-06 06:08:35 +00:00
TLV::DeleteRecord(backend, cursor);
assert(cursor[0] == 3);
2023-10-06 03:13:46 +00:00
}
bool
2023-10-06 06:17:09 +00:00
runSuite(Arena &backend, const char *label)
2023-10-06 03:13:46 +00:00
{
2023-10-10 23:44:29 +00:00
std::exception exc;
std::cout << backend << "\n";
2023-10-06 03:13:46 +00:00
std::cout << "running test suite " << label << ": ";
try {
tlvTestSuite(backend);
} catch (std::exception &exc){
2023-10-10 23:44:29 +00:00
std::cout << "FAILED: " << exc.what() << "\n";
2023-10-06 03:13:46 +00:00
return false;
}
2023-10-10 23:44:29 +00:00
std::cout << "OK" << "\n";
2023-10-06 03:13:46 +00:00
std::cout << "\tdestroying arena: ";
backend.Destroy();
2023-10-06 03:13:46 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "OK" << "\n";
2023-10-06 03:13:46 +00:00
return true;
}
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 arenaStatic;
Arena arenaMem;
2023-10-06 03:13:46 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "TESTPROG: " << argv[0] << "\n";
2023-10-06 03:13:46 +00:00
if (-1 == arenaStatic.SetStatic(arenaBuffer, ARENA_SIZE)) {
2023-10-06 03:13:46 +00:00
abort();
2023-10-06 06:17:09 +00:00
} else if (!runSuite(arenaStatic, "arenaStatic")) {
2023-10-06 03:13:46 +00:00
abort();
}
arenaStatic.Clear();
2023-10-06 03:13:46 +00:00
2023-10-06 06:17:09 +00:00
Arena arenaFile;
2023-10-10 23:44:29 +00:00
auto status = arenaFile.Create(ARENA_FILE, ARENA_SIZE);
2023-10-06 06:17:09 +00:00
2023-10-10 23:44:29 +00:00
if (status != 0) {
std::cerr << "Create failed with error " << status << "\n";
2023-10-06 03:13:46 +00:00
abort();
} else if (!runSuite(arenaFile, "arenaFile")) {
2023-10-06 03:13:46 +00:00
abort();
}
if (-1 == arenaMem.SetAlloc(ARENA_SIZE)) {
2023-10-06 03:13:46 +00:00
abort();
2023-10-06 06:17:09 +00:00
} else if (!runSuite(arenaMem, "arenaMem")) {
2023-10-06 03:13:46 +00:00
abort();
}
arenaMem.Clear();
2023-10-06 03:13:46 +00:00
2023-10-10 23:44:29 +00:00
std::cout << "OK" << "\n";
2023-10-06 03:13:46 +00:00
return 0;
}