1 /*
2  * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 package com.sun.hotspot.igv.data;
26 
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
32 import org.junit.*;
33 
34 /**
35  *
36  * @author Thomas Wuerthinger
37  */
38 public class InputGraphTest {
39 
40     /**
41      *    1
42      *   / \
43      *  2   3
44      *   \  |  5
45      *    \ | /
46      *      4
47      */
48     private static InputGraph referenceGraph;
49 
50     private static InputGraph emptyGraph;
51 
52     private static final InputNode N1 = new InputNode(1);
53     private static final InputNode N2 = new InputNode(2);
54     private static final InputNode N3 = new InputNode(3);
55     private static final InputNode N4 = new InputNode(4);
56     private static final InputNode N5 = new InputNode(5);
57     private static final InputEdge E12 = new InputEdge((char)0, 1, 2);
58     private static final InputEdge E13 = new InputEdge((char)0, 1, 3);
59     private static final InputEdge E24 = new InputEdge((char)0, 2, 4);
60     private static final InputEdge E34 = new InputEdge((char)0, 3, 4);
61     private static final InputEdge E54 = new InputEdge((char)0, 5, 4);
62 
InputGraphTest()63     public InputGraphTest() {
64     }
65 
66     @BeforeClass
setUpClass()67     public static void setUpClass() throws Exception {
68         Group group = new Group(null);
69 
70         emptyGraph = new InputGraph("emptyGraph");
71         group.addElement(emptyGraph);
72 
73         referenceGraph = new InputGraph("referenceGraph");
74         group.addElement(referenceGraph);
75         referenceGraph.addNode(N1);
76         referenceGraph.addNode(N2);
77         referenceGraph.addNode(N3);
78         referenceGraph.addNode(N4);
79         referenceGraph.addNode(N5);
80 
81         referenceGraph.addEdge(E12);
82         referenceGraph.addEdge(E13);
83         referenceGraph.addEdge(E24);
84         referenceGraph.addEdge(E34);
85         referenceGraph.addEdge(E54);
86     }
87 
88     @AfterClass
tearDownClass()89     public static void tearDownClass() throws Exception {
90     }
91 
92     @Before
setUp()93     public void setUp() {
94     }
95 
96     @After
tearDown()97     public void tearDown() {
98     }
99 
100     /**
101      * Test of equals method, of class InputGraph.
102      */
103     @Test
testEquals()104     public void testEquals() {
105 
106         Group parentA = new Group(null);
107         InputGraph a = new InputGraph("graph");
108         parentA.addElement(a);
109 
110         Group parentB = new Group(null);
111         InputGraph b = new InputGraph("graph");
112         parentB.addElement(b);
113 
114         InputGraph c = new InputGraph("graph");
115         parentB.addElement(b);
116 
117         Util.assertGraphEquals(a, b);
118         Util.assertGraphEquals(b, c);
119 
120         a.addNode(new InputNode(1));
121         Util.assertGraphNotEquals(a, b);
122 
123         b.addNode(new InputNode(1));
124         Util.assertGraphEquals(a, b);
125     }
126 
127     /**
128      * Test of findRootNodes method, of class InputGraph.
129      */
130     @Test
testFindRootNodes()131     public void testFindRootNodes() {
132         assertTrue(emptyGraph.findRootNodes().isEmpty());
133 
134         List<InputNode> result = referenceGraph.findRootNodes();
135         assertTrue(result.size() == 2);
136         assertTrue(result.contains(N1));
137         assertTrue(result.contains(N5));
138     }
139 
140     /**
141      * Test of findAllOutgoingEdges method, of class InputGraph.
142      */
143     @Test
testFindAllOutgoingEdges()144     public void testFindAllOutgoingEdges() {
145         assertTrue(emptyGraph.findAllOutgoingEdges().isEmpty());
146 
147         Map<InputNode, List<InputEdge>> result = referenceGraph.findAllOutgoingEdges();
148         assertTrue(result.size() == 5);
149         assertEquals(result.get(N1), Arrays.asList(E12, E13));
150         assertEquals(result.get(N2), Arrays.asList(E24));
151         assertEquals(result.get(N3), Arrays.asList(E34));
152         assertEquals(result.get(N4), Arrays.asList());
153         assertEquals(result.get(N5), Arrays.asList(E54));
154     }
155 
156     /**
157      * Test of findAllIngoingEdges method, of class InputGraph.
158      */
159     @Test
testFindAllIngoingEdges()160     public void testFindAllIngoingEdges() {
161         assertTrue(emptyGraph.findAllIngoingEdges().isEmpty());
162 
163         Map<InputNode, List<InputEdge>> result = referenceGraph.findAllIngoingEdges();
164         assertTrue(result.size() == 5);
165         assertEquals(result.get(N1), Arrays.asList());
166         assertEquals(result.get(N2), Arrays.asList(E12));
167         assertEquals(result.get(N3), Arrays.asList(E13));
168         assertEquals(result.get(N4), Arrays.asList(E24, E34, E54));
169         assertEquals(result.get(N5), Arrays.asList());
170     }
171 
172     /**
173      * Test of findOutgoingEdges method, of class InputGraph.
174      */
175     @Test
testFindOutgoingEdges()176     public void testFindOutgoingEdges() {
177         assertTrue(emptyGraph.findOutgoingEdges(new InputNode(1)).isEmpty());
178 
179         assertEquals(referenceGraph.findOutgoingEdges(N1), Arrays.asList(E12, E13));
180         assertEquals(referenceGraph.findOutgoingEdges(N2), Arrays.asList(E24));
181         assertEquals(referenceGraph.findOutgoingEdges(N3), Arrays.asList(E34));
182         assertEquals(referenceGraph.findOutgoingEdges(N4), Arrays.asList());
183         assertEquals(referenceGraph.findOutgoingEdges(N5), Arrays.asList(E54));
184     }
185 
186     /**
187      * Test of getNext method, of class InputGraph.
188      */
189     @Test
testGetNextPrev()190     public void testGetNextPrev() {
191         final Group group = new Group(null);
192 
193         final InputGraph a = new InputGraph("a");
194 
195         final InputGraph b = new InputGraph("b");
196 
197         final InputGraph c = new InputGraph("c");
198         group.addElement(a);
199         group.addElement(b);
200         group.addElement(c);
201 
202         assertEquals(null, a.getPrev());
203         assertEquals(b, a.getNext());
204 
205         assertEquals(a, b.getPrev());
206         assertEquals(c, b.getNext());
207 
208         assertEquals(b, c.getPrev());
209         assertEquals(null, c.getNext());
210     }
211 }
212