Skip to content

Commit

Permalink
Test Type::SortByLoadDependencies()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Feb 21, 2024
1 parent fa4adf0 commit 2ddd3d9
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ add_boost_test(base
base_type/assign
base_type/byname
base_type/instantiate
base_type/sortbyloaddependencies0
base_type/sortbyloaddependencies1
base_type/sortbyloaddependencies2related
base_type/sortbyloaddependencies2unrelated
base_type/sortbyloaddependencies3related
base_type/sortbyloaddependencies3unrelated
base_type/sortbyloaddependencies4related
base_type/sortbyloaddependencies4unrelated
base_type/sortbyloaddependencies5related
base_type/sortbyloaddependencies5unrelated
base_type/sortbyloaddependencies1_withforeign
base_type/sortbyloaddependencies2related_withforeign
base_type/sortbyloaddependencies2unrelated_withforeign
base_type/sortbyloaddependencies3related_withforeign
base_type/sortbyloaddependencies3unrelated_withforeign
base_type/sortbyloaddependencies4related_withforeign
base_type/sortbyloaddependencies4unrelated_withforeign
base_type/sortbyloaddependencies5related_withforeign
base_type/sortbyloaddependencies5unrelated_withforeign
base_utility/parse_version
base_utility/compare_version
base_utility/comparepasswords_works
Expand Down
205 changes: 205 additions & 0 deletions test/base-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,110 @@
#include "base/application.hpp"
#include "base/type.hpp"
#include <BoostTestTargetConfig.h>
#include <algorithm>
#include <stdexcept>

using namespace icinga;

class TestType : public Type
{
public:
TestType(std::unordered_set<Type*> loadDependencies = {}) : m_LoadDependencies(std::move(loadDependencies))
{
}

const std::unordered_set<Type*>& GetLoadDependencies() const override
{
return m_LoadDependencies;
}

String GetName() const override
{
throw std::runtime_error("not implemented");
}

Type::Ptr GetBaseType() const override
{
throw std::runtime_error("not implemented");
}

int GetAttributes() const override
{
throw std::runtime_error("not implemented");
}

int GetFieldId(const String&) const override
{
throw std::runtime_error("not implemented");
}

Field GetFieldInfo(int) const override
{
throw std::runtime_error("not implemented");
}

int GetFieldCount() const override
{
throw std::runtime_error("not implemented");
}

protected:
ObjectFactory GetFactory() const override
{
throw std::runtime_error("not implemented");
}

private:
std::unordered_set<Type*> m_LoadDependencies;
};

static void TestSortByLoadDependenciesUnrelated(int amount, bool withDepsToForeign = false)
{
std::vector<Type::Ptr> expected;

for (int i = 0; i < amount; ++i) {
expected.emplace_back(withDepsToForeign ? new TestType({new TestType()}) : new TestType());
}

std::sort(expected.begin(), expected.end());

auto input (expected);

do {
auto actual (Type::SortByLoadDependencies(input));

std::sort(actual.begin(), actual.end());
BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
} while (std::next_permutation(input.begin(), input.end()));
}

static void TestSortByLoadDependenciesRelated(int amount, bool withDepsToForeign = false)
{
std::vector<Type::Ptr> expected;

for (int i = 0; i < amount; ++i) {
if (i) {
expected.emplace_back(
withDepsToForeign
? new TestType({expected.at(expected.size() - 1u).get(), new TestType()})
: new TestType({expected.at(expected.size() - 1u).get()})
);
} else {
expected.emplace_back(withDepsToForeign ? new TestType({new TestType()}) : new TestType());
}
}

auto input (expected);

std::sort(input.begin(), input.end());

do {
auto actual (Type::SortByLoadDependencies(input));

BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
} while (std::next_permutation(input.begin(), input.end()));
}

BOOST_AUTO_TEST_SUITE(base_type)

BOOST_AUTO_TEST_CASE(gettype)
Expand Down Expand Up @@ -44,4 +145,108 @@ BOOST_AUTO_TEST_CASE(instantiate)
BOOST_CHECK(p);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies0)
{
std::vector<Type::Ptr> empty;
auto actual (Type::SortByLoadDependencies(empty));

BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), empty.begin(), empty.end());
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies1)
{
std::vector<Type::Ptr> one ({new TestType()});
auto actual (Type::SortByLoadDependencies(one));

BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), one.begin(), one.end());
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies2unrelated)
{
TestSortByLoadDependenciesUnrelated(2);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies2related)
{
TestSortByLoadDependenciesRelated(2);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies3unrelated)
{
TestSortByLoadDependenciesUnrelated(3);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies3related)
{
TestSortByLoadDependenciesRelated(3);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies4unrelated)
{
TestSortByLoadDependenciesUnrelated(4);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies4related)
{
TestSortByLoadDependenciesRelated(4);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies5unrelated)
{
TestSortByLoadDependenciesUnrelated(5);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies5related)
{
TestSortByLoadDependenciesRelated(5);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies1_withforeign)
{
std::vector<Type::Ptr> one ({new TestType({new TestType()})});
auto actual (Type::SortByLoadDependencies(one));

BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), one.begin(), one.end());
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies2unrelated_withforeign)
{
TestSortByLoadDependenciesUnrelated(2, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies2related_withforeign)
{
TestSortByLoadDependenciesRelated(2, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies3unrelated_withforeign)
{
TestSortByLoadDependenciesUnrelated(3, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies3related_withforeign)
{
TestSortByLoadDependenciesRelated(3, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies4unrelated_withforeign)
{
TestSortByLoadDependenciesUnrelated(4, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies4related_withforeign)
{
TestSortByLoadDependenciesRelated(4, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies5unrelated_withforeign)
{
TestSortByLoadDependenciesUnrelated(5, true);
}

BOOST_AUTO_TEST_CASE(sortbyloaddependencies5related_withforeign)
{
TestSortByLoadDependenciesRelated(5, true);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2ddd3d9

Please sign in to comment.