1 /* 2 * [The "BSD license"] 3 * Copyright (c) 2010 Terence Parr 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.test; 29 30 import org.antlr.misc.Graph; 31 import org.junit.Test; 32 33 import java.util.List; 34 35 import static org.junit.Assert.*; 36 37 /** Test topo sort in GraphNode. */ 38 public class TestTopologicalSort extends BaseTest { 39 @Test testFairlyLargeGraph()40 public void testFairlyLargeGraph() throws Exception { 41 Graph<String> g = new Graph<String>(); 42 g.addEdge("C", "F"); 43 g.addEdge("C", "G"); 44 g.addEdge("C", "A"); 45 g.addEdge("C", "B"); 46 g.addEdge("A", "D"); 47 g.addEdge("A", "E"); 48 g.addEdge("B", "E"); 49 g.addEdge("D", "E"); 50 g.addEdge("D", "F"); 51 g.addEdge("F", "H"); 52 g.addEdge("E", "F"); 53 54 String expecting = "[H, F, E, D, G, A, B, C]"; 55 List<String> nodes = g.sort(); 56 String result = nodes.toString(); 57 assertEquals(expecting, result); 58 } 59 60 @Test testCyclicGraph()61 public void testCyclicGraph() throws Exception { 62 Graph<String> g = new Graph<String>(); 63 g.addEdge("A", "B"); 64 g.addEdge("B", "C"); 65 g.addEdge("C", "A"); 66 g.addEdge("C", "D"); 67 68 String expecting = "[D, C, B, A]"; 69 List<String> nodes = g.sort(); 70 String result = nodes.toString(); 71 assertEquals(expecting, result); 72 } 73 74 @Test testRepeatedEdges()75 public void testRepeatedEdges() throws Exception { 76 Graph<String> g = new Graph<String>(); 77 g.addEdge("A", "B"); 78 g.addEdge("B", "C"); 79 g.addEdge("A", "B"); // dup 80 g.addEdge("C", "D"); 81 82 String expecting = "[D, C, B, A]"; 83 List<String> nodes = g.sort(); 84 String result = nodes.toString(); 85 assertEquals(expecting, result); 86 } 87 88 @Test testSimpleTokenDependence()89 public void testSimpleTokenDependence() throws Exception { 90 Graph<String> g = new Graph<String>(); 91 g.addEdge("Java.g", "MyJava.tokens"); // Java feeds off manual token file 92 g.addEdge("Java.tokens", "Java.g"); 93 g.addEdge("Def.g", "Java.tokens"); // walkers feed off generated tokens 94 g.addEdge("Ref.g", "Java.tokens"); 95 96 String expecting = "[MyJava.tokens, Java.g, Java.tokens, Ref.g, Def.g]"; 97 List<String> nodes = g.sort(); 98 String result = nodes.toString(); 99 assertEquals(expecting, result); 100 } 101 102 @Test testParserLexerCombo()103 public void testParserLexerCombo() throws Exception { 104 Graph<String> g = new Graph<String>(); 105 g.addEdge("JavaLexer.tokens", "JavaLexer.g"); 106 g.addEdge("JavaParser.g", "JavaLexer.tokens"); 107 g.addEdge("Def.g", "JavaLexer.tokens"); 108 g.addEdge("Ref.g", "JavaLexer.tokens"); 109 110 String expecting = "[JavaLexer.g, JavaLexer.tokens, JavaParser.g, Ref.g, Def.g]"; 111 List<String> nodes = g.sort(); 112 String result = nodes.toString(); 113 assertEquals(expecting, result); 114 } 115 } 116