2021-05-18 12:30:32 +00:00
|
|
|
#include "closure.hh"
|
|
|
|
#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) {
|
|
|
|
struct TestExn {};
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|