aboutsummaryrefslogtreecommitdiffstats
path: root/host/tests/graph_search_test.cpp
blob: 8d35b3b1a135e7a47d464684629c82fdfc10f697 (plain)
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
//
// Copyright 2014-2016 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include "graph.hpp"
#include <boost/test/unit_test.hpp>
#include <iostream>

using namespace uhd::rfnoc;

// test class derived, this is what we search for
class result_node : public test_node
{
public:
    typedef boost::shared_ptr<result_node> sptr;

    result_node(const std::string &test_id) : test_node(test_id) {};

}; /* class result_node */

#define MAKE_RESULT_NODE(name) result_node::sptr name(new result_node(#name));

BOOST_AUTO_TEST_CASE(test_simplest_downstream_search)
{
    MAKE_NODE(node_A);
    MAKE_NODE(node_B);

    // Simplest possible scenario: Connect B downstream of A and let
    // A find B
    connect_nodes(node_A, node_B);

    test_node::sptr result = node_A->find_downstream_node<test_node>()[0];
    BOOST_REQUIRE(result);
    BOOST_CHECK_EQUAL(result->get_test_id(), "node_B");
}

BOOST_AUTO_TEST_CASE(test_simple_downstream_search)
{
    MAKE_NODE(node_A);
    MAKE_NODE(node_B0);
    MAKE_NODE(node_B1);

    // Simple scenario: Connect both B{1,2} downstream of A and let
    // it find them
    connect_nodes(node_A, node_B0);
    connect_nodes(node_A, node_B1);

    // We're still searching for test_node, so any downstream block will match
    std::vector< test_node::sptr > result = node_A->find_downstream_node<test_node>();
    BOOST_REQUIRE(result.size() == 2);
    BOOST_CHECK(
            (result[0]->get_test_id() == "node_B0" and result[1]->get_test_id() == "node_B1") or
            (result[1]->get_test_id() == "node_B0" and result[0]->get_test_id() == "node_B1")
    );
    BOOST_CHECK(result[0] == node_B0 or result[0] == node_B1);
}

BOOST_AUTO_TEST_CASE(test_linear_downstream_search)
{
    MAKE_NODE(node_A);
    MAKE_RESULT_NODE(node_B);
    MAKE_RESULT_NODE(node_C);

    // Slightly more complex graph:
    connect_nodes(node_A, node_B);
    connect_nodes(node_B, node_C);

    // This time, we search for result_node
    std::vector< result_node::sptr > result = node_A->find_downstream_node<result_node>();
    std::cout << "size: " << result.size() << std::endl;
    BOOST_CHECK_EQUAL(result.size(), 1);
    BOOST_CHECK_EQUAL(result[0]->get_test_id(), "node_B");
    for(const result_node::sptr &node:  result) {
        std::cout << node->get_test_id() << std::endl;
    }
}

BOOST_AUTO_TEST_CASE(test_multi_iter_downstream_search)
{
    MAKE_NODE(node_A);
    MAKE_NODE(node_B0);
    MAKE_NODE(node_B1);
    MAKE_NODE(node_C0);
    MAKE_RESULT_NODE(node_C1);
    MAKE_RESULT_NODE(node_C2);
    MAKE_RESULT_NODE(node_C3);
    MAKE_RESULT_NODE(node_D0);

    // Slightly more complex graph:
    connect_nodes(node_A, node_B0);
    connect_nodes(node_A, node_B1);
    connect_nodes(node_B0, node_C0);
    connect_nodes(node_B0, node_C1);
    connect_nodes(node_B1, node_C2);
    connect_nodes(node_B1, node_C3);
    connect_nodes(node_C0, node_D0);

    // This time, we search for result_node
    std::vector< result_node::sptr > result = node_A->find_downstream_node<result_node>();
    BOOST_REQUIRE(result.size() == 4);
    for(const result_node::sptr &node:  result) {
        std::cout << node->get_test_id() << std::endl;
    }
}

BOOST_AUTO_TEST_CASE(test_multi_iter_cycle_downstream_search)
{
    MAKE_NODE(node_A);
    MAKE_NODE(node_B0);
    MAKE_NODE(node_B1);
    MAKE_NODE(node_C0);
    MAKE_RESULT_NODE(node_C1);
    MAKE_RESULT_NODE(node_C2);
    MAKE_RESULT_NODE(node_C3);
    MAKE_RESULT_NODE(node_D0);

    // Slightly more complex graph:
    connect_nodes(node_A, node_B0);
    // This connection goes both ways, causing a cycle
    connect_nodes(node_A, node_B1); connect_nodes(node_B1, node_A);
    connect_nodes(node_B0, node_C0);
    connect_nodes(node_B0, node_C1);
    connect_nodes(node_B1, node_C2);
    connect_nodes(node_B1, node_C3);
    connect_nodes(node_C0, node_D0);

    // This time, we search for result_node
    std::vector< result_node::sptr > result = node_A->find_downstream_node<result_node>();
    BOOST_REQUIRE(result.size() == 4);
    for(const result_node::sptr &node:  result) {
        std::cout << node->get_test_id() << std::endl;
    }
}

BOOST_AUTO_TEST_CASE(test_mini_cycle_downstream_and_upstream)
{
    MAKE_NODE(node_A);
    MAKE_NODE(node_B);

    // Connect them in a loop
    connect_nodes(node_A, node_B); connect_nodes(node_B, node_A);

    std::vector< test_node::sptr > result;
    result = node_A->find_downstream_node<test_node>();
    BOOST_REQUIRE_EQUAL(result.size(), 1);
    BOOST_REQUIRE(result[0] == node_B);
    result = node_B->find_downstream_node<test_node>();
    BOOST_REQUIRE_EQUAL(result.size(), 1);
    BOOST_REQUIRE(result[0] == node_A);
    result = node_A->find_upstream_node<test_node>();
    BOOST_REQUIRE_EQUAL(result.size(), 1);
    BOOST_REQUIRE(result[0] == node_B);
    result = node_B->find_upstream_node<test_node>();
    BOOST_REQUIRE_EQUAL(result.size(), 1);
    BOOST_REQUIRE(result[0] == node_A);
}