Further code cleanups and documentation.

- Coverity defects.
- Documentation.
This commit is contained in:
2023-10-20 01:31:06 -07:00
parent 2a23d2e204
commit 4eb4008130
16 changed files with 254 additions and 99 deletions

View File

@@ -99,7 +99,7 @@ Point2D::Rotate(std::vector<Polar2D> vertices, double theta)
{
std::vector<Point2D> rotated;
for (auto v : vertices) {
for (auto& v : vertices) {
Point2D p;
v.RotateAround(*this, p, theta);
rotated.push_back(p) ;

View File

@@ -111,12 +111,12 @@ Arena::Open(const char *path)
this->Destroy();
}
if (stat(path, &st) != 0) {
this->fd = open(path, O_RDWR);
if (this->fd == -1) {
return -1;
}
this->fd = open(path, O_RDWR);
if (this->fd == -1) {
if (stat(path, &st) != 0) {
return -1;
}
@@ -152,6 +152,10 @@ Arena::Create(const char *path, size_t fileSize)
bool
Arena::CursorInArena(const uint8_t *cursor)
{
if (cursor == nullptr) {
return false;
}
if (cursor < this->store) {
return false;
}
@@ -188,28 +192,26 @@ Arena::Destroy()
}
switch (this->arenaType) {
case ArenaType::Static:
break;
case ArenaType::Alloc:
delete[] this->store;
break;
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
case ArenaType::MemoryMapped:
if (munmap(this->store, this->size) == -1) {
abort();
return;
}
if (close(this->fd) == -1) {
abort();
}
this->fd = 0;
break;
#endif
default:
#if defined(NDEBUG)
case ArenaType::Static:
break;
case ArenaType::Alloc:
delete[] this->store;
break;
case ArenaType::MemoryMapped:
if (munmap(this->store, this->size) == -1) {
abort();
return;
}
if (close(this->fd) == -1) {
abort();
}
this->fd = 0;
break;
default:
#if defined(NDEBUG)
return;
#else
abort();
#endif
@@ -241,11 +243,9 @@ operator<<(std::ostream &os, Arena &arena)
case ArenaType::Alloc:
os << "allocated";
break;
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
case ArenaType::MemoryMapped:
os << "mmap/file";
break;
#endif
case ArenaType::MemoryMapped:
os << "mmap/file";
break;
default:
os << "unknown (this is a bug)";
}
@@ -263,15 +263,10 @@ operator<<(std::ostream &os, Arena &arena)
int
Arena::Write(const char *path)
{
FILE *arenaFile = nullptr;
int retc = -1;
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
arenaFile = fopen(path, "w");
FILE *arenaFile = fopen(path, "w");
if (arenaFile == nullptr) {
#else
if (fopen_s(&arenaFile, path, "w") != 0) {
#endif
return -1;
}

View File

@@ -63,7 +63,7 @@ NewFlag(std::string fName, FlagType fType, std::string fDescription)
flag->Type = fType;
flag->WasSet = false;
flag->Name = std::move(fName);
flag->Description = fDescription;
flag->Description = std::move(fDescription);
flag->Value = FlagValue{};
return flag;
@@ -114,7 +114,7 @@ Flags::Register(std::string fName, FlagType fType, std::string fDescription)
bool
Flags::Register(std::string fName, bool defaultValue, std::string fDescription)
{
if (!this->Register(fName, FlagType::Boolean, fDescription)) {
if (!this->Register(fName, FlagType::Boolean, std::move(fDescription))) {
return false;
}
@@ -164,7 +164,7 @@ Flags::Register(std::string fName, size_t defaultValue, std::string fDescription
bool
Flags::Register(std::string fName, std::string defaultValue, std::string fDescription)
{
if (!this->Register(fName, FlagType::String, fDescription)) {
if (!this->Register(fName, FlagType::String, std::move(fDescription))) {
return false;
}

View File

@@ -38,7 +38,7 @@ namespace S {
std::vector<std::string>
SplitKeyValuePair(std::string line, std::string delimiter)
{
auto pair = SplitN(std::move(line), delimiter, 2);
auto pair = SplitN(std::move(line), std::move(delimiter), 2);
if (pair.size() == 0) {
return {"", ""};
@@ -198,7 +198,7 @@ WriteTabIndented(std::ostream &os, std::string line, size_t maxLength,
int tabStop, bool indentFirst)
{
auto lines = WrapText(line, maxLength);
WriteTabIndented(os, lines, tabStop, indentFirst);
WriteTabIndented(os, std::move(lines), tabStop, indentFirst);
}

View File

@@ -25,12 +25,13 @@
#include <scsl/TLV.h>
using namespace scsl;
/// REC_SIZE calculates the total length of a TLV record, including the
/// two byte header.
#define REC_SIZE(x) ((std::size_t)x.Len + 2)
#define REC_SIZE(x) ((std::size_t)x.Len + 2)
namespace scsl {
@@ -100,6 +101,10 @@ SetRecord(Record &rec, uint8_t tag, uint8_t len, const char *val)
void
ReadFromMemory(Record &rec, uint8_t *cursor)
{
assert(cursor != nullptr);
if (cursor == nullptr) {
return;
}
rec.Tag = cursor[0];
rec.Len = cursor[1];
memcpy(rec.Val, cursor + 2, rec.Len);
@@ -117,9 +122,10 @@ FindTag(Arena &arena, uint8_t *cursor, Record &rec)
cursor = LocateTag(arena, cursor, rec);
if (rec.Tag != TAG_EMPTY) {
cursor = SkipRecord(rec, cursor);
if (!arena.CursorInArena(cursor)) {
cursor = nullptr;
}
}
if (!arena.CursorInArena(cursor)) {
cursor = nullptr;
}
return cursor;
@@ -129,18 +135,19 @@ FindTag(Arena &arena, uint8_t *cursor, Record &rec)
uint8_t *
LocateTag(Arena &arena, uint8_t *cursor, Record &rec)
{
uint8_t tag, len;
if (!arena.CursorInArena(cursor)) {
cursor = nullptr;
}
uint8_t tag = TAG_EMPTY;
uint8_t len;
if (cursor == nullptr) {
cursor = arena.Start();
}
while (((tag = cursor[0]) != rec.Tag) &&
(arena.CursorInArena(cursor))) {
if (!arena.CursorInArena(cursor)) {
cursor = arena.Start();
}
while (arena.CursorInArena(cursor) &&
((tag = cursor[0]) != rec.Tag)) {
assert(arena.CursorInArena(cursor));
len = cursor[1];
if (!spaceAvailable(arena, cursor, len)) {
@@ -193,7 +200,7 @@ DeleteRecord(Arena &arena, uint8_t *cursor)
return;
}
uint8_t len = cursor[1] + 2;
uint8_t len = cursor[1] + 2;
uint8_t *stop = arena.Start() + arena.Size();
stop -= len;

View File

@@ -1,8 +1,8 @@
///
/// \file Test.cc
/// \file src/sctest/Assert.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-09
/// \brief Tooling to assist in building test programs..
/// \brief Assertion tooling useful in building test programs.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///

View File

@@ -1,5 +1,5 @@
///
/// \file Exceptions.cc
/// \file src/test/Exceptions.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-10
/// \brief Custom exceptions used in writing test programs.
@@ -26,7 +26,7 @@
namespace sctest {
AssertionFailed::AssertionFailed(std::string message) : msg(message) {}
AssertionFailed::AssertionFailed(std::string message) : msg(std::move(message)) {}
const char *

View File

@@ -45,6 +45,13 @@ Report::Failing() const
}
size_t
Report::Passing() const
{
return this->passed;
}
size_t
Report::Total() const
{
@@ -59,6 +66,13 @@ Report::Failed()
}
void
Report::Passed()
{
this->passed++;
}
void
Report::AddTest(size_t testCount)
{
@@ -71,6 +85,7 @@ Report::Reset(size_t testCount)
{
auto now = std::chrono::steady_clock::now();
this->total = testCount;
this->passed = 0;
this->failing = 0;
this->Start();
@@ -105,13 +120,18 @@ operator<<(std::ostream &os, const Report &report)
{
auto elapsed = report.Elapsed();
os << report.Total() - report.Failing() << "/"
os << report.Passing() << "/"
<< report.Total() << " tests passed in "
<< std::setw(3) << elapsed.count() << "ms";
auto failed = report.Failing();
if (failed > 0) {
os << " (" << failed << " tests failed)";
}
return os;
}
} // end namespace sctest
} // end namespace sctest

View File

@@ -53,7 +53,7 @@ SimpleSuite::Silence()
void
SimpleSuite::AddTest(std::string name, std::function<bool()> test)
{
const UnitTest test_case = {std::move(name), test};
const UnitTest test_case = {std::move(name), std::move(test), true};
tests.push_back(test_case);
}
@@ -61,8 +61,7 @@ SimpleSuite::AddTest(std::string name, std::function<bool()> test)
void
SimpleSuite::AddFailingTest(std::string name, std::function<bool()> test)
{
// auto ntest = [&test]() { return !test(); };
const UnitTest test_case = {std::move(name), [&test]() { return !test(); }};
const UnitTest test_case = {std::move(name), test, false};
tests.push_back(test_case);
}
@@ -87,14 +86,19 @@ SimpleSuite::Run()
<< testCase.name << ": ";
}
this->hasPassed = testCase.test();
this->hasPassed = (testCase.test() == testCase.expect);
if (this->hasPassed) {
report.Passed();
} else {
report.Failed();
}
if (quiet) { continue; }
if (this->hasPassed) {
std::cout << "[PASS]";
} else {
std::cout << "[FAIL]";
report.Failed();
}
std::cout << "\n";
}
@@ -142,4 +146,4 @@ operator<<(std::ostream &os, SimpleSuite &suite)
}
} // end namespace sctest
} // end namespace sctest