diff --git a/uebung03/test/graph/GraphTest.java b/uebung03/test/graph/GraphTest.java new file mode 100644 index 0000000..64a690c --- /dev/null +++ b/uebung03/test/graph/GraphTest.java @@ -0,0 +1,79 @@ +package graph; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + +public class GraphTest { + + private final Node nodeA = new Node("A"); + private final Node nodeB = new Node("B"); + private final Node nodeC = new Node("C"); + private final Node nodeD = new Node("D"); + private final Node nodeE = new Node("E"); + private final Node nodeF = new Node("F"); + private final Node nodeG = new Node("G"); + private final Node nodeH = new Node("H"); + private final Graph graph = new Graph(); + + @Before + public void setup() { + graph.addNode(nodeA); + graph.addNode(nodeB); + graph.addNode(nodeC); + graph.addNode(nodeD); + graph.addNode(nodeE); + graph.addNode(nodeF); + graph.addNode(nodeG); + nodeA.addEdge(new Edge(3, nodeB)); + nodeB.addEdge(new Edge(7, nodeD)); + nodeD.addEdge(new Edge(1, nodeE)); + nodeE.addEdge(new Edge(2, nodeC)); + nodeE.addEdge(new Edge(2, nodeH)); + nodeE.addEdge(new Edge(4, nodeA)); + nodeF.addEdge(new Edge(3, nodeG)); + nodeH.addEdge(new Edge(6, nodeB)); + } + + @Test + public void testBreadthFirstGraphE() { + final Set result = new HashSet<>(graph.breadthFirst(nodeE)); + assertEquals(Set.of(nodeA, nodeB, nodeC, nodeD, nodeE, nodeH), result); + } + + @Test + public void testBreadthFirstGraphF() { + final Set result = new HashSet<>(graph.breadthFirst(nodeF)); + assertEquals(Set.of(nodeF, nodeG), result); + } + + @Test + public void testBreadthFirstGraphC() { + final ArrayList result = graph.breadthFirst(nodeC); + assertEquals(List.of(nodeC), result); + } + + @Test + public void testDepthFirstGraphE() { + final Set result = new HashSet<>(graph.depthFirst(nodeE)); + assertEquals(Set.of(nodeA, nodeB, nodeC, nodeD, nodeE, nodeH), result); + } + + @Test + public void testDepthFirstGraphF() { + final Set result = new HashSet<>(graph.depthFirst(nodeF)); + assertEquals(Set.of(nodeF, nodeG), result); + } + + @Test + public void testDepthFirstGraphC() { + final ArrayList result = graph.depthFirst(nodeC); + assertEquals(List.of(nodeC), result); + } +} diff --git a/uebung03/test/polynomial/PolynomialTest.java b/uebung03/test/polynomial/PolynomialTest.java new file mode 100644 index 0000000..c39f92b --- /dev/null +++ b/uebung03/test/polynomial/PolynomialTest.java @@ -0,0 +1,69 @@ +package polynomial; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PolynomialTest { + private final Polynomial p1 = new Polynomial(new int[]{1, 2, 3, 4}); + private final Polynomial p2 = new Polynomial(new int[]{0, 0, 0}); + private final Polynomial p3 = new Polynomial(new int[]{3}); + private final Polynomial p4 = new Polynomial(); + private final Polynomial p5 = Polynomial.constant(5); + private final Polynomial p6 = new Polynomial(new int[]{0, -1, 2, +3, -2, 0, 1}); + + @Test + public void testDegree() { + assertEquals(3, p1.getDegree()); + assertEquals(0, p2.getDegree()); + assertEquals(0, p3.getDegree()); + } + + @Test + public void testToString() { + assertEquals("4*x^3 + 3*x^2 + 2*x + 1", p1.toString()); + assertEquals("0", p2.toString()); + assertEquals("3", p3.toString()); + assertEquals("x", p4.toString()); + assertEquals("5", p5.toString()); + assertEquals("x^6 - 2*x^4 + 3*x^3 + 2*x^2 - x", p6.toString()); + } + + @Test + public void testAdd() { + assertEquals("x^6 - 2*x^4 + 7*x^3 + 5*x^2 + x + 1", p1.add(p6).toString()); + } + + @Test + public void testSub() { + assertEquals("-x^6 + 2*x^4 + x^3 + x^2 + 3*x + 1", p1.sub(p6).toString()); + assertEquals("0", p6.sub(p6).toString()); + } + + @Test + public void testMult() { + assertEquals("16*x^6 + 24*x^5 + 25*x^4 + 20*x^3 + 10*x^2 + 4*x + 1", p1.mult(p1).toString()); + assertEquals("4*x^9 + 3*x^8 - 6*x^7 + 7*x^6 + 13*x^5 + 6*x^4 + 4*x^3 - x", p1.mult(p6).toString()); + } + + @Test + public void testOverloaded() { + assertEquals("4*x^3 + 3*x^2 + 2*x + 6", p1.add(5).toString()); + assertEquals("x^6 - 2*x^4 + 3*x^3 + 2*x^2 - x - 5", p6.sub(5).toString()); + assertEquals("20*x^3 + 15*x^2 + 10*x + 5", p1.mult(5).toString()); + } + + @Test + public void testExp() { + assertEquals("1024*x^15 + 3840*x^14 + 8320*x^13 + 13280*x^12 + 16660*x^11 + 17203*x^10 + 14970*x^9 + 11085*x^8 + 7040*x^7 + 3830*x^6 + 1772*x^5 + 690*x^4 + 220*x^3 + 55*x^2 + 10*x + 1", + p1.exp(5).toString()); + } + + @Test + public void testApply() { + assertEquals(1, p1.apply(0)); + assertEquals(10, p1.apply(1)); + assertEquals(49, p1.apply(2)); + assertEquals(3804, p6.apply(4)); + } +}