Esempio n. 1
0
        public void T_GraphArc()
        {
            // Arrange
            GraphNode graphNodeBgn = new GraphNode(new Cell());
            GraphNode graphNodeEnd = new GraphNode(new Cell());

            // Act
            GraphArc      graphArc = new GraphArc(graphNodeBgn, graphNodeEnd);
            PrivateObject po       = new PrivateObject(graphArc);

            // Assert
            Assert.AreEqual((GraphNode)po.GetField("node1"), graphNodeBgn, "Wrong first node.");
            Assert.AreEqual((GraphNode)po.GetField("node2"), graphNodeEnd, "Wrong second node.");
        }
Esempio n. 2
0
        public void T_IsConsistant_True_SameSquare()
        {
            // Arrange
            CSP           csp = new CSP();
            PrivateObject obj = new PrivateObject(csp);

            GraphNode gn0 = new GraphNode(new Cell(0, 2));

            gn0.Cell.Value = '9';
            GraphNode gn1 = new GraphNode(new Cell(1, 1));

            gn1.Cell.Value = '2';
            GraphNode gn2 = new GraphNode(new Cell(2, 0));

            gn2.Cell.Value = '3';
            csp.Nodes.Add(gn0);
            csp.Nodes.Add(gn1);
            csp.Nodes.Add(gn2);

            GraphArc ga01 = new GraphArc(gn0, gn1);
            GraphArc ga02 = new GraphArc(gn0, gn2);

            gn0.ConnectedArcs = new List <GraphArc> {
                ga01, ga02
            };
            GraphArc ga10 = new GraphArc(gn1, gn0);
            GraphArc ga12 = new GraphArc(gn1, gn2);

            gn1.ConnectedArcs = new List <GraphArc> {
                ga10, ga12
            };
            GraphArc ga20 = new GraphArc(gn2, gn0);
            GraphArc ga21 = new GraphArc(gn2, gn1);

            gn2.ConnectedArcs = new List <GraphArc> {
                ga20, ga21
            };

            // Act
            var isConsistant = obj.Invoke("IsConsistant");

            // Assert
            Assert.IsInstanceOfType(isConsistant, typeof(bool), "Wrong type.");
            Assert.IsTrue((bool)isConsistant, "Wrong value.");
        }
Esempio n. 3
0
        public void T_IsConsistant_WithParameter_False_SameColumn()
        {
            // Arrange
            CSP           csp = new CSP();
            PrivateObject obj = new PrivateObject(csp);

            GraphNode gn0 = new GraphNode(new Cell(0, 0));

            gn0.Cell.Value = '9';
            GraphNode gn1 = new GraphNode(new Cell(3, 0));

            gn1.Cell.Value = '9';
            GraphNode gn2 = new GraphNode(new Cell(8, 0));

            gn2.Cell.Value = '1';
            csp.Nodes.Add(gn0);
            csp.Nodes.Add(gn1);
            csp.Nodes.Add(gn2);

            GraphArc ga01 = new GraphArc(gn0, gn1);
            GraphArc ga02 = new GraphArc(gn0, gn2);

            gn0.ConnectedArcs = new List <GraphArc> {
                ga01, ga02
            };
            GraphArc ga10 = new GraphArc(gn1, gn0);
            GraphArc ga12 = new GraphArc(gn1, gn2);

            gn1.ConnectedArcs = new List <GraphArc> {
                ga10, ga12
            };
            GraphArc ga20 = new GraphArc(gn2, gn0);
            GraphArc ga21 = new GraphArc(gn2, gn1);

            gn2.ConnectedArcs = new List <GraphArc> {
                ga20, ga21
            };

            // Act
            var isConsistant = obj.Invoke("IsConsistant", gn0);

            // Assert
            Assert.IsInstanceOfType(isConsistant, typeof(bool), "Wrong type.");
            Assert.IsFalse((bool)isConsistant, "Wrong value.");
        }
Esempio n. 4
0
        public void T_IsConsistant_False()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = '1';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            bool isConsistant = graphArc.IsConsistant();

            // Assert
            Assert.IsFalse(isConsistant);
        }
Esempio n. 5
0
        public void T_Equals_WithObject_False_DifferentTypes()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            bool isEqual = graphArc.Equals(graphNodeBgn);

            // Assert
            Assert.IsFalse(isEqual);
        }
Esempio n. 6
0
        public void T_Equals_WithObject_True_SameReference()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            bool isEqual = graphArc.Equals((Object)graphArc);

            // Assert
            Assert.IsTrue(isEqual);
        }
Esempio n. 7
0
        public void T_GetOtherNode_Node2()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            GraphNode otherNode = graphArc.GetOtherNode(graphNodeBgn);

            // Assert
            Assert.AreEqual(otherNode, graphNodeEnd);
        }
Esempio n. 8
0
        public void T_GetOtherCellValue_Node1()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            char value = graphArc.GetOtherCellValue(cell2);

            // Assert
            Assert.AreEqual(value, '1');
        }
Esempio n. 9
0
        public void T_IsCellIn_True_Node2()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            bool isCellIn = graphArc.IsCellIn(cell2);

            // Assert
            Assert.IsTrue(isCellIn);
        }
Esempio n. 10
0
        public void T_Equals_GetReverseArc()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);
            GraphArc  graphArc2    = new GraphArc(graphNodeEnd, graphNodeBgn);

            // Act
            GraphArc graphArcReverse = graphArc.GetReverseArc();

            // Assert
            Assert.AreEqual(graphArcReverse, graphArc2);
        }
Esempio n. 11
0
        public void T_GetOtherNode_InexistantCell()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();
            Cell cell3 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn = new GraphNode(cell);
            GraphNode graphNodeEnd = new GraphNode(cell2);
            GraphNode graphNode    = new GraphNode(cell3);
            GraphArc  graphArc     = new GraphArc(graphNodeBgn, graphNodeEnd);

            // Act
            GraphNode otherNode = graphArc.GetOtherNode(graphNode);

            // Assert
            Assert.IsNull(otherNode);
        }
Esempio n. 12
0
        public void T_Equals_True()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn  = new GraphNode(cell);
            GraphNode graphNodeEnd  = new GraphNode(cell2);
            GraphNode graphNodeBgn2 = new GraphNode(cell);
            GraphNode graphNodeEnd2 = new GraphNode(cell2);
            GraphArc  graphArc      = new GraphArc(graphNodeBgn, graphNodeEnd);
            GraphArc  graphArc2     = new GraphArc(graphNodeBgn2, graphNodeEnd2);

            // Act
            bool isEqual = graphArc.Equals(graphArc2);

            // Assert
            Assert.IsTrue(isEqual);
        }
Esempio n. 13
0
        public void T_IsDuplicata_True_Reverse()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            GraphNode graphNodeBgn  = new GraphNode(cell);
            GraphNode graphNodeEnd  = new GraphNode(cell2);
            GraphNode graphNodeBgn1 = new GraphNode(cell);
            GraphNode graphNodeEnd1 = new GraphNode(cell2);
            GraphArc  graphArc      = new GraphArc(graphNodeBgn, graphNodeEnd);
            GraphArc  graphArc2     = new GraphArc(graphNodeEnd1, graphNodeBgn1);

            // Act
            bool isDuplicata = graphArc.IsDuplicata(graphArc2);

            // Assert
            Assert.IsTrue(isDuplicata);
        }
Esempio n. 14
0
        public void T_IsDuplicata_False_Node2()
        {
            // Arrange
            Cell cell  = new Cell();
            Cell cell2 = new Cell();
            Cell cell3 = new Cell();

            cell.Value  = '1';
            cell2.Value = 'A';
            cell3.Value = '8';
            GraphNode graphNodeBgn  = new GraphNode(cell);
            GraphNode graphNodeEnd  = new GraphNode(cell3);
            GraphNode graphNodeBgn1 = new GraphNode(cell);
            GraphNode graphNodeEnd1 = new GraphNode(cell2);
            GraphArc  graphArc      = new GraphArc(graphNodeBgn, graphNodeEnd);
            GraphArc  graphArc2     = new GraphArc(graphNodeBgn1, graphNodeEnd1);

            // Act
            bool isDuplicata = graphArc.IsDuplicata(graphArc2);

            // Assert
            Assert.IsFalse(isDuplicata);
        }
Esempio n. 15
0
        public void T_GenerateArcs_SameColumn()
        {
            // Arrange
            CSP       csp = new CSP();
            GraphNode gn0 = new GraphNode(new Cell(0, 9));
            GraphNode gn1 = new GraphNode(new Cell(3, 9));
            GraphNode gn2 = new GraphNode(new Cell(9, 9));

            csp.Nodes.Add(gn0);
            csp.Nodes.Add(gn1);
            csp.Nodes.Add(gn2);

            GraphArc ga01 = new GraphArc(gn0, gn1);
            GraphArc ga02 = new GraphArc(gn0, gn2);
            GraphArc ga10 = new GraphArc(gn1, gn0);
            GraphArc ga12 = new GraphArc(gn1, gn2);
            GraphArc ga20 = new GraphArc(gn2, gn0);
            GraphArc ga21 = new GraphArc(gn2, gn1);

            // Act
            csp.GenerateArcs();

            // Assert
            CollectionAssert.AreEqual(
                gn0.ConnectedArcs,
                new List <GraphArc>(new GraphArc[] { ga01, ga02 })
                );
            CollectionAssert.AreEqual(
                gn1.ConnectedArcs,
                new List <GraphArc>(new GraphArc[] { ga10, ga12 })
                );
            CollectionAssert.AreEqual(
                gn2.ConnectedArcs,
                new List <GraphArc>(new GraphArc[] { ga20, ga21 })
                );
        }
Esempio n. 16
0
 public override void Step()
 {
     if (_NodeQueue.Count > 0)
     {
         GraphNode currentNode = _NodeQueue.Dequeue();             // kivesszük a sor első elemét
         if (!_FinishedNodes.Contains(currentNode))                //ha még nem dolgoztuk fel a csúcsot
         {
             List <GraphArc> templist = new List <GraphArc>();     //mivel a legkisebb súlyú élet kell választani először, szükség lesz egy másolatra szomszédsági tömbről az adott csúcshoz
             foreach (GraphArc arc in _Graph.AdjList[currentNode]) //lemásoljuk a listát
             {
                 templist.Add(arc);
             }
             while (templist.Count > 0) //végigmegyünk a templisten
             {
                 GraphArc minWeightArc = new GraphArc(GraphNode.Empty, GraphNode.Empty, int.MaxValue);
                 foreach (GraphArc arc in templist)
                 {
                     if (arc.Weight < minWeightArc.Weight) //megkeressük a legkisebb súlyú élt
                     {
                         minWeightArc = arc;
                     }
                 }
                 int pathToCurrentNodeWeight;
                 int.TryParse(_ShortestPath[currentNode], out pathToCurrentNodeWeight);
                 if (_ShortestPath[minWeightArc.Target] == "inf") //ha a cél csúcshoz nincs még út, ez biztosan javító él
                 {
                     _ShortestPath[minWeightArc.Target] =
                         (minWeightArc.Weight + pathToCurrentNodeWeight).ToString(); //a legrövidebb utakat tartalmazó tömbjöz hozzáadjuk a csúcsot és a hozzá tartozó súlyt
                     _PreviousNode.Add(minWeightArc.Target, currentNode);            //eltároljuk az előző csúcsot
                     _ShortestPathWithArc.Add(minWeightArc.Target, minWeightArc);    //eltároljuk az ide vezető utat
                 }
                 else // már van ide egy út
                 {
                     int pathToTargetNodeWeight;
                     int.TryParse(_ShortestPath[minWeightArc.Target], out pathToTargetNodeWeight);
                     if (pathToTargetNodeWeight > minWeightArc.Weight + pathToCurrentNodeWeight) //ha ez az út hosszabb, mint a mostani
                     {
                         _ShortestPath[minWeightArc.Target] =
                             (minWeightArc.Weight + pathToCurrentNodeWeight).ToString(); // beállítjuk a mostani csúcsig vezető út súlyát + az él súlyát
                         if (_PreviousNode.ContainsKey(minWeightArc.Target))
                         {
                             _PreviousNode.Remove(minWeightArc.Target);
                         }
                         _PreviousNode.Add(minWeightArc.Target, currentNode);//eltároljuk az előző csúcsot
                         if (_ShortestPathWithArc.ContainsKey(minWeightArc.Target))
                         {
                             _ShortestPathWithArc.Remove(minWeightArc.Target);
                         }
                         _ShortestPathWithArc.Add(minWeightArc.Target, minWeightArc);//eltároljuk az ide vezető utat
                     }
                 }
                 if (!_NodeQueue.Contains(minWeightArc.Target))
                 {
                     _NodeQueue.Enqueue(minWeightArc.Target); //ha még nincs bent a cél csúcs a vizsgálandók között, felvesszük
                 }
                 templist.Remove(minWeightArc);               //a jelenlegi legröviebb élt kidobjuk  a templistből, hogy a következő legrövidebbel lehessen foglalkozni
             }
             if (!_FinishedNodes.Contains(currentNode))
             {
                 _FinishedNodes.Add(currentNode); // felvesszük a csúcsot a bejártak közé
             }
         }
     }
 }