File: BinaryMatchImplTest.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (123 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download | duplicates (2)
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
#include <gtest/gtest.h>
#include <set>

#include "test_util.h"

#include "nomnigraph/Converters/Dot.h"
#include "nomnigraph/Graph/Algorithms.h"
#include "nomnigraph/Graph/Graph.h"

TEST(BinaryMatch, NoMatch) {
  auto graph = createGraph();
  auto matches = nom::algorithm::binaryMatch(
      &graph, [](decltype(graph)::NodeRef n) { return false; });
  EXPECT_EQ(matches.size(), 0);
}

TEST(BinaryMatch, AllMatch) {
  auto graph = createGraph();
  auto matches = nom::algorithm::binaryMatch(
      &graph, [](decltype(graph)::NodeRef n) { return true; });
  EXPECT_EQ(matches.size(), 1);
  EXPECT_EQ(matches.front().getNodesCount(), graph.getMutableNodes().size());
}

TEST(BinaryMatch, EmptyGraph) {
  nom::Graph<std::string> graph;
  auto matches = nom::algorithm::binaryMatch(
      &graph, [](decltype(graph)::NodeRef n) { return true; });
  EXPECT_EQ(matches.size(), 0);
}

// We should get this back:
// +---+     +-------+
// | 4 | <-- |   2   |
// +---+     +-------+
//   |         |
//   |         |
//   |         v
//   |       +-------+
//   |       |   3   |
//   |       +-------+
//   |         |
//   |         |
//   |         v
//   |       +-------+
//   +-----> |   6   |
//           +-------+
TEST(BinaryMatch, Basic) {
  auto graph = createGraph();
  auto matches =
      nom::algorithm::binaryMatch(&graph, [](decltype(graph)::NodeRef n) {
        if (n->data() == "2" || n->data() == "3" || n->data() == "4" ||
            n->data() == "6") {
          return true;
        }
        return false;
      });

  EXPECT_EQ(matches.size(), 1);
  auto match = matches.front();
  EXPECT_EQ(match.getNodesCount(), 4);
  std::set<std::string> exp{"2", "3", "4", "6"};
  for (auto n : match.getNodes()) {
    EXPECT_EQ(exp.count(n->data()), 1);
    exp.erase(n->data());
  }

  // We found all the those nodes.
  EXPECT_EQ(exp.size(), 0);
}

// The interesting bit about this test case is that
// the predicate does not match on 3.
//
// As such, this part of the graph
// +---+     +-------+
// | 4 | <-- |   2   |
// +---+     +-------+
//   |         |
//   |         |
//   |         v
//   |       +-------+
//   |       |   3   |
//   |       +-------+
//   |         |
//   |         |
//   |         v
//   |       +-------+
//   +-----> |   6   |
//           +-------+
//
// should match as { 4, 2 }, { 6 } not { 4, 2, 6 }
TEST(BinaryMatch, RemovedMiddleNode) {
  auto graph = createGraph();
  auto matches =
      nom::algorithm::binaryMatch(&graph, [](decltype(graph)::NodeRef n) {
        if (n->data() == "2" || n->data() == "4" || n->data() == "6") {
          return true;
        }
        return false;
      });

  EXPECT_EQ(matches.size(), 2);
  auto match1 = matches.front();
  auto match2 = matches.back();

  EXPECT_EQ(match1.getNodesCount(), 2);
  EXPECT_EQ(match2.getNodesCount(), 1);

  std::set<std::string> exp1{"2", "4"};
  std::set<std::string> exp2{"6"};
  for (auto n : match1.getNodes()) {
    EXPECT_EQ(exp1.count(n->data()), 1);
    exp1.erase(n->data());
  }
  for (auto n : match2.getNodes()) {
    EXPECT_EQ(exp2.count(n->data()), 1);
    exp2.erase(n->data());
  }

  EXPECT_EQ(exp1.size(), 0);
  EXPECT_EQ(exp2.size(), 0);
}