1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
//===- llvm/unittest/XRay/GraphTest.cpp - XRay Graph unit tests -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/XRay/Graph.h"
#include "gtest/gtest.h"
#include <iostream>
#include <set>
#include <type_traits>
using namespace llvm;
using namespace xray;
namespace {
struct VAttr {
unsigned VA;
};
struct EAttr {
unsigned EA;
};
typedef Graph<VAttr, EAttr, unsigned> GraphT;
typedef typename GraphT::VertexIdentifier VI;
typedef typename GraphT::EdgeIdentifier EI;
// Test Fixture
template <typename T> class GraphTest : public testing::Test {
protected:
T Graph = getTestGraph();
private:
static T getTestGraph() {
using std::make_pair;
std::remove_const_t<T> G;
G.insert(make_pair(1u, VAttr({3u})));
G.insert(make_pair(2u, VAttr({5u})));
G.insert(make_pair(3u, VAttr({7u})));
G.insert(make_pair(4u, VAttr({11u})));
G.insert(make_pair(5u, VAttr({13u})));
G.insert(make_pair(6u, VAttr({17u})));
G.insert(std::make_pair(EI(1u, 2u), EAttr({3u * 5u})));
G.insert(std::make_pair(EI(2u, 3u), EAttr({5u * 7u})));
G.insert(std::make_pair(EI(6u, 3u), EAttr({2u * 7u * 17u})));
G.insert(std::make_pair(EI(4u, 6u), EAttr({11u * 17u})));
G.insert(std::make_pair(EI(2u, 4u), EAttr({5u * 11u})));
G.insert(std::make_pair(EI(2u, 5u), EAttr({5u * 13u})));
G.insert(std::make_pair(EI(4u, 5u), EAttr({11u * 13u})));
return G;
}
};
typedef ::testing::Types<GraphT, const GraphT> GraphTestTypes;
using VVT = typename GraphT::VertexValueType;
using EVT = typename GraphT::EdgeValueType;
TYPED_TEST_SUITE(GraphTest, GraphTestTypes, );
template <typename T> void graphVertexTester(T &G) {
std::set<unsigned> V({1u, 2u, 3u, 4u, 5u, 6u});
std::vector<unsigned> VA({0u, 3u, 5u, 7u, 11u, 13u, 17u});
EXPECT_EQ(V.size(), G.vertices().size());
EXPECT_FALSE(G.vertices().empty());
for (unsigned u : V) {
auto EVV = G.at(u);
ASSERT_TRUE(!!EVV);
EXPECT_EQ(1u, G.count(u));
EXPECT_EQ(VA[u], EVV->VA);
EXPECT_NE(G.vertices().end(),
std::find_if(G.vertices().begin(), G.vertices().end(),
[&](const VVT &VV) { return VV.first == u; }));
consumeError(EVV.takeError());
}
for (auto &VVT : G.vertices()) {
EXPECT_EQ(1u, V.count(VVT.first));
EXPECT_EQ(VA[VVT.first], VVT.second.VA);
}
}
template <typename T> void graphEdgeTester(T &G) {
std::set<unsigned> V({1u, 2u, 3u, 4u, 5u, 6u});
std::set<std::pair<unsigned, unsigned>> E(
{{1u, 2u}, {2u, 3u}, {6u, 3u}, {4u, 6u}, {2u, 4u}, {2u, 5u}, {4u, 5u}});
std::vector<unsigned> VA({0u, 3u, 5u, 7u, 11u, 13u, 17u});
EXPECT_EQ(E.size(), G.edges().size());
EXPECT_FALSE(G.edges().empty());
for (std::pair<unsigned, unsigned> u : E) {
auto EEV = G.at(u);
ASSERT_TRUE(!!EEV);
EXPECT_EQ(1u, G.count(u));
EXPECT_EQ(VA[u.first] * VA[u.second] * ((u.first > u.second) ? 2 : 1),
EEV->EA);
auto Pred = [&](const EVT &EV) { return EV.first == u; };
EXPECT_NE(G.edges().end(),
std::find_if(G.edges().begin(), G.edges().end(), Pred));
consumeError(EEV.takeError());
}
for (auto &EV : G.edges()) {
EXPECT_EQ(1u, E.count(EV.first));
EXPECT_EQ(VA[EV.first.first] * VA[EV.first.second] *
((EV.first.first > EV.first.second) ? 2 : 1),
EV.second.EA);
const auto &IE = G.inEdges(EV.first.second);
const auto &OE = G.outEdges(EV.first.first);
EXPECT_NE(IE.size(), 0u);
EXPECT_NE(OE.size(), 0u);
EXPECT_NE(IE.begin(), IE.end());
EXPECT_NE(OE.begin(), OE.end());
{
auto It = std::find_if(
G.inEdges(EV.first.second).begin(), G.inEdges(EV.first.second).end(),
[&](const EVT &EVI) { return EVI.first == EV.first; });
EXPECT_NE(G.inEdges(EV.first.second).end(), It);
}
{
auto It = std::find_if(
G.inEdges(EV.first.first).begin(), G.inEdges(EV.first.first).end(),
[&](const EVT &EVI) { return EVI.first == EV.first; });
EXPECT_EQ(G.inEdges(EV.first.first).end(), It);
}
{
auto It =
std::find_if(G.outEdges(EV.first.second).begin(),
G.outEdges(EV.first.second).end(),
[&](const EVT &EVI) { return EVI.first == EV.first; });
EXPECT_EQ(G.outEdges(EV.first.second).end(), It);
}
{
auto It = std::find_if(
G.outEdges(EV.first.first).begin(), G.outEdges(EV.first.first).end(),
[&](const EVT &EVI) { return EVI.first == EV.first; });
EXPECT_NE(G.outEdges(EV.first.first).end(), It);
}
}
}
TYPED_TEST(GraphTest, TestGraphEdge) {
auto &G = this->Graph;
graphEdgeTester(G);
}
TYPED_TEST(GraphTest, TestGraphVertex) {
auto &G = this->Graph;
graphVertexTester(G);
}
TYPED_TEST(GraphTest, TestCopyConstructor) {
TypeParam G(this->Graph);
graphEdgeTester(G);
graphVertexTester(G);
}
TYPED_TEST(GraphTest, TestCopyAssign) {
TypeParam G = this->Graph;
graphEdgeTester(G);
graphVertexTester(G);
}
TYPED_TEST(GraphTest, TestMoveConstructor) {
TypeParam G(std::move(this->Graph));
graphEdgeTester(G);
graphVertexTester(G);
}
// Tests the incremental Construction of a graph
TEST(GraphTest, TestConstruction) {
GraphT MG;
const GraphT &G = MG;
EXPECT_EQ(0u, G.count(0u));
EXPECT_EQ(0u, G.count({0u, 1u}));
auto VE = G.at(0);
auto EE = G.at({0, 0});
EXPECT_FALSE(VE); // G.at[0] returns an error
EXPECT_FALSE(EE); // G.at[{0,0}] returns an error
consumeError(VE.takeError());
consumeError(EE.takeError());
EXPECT_TRUE(G.vertices().empty());
EXPECT_TRUE(G.edges().empty());
EXPECT_EQ(G.vertices().begin(), G.vertices().end());
EXPECT_EQ(G.edges().begin(), G.edges().end());
}
TEST(GraphTest, TestiVertexAccessOperator) {
GraphT MG;
const GraphT &G = MG;
MG[0u] = {1u};
EXPECT_EQ(1u, MG[0u].VA);
EXPECT_EQ(1u, G.count(0u));
EXPECT_EQ(0u, G.count(1u));
EXPECT_EQ(1u, MG[0u].VA);
auto T = G.at(0u);
EXPECT_TRUE(!!T);
EXPECT_EQ(1u, T->VA);
EXPECT_EQ(1u, G.vertices().size());
EXPECT_EQ(0u, G.edges().size());
EXPECT_FALSE(G.vertices().empty());
EXPECT_TRUE(G.edges().empty());
EXPECT_NE(G.vertices().begin(), G.vertices().end());
EXPECT_EQ(G.edges().begin(), G.edges().end());
EXPECT_EQ(1u, G.vertices().begin()->second.VA);
EXPECT_EQ(0u, G.vertices().begin()->first);
EXPECT_EQ(0u, G.outEdges(0u).size());
EXPECT_TRUE(G.outEdges(0u).empty());
EXPECT_EQ(G.outEdges(0u).begin(), G.outEdges(0u).end());
EXPECT_EQ(0u, G.inEdges(0u).size());
EXPECT_TRUE(G.inEdges(0u).empty());
EXPECT_EQ(G.inEdges(0u).begin(), G.inEdges(0u).end());
}
TEST(GraphTest, TestEdgeAccessOperator) {
GraphT MG;
const GraphT &G = MG;
MG[{0u, 0u}] = {2u};
EI EdgeIdent({0u, 0u});
EXPECT_EQ(2u, MG[EdgeIdent].EA);
EXPECT_EQ(1u, G.count({0u, 0u}));
EXPECT_EQ(0u, G.count({0u, 1u}));
EXPECT_EQ(1u, G.count(0u));
EXPECT_NE(1u, G.count(1u));
auto T = G.at({0u, 0u});
EXPECT_TRUE(T && T->EA == 2u);
EXPECT_EQ(1u, G.edges().size());
EXPECT_EQ(1u, G.vertices().size());
EXPECT_FALSE(G.edges().empty());
EXPECT_FALSE(G.vertices().empty());
EXPECT_NE(G.edges().begin(), G.edges().end());
EXPECT_EQ(EI(0u, 0u), G.edges().begin()->first);
EXPECT_EQ(2u, G.edges().begin()->second.EA);
EXPECT_EQ(1u, G.outEdges(0u).size());
EXPECT_FALSE(G.outEdges(0u).empty());
EXPECT_NE(G.outEdges(0u).begin(), G.outEdges(0u).end());
EXPECT_EQ(EI(0u, 0u), G.outEdges(0u).begin()->first);
EXPECT_EQ(2u, G.outEdges(0u).begin()->second.EA);
EXPECT_EQ(++(G.outEdges(0u).begin()), G.outEdges(0u).end());
EXPECT_EQ(1u, G.inEdges(0u).size());
EXPECT_FALSE(G.inEdges(0u).empty());
EXPECT_NE(G.inEdges(0u).begin(), G.inEdges(0u).end());
EXPECT_EQ(EI(0u, 0u), G.inEdges(0u).begin()->first);
EXPECT_EQ(2u, G.inEdges(0u).begin()->second.EA);
EXPECT_EQ(++(G.inEdges(0u).begin()), G.inEdges(0u).end());
}
}
|