github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/runtime/nr/gtest_graph.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 21 #include "common/common.h" 22 #include "runtime/nr/graph/graph.h" 23 #include <gtest/gtest.h> 24 #include <random> 25 26 TEST(test_runtime_graph, add_edge_simple) { 27 neb::rt::transaction_graph tg; 28 auto s = neb::to_address(std::string("s")); 29 auto t = neb::to_address(std::string("t")); 30 neb::wei_t val = 1; 31 int64_t ts = 1; 32 tg.add_edge(s, t, val, ts); 33 34 auto g = tg.internal_graph(); 35 neb::rt::transaction_graph::viterator_t vi, vi_end; 36 boost::tie(vi, vi_end) = boost::vertices(g); 37 neb::rt::transaction_graph::oeiterator_t oei, oei_end; 38 boost::tie(oei, oei_end) = boost::out_edges(*vi, g); 39 40 auto target = boost::target(*oei, g); 41 std::string target_name = boost::get(boost::vertex_name_t(), g, target); 42 EXPECT_EQ(neb::to_address(target_name), t); 43 44 auto source = boost::source(*oei, g); 45 std::string source_name = boost::get(boost::vertex_name_t(), g, source); 46 EXPECT_EQ(neb::to_address(source_name), s); 47 48 auto w = boost::get(boost::edge_weight_t(), g, *oei); 49 EXPECT_EQ(w, val); 50 auto timestamp = boost::get(boost::edge_timestamp_t(), g, *oei); 51 EXPECT_EQ(timestamp, ts); 52 } 53 54 TEST(test_runtime_graph, add_edge_self_cycle) { 55 neb::rt::transaction_graph tg; 56 auto s = neb::to_address(std::string("s")); 57 neb::wei_t val = 1; 58 int64_t ts = 1; 59 tg.add_edge(s, s, val, ts); 60 61 auto g = tg.internal_graph(); 62 neb::rt::transaction_graph::viterator_t vi, vi_end; 63 boost::tie(vi, vi_end) = boost::vertices(g); 64 neb::rt::transaction_graph::oeiterator_t oei, oei_end; 65 boost::tie(oei, oei_end) = boost::out_edges(*vi, g); 66 67 auto target = boost::target(*oei, g); 68 std::string target_name = boost::get(boost::vertex_name_t(), g, target); 69 EXPECT_EQ(neb::to_address(target_name), s); 70 71 auto source = boost::source(*oei, g); 72 std::string source_name = boost::get(boost::vertex_name_t(), g, source); 73 EXPECT_EQ(neb::to_address(source_name), s); 74 75 auto w = boost::get(boost::edge_weight_t(), g, *oei); 76 EXPECT_EQ(w, val); 77 auto timestamp = boost::get(boost::edge_timestamp_t(), g, *oei); 78 EXPECT_EQ(timestamp, ts); 79 } 80 81 TEST(test_runtime_graph, add_edge_multi) { 82 neb::rt::transaction_graph tg; 83 auto s = neb::to_address(std::string("s")); 84 auto t = neb::to_address(std::string("t")); 85 86 struct edge_t { 87 neb::wei_t m_val; 88 int64_t m_ts; 89 }; 90 std::vector<edge_t> v({{1, 2}, {3, 4}, {5, 6}, {7, 8}}); 91 for (size_t i = 0; i < v.size(); i++) { 92 tg.add_edge(s, t, v[i].m_val, v[i].m_ts); 93 } 94 auto it_edge = v.begin(); 95 96 auto g = tg.internal_graph(); 97 neb::rt::transaction_graph::viterator_t vi, vi_end; 98 99 for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; vi++) { 100 neb::rt::transaction_graph::oeiterator_t oei, oei_end; 101 for (boost::tie(oei, oei_end) = boost::out_edges(*vi, g); oei != oei_end; 102 oei++) { 103 auto source = boost::source(*oei, g); 104 auto target = boost::target(*oei, g); 105 auto ss = neb::to_address(boost::get(boost::vertex_name_t(), g, source)); 106 auto tt = neb::to_address(boost::get(boost::vertex_name_t(), g, target)); 107 neb::wei_t w = boost::get(boost::edge_weight_t(), g, *oei); 108 int64_t ts = boost::get(boost::edge_timestamp_t(), g, *oei); 109 EXPECT_EQ(ss, s); 110 EXPECT_EQ(tt, t); 111 EXPECT_EQ(w, it_edge->m_val); 112 EXPECT_EQ(ts, it_edge->m_ts); 113 it_edge++; 114 } 115 } 116 } 117 118 struct edge_t { 119 neb::address_t m_s; 120 neb::address_t m_t; 121 neb::wei_t m_val; 122 int64_t m_ts; 123 }; 124 125 TEST(test_runtime_graph, add_edge_random) { 126 neb::rt::transaction_graph tg; 127 128 size_t ch_size = 'z' - 'a'; 129 std::random_device rd; 130 std::mt19937 mt(rd()); 131 std::uniform_int_distribution<> dis(0, ch_size); 132 size_t edge_size = dis(mt) * dis(mt); 133 edge_size++; 134 135 std::vector<edge_t> v; 136 for (size_t i = 0; i < edge_size; i++) { 137 char ch_s = 'a' + dis(mt); 138 char ch_t = 'a' + dis(mt); 139 v.push_back(edge_t{neb::to_address(std::string(1, ch_s)), 140 neb::to_address(std::string(1, ch_t)), dis(mt), 141 dis(mt)}); 142 tg.add_edge(v[i].m_s, v[i].m_t, v[i].m_val, v[i].m_ts); 143 } 144 145 auto g = tg.internal_graph(); 146 neb::rt::transaction_graph::viterator_t vi, vi_end; 147 148 for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; vi++) { 149 neb::rt::transaction_graph::oeiterator_t oei, oei_end; 150 for (boost::tie(oei, oei_end) = boost::out_edges(*vi, g); oei != oei_end; 151 oei++) { 152 auto source = boost::source(*oei, g); 153 auto target = boost::target(*oei, g); 154 auto ss = neb::to_address(boost::get(boost::vertex_name_t(), g, source)); 155 auto tt = neb::to_address(boost::get(boost::vertex_name_t(), g, target)); 156 neb::wei_t w = boost::get(boost::edge_weight_t(), g, *oei); 157 int64_t ts = boost::get(boost::edge_timestamp_t(), g, *oei); 158 159 bool is_found = false; 160 for (auto &e : v) { 161 if (e.m_s == ss && e.m_t == tt && e.m_val == w && e.m_ts == ts) { 162 is_found = true; 163 break; 164 } 165 } 166 EXPECT_TRUE(is_found); 167 } 168 } 169 } 170 171 TEST(test_runtime_graph, build_graph_from_internal) { 172 neb::rt::transaction_graph tg; 173 174 size_t ch_size = 'z' - 'a'; 175 std::random_device rd; 176 std::mt19937 mt(rd()); 177 std::uniform_int_distribution<> dis(0, ch_size); 178 size_t edge_size = dis(mt) * dis(mt); 179 edge_size++; 180 181 std::vector<edge_t> v; 182 for (size_t i = 0; i < edge_size; i++) { 183 char ch_s = 'a' + dis(mt); 184 char ch_t = 'a' + dis(mt); 185 v.push_back(edge_t{neb::to_address(std::string(1, ch_s)), 186 neb::to_address(std::string(1, ch_t)), dis(mt), 187 dis(mt)}); 188 tg.add_edge(v[i].m_s, v[i].m_t, v[i].m_val, v[i].m_ts); 189 } 190 191 auto g = tg.internal_graph(); 192 auto tg_ptr = neb::rt::build_graph_from_internal(g); 193 auto gg = tg_ptr->internal_graph(); 194 195 neb::rt::transaction_graph::viterator_t vi, vi_end; 196 for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; vi++) { 197 neb::rt::transaction_graph::oeiterator_t oei, oei_end; 198 for (boost::tie(oei, oei_end) = boost::out_edges(*vi, g); oei != oei_end; 199 oei++) { 200 auto source = boost::source(*oei, g); 201 auto target = boost::target(*oei, g); 202 auto ss = neb::to_address(boost::get(boost::vertex_name_t(), g, source)); 203 auto tt = neb::to_address(boost::get(boost::vertex_name_t(), g, target)); 204 neb::wei_t w = boost::get(boost::edge_weight_t(), g, *oei); 205 int64_t ts = boost::get(boost::edge_timestamp_t(), g, *oei); 206 207 bool is_found = false; 208 neb::rt::transaction_graph::viterator_t _vi, _vi_end; 209 for (boost::tie(_vi, _vi_end) = boost::vertices(gg); _vi != _vi_end; 210 _vi++) { 211 neb::rt::transaction_graph::oeiterator_t _oei, _oei_end; 212 for (boost::tie(_oei, _oei_end) = boost::out_edges(*_vi, gg); 213 _oei != _oei_end; _oei++) { 214 auto _source = boost::source(*_oei, gg); 215 auto _target = boost::target(*_oei, gg); 216 auto _ss = 217 neb::to_address(boost::get(boost::vertex_name_t(), gg, _source)); 218 auto _tt = 219 neb::to_address(boost::get(boost::vertex_name_t(), gg, _target)); 220 neb::wei_t _w = boost::get(boost::edge_weight_t(), gg, *_oei); 221 int64_t _ts = boost::get(boost::edge_timestamp_t(), gg, *_oei); 222 if (ss == _ss && tt == _tt & w == _w && ts == _ts) { 223 is_found = true; 224 break; 225 } 226 } 227 } 228 EXPECT_TRUE(is_found); 229 } 230 } 231 } 232