Restructure project, start importing sc3 code.

This commit is contained in:
2023-10-18 23:44:05 -07:00
parent 3122ed6ac7
commit 5f3dc6e9f6
46 changed files with 2300 additions and 66 deletions

59
test/buffer.cc Normal file
View File

@@ -0,0 +1,59 @@
#include <cassert>
#include <iostream>
#include <scsl/Buffer.h>
using namespace scsl;
int
main(int argc, char *argv[])
{
(void) argc;
(void) argv;
Buffer buffer("hlo, world");
Buffer helloWorld("hello, world!");
Buffer goodbyeWorld("goodbye, world");
Buffer goodbyeCruelWorld("goodbye, cruel world");
std::cout << buffer << "\n";
buffer.Insert(1, (uint8_t *) "el", 2);
buffer.Append('!');
assert(buffer == helloWorld);
std::cout << buffer << "\n";
buffer.Remove(buffer.Length() - 1);
std::cout << buffer << "\n";
buffer.Remove(0, 5);
std::cout << buffer << "\n";
buffer.Insert(0, 'g');
std::cout << buffer << "\n";
buffer.Insert(1, (uint8_t *) "oodbye", 6);
std::cout << buffer << "\n";
assert(buffer == goodbyeWorld);
buffer.Insert(9, (uint8_t *)"cruel ", 6);
std::cout << buffer << "\n";
buffer.HexDump(std::cout);
buffer.Reclaim();
buffer.Append("and now for something completely different...");
std::cout << buffer.Contents() << "\n";
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << "\n";
buffer.Resize(128);
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << "\n";
buffer.Trim();
std::cout << "Length: " << buffer.Length() << ", capacity " << buffer.Capacity() << "\n";
Buffer buffer2("and now for something completely different...");
assert(buffer == buffer2);
buffer2.Remove(buffer2.Length()-3, 3);
std::cout << buffer << "\n";
assert(buffer != buffer2);
return 0;
}

108
test/dictionary.cc Normal file
View File

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

61
test/flag.cc Normal file
View File

@@ -0,0 +1,61 @@
//
// Created by kyle on 10/15/23.
//
#include <iostream>
#include <scsl/Flag.h>
#include <sctest/Assert.h>
using namespace scsl;
int
main(int argc, char *argv[])
{
bool testFlag = false;
size_t testSize = 0;
unsigned int testUnsigned = 0;
int testInteger = 0;
std::string testString;
auto flags = new Flags("flag_test", "this is a test of the flag functionality. This line is particularly long to make sure the wrapping works.");
flags->Register("-b", FlagType::Boolean, "test boolean");
flags->Register("-s", FlagType::String, "test string");
flags->Register("-u", (unsigned int)42, "test unsigned integer with a long description line. This should trigger multiline text-wrapping.");
flags->Register("-i", -42, "test integer");
flags->Register("-size", FlagType::SizeT, "test size_t");
TestAssert(flags->Size() == 5, "flags weren't registered");
auto status = flags->Parse(argc, argv);
if (status != Flags::ParseStatus::OK) {
std::cerr << "failed to parse flags: "
<< Flags::ParseStatusToString(status) << "\n";
exit(1);
}
auto wasSet = flags->GetBool("-b", testFlag);
std::cout << " (bool) test flag was set: " << wasSet << "\n";
std::cout << " (bool) test flag value: " << testFlag << "\n";
wasSet = flags->GetInteger("-i", testInteger);
std::cout << " (int) test flag was set: " << wasSet << "\n";
std::cout << " (int) test flag value: " << testInteger << "\n";
wasSet = flags->GetUnsignedInteger("-u", testUnsigned);
std::cout << " (uint) test flag was set: " << wasSet << "\n";
std::cout << " (uint) test flag value: " << testUnsigned << "\n";
wasSet = flags->GetSizeT("-size", testSize);
std::cout << "(size_t) test flag was set: " << wasSet << "\n";
std::cout << "(size_t) test flag value: " << testSize << "\n";
wasSet = flags->GetString("-s", testString);
std::cout << "(string) test flag was set: " << wasSet << "\n";
std::cout << "(string) test flag value: " << testString << "\n";
delete flags;
return 0;
}

72
test/simple_suite_example.cc Executable file
View File

@@ -0,0 +1,72 @@
//
// Project: scccl
// File: test/math/simple_suite_example.cpp
// Author: Kyle Isom
// Date: 2017-06-05
//
// simple_suite_example demonstrates the usage of the SimpleSuite test class
// and serves to unit test the unit tester (qui custodiet ipsos custodes)?
//
// Copyright 2017 Kyle Isom <kyle@imap.cc>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <sctest/SimpleSuite.h>
static bool
prepareTests()
{
std::cout << "time passes...\n";
std::cout << "tests are ready.\n";
return true;
}
static bool
destroyTests()
{
std::cout << "time passes...\n" ;
std::cout << "tests have been destroyed.\n";
return true;
}
static bool addOne() { return 1 + 1 == 2; }
static bool four() { return 2 + 2 == 4; }
static bool nope() { return 2 + 2 == 5; }
int
main()
{
sctest::SimpleSuite TestSuite;
TestSuite.Setup(prepareTests);
TestSuite.Teardown(destroyTests);
TestSuite.AddTest("1 + 1", addOne);
TestSuite.AddTest("fourness", four);
TestSuite.AddFailingTest("self-evident truth", nope);
bool result = TestSuite.Run();
if (TestSuite.IsReportReady()) {
auto report = TestSuite.GetReport();
std::cout << report.Failing << " / " << report.Total;
std::cout << " tests failed.\n";
}
if (result) {
return 0;
}
else {
return 1;
}
}

152
test/stringutil.cc Normal file
View File

@@ -0,0 +1,152 @@
///
/// \file stringutil_test.cc
/// \author kyle
/// \date 10/14/23
/// \brief Ensure the stringutil functions work.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that the
/// above copyright notice and this permission notice appear in all copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
/// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
/// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
/// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
/// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
/// SOFTWARE.
///
#include <iostream>
#include <sstream>
#include <scsl/StringUtil.h>
#include <sctest/Assert.h>
using namespace scsl;
static void
TestTrimming(std::string line, std::string lExpected, std::string rExpected, std::string expected)
{
std::string result;
std::string message;
result = U::S::TrimLeadingWhitespaceDup(line);
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == lExpected, message);
result = U::S::TrimTrailingWhitespaceDup(line);
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == rExpected, message);
result = U::S::TrimWhitespaceDup(line);
message = "TrimDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == expected, message);
result = line;
U::S::TrimLeadingWhitespace(result);
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == lExpected, message);
result = line;
U::S::TrimTrailingWhitespace(result);
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == rExpected, message);
result = line;
U::S::TrimWhitespace(result);
message = "TrimDup(\"" + line + "\"): '" + result + "'";
TestAssert(result == expected, message);
}
static std::string
vec2string(std::vector<std::string> v)
{
std::stringstream ss;
ss << "(";
ss << v.size();
ss << ")";
ss << "{";
for (size_t i = 0; i < v.size(); i++) {
if (i > 0) { ss << ", "; }
ss << v[i];
}
ss << "}";
return ss.str();
}
static void
TestSplit(std::string line, std::string delim, size_t maxCount, std::vector<std::string> expected)
{
std::cout << "test split\n";
std::cout << "\t line: \"" << line << "\"\n";
std::cout << "\t delim: \"" << delim << "\"\n";
std::cout << "\t count: " << maxCount << "\n";
std::cout << "\texpect: " << vec2string(expected) << "\n";
auto result = U::S::SplitN(line, delim, maxCount);
std::cout << "\tresult: " << U::S::VectorToString(result) << "\n";
TestAssert(result == expected, U::S::VectorToString(result));
std::cout << "OK!\n";
}
static void
TestWrapping()
{
std::string testLine = "A much longer line, something that can be tested with WrapText. ";
testLine += "Does it handle puncuation? I hope so.";
std::vector<std::string> expected{
"A much longer",
"line, something",
"that can be",
"tested with",
"WrapText. Does",
"it handle",
"puncuation? I",
"hope so.",
};
auto wrapped = U::S::WrapText(testLine, 16);
TestAssert(wrapped.size() == expected.size(),
U::S::VectorToString(wrapped) + " != " + U::S::VectorToString(expected));
for (size_t i = 0; i < wrapped.size(); i++) {
TestAssert(wrapped[i] == expected[i],
"\"" + wrapped[i] + "\" != \"" + expected[i] + "\"");
}
U::S::WriteTabIndented(std::cout, wrapped, 4, true);
}
int
main()
{
TestTrimming(" foo\t ", "foo\t ", " foo", "foo");
TestTrimming(" foo\tbar ", "foo\tbar ", " foo\tbar", "foo\tbar");
TestSplit("abc:def:ghij:klm", ":", 0,
std::vector<std::string>{"abc", "def", "ghij", "klm"});
TestSplit("abc:def:ghij:klm", ":", 3,
std::vector<std::string>{"abc", "def", "ghij:klm"});
TestSplit("abc:def:ghij:klm", ":", 2,
std::vector<std::string>{"abc", "def:ghij:klm"});
TestSplit("abc:def:ghij:klm", ":", 1,
std::vector<std::string>{"abc:def:ghij:klm"});
TestSplit("abc::def:ghi", ":", 0,
std::vector<std::string>{"abc", "", "def", "ghi"});
TestWrapping();
}

50
test/test_fixtures.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef SCSL_TESTFIXTURES_H
#define SCSL_TESTFIXTURES_H
#include <string.h>
#include <scsl/TLV.h>
#define ARENA_SIZE 128
#define ARENA_FILE "arena_test.bin"
/* strlen=13 */
#define TEST_STR1 "Hello, world"
#define TEST_STRLEN1 13
#define TEST_STR2 "Bye, world!!"
#define TEST_STRLEN2 13
#define TEST_STR3 "Hello, arena"
#define TEST_STRLEN3 13
/* strlen 35 */
#define TEST_STR4 "How is a raven like a writing desk?"
#define TEST_STRLEN4 35
namespace scsl {
static bool
cmpRecord(TLV::Record &a, TLV::Record &b)
{
if (a.Tag != b.Tag) {
return false;
}
if (a.Len != b.Len) {
return false;
}
if (memcmp(a.Val, b.Val, a.Len) != 0) {
return false;
}
return true;
}
} // namespace scsl
#endif // SCSL_TESTFIXTURES_H

124
test/tlv.cc Normal file
View File

@@ -0,0 +1,124 @@
#include <cassert>
#include <cstring>
#include <exception>
#include <iostream>
#include <scsl/Arena.h>
#include <scsl/TLV.h>
#include <sctest/Assert.h>
#include "test_fixtures.h"
using namespace scsl;
static uint8_t arenaBuffer[ARENA_SIZE];
void
tlvTestSuite(Arena &backend)
{
TLV::Record rec1, rec2, rec3, rec4;
uint8_t *cursor = nullptr;
std::cout << "\tSetting first three records." << "\n";
TLV::SetRecord(rec1, 1, TEST_STRLEN1, TEST_STR1);
TLV::SetRecord(rec2, 2, TEST_STRLEN2, TEST_STR2);
TLV::SetRecord(rec3, 1, TEST_STRLEN4, TEST_STR4);
rec4.Tag = 1;
std::cout << "\twriting new rec1" << "\n";
assert(TLV::WriteToMemory(backend, cursor, rec1) != nullptr);
std::cout << "\twriting new rec2" << "\n";
assert((cursor = TLV::WriteToMemory(backend, cursor, rec2)) != nullptr);
std::cout << "\twriting new rec3" << "\n";
assert(TLV::WriteToMemory(backend, cursor, rec3) != nullptr);
cursor = nullptr;
// the cursor should point at the next record,
// and rec4 should contain the same data as rec1.
std::cout << "\tFindTag 1" << "\n";
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != nullptr);
assert(cursor != backend.Start());
assert(cmpRecord(rec1, rec4));
std::cout << "\tFindTag 2" << "\n";
cursor = TLV::FindTag(backend, cursor, rec4);
assert(cursor != nullptr);
assert(cmpRecord(rec3, rec4));
std::cout << "\tSetRecord 1\n";
TLV::SetRecord(rec4, 3, TEST_STRLEN3, TEST_STR3);
assert(TLV::WriteToMemory(backend, nullptr, rec4));
std::cout << "FindTag 3\n";
rec4.Tag = 2;
cursor = TLV::FindTag(backend, nullptr, rec4);
assert(cursor != nullptr);
std::cout << "DeleteRecord\n";
TLV::DeleteRecord(backend, cursor);
assert(cursor[0] == 3);
}
bool
runSuite(Arena &backend, const char *label)
{
std::exception exc;
std::cout << backend << "\n";
std::cout << "running test suite " << label << ": ";
try {
tlvTestSuite(backend);
} catch (std::exception &exc){
std::cout << "FAILED: " << exc.what() << "\n";
return false;
}
std::cout << "OK" << "\n";
std::cout << "\tdestroying arena: ";
backend.Destroy();
std::cout << "OK" << "\n";
return true;
}
int
main(int argc, const char *argv[])
{
(void)argc; (void)argv;
Arena arenaStatic;
Arena arenaMem;
std::cout << "TESTPROG: " << argv[0] << "\n";
if (-1 == arenaStatic.SetStatic(arenaBuffer, ARENA_SIZE)) {
abort();
} else if (!runSuite(arenaStatic, "arenaStatic")) {
abort();
}
arenaStatic.Clear();
Arena arenaFile;
auto status = arenaFile.Create(ARENA_FILE, ARENA_SIZE);
if (status != 0) {
std::cerr << "Create failed with error " << status << "\n";
abort();
} else if (!runSuite(arenaFile, "arenaFile")) {
abort();
}
if (-1 == arenaMem.SetAlloc(ARENA_SIZE)) {
abort();
} else if (!runSuite(arenaMem, "arenaMem")) {
abort();
}
arenaMem.Clear();
std::cout << "OK" << "\n";
return 0;
}