File indexing completed on 2024-12-22 04:12:52

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_node_dummies_graph_test.h"
0008 
0009 #include <simpletest.h>
0010 
0011 #include "kis_node_dummies_graph.h"
0012 #include "node_shapes_utils.h"
0013 
0014 inline KisNodeDummy* nodeDummyFromId(int id) {
0015     KisNodeShape *nodeShape = nodeShapeFromId(id);
0016     return new KisNodeDummy(nodeShape, nodeShape->node());
0017 }
0018 
0019 
0020 /**
0021  * node0
0022  * +--node1
0023  *    +--node7
0024  *    +--node8
0025  * +--node2
0026  * +--node3
0027  * +--node4
0028  *    +--node9
0029  *    +--node10
0030  *    +--node11
0031  * +--node5
0032  * +--node6
0033  */
0034 
0035 void KisNodeDummiesGraphTest::init()
0036 {
0037     m_dummiesGraph = new KisNodeDummiesGraph();
0038     m_rootDummy = nodeDummyFromId(0);
0039 
0040     m_dummiesGraph->addNode(m_rootDummy, 0, 0);
0041     KisNodeDummy *parent;
0042 
0043     parent = m_rootDummy;
0044     for(int i = 6; i >= 1; i--) {
0045         KisNodeDummy *dummy = nodeDummyFromId(i);
0046         m_dummiesGraph->addNode(dummy, parent, 0);
0047     }
0048 
0049     parent = findDummyById(m_rootDummy, 1);
0050     Q_ASSERT(parent);
0051     for(int i = 8; i >= 7; i--) {
0052         KisNodeDummy *dummy = nodeDummyFromId(i);
0053         m_dummiesGraph->addNode(dummy, parent, 0);
0054     }
0055 
0056     parent = findDummyById(m_rootDummy, 4);
0057     Q_ASSERT(parent);
0058     for(int i = 11; i >= 9; i--) {
0059         KisNodeDummy *dummy = nodeDummyFromId(i);
0060         m_dummiesGraph->addNode(dummy, parent, 0);
0061     }
0062 
0063     QString realGraph = collectGraphPattern(m_rootDummy);
0064     QString expectedGraph = "0 1 7 8 2 3 4 9 10 11 5 6";
0065 
0066     QCOMPARE(realGraph, expectedGraph);
0067 }
0068 
0069 void KisNodeDummiesGraphTest::cleanup()
0070 {
0071     delete m_rootDummy;
0072     delete m_dummiesGraph;
0073 }
0074 
0075 void KisNodeDummiesGraphTest::testIndexing()
0076 {
0077     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0078     KisNodeDummy *dummy10 = findDummyById(m_rootDummy, 10);
0079 
0080     QCOMPARE(parent->childCount(), 3);
0081     QCOMPARE(parent->indexOf(dummy10), 1);
0082     QCOMPARE(parent->at(1), dummy10);
0083 }
0084 
0085 void KisNodeDummiesGraphTest::testPrepend()
0086 {
0087     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0088     KisNodeDummy *dummy = nodeDummyFromId(13);
0089     m_dummiesGraph->addNode(dummy, parent, 0);
0090 
0091     QString realGraph = collectGraphPattern(m_rootDummy);
0092     QString expectedGraph = "0 1 7 8 2 3 4 13 9 10 11 5 6";
0093 
0094     QCOMPARE(realGraph, expectedGraph);
0095 }
0096 
0097 void KisNodeDummiesGraphTest::testAppend()
0098 {
0099     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0100     KisNodeDummy *dummy = nodeDummyFromId(13);
0101     m_dummiesGraph->addNode(dummy, parent, parent->lastChild());
0102 
0103     QString realGraph = collectGraphPattern(m_rootDummy);
0104     QString expectedGraph = "0 1 7 8 2 3 4 9 10 11 13 5 6";
0105 
0106     QCOMPARE(realGraph, expectedGraph);
0107 }
0108 
0109 void KisNodeDummiesGraphTest::testInsert()
0110 {
0111     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0112     KisNodeDummy *aboveThis = findDummyById(m_rootDummy, 10);
0113 
0114     KisNodeDummy *dummy = nodeDummyFromId(13);
0115     m_dummiesGraph->addNode(dummy, parent, aboveThis);
0116 
0117     QString realGraph = collectGraphPattern(m_rootDummy);
0118     QString expectedGraph = "0 1 7 8 2 3 4 9 10 13 11 5 6";
0119 
0120     QCOMPARE(realGraph, expectedGraph);
0121 }
0122 
0123 void KisNodeDummiesGraphTest::testNewSubgraph()
0124 {
0125     KisNodeDummy *parent = findDummyById(m_rootDummy, 3);
0126 
0127     KisNodeDummy *dummy = nodeDummyFromId(13);
0128     m_dummiesGraph->addNode(dummy, parent, 0);
0129 
0130     QString realGraph = collectGraphPattern(m_rootDummy);
0131     QString expectedGraph = "0 1 7 8 2 3 13 4 9 10 11 5 6";
0132 
0133     QCOMPARE(realGraph, expectedGraph);
0134 }
0135 
0136 void KisNodeDummiesGraphTest::testRemoveFirst()
0137 {
0138     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0139     KisNodeDummy *child = parent->firstChild();
0140     m_dummiesGraph->removeNode(child);
0141     delete child;
0142 
0143     QString realGraph = collectGraphPattern(m_rootDummy);
0144     QString expectedGraph = "0 1 7 8 2 3 4 10 11 5 6";
0145 
0146     QCOMPARE(realGraph, expectedGraph);
0147 }
0148 
0149 void KisNodeDummiesGraphTest::testRemoveLast()
0150 {
0151     KisNodeDummy *parent = findDummyById(m_rootDummy, 4);
0152     KisNodeDummy *child = parent->lastChild();
0153     m_dummiesGraph->removeNode(child);
0154     delete child;
0155 
0156     QString realGraph = collectGraphPattern(m_rootDummy);
0157     QString expectedGraph = "0 1 7 8 2 3 4 9 10 5 6";
0158 
0159     QCOMPARE(realGraph, expectedGraph);
0160 }
0161 
0162 void KisNodeDummiesGraphTest::testRemoveBranch()
0163 {
0164     KisNodeDummy *parent = findDummyById(m_rootDummy, 1);
0165     KisNodeDummy *child;
0166 
0167     child = parent->firstChild();
0168     m_dummiesGraph->removeNode(child);
0169     delete child;
0170 
0171     child = parent->lastChild();
0172     m_dummiesGraph->removeNode(child);
0173     delete child;
0174 
0175     QString realGraph = collectGraphPattern(m_rootDummy);
0176     QString expectedGraph = "0 1 2 3 4 9 10 11 5 6";
0177 
0178     QCOMPARE(realGraph, expectedGraph);
0179 }
0180 
0181 void KisNodeDummiesGraphTest::testReverseTraversing()
0182 {
0183     QString forwardGraph = collectGraphPattern(m_rootDummy);
0184     QString reverseGraph = collectGraphPatternReverse(m_rootDummy);
0185 
0186     QCOMPARE(reverseGraph, forwardGraph);
0187 }
0188 
0189 SIMPLE_TEST_MAIN(KisNodeDummiesGraphTest)