2024-11-12 23:07:17 +00:00
|
|
|
#include "lix/libutil/closure.hh"
|
2021-05-18 12:30:32 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
map<string, set<string>> testGraph = {
|
|
|
|
{ "A", { "B", "C", "G" } },
|
|
|
|
{ "B", { "A" } }, // Loops back to A
|
|
|
|
{ "C", { "F" } }, // Indirect reference
|
|
|
|
{ "D", { "A" } }, // Not reachable, but has backreferences
|
|
|
|
{ "E", {} }, // Just not reachable
|
|
|
|
{ "F", {} },
|
|
|
|
{ "G", { "G" } }, // Self reference
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(closure, correctClosure) {
|
|
|
|
set<string> expectedClosure = {"A", "B", "C", "F", "G"};
|
2024-04-26 23:02:22 +00:00
|
|
|
set<string> aClosure = computeClosure<string>(
|
2021-05-18 12:30:32 +00:00
|
|
|
{"A"},
|
2024-04-26 23:02:22 +00:00
|
|
|
[&](const string currentNode) {
|
|
|
|
return testGraph[currentNode];
|
2021-05-18 12:30:32 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ASSERT_EQ(aClosure, expectedClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(closure, properlyHandlesDirectExceptions) {
|
2024-08-05 03:19:58 +00:00
|
|
|
struct TestExn : std::exception {};
|
2021-05-18 12:30:32 +00:00
|
|
|
EXPECT_THROW(
|
|
|
|
computeClosure<string>(
|
|
|
|
{"A"},
|
2024-04-26 23:02:22 +00:00
|
|
|
[&](const string currentNode) -> set<string> {
|
2021-05-18 12:30:32 +00:00
|
|
|
throw TestExn();
|
|
|
|
}
|
|
|
|
),
|
|
|
|
TestExn
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|