Skip to content

Commit

Permalink
add proptests for multikey objects
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyTinni committed Nov 16, 2024
1 parent d8a2f18 commit ca44fd8
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 77 deletions.
2 changes: 1 addition & 1 deletion tests/proptests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CPMAddPackage(NAME rapidcheck
get_property(rapidcheck_include_dirs TARGET rapidcheck PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
set_property(TARGET rapidcheck PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${rapidcheck_include_dirs}")

add_executable(rapidcheck_tests "main_rapidcheck.cpp")
add_executable(rapidcheck_tests "main_rapidcheck.cpp;generators/vdf_object_generator.cpp;generators/vdf_multiobject_generator.cpp")
target_compile_features(rapidcheck_tests PUBLIC cxx_std_20)
target_link_libraries(rapidcheck_tests PRIVATE ValveFileVDF rapidcheck)

Expand Down
63 changes: 63 additions & 0 deletions tests/proptests/generators/vdf_multiobject_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "vdf_multiobject_generator.hpp"

namespace tyti::vdf
{
bool operator==(const tyti::vdf::multikey_object &rhs,
const tyti::vdf::multikey_object &lhs)
{
if (rhs.name != lhs.name)
return false;
if (rhs.attribs != lhs.attribs)
return false;
for (const auto &[k, v] : rhs.childs)
{
auto [begin, end] = lhs.childs.equal_range(k);

#ifdef _MSC_VER
// suppress warning about recursive call of operator==. This is here
// by intention
#pragma warning(disable : 5232)
#endif
while (begin != end)
{
if (begin->second == nullptr && v == nullptr)
return true;
if (*(begin->second) == *v)
return true;
++begin;
}
return false;
#ifdef _MSC_VER
#pragma warning(default : 5232)
#endif
}

return true;
}
} // namespace tyti::vdf

namespace rc::detail
{

void showValue(tyti::vdf::multikey_object obj, std::ostream &os)
{
os << "name: " << obj.name << "\n";
os << "attribs (size:" << obj.attribs.size() << "): \n";
for (const auto &[k, v] : obj.attribs)
os << k << "\t" << v << "\n";
os << "childs: (size:" << obj.childs.size() << "): \n";
for (const auto &[k, v] : obj.childs)
{
os << "'" << k << "'\t'";
if (v)
showValue(*v, os);
else
os << "nullptr!";

os << "'\n";
}

os << "'\n";
}

} // namespace rc::detail
31 changes: 31 additions & 0 deletions tests/proptests/generators/vdf_multiobject_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <rapidcheck.h>
#include <vdf_parser.hpp>

namespace tyti::vdf
{
bool operator==(const tyti::vdf::multikey_object &rhs,
const tyti::vdf::multikey_object &lhs);
} // namespace tyti::vdf

namespace rc
{

template <> struct Arbitrary<tyti::vdf::multikey_object>
{
static Gen<tyti::vdf::multikey_object> arbitrary()
{
using tyti::vdf::multikey_object;
return gen::build<multikey_object>(gen::set(&multikey_object::name),
gen::set(&multikey_object::attribs));
}
};
} // namespace rc

namespace rc::detail
{

void showValue(tyti::vdf::multikey_object obj, std::ostream &os);

} // namespace rc::detail
66 changes: 66 additions & 0 deletions tests/proptests/generators/vdf_object_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "vdf_object_generator.hpp"

namespace tyti::vdf
{
bool operator==(const tyti::vdf::object &rhs, const tyti::vdf::object &lhs)
{
if (rhs.name != lhs.name)
return false;
if (rhs.attribs != lhs.attribs)
return false;
for (const auto &[k, v] : rhs.childs)
{
auto itr = lhs.childs.find(k);
if (itr == lhs.childs.end())
{
return false;
}

#ifdef _MSC_VER
// suppress warning about recursive call of operator==. This is here
// by intention
#pragma warning(disable : 5232)
#endif
if (itr->second != v)
{
if (itr->second == nullptr || v == nullptr)
return *(itr->second) != *v;
}
#ifdef _MSC_VER
#pragma warning(default : 5232)
#endif
}

return true;
}
} // namespace tyti::vdf

namespace rc::details
{
void showValue(tyti::vdf::object obj, std::ostream &os)
{
os << "name: " << obj.name << "\n";
os << "attribs (size:" << obj.attribs.size() << "): \n";
for (const auto &[k, v] : obj.attribs)
os << k << "\t" << v << "\n";
os << "childs: (size:" << obj.childs.size() << "): \n";
for (const auto &[k, v] : obj.childs)
{
os << k << "\t";
if (v)
showValue(*v, os);
else
os << "nullptr!";
os << "\n";
}
}

void showValue(const std::tuple<tyti::vdf::object, tyti::vdf::object> &objs,
std::ostream &os)
{
os << "[LHS]: \n";
showValue(std::get<0>(objs), os);
os << "[RHS]: \n";
showValue(std::get<1>(objs), os);
}
} // namespace rc::details
32 changes: 32 additions & 0 deletions tests/proptests/generators/vdf_object_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <rapidcheck.h>
#include <vdf_parser.hpp>

namespace tyti::vdf
{
bool operator==(const tyti::vdf::object &rhs, const tyti::vdf::object &lhs);

} // namespace tyti::vdf

namespace rc
{
template <> struct Arbitrary<tyti::vdf::object>
{
static Gen<tyti::vdf::object> arbitrary()
{
using tyti::vdf::object;
return gen::build<object>(gen::set(&object::name),
gen::set(&object::attribs));
}
};
} // namespace rc

namespace rc::details
{

void showValue(tyti::vdf::object obj, std::ostream &os);

void showValue(const std::tuple<tyti::vdf::object, tyti::vdf::object> &objs,
std::ostream &os);
} // namespace rc::details
101 changes: 25 additions & 76 deletions tests/proptests/main_rapidcheck.cpp
Original file line number Diff line number Diff line change
@@ -1,82 +1,9 @@
#include "generators/vdf_multiobject_generator.hpp"
#include "generators/vdf_object_generator.hpp"
#include <algorithm>
#include <string>
#include <vdf_parser.hpp>

namespace rc::detail
{
bool operator==(const tyti::vdf::object &rhs, const tyti::vdf::object &lhs)
{
if (rhs.name != lhs.name)
return false;
if (rhs.attribs != lhs.attribs)
return false;
for (const auto &[k, v] : rhs.childs)
{
auto itr = lhs.childs.find(k);
if (itr == lhs.childs.end())
return false;

#ifdef _MSC_VER
// suppress warning about recursive call of operator==. This is here
// by intention
#pragma warning(disable : 5232)
#endif
if (itr->second != v)
{
if (itr->second == nullptr || v == nullptr || *(itr->second) != *v)
return false;
}
#ifdef _MSC_VER
#pragma warning(default : 5232)
#endif
}

return true;
}

void showValue(tyti::vdf::object obj, std::ostream &os)
{
os << "name: " << obj.name << "\n";
os << "attribs (size:" << obj.attribs.size() << "): \n";
for (const auto &[k, v] : obj.attribs)
os << k << "\t" << v << "\n";
os << "childs: (size:" << obj.childs.size() << "): \n";
for (const auto &[k, v] : obj.childs)
{
os << k << "\t";
if (v)
showValue(*v, os);
else
os << "nullptr!";
os << "\n";
}
}
void showValue(const std::tuple<tyti::vdf::object, tyti::vdf::object> &objs,
std::ostream &os)
{
os << "[LHS]: \n";
showValue(std::get<0>(objs), os);
os << "[RHS]: \n";
showValue(std::get<1>(objs), os);
}
} // namespace rc::detail

#include <rapidcheck.h>

namespace rc
{

template <> struct Arbitrary<tyti::vdf::object>
{
static Gen<tyti::vdf::object> arbitrary()
{
using tyti::vdf::object;
return gen::build<object>(gen::set(&object::name),
gen::set(&object::attribs));
}
};
} // namespace rc

bool containsSurrogate(const std::wstring &str)
{
return str.find(wchar_t(-1)) != str.npos;
Expand Down Expand Up @@ -194,7 +121,7 @@ int main()

for (const auto &c : childs)
{
in.childs[c->name] = c;
in.childs.emplace(c->name, c);
}

std::stringstream sstr;
Expand All @@ -203,6 +130,28 @@ int main()
RC_ASSERT(in == to_test);
});

success &= rc::check(
"check if the childs are also written and parsed correctly - multikey",
[](vdf::multikey_object in)
{
// todo this just tests childs with depth 1
using child_vec =
std::vector<std::shared_ptr<vdf::multikey_object>>;
child_vec childs = *rc::gen::container<child_vec>(
rc::gen::makeShared<vdf::multikey_object>(
rc::gen::arbitrary<vdf::multikey_object>()));

for (const auto &c : childs)
{
in.childs.emplace(c->name, c);
}

std::stringstream sstr;
vdf::write(sstr, in);
auto to_test = tyti::vdf::read<vdf::multikey_object>(sstr);
RC_ASSERT(in == to_test);
});

////////////////////////////////////////////////////////////////
// comments parsing tests

Expand Down

0 comments on commit ca44fd8

Please sign in to comment.