Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: multikey_object proptests #32

Merged
merged 8 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Test
working-directory: ${{github.workspace}}/build
env:
RC_PARAMS: "max_success=10000"
RC_PARAMS: "max_success=1000"
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --output-on-failure -C ${{env.BUILD_TYPE}}
Expand Down
4 changes: 2 additions & 2 deletions tests/proptests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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)

target_compile_options(rapidcheck_tests PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -Wconversion -pedantic-errors -Wsign-conversion>
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)
/W4 /bigobj>)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_link_libraries(rapidcheck_tests PUBLIC -fsanitize=address,undefined)
Expand Down
42 changes: 42 additions & 0 deletions tests/proptests/generators/string_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <rapidcheck.h>
#include <string>

inline bool contains_surrogate(const std::wstring &str)
{
return str.find(wchar_t(-1)) != str.npos;
}

////////////////////////////////////////////////////////////////
template <typename charT> rc::Gen<std::basic_string<charT>> gen_name_string();

template <> inline rc::Gen<std::basic_string<char>> gen_name_string<char>()
{
return rc::gen::string<std::string>();
}

template <>
inline rc::Gen<std::basic_string<wchar_t>> gen_name_string<wchar_t>()
{
return rc::gen::suchThat(rc::gen::string<std::wstring>(),
[](const auto &str)
{ return !contains_surrogate(str); });
}

////////////////////////////////////////////////////////////////
template <typename charT>
rc::Gen<std::basic_string<charT>> gen_unescaped_name_string();

template <> inline rc::Gen<std::string> gen_unescaped_name_string<char>()
{
return rc::gen::suchThat(gen_name_string<char>(), [](const std::string &str)
{ return str.find("\"") == str.npos; });
}

template <> inline rc::Gen<std::wstring> gen_unescaped_name_string<wchar_t>()
{
return rc::gen::suchThat(gen_name_string<wchar_t>(),
[](const std::wstring &str)
{ return str.find(L"\"") == str.npos; });
}
76 changes: 76 additions & 0 deletions tests/proptests/generators/vdf_multiobject_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "vdf_multiobject_generator.hpp"

template <typename charT>
bool equal_impl(const tyti::vdf::basic_multikey_object<charT> &rhs,
const tyti::vdf::basic_multikey_object<charT> &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
{

bool operator==(const tyti::vdf::multikey_object &rhs,
const tyti::vdf::multikey_object &lhs)
{
return equal_impl(rhs, lhs);
}
bool operator==(const tyti::vdf::wmultikey_object &rhs,
const tyti::vdf::wmultikey_object &lhs)
{
return equal_impl(rhs, lhs);
}

} // 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
50 changes: 50 additions & 0 deletions tests/proptests/generators/vdf_multiobject_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

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

#include "string_generator.hpp"

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

namespace rc
{

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

template <> struct Arbitrary<tyti::vdf::wmultikey_object>
{
static Gen<tyti::vdf::wmultikey_object> arbitrary()
{
using obj = tyti::vdf::wmultikey_object;
return gen::build<obj>(
gen::set(&obj::name, gen_name_string<wchar_t>()),
gen::set(
&obj::attribs,

rc::gen::container<
std::unordered_multimap<std::wstring, std::wstring>>(
gen_name_string<wchar_t>(), gen_name_string<wchar_t>())));
}
};
} // namespace rc

namespace rc::detail
{

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

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

template <typename charT>
bool equal_impl(const tyti::vdf::basic_object<charT> &rhs,
const tyti::vdf::basic_object<charT> &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
{

bool operator==(const tyti::vdf::wobject &rhs, const tyti::vdf::wobject &lhs)
{
return equal_impl(rhs, lhs);
}

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

} // 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
50 changes: 50 additions & 0 deletions tests/proptests/generators/vdf_object_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

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

#include "string_generator.hpp"

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

} // namespace tyti::vdf

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

template <> struct Arbitrary<tyti::vdf::wobject>
{
static Gen<tyti::vdf::wobject> arbitrary()
{
using obj = tyti::vdf::wobject;
return gen::build<obj>(
gen::set(&obj::name, gen_name_string<wchar_t>()),
gen::set(
&obj::attribs,

rc::gen::container<
std::unordered_map<std::wstring, std::wstring>>(
gen_name_string<wchar_t>(), gen_name_string<wchar_t>())));
}
};
} // 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
Loading