[ox/oc] Cleanup test scaffolding and replace cout with oxOut

This commit is contained in:
Gary Talent 2021-04-25 14:49:57 -05:00
parent 356d6a5036
commit 6c629a9f13

View File

@ -8,7 +8,6 @@
#undef NDEBUG #undef NDEBUG
#include <iostream>
#include <map> #include <map>
#include <ox/model/model.hpp> #include <ox/model/model.hpp>
@ -24,12 +23,16 @@ union TestUnion {
}; };
struct TestStructNest { struct TestStructNest {
static constexpr auto TypeName = "TestStructNest";
static constexpr auto Fields = 3;
bool Bool = false; bool Bool = false;
uint32_t Int = 0; uint32_t Int = 0;
ox::BString<32> String = ""; ox::BString<32> String = "";
}; };
struct TestStruct { struct TestStruct {
static constexpr auto TypeName = "TestStruct";
static constexpr auto Fields = 17;
bool Bool = false; bool Bool = false;
int32_t Int = 0; int32_t Int = 0;
int32_t Int1 = 0; int32_t Int1 = 0;
@ -50,35 +53,22 @@ struct TestStruct {
TestStruct() noexcept = default; TestStruct() noexcept = default;
TestStruct(TestStruct &&other) noexcept { TestStruct(const TestStruct&) noexcept;
Bool = other.Bool;
Int = other.Int;
Int1 = other.Int1;
Int2 = other.Int2;
Int3 = other.Int3;
Int4 = other.Int4;
Int5 = other.Int5;
Int6 = other.Int6;
Int7 = other.Int7;
Int8 = other.Int8;
Union = other.Union;
CString = other.CString;
other.CString = nullptr;
String = other.String;
memcpy(List, other.List, sizeof(List));
Map = ox::move(other.Map);
EmptyStruct = ox::move(other.EmptyStruct);
Struct = ox::move(other.Struct);
}
~TestStruct() { TestStruct(TestStruct &&other) noexcept;
~TestStruct() noexcept {
delete[] CString; delete[] CString;
} }
constexpr TestStruct &operator=(const TestStruct&) noexcept;
constexpr TestStruct &operator=(TestStruct&&) noexcept;
}; };
template<typename T> template<typename T>
ox::Error model(T *io, TestUnion *obj) { constexpr ox::Error model(T *io, TestUnion *obj) noexcept {
io->template setTypeInfo<TestUnion>(); io->template setTypeInfo<TestUnion>();
oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Bool", &obj->Bool));
oxReturnError(io->field("Int", &obj->Int)); oxReturnError(io->field("Int", &obj->Int));
@ -87,8 +77,8 @@ ox::Error model(T *io, TestUnion *obj) {
} }
template<typename T> template<typename T>
ox::Error model(T *io, TestStructNest *obj) { constexpr ox::Error model(T *io, TestStructNest *obj) noexcept {
io->setTypeInfo("TestStructNest", 3); io->template setTypeInfo<TestStructNest>();
oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Bool", &obj->Bool));
oxReturnError(io->field("Int", &obj->Int)); oxReturnError(io->field("Int", &obj->Int));
oxReturnError(io->field("String", &obj->String)); oxReturnError(io->field("String", &obj->String));
@ -96,8 +86,8 @@ ox::Error model(T *io, TestStructNest *obj) {
} }
template<typename T> template<typename T>
ox::Error model(T *io, TestStruct *obj) { constexpr ox::Error model(T *io, TestStruct *obj) noexcept {
io->setTypeInfo("TestStruct", 17); io->template setTypeInfo<TestStruct>();
oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Bool", &obj->Bool));
oxReturnError(io->field("Int", &obj->Int)); oxReturnError(io->field("Int", &obj->Int));
oxReturnError(io->field("Int1", &obj->Int1)); oxReturnError(io->field("Int1", &obj->Int1));
@ -118,7 +108,25 @@ ox::Error model(T *io, TestStruct *obj) {
return OxError(0); return OxError(0);
} }
std::map<std::string, ox::Error(*)()> tests = { TestStruct::TestStruct(const TestStruct &other) noexcept {
ox::copyModel(this, &other);
}
TestStruct::TestStruct(TestStruct &&other) noexcept {
ox::moveModel(this, &other);
}
constexpr TestStruct &TestStruct::operator=(const TestStruct &other) noexcept {
ox::copyModel(this, &other);
return *this;
}
constexpr TestStruct &TestStruct::operator=(TestStruct &&other) noexcept {
ox::moveModel(this, &other);
return *this;
}
const std::map<std::string_view, ox::Error(*)()> tests = {
{ {
{ {
"OrganicClawWriter", "OrganicClawWriter",
@ -151,7 +159,7 @@ std::map<std::string, ox::Error(*)()> tests = {
auto [oc, writeErr] = ox::writeOC(&testIn); auto [oc, writeErr] = ox::writeOC(&testIn);
oxAssert(writeErr, "writeOC failed"); oxAssert(writeErr, "writeOC failed");
std::cout << oc.data() << '\n'; oxOutf("{}\n", oc.data());
auto [testOut, readErr] = ox::readOC<TestStruct>(oc.data()); auto [testOut, readErr] = ox::readOC<TestStruct>(oc.data());
oxAssert(readErr, "readOC failed"); oxAssert(readErr, "readOC failed");
@ -206,80 +214,78 @@ std::map<std::string, ox::Error(*)()> tests = {
oxAssert(type.error, "Descriptor write failed"); oxAssert(type.error, "Descriptor write failed");
oxReturnError(ox::walkModel<ox::OrganicClawReader>(type.value, ox::bit_cast<uint8_t*>(oc.data()), oc.size(), oxReturnError(ox::walkModel<ox::OrganicClawReader>(type.value, ox::bit_cast<uint8_t*>(oc.data()), oc.size(),
[](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::TypeName>&, const ox::DescriptorField &f, ox::OrganicClawReader *rdr) -> ox::Error { [](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::TypeName>&, const ox::DescriptorField &f, ox::OrganicClawReader *rdr) -> ox::Error {
//std::cout << f.fieldName.c_str() << '\n';
auto fieldName = f.fieldName.c_str(); auto fieldName = f.fieldName.c_str();
switch (f.type->primitiveType) { switch (f.type->primitiveType) {
case ox::PrimitiveType::UnsignedInteger: case ox::PrimitiveType::UnsignedInteger:
std::cout << fieldName << ":\tuint" << f.type->length * 8 << "_t:\t"; oxOutf("{}:\tuint{}_t:\t", fieldName, f.type->length * 8);
switch (f.type->length) { switch (f.type->length) {
case 1: { case 1: {
uint8_t i = {}; uint8_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 2: { case 2: {
uint16_t i = {}; uint16_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 4: { case 4: {
uint32_t i = {}; uint32_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 8: { case 8: {
uint64_t i = {}; uint64_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
} }
std::cout << '\n'; oxOut("\n");
break; break;
case ox::PrimitiveType::SignedInteger: case ox::PrimitiveType::SignedInteger:
std::cout << fieldName << ":\tint" << f.type->length * 8 << "_t:\t"; oxOutf("{}:\tint{}_t:\t", fieldName, f.type->length * 8);
switch (f.type->length) { switch (f.type->length) {
case 1: { case 1: {
int8_t i = {}; int8_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 2: { case 2: {
int16_t i = {}; int16_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 4: { case 4: {
int32_t i = {}; int32_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
case 8: { case 8: {
int64_t i = {}; int64_t i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << i; oxOutf("{}", i);
break; break;
} }
} }
std::cout << '\n'; oxOut("\n");
break; break;
case ox::PrimitiveType::Bool: { case ox::PrimitiveType::Bool: {
bool i = {}; bool i = {};
oxAssert(rdr->field(fieldName, &i), "Walking model failed."); oxAssert(rdr->field(fieldName, &i), "Walking model failed.");
std::cout << fieldName << ":\t" << "bool:\t" << (i ? "true" : "false") << '\n'; oxOutf("{}:\tbool:\t{}\n", fieldName, i ? "true" : "false");
break; break;
} }
case ox::PrimitiveType::String: { case ox::PrimitiveType::String: {
ox::Vector<char> v(rdr->stringLength(fieldName) + 1); ox::Vector<char> v(rdr->stringLength(fieldName) + 1);
//std::cout << rdr->stringLength() << '\n';
oxAssert(rdr->field(fieldName, ox::SerStr(v.data(), v.size())), "Walking model failed."); oxAssert(rdr->field(fieldName, ox::SerStr(v.data(), v.size())), "Walking model failed.");
std::cout << fieldName << ":\t" << "string: " << v.data() << '\n'; oxOutf("{}:\tstring:\t{}\n", fieldName, v.data());
break; break;
} }
case ox::PrimitiveType::Struct: case ox::PrimitiveType::Struct:
@ -299,12 +305,16 @@ std::map<std::string, ox::Error(*)()> tests = {
}; };
int main(int argc, const char **args) { int main(int argc, const char **args) {
int retval = -1; if (argc < 2) {
if (argc > 0) { oxError("Must specify test to run");
auto testName = args[1];
if (tests.find(testName) != tests.end()) {
retval = tests[testName]();
}
} }
return retval; const auto testName = args[1];
ox::Error(*test)();
try {
test = tests.at(testName);
} catch (const std::out_of_range&) {
oxErrorf("Test {} not found", testName);
return 1;
}
return test();
} }