Path: blob/master/modules/gapi/test/internal/gapi_transactions_test.cpp
16345 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#include "test_precomp.hpp"8#include <ade/graph.hpp>9#include "compiler/transactions.hpp"1011namespace opencv_test12{13namespace14{1516bool contains(const ade::Graph& graph, const ade::NodeHandle& node)17{18auto nodes = graph.nodes();19return nodes.end() != std::find(nodes.begin(), nodes.end(), node);20}2122bool connected(const ade::NodeHandle& src_node, const ade::NodeHandle& dst_node)23{24auto nodes = src_node->outNodes();25return nodes.end() != std::find(nodes.begin(), nodes.end(), dst_node);26}2728struct SimpleGraph29{30// ehs[0] ehs[1] ehs[2] ehs[3]31// nhs[0] -- > nhs[1] --> nhs[2] --> nhs[3] --> nhs[4]3233enum { node_nums = 5 };34ade::Graph graph;35ade::NodeHandle fused_nh; /* For check that fusion node is connected to the36inputs of the prod and the outputs of the cons */37std::array<ade::NodeHandle, node_nums> nhs;38std::array<ade::EdgeHandle, node_nums - 1> ehs;39Change::List changes;4041SimpleGraph()42{43nhs[0] = graph.createNode();44for (int i = 1; i < node_nums; ++i)45{46nhs[i ] = graph.createNode();47ehs[i - 1] = graph.link(nhs[i - 1], nhs[i]);48}49}5051void fuse()52{53// nhs[0] --> fused_nh --> nhs[4]5455fused_nh = graph.createNode();56changes.enqueue<Change::NodeCreated>(fused_nh);57changes.enqueue<Change::NewLink> (graph, nhs[0], fused_nh);58changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[0]);59changes.enqueue<Change::NewLink> (graph, fused_nh, nhs[4]);60changes.enqueue<Change::DropLink>(graph, nhs[3], ehs[3]);61changes.enqueue<Change::DropLink>(graph, nhs[1], ehs[1]);62changes.enqueue<Change::DropLink>(graph, nhs[2], ehs[2]);63changes.enqueue<Change::DropNode>(nhs[1]);64changes.enqueue<Change::DropNode>(nhs[2]);65changes.enqueue<Change::DropNode>(nhs[3]);66}6768void commit() { changes.commit(graph); }69void rollback() { changes.rollback(graph); }7071};7273struct Transactions: public ::testing::Test, public SimpleGraph {};7475} // anonymous namespace7677TEST_F(Transactions, NodeCreated_Create)78{79auto new_nh = graph.createNode();80Change::NodeCreated node_created(new_nh);8182EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));83EXPECT_TRUE(contains(graph, new_nh));84}8586TEST_F(Transactions, NodeCreated_RollBack)87{88auto new_nh = graph.createNode();89Change::NodeCreated node_created(new_nh);9091node_created.rollback(graph);9293EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));94EXPECT_FALSE(contains(graph, new_nh));95}9697TEST_F(Transactions, NodeCreated_Commit)98{99auto new_nh = graph.createNode();100Change::NodeCreated node_created(new_nh);101102node_created.commit(graph);103104EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));105EXPECT_TRUE(contains(graph, new_nh));106}107108TEST_F(Transactions, DropLink_Create)109{110Change::DropLink drop_link(graph, nhs[0], ehs[0]);111112EXPECT_FALSE(connected(nhs[0], nhs[1]));113}114115TEST_F(Transactions, DropLink_RollBack)116{117Change::DropLink drop_link(graph, nhs[0], ehs[0]);118119drop_link.rollback(graph);120121EXPECT_TRUE(connected(nhs[0], nhs[1]));122}123124TEST_F(Transactions, DropLink_Commit)125{126Change::DropLink drop_link(graph, nhs[0], ehs[0]);127128drop_link.commit(graph);129130EXPECT_FALSE(connected(nhs[0], nhs[1]));131}132133TEST_F(Transactions, NewLink_Create)134{135auto new_nh = graph.createNode();136Change::NewLink new_link(graph, new_nh, nhs[0]);137138EXPECT_TRUE(connected(new_nh, nhs[0]));139}140141TEST_F(Transactions, NewLink_RollBack)142{143auto new_nh = graph.createNode();144Change::NewLink new_link(graph, new_nh, nhs[0]);145146new_link.rollback(graph);147148EXPECT_FALSE(connected(new_nh, nhs[0]));149}150151TEST_F(Transactions, NewLink_Commit)152{153auto new_nh = graph.createNode();154Change::NewLink new_link(graph, new_nh, nhs[0]);155156new_link.commit(graph);157158EXPECT_TRUE(connected(new_nh, nhs[0]));159}160161TEST_F(Transactions, DropNode_Create)162{163auto new_nh = graph.createNode();164Change::DropNode drop_node(new_nh);165166EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));167EXPECT_TRUE(contains(graph, new_nh));168}169170TEST_F(Transactions, DropNode_RollBack)171{172auto new_nh = graph.createNode();173Change::DropNode drop_node(new_nh);174175drop_node.rollback(graph);176177EXPECT_EQ(6u, static_cast<std::size_t>(graph.nodes().size()));178EXPECT_TRUE(contains(graph, new_nh));179}180181TEST_F(Transactions, DropNode_Commit)182{183auto new_nh = graph.createNode();184Change::DropNode drop_node(new_nh);185186drop_node.commit(graph);187188EXPECT_EQ(5u, static_cast<std::size_t>(graph.nodes().size()));189EXPECT_FALSE(contains(graph, new_nh));190}191192TEST_F(Transactions, Fusion_Commit)193{194namespace C = Change;195196fuse();197commit();198199EXPECT_EQ(3u, static_cast<std::size_t>(graph.nodes().size()));200EXPECT_TRUE(connected(nhs[0] , fused_nh));201EXPECT_TRUE(connected(fused_nh, nhs[4]));202}203204TEST_F(Transactions, Fusion_RollBack)205{206namespace C = Change;207208fuse();209rollback();210211EXPECT_EQ(static_cast<std::size_t>(node_nums),212static_cast<std::size_t>(graph.nodes().size()));213EXPECT_FALSE(contains(graph, fused_nh));214215for (int i = 0; i < static_cast<int>(node_nums) - 1; ++i)216{217EXPECT_TRUE(connected(nhs[i], nhs[i + 1]));218}219}220221} // opencv_test222223224