| 196 | } |
| 197 | |
| 198 | TEST(GraphCycles, RandomizedTest) { |
| 199 | Nodes nodes; |
| 200 | Edges edges; // from, to |
| 201 | tensorflow::GraphCycles graph_cycles; |
| 202 | static const int kMaxNodes = 7; // use <= 7 nodes to keep test short |
| 203 | static const int kDataOffset = 17; // an offset to the node-specific data |
| 204 | int n = 100000; |
| 205 | int op = 0; |
| 206 | std::mt19937 rnd(tensorflow::testing::RandomSeed() + 1); |
| 207 | |
| 208 | for (int iter = 0; iter != n; iter++) { |
| 209 | if ((iter % 10000) == 0) VLOG(0) << "Iter " << iter << " of " << n; |
| 210 | |
| 211 | if (VLOG_IS_ON(3)) { |
| 212 | LOG(INFO) << "==============="; |
| 213 | LOG(INFO) << "last op " << op; |
| 214 | PrintNodes(&nodes); |
| 215 | PrintEdges(&edges); |
| 216 | PrintGCEdges(&nodes, &graph_cycles); |
| 217 | } |
| 218 | for (int i = 0; i != nodes.size(); i++) { |
| 219 | ASSERT_EQ(reinterpret_cast<intptr_t>(graph_cycles.GetNodeData(i)), |
| 220 | i + kDataOffset) |
| 221 | << " node " << i; |
| 222 | } |
| 223 | CheckEdges(&nodes, &edges, &graph_cycles); |
| 224 | CheckTransitiveClosure(&nodes, &edges, &graph_cycles); |
| 225 | std::uniform_int_distribution<int> distribution(0, 5); |
| 226 | op = distribution(rnd); |
| 227 | switch (op) { |
| 228 | case 0: // Add a node |
| 229 | if (nodes.size() < kMaxNodes) { |
| 230 | int new_node = graph_cycles.NewNode(); |
| 231 | ASSERT_NE(-1, new_node); |
| 232 | VLOG(1) << "adding node " << new_node; |
| 233 | ASSERT_EQ(nullptr, graph_cycles.GetNodeData(new_node)); |
| 234 | graph_cycles.SetNodeData( |
| 235 | new_node, reinterpret_cast<void *>( |
| 236 | static_cast<intptr_t>(new_node + kDataOffset))); |
| 237 | ASSERT_GE(new_node, 0); |
| 238 | for (int i = 0; i != nodes.size(); i++) { |
| 239 | ASSERT_NE(nodes[i], new_node); |
| 240 | } |
| 241 | nodes.push_back(new_node); |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case 1: // Remove a node |
| 246 | if (!nodes.empty()) { |
| 247 | int node_index = RandomNode(&rnd, &nodes); |
| 248 | int node = nodes[node_index]; |
| 249 | nodes[node_index] = nodes.back(); |
| 250 | nodes.pop_back(); |
| 251 | VLOG(1) << "removing node " << node; |
| 252 | graph_cycles.RemoveNode(node); |
| 253 | int i = 0; |
| 254 | while (i != edges.size()) { |
| 255 | if (edges[i].from == node || edges[i].to == node) { |
nothing calls this directly
no test coverage detected