Go to the documentation of this file.
15 #include "absl/synchronization/internal/graphcycles.h"
19 #include <unordered_set>
23 #include "gtest/gtest.h"
24 #include "absl/base/internal/raw_logging.h"
25 #include "absl/base/macros.h"
29 namespace synchronization_internal {
39 using Edges = std::vector<Edge>;
43 typedef std::map<int, GraphId>
IdMap;
51 std::unordered_set<int> *
seen) {
53 if (
from ==
to)
return true;
54 for (
const auto &edge : *edges) {
55 if (edge.from ==
from) {
58 }
else if (
seen->find(edge.to) ==
seen->end() &&
69 for (
const auto &edge : *edges) {
79 for (
int a : *nodes) {
80 for (
int b : *nodes) {
91 for (
int a : *nodes) {
92 for (
int b : *nodes) {
93 std::unordered_set<int>
seen;
105 for (
int a : *nodes) {
106 for (
int b : *nodes) {
107 if (
gc->IsReachable(
Get(
id,
a),
Get(
id,
b))) {
117 std::unordered_set<int>
seen;
118 for (
const auto &
a : *nodes) {
119 for (
const auto &
b : *nodes) {
121 bool gc_reachable =
gc->IsReachable(
Get(
id,
a),
Get(
id,
b));
123 if (gc_reachable != reachable) {
129 gc_reachable ?
"true" :
"false",
130 reachable ?
"true" :
"false",
a,
b);
139 for (
const auto &edge : *edges) {
148 for (
const auto &
a : *nodes) {
149 for (
const auto &
b : *nodes) {
155 if (
count != edges->size()) {
170 std::uniform_int_distribution<int> uniform(0, nodes->size()-1);
171 return uniform(*rng);
177 std::uniform_int_distribution<int> uniform(0, edges->size()-1);
178 return uniform(*rng);
184 while (
i != edges->size() &&
185 ((*edges)[
i].from !=
from || (*edges)[
i].to !=
to)) {
188 return i == edges->size()? -1 :
i;
197 static const int kMaxNodes = 7;
198 static const int kDataOffset = 17;
202 std::uniform_int_distribution<int> uniform(0, 5);
205 return reinterpret_cast<void*
>(
i + kDataOffset);
209 for (
const auto &node : nodes) {
212 CheckEdges(&nodes, &edges,
id, &graph_cycles);
217 if (nodes.size() < kMaxNodes) {
228 if (nodes.size() > 0) {
230 int node = nodes[node_index];
231 nodes[node_index] = nodes.back();
237 while (
i != edges.size()) {
238 if (edges[
i].
from == node || edges[
i].
to == node) {
239 edges[
i] = edges.back();
249 if (nodes.size() > 0) {
256 new_edge.
to = nodes[
to];
257 edges.push_back(new_edge);
259 std::unordered_set<int>
seen;
261 <<
"Edge " << nodes[
to] <<
"->" << nodes[
from];
268 if (edges.size() > 0) {
270 int from = edges[
i].from;
271 int to = edges[
i].to;
273 edges[
i] = edges.back();
281 if (nodes.size() > 0) {
285 int path_len = graph_cycles.
FindPath(
id[nodes[
from]],
id[nodes[
to]],
287 std::unordered_set<int>
seen;
300 for (
int i = 1;
i < path_len;
i++) {
316 std::bernoulli_distribution one_in_1024(1.0 / 1024);
317 if (one_in_1024(rng)) {
318 CheckEdges(&nodes, &edges,
id, &graph_cycles);
320 for (
int i = 0;
i != 256;
i++) {
326 for (
const auto &node : nodes) {
331 for (
int i = 0;
i != 256;
i++) {
334 int node = nodes[node_index];
335 nodes[node_index] = nodes.back();
340 while (j != edges.size()) {
341 if (edges[j].
from == node || edges[j].
to == node) {
342 edges[j] = edges.back();
360 return reinterpret_cast<void*
>(
static_cast<uintptr_t>(
i));
364 return static_cast<int>(
reinterpret_cast<uintptr_t>(
ptr));
369 for (
int i = 0;
i < 100;
i++) {
381 for (
int x = 1;
x < 25;
x++) {
392 for (
int i = 0;
i < np;
i++) {
434 EXPECT_EQ(
"2 4 6 8 10 ...", Path(2, 12));
450 for (
int i = 0;
i <
N;
i++) {
451 for (
int j = 1; j <
N; j++) {
#define ABSL_PREDICT_FALSE(x)
#define EXPECT_FALSE(condition)
bool InsertEdge(GraphId source_node, GraphId dest_node)
bool HasEdge(GraphId source_node, GraphId dest_node) const
#define ASSERT_NE(val1, val2)
static void PrintEdges(Edges *edges)
static int EdgeIndex(Edges *edges, int from, int to)
static void PrintTransitiveClosure(Nodes *nodes, Edges *edges)
TEST(GraphCycles, RandomizedTest)
bool IsReachable(GraphId source_node, GraphId dest_node) const
#define ASSERT_LE(val1, val2)
#define ABSL_ARRAYSIZE(array)
#define ABSL_NAMESPACE_END
static void PrintGCTransitiveClosure(Nodes *nodes, const IdMap &id, GraphCycles *gc)
void RemoveNode(void *ptr)
static GraphId Get(const IdMap &id, int num)
#define ABSL_NAMESPACE_BEGIN
void RemoveEdge(GraphId source_node, GraphId dest_node)
std::vector< Edge > Edges
std::string Path(int x, int y)
static bool IsReachable(Edges *edges, int from, int to, std::unordered_set< int > *seen)
TEST_F(GraphCyclesTest, NoCycle)
std::mt19937_64 RandomEngine
static int RandomNode(RandomEngine *rng, Nodes *nodes)
_W64 unsigned int uintptr_t
static void PrintGCEdges(Nodes *nodes, const IdMap &id, GraphCycles *gc)
std::map< int, GraphId > IdMap
static void CheckInvariants(const GraphCycles &gc)
void gc(uv_timer_t *handle)
static void CheckEdges(Nodes *nodes, Edges *edges, const IdMap &id, GraphCycles *gc)
#define ASSERT_TRUE(condition)
#define ASSERT_FALSE(condition)
static UnitTest * GetInstance()
static test_node * new_node(size_t i, size_t *ctr)
#define EXPECT_TRUE(condition)
bool AddEdge(int x, int y)
static void CheckTransitiveClosure(Nodes *nodes, Edges *edges, const IdMap &id, GraphCycles *gc)
static int RandomEdge(RandomEngine *rng, Edges *edges)
#define ASSERT_GT(val1, val2)
static int Num(void *ptr)
#define ABSL_RAW_LOG(severity,...)
int FindPath(GraphId source, GraphId dest, int max_path_len, GraphId path[]) const
#define ASSERT_EQ(val1, val2)
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:44