|
| 1 | +#include "nix/store/build/find-cycles.hh" |
| 2 | +#include "nix/store/dependency-graph.hh" |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | +#include <ranges> |
| 6 | + |
| 7 | +namespace nix { |
| 8 | + |
| 9 | +/** |
| 10 | + * Parameters for DependencyGraph cycle detection tests |
| 11 | + */ |
| 12 | +struct FindCyclesParams |
| 13 | +{ |
| 14 | + std::string description; |
| 15 | + std::vector<std::pair<std::string, std::string>> inputEdges; // (from, to) pairs |
| 16 | + std::vector<std::vector<std::string>> expectedCycles; |
| 17 | + |
| 18 | + friend std::ostream & operator<<(std::ostream & os, const FindCyclesParams & params) |
| 19 | + { |
| 20 | + os << "Test: " << params.description << "\n"; |
| 21 | + os << "Input edges (" << params.inputEdges.size() << "):\n"; |
| 22 | + for (const auto & [from, to] : params.inputEdges) { |
| 23 | + os << " " << from << " -> " << to << "\n"; |
| 24 | + } |
| 25 | + os << "Expected cycles (" << params.expectedCycles.size() << "):\n"; |
| 26 | + for (const auto & cycle : params.expectedCycles) { |
| 27 | + os << " "; |
| 28 | + for (size_t i = 0; i < cycle.size(); ++i) { |
| 29 | + if (i > 0) |
| 30 | + os << " -> "; |
| 31 | + os << cycle[i]; |
| 32 | + } |
| 33 | + os << "\n"; |
| 34 | + } |
| 35 | + return os; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +class FindCyclesTest : public ::testing::TestWithParam<FindCyclesParams> |
| 40 | +{}; |
| 41 | + |
| 42 | +namespace { |
| 43 | +// Comparator for sorting cycles deterministically |
| 44 | +bool compareCycles(const std::vector<std::string> & a, const std::vector<std::string> & b) |
| 45 | +{ |
| 46 | + if (a.size() != b.size()) |
| 47 | + return a.size() < b.size(); |
| 48 | + return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); |
| 49 | +} |
| 50 | +} // namespace |
| 51 | + |
| 52 | +TEST_P(FindCyclesTest, FindCycles) |
| 53 | +{ |
| 54 | + const auto & params = GetParam(); |
| 55 | + |
| 56 | + // Build graph from input edges |
| 57 | + FilePathGraph depGraph; |
| 58 | + for (const auto & [from, to] : params.inputEdges) { |
| 59 | + depGraph.addEdge(from, to); |
| 60 | + } |
| 61 | + |
| 62 | + // Find cycles - returns vector<vector<string>> directly! |
| 63 | + auto actualCycles = depGraph.findCycles(); |
| 64 | + |
| 65 | + EXPECT_EQ(actualCycles.size(), params.expectedCycles.size()) << "Number of cycles doesn't match expected"; |
| 66 | + |
| 67 | + // Sort both for comparison using ranges (order may vary, but content should match) |
| 68 | + std::ranges::sort(actualCycles, compareCycles); |
| 69 | + auto expectedCycles = params.expectedCycles; |
| 70 | + std::ranges::sort(expectedCycles, compareCycles); |
| 71 | + |
| 72 | + // Compare each cycle |
| 73 | + EXPECT_EQ(actualCycles, expectedCycles); |
| 74 | +} |
| 75 | + |
| 76 | +INSTANTIATE_TEST_CASE_P( |
| 77 | + FindCycles, |
| 78 | + FindCyclesTest, |
| 79 | + ::testing::Values( |
| 80 | + // Empty input - no cycles |
| 81 | + FindCyclesParams{"empty input", {}, {}}, |
| 82 | + |
| 83 | + // Single edge - no cycle |
| 84 | + FindCyclesParams{"single edge no cycle", {{"a", "b"}}, {}}, |
| 85 | + |
| 86 | + // Simple cycle: A->B, B->A |
| 87 | + FindCyclesParams{"simple cycle", {{"a", "b"}, {"b", "a"}}, {{"a", "b", "a"}}}, |
| 88 | + |
| 89 | + // Complete cycle: A->B->C->A |
| 90 | + FindCyclesParams{"three node cycle", {{"a", "b"}, {"b", "c"}, {"c", "a"}}, {{"a", "b", "c", "a"}}}, |
| 91 | + |
| 92 | + // Four node cycle: A->B->C->D->A |
| 93 | + FindCyclesParams{ |
| 94 | + "four node cycle", {{"a", "b"}, {"b", "c"}, {"c", "d"}, {"d", "a"}}, {{"a", "b", "c", "d", "a"}}}, |
| 95 | + |
| 96 | + // Multiple disjoint cycles |
| 97 | + FindCyclesParams{ |
| 98 | + "multiple disjoint cycles", |
| 99 | + {{"a", "b"}, {"b", "a"}, {"c", "d"}, {"d", "c"}}, |
| 100 | + {{"a", "b", "a"}, {"c", "d", "c"}}}, |
| 101 | + |
| 102 | + // Cycle with non-cycle edges (A->B->A is cycle, C->D is not) |
| 103 | + FindCyclesParams{"cycle with extra edges", {{"a", "b"}, {"b", "a"}, {"c", "d"}}, {{"a", "b", "a"}}}, |
| 104 | + |
| 105 | + // Self-loop |
| 106 | + FindCyclesParams{"self-loop", {{"a", "a"}}, {{"a", "a"}}}, |
| 107 | + |
| 108 | + // Chain without cycle |
| 109 | + FindCyclesParams{"chain no cycle", {{"a", "b"}, {"b", "c"}, {"c", "d"}}, {}}, |
| 110 | + |
| 111 | + // Complex: cycle with incoming/outgoing edges |
| 112 | + // X->A->B->C->A (only A->B->C->A is the cycle) |
| 113 | + FindCyclesParams{"cycle with tail", {{"x", "a"}, {"a", "b"}, {"b", "c"}, {"c", "a"}}, {{"a", "b", "c", "a"}}})); |
| 114 | + |
| 115 | +} // namespace nix |
0 commit comments