Skip to content

Commit

Permalink
Add ObjSolver::solve test
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed May 25, 2023
1 parent 3c14f6d commit b0e2acd
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions libmamba/tests/src/solv-cpp/test_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <string>
#include <vector>

#include <doctest/doctest.h>
#include <solv/solver.h>

Expand All @@ -12,11 +15,57 @@

using namespace mamba::solv;

struct SimplePkg
{
std::string name;
std::string version;
std::vector<std::string> dependencies = {};
};

auto
make_simple_packages() -> std::vector<SimplePkg>
{
return {
{ "menu", "1.5.0", { "dropdown=2.*" } },
{ "menu", "1.4.0", { "dropdown=2.*" } },
{ "menu", "1.3.0", { "dropdown=2.*" } },
{ "menu", "1.2.0", { "dropdown=2.*" } },
{ "menu", "1.1.0", { "dropdown=2.*" } },
{ "menu", "1.0.0", { "dropdown=1.*" } },
{ "dropdown", "2.3.0", { "icons=2.*" } },
{ "dropdown", "2.2.0", { "icons=2.*" } },
{ "dropdown", "2.1.0", { "icons=2.*" } },
{ "dropdown", "2.0.0", { "icons=2.*" } },
{ "dropdown", "1.8.0", { "icons=1.*", "intl=3.*" } },
{ "icons", "2.0.0" },
{ "icons", "1.0.0" },
{ "intl", "5.0.0" },
{ "intl", "4.0.0" },
{ "intl", "3.0.0" },
};
}


TEST_SUITE("ObjSolver")
{
TEST_CASE("Create a solver")
{
auto pool = ObjPool();
auto [repo_id, repo] = pool.add_repo("forge");

for (const auto& pkg : make_simple_packages())
{
auto [solv_id, solv] = repo.add_solvable();
solv.set_name(pkg.name);
solv.set_version(pkg.version);
for (const auto& dep : pkg.dependencies)
{
solv.add_dependency(pool.add_conda_dependency(dep));
}
solv.add_self_provide();
}
repo.internalize();

auto solver = ObjSolver(pool);

CHECK_EQ(solver.problem_count(), 0);
Expand All @@ -31,5 +80,47 @@ TEST_SUITE("ObjSolver")
solver.set_flag(SOLVER_FLAG_ALLOW_DOWNGRADE, true);
CHECK(solver.get_flag(SOLVER_FLAG_ALLOW_DOWNGRADE));
}

SUBCASE("Add packages")
{
SUBCASE("Solve successfully")
{
// The job is matched with the ``provides`` field of the solvable
auto jobs = ObjQueue{
SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES,
pool.add_conda_dependency("menu"),
SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES,
pool.add_conda_dependency("icons=2.*"),
};
CHECK(solver.solve(pool, jobs));
CHECK_EQ(solver.problem_count(), 0);
}

SUBCASE("Solve unsuccessfully")
{
// The job is matched with the ``provides`` field of the solvable
auto jobs = ObjQueue{
SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES,
pool.add_conda_dependency("menu"),
SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES,
pool.add_conda_dependency("icons=1.*"),
SOLVER_INSTALL | SOLVER_SOLVABLE_PROVIDES,
pool.add_conda_dependency("intl=5.*"),
};

CHECK_FALSE(solver.solve(pool, jobs));
CHECK_NE(solver.problem_count(), 0);

auto all_rules = ObjQueue{};
solver.for_each_problem_id(
[&](auto pb)
{
auto pb_rules = solver.problem_rules(pb);
all_rules.insert(all_rules.end(), pb_rules.cbegin(), pb_rules.cend());
}
);
CHECK_FALSE(all_rules.empty());
}
}
}
}

0 comments on commit b0e2acd

Please sign in to comment.