scsl/tlvTest.cc

116 lines
2.4 KiB
C++
Raw Normal View History

2023-10-06 03:13:46 +00:00
#include <cassert>
#include <cstring>
#include <iostream>
#include "Arena.h"
#include "TLV.h"
2023-10-06 06:17:09 +00:00
#include "testFixtures.h"
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
bool
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 = NULL;
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-06 06:08:35 +00:00
assert(TLV::WriteToMemory(backend, cursor, rec1) != NULL);
assert((cursor = TLV::WriteToMemory(backend, cursor, rec2)) != NULL);
assert(TLV::WriteToMemory(backend, cursor, rec3) != NULL);
2023-10-06 03:13:46 +00:00
cursor = NULL;
// the cursor should point at the next record,
// and rec4 should contain the same data as rec1.
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(backend, cursor, rec4);
2023-10-06 03:13:46 +00:00
assert(cursor != NULL);
2023-10-06 06:08:35 +00:00
assert(cursor != backend.Store);
2023-10-06 06:17:09 +00:00
assert(cmpRecord(rec1, rec4));
2023-10-06 03:13:46 +00:00
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(backend, cursor, rec4);
2023-10-06 03:13:46 +00:00
assert(cursor != NULL);
2023-10-06 06:17:09 +00:00
assert(cmpRecord(rec3, rec4));
2023-10-06 03:13:46 +00:00
2023-10-06 06:08:35 +00:00
TLV::SetRecord(rec4, 3, TEST_STRLEN3, TEST_STR3);
assert(TLV::WriteToMemory(backend, NULL, rec4));
2023-10-06 03:13:46 +00:00
rec4.Tag = 2;
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(backend, NULL, rec4);
assert(cursor != NULL);
2023-10-06 06:08:35 +00:00
TLV::DeleteRecord(backend, cursor);
assert(cursor[0] == 3);
2023-10-06 03:13:46 +00:00
return true;
}
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-06 06:08:35 +00:00
DisplayArena(backend);
2023-10-06 03:13:46 +00:00
std::cout << "running test suite " << label << ": ";
2023-10-06 06:17:09 +00:00
if (!tlvTestSuite(backend)) {
2023-10-06 03:13:46 +00:00
std::cout << "FAILED" << std::endl;
return false;
}
std::cout << "OK" << std::endl;
std::cout << "\tdestroying arena: ";
2023-10-06 06:08:35 +00:00
if (DestroyArena(backend) != 0) {
2023-10-06 03:13:46 +00:00
std::cout << "FAILED" << std::endl;
return false;
}
std::cout << "OK" << std::endl;
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-06 06:08:35 +00:00
std::cout << "TESTPROG: " << argv[0] << std::endl;
2023-10-06 06:17:09 +00:00
InitializeArena(arenaStatic);
InitializeArena(arenaMem);
2023-10-06 03:13:46 +00:00
2023-10-06 06:17:09 +00:00
if (-1 == NewStaticArena(arenaStatic, 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();
}
2023-10-06 06:17:09 +00:00
ClearArena(arenaStatic);
2023-10-06 03:13:46 +00:00
2023-10-06 06:08:35 +00:00
#if defined(__linux__)
2023-10-06 06:17:09 +00:00
Arena arenaFile;
InitializeArena(arenaFile);
if (-1 == CreateArena(arenaFile, ARENA_FILE, ARENA_SIZE, 0644)) {
2023-10-06 03:13:46 +00:00
abort();
} else if (!runSuite(arenaFile, "arenaFile")) {
2023-10-06 03:13:46 +00:00
abort();
}
2023-10-06 06:08:35 +00:00
#endif
2023-10-06 03:13:46 +00:00
2023-10-06 06:17:09 +00:00
if (-1 == AllocNewArena(arenaMem, 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();
}
std::cout << "OK" << std::endl;
return 0;
}