Changeset 15191 for trunk/sources
- Timestamp:
- 07/11/17 11:19:37 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphLayoutTest.cs
r14277 r15191 33 33 [TestProperty("Time", "short")] 34 34 public void IGraphWrappersLayoutFruchtermanReingoldTest() { 35 var graph = new Graph(5, new[] {35 using (var graph = new Graph(5, new[] { 36 36 Tuple.Create(0, 1), 37 37 Tuple.Create(0, 2), … … 40 40 Tuple.Create(2, 4), 41 41 Tuple.Create(3, 4), 42 }) ;43 Assert.AreEqual(5, graph.Vertices);44 try {45 using (var matrix = graph.LayoutWithFruchtermanReingold()) {46 Assert.AreEqual(5, matrix.Rows);47 Assert.AreEqual(2, matrix.Columns);48 }49 } catch { Assert.Fail("Layouting with fruchterman-reingold using default parameters failed."); }42 })) { 43 Assert.AreEqual(5, graph.Vertices); 44 try { 45 using (var matrix = graph.LayoutWithFruchtermanReingold()) { 46 Assert.AreEqual(5, matrix.Rows); 47 Assert.AreEqual(2, matrix.Columns); 48 } 49 } catch { Assert.Fail("Layouting with fruchterman-reingold using default parameters failed."); } 50 50 51 try {52 using (var matrix = new Matrix(1, 1)) {53 graph.LayoutWithFruchtermanReingold(50, 2, matrix);54 }55 Assert.Fail("Layouting with fruchterman-reingold using too little pre-initialized coordinates failed.");56 } catch (ArgumentException) { }51 try { 52 using (var matrix = new Matrix(1, 1)) { 53 graph.LayoutWithFruchtermanReingold(50, 2, matrix); 54 } 55 Assert.Fail("Layouting with fruchterman-reingold using too little pre-initialized coordinates failed."); 56 } catch (ArgumentException) { } 57 57 58 try {59 using (var matrix = new Matrix(7, 3)) {60 graph.LayoutWithFruchtermanReingold(50, 2, matrix);61 Assert.Fail("Layouting with fruchterman-reingold using too many pre-initialized coordinates failed.");62 }63 } catch (ArgumentException) { }58 try { 59 using (var matrix = new Matrix(7, 3)) { 60 graph.LayoutWithFruchtermanReingold(50, 2, matrix); 61 Assert.Fail("Layouting with fruchterman-reingold using too many pre-initialized coordinates failed."); 62 } 63 } catch (ArgumentException) { } 64 64 65 try { 66 using (var matrix = new Matrix(5, 2)) { 67 matrix[0, 0] = matrix[0, 1] = 1; 68 matrix[1, 0] = matrix[1, 1] = 2; 69 matrix[2, 0] = matrix[2, 1] = 3; 70 matrix[3, 0] = matrix[3, 1] = 4; 71 matrix[4, 0] = matrix[4, 1] = 5; 72 graph.LayoutWithFruchtermanReingold(50, 2, matrix); 73 Assert.AreEqual(5, matrix.Rows); 74 Assert.AreEqual(2, matrix.Columns); 75 Assert.IsFalse( 76 matrix[0, 0].IsAlmost(1) && matrix[0, 1].IsAlmost(1) 77 && matrix[1, 0].IsAlmost(2) && matrix[1, 1].IsAlmost(2) 78 && matrix[2, 0].IsAlmost(2) && matrix[2, 1].IsAlmost(2) 79 && matrix[3, 0].IsAlmost(2) && matrix[3, 1].IsAlmost(2) 80 && matrix[4, 0].IsAlmost(2) && matrix[4, 1].IsAlmost(2)); 81 } 82 } catch { Assert.Fail("Layouting with fruchterman-reingold using pre-initialized coordinates failed."); } 65 try { 66 using (var matrix = new Matrix(5, 2)) { 67 matrix[0, 0] = matrix[0, 1] = 1; 68 matrix[1, 0] = matrix[1, 1] = 2; 69 matrix[2, 0] = matrix[2, 1] = 3; 70 matrix[3, 0] = matrix[3, 1] = 4; 71 matrix[4, 0] = matrix[4, 1] = 5; 72 graph.LayoutWithFruchtermanReingold(50, 2, matrix); 73 Assert.AreEqual(5, matrix.Rows); 74 Assert.AreEqual(2, matrix.Columns); 75 Assert.IsFalse( 76 matrix[0, 0].IsAlmost(1) && matrix[0, 1].IsAlmost(1) 77 && matrix[1, 0].IsAlmost(2) && matrix[1, 1].IsAlmost(2) 78 && matrix[2, 0].IsAlmost(2) && matrix[2, 1].IsAlmost(2) 79 && matrix[3, 0].IsAlmost(2) && matrix[3, 1].IsAlmost(2) 80 && matrix[4, 0].IsAlmost(2) && matrix[4, 1].IsAlmost(2)); 81 } 82 } catch { Assert.Fail("Layouting with fruchterman-reingold using pre-initialized coordinates failed."); } 83 } 83 84 } 84 85 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersGraphTest.cs
r14277 r15191 36 36 [TestProperty("Time", "short")] 37 37 public void IGraphWrappersGraphConstructionAndFinalizationTest() { 38 var graph = new Graph(5, new[] {38 using (var graph = new Graph(5, new[] { 39 39 Tuple.Create(0, 1), 40 40 Tuple.Create(0, 2), … … 43 43 Tuple.Create(2, 4), 44 44 Tuple.Create(3, 4), 45 }); 46 Assert.AreEqual(5, graph.Vertices); 47 Assert.IsFalse(graph.IsDirected); 45 })) { 46 Assert.AreEqual(5, graph.Vertices); 47 Assert.IsFalse(graph.IsDirected); 48 } 48 49 49 graph = new Graph(3, new[] {50 using (var graph = new Graph(3, new[] { 50 51 Tuple.Create(0, 1), 51 52 Tuple.Create(0, 2), 52 53 Tuple.Create(1, 2), 53 }, directed: true); 54 Assert.AreEqual(3, graph.Vertices); 55 Assert.IsTrue(graph.IsDirected); 54 }, directed: true)) { 55 Assert.AreEqual(3, graph.Vertices); 56 Assert.IsTrue(graph.IsDirected); 57 } 56 58 } 57 59 … … 61 63 [TestProperty("Time", "short")] 62 64 public void IGraphWrappersGraphDensityTest() { 63 var graph = new Graph(5, new[] {65 using (var graph = new Graph(5, new[] { 64 66 Tuple.Create(0, 1), 65 67 Tuple.Create(0, 2), … … 68 70 Tuple.Create(2, 4), 69 71 Tuple.Create(3, 4), 70 }) ;72 })) { 71 73 72 var density = graph.Density(); 73 // in un-directed graphs edges count twice 74 Assert.IsTrue(density.IsAlmost(12 / 20.0)); 74 var density = graph.Density(); 75 // in un-directed graphs edges count twice 76 Assert.IsTrue(density.IsAlmost(12 / 20.0)); 77 } 75 78 76 graph.Dispose(); 77 78 graph = new Graph(5, new[] { 79 using (var graph = new Graph(5, new[] { 79 80 Tuple.Create(0, 1), 80 81 Tuple.Create(0, 2), … … 83 84 Tuple.Create(2, 4), 84 85 Tuple.Create(3, 4), 85 }, directed: true) ;86 }, directed: true)) { 86 87 87 density = graph.Density(); 88 // in directed graphs edges count twice 89 Assert.IsTrue(density.IsAlmost(6 / 20.0)); 88 var density = graph.Density(); 89 // in directed graphs edges count twice 90 Assert.IsTrue(density.IsAlmost(6 / 20.0)); 91 } 90 92 } 91 93 … … 95 97 [TestProperty("Time", "short")] 96 98 public void IGraphWrappersGraphPageRankTest() { 97 var graph = new Graph(4, new[] {99 using (var graph = new Graph(4, new[] { 98 100 Tuple.Create(0, 1), 99 101 Tuple.Create(0, 2), … … 101 103 Tuple.Create(2, 0), 102 104 Tuple.Create(3, 2), 103 }, directed: true) ;104 var ranks = graph.PageRank();105 Assert.AreEqual(4, ranks.Length);106 Assert.AreEqual(0.372, ranks[0], 0.01);107 Assert.AreEqual(0.195, ranks[1], 0.01);108 Assert.AreEqual(0.394, ranks[2], 0.01);109 Assert.AreEqual(0.037, ranks[3], 0.01);110 111 graph = new Graph(4, new[] {105 }, directed: true)) { 106 var ranks = graph.PageRank(); 107 Assert.AreEqual(4, ranks.Length); 108 Assert.AreEqual(0.372, ranks[0], 0.01); 109 Assert.AreEqual(0.195, ranks[1], 0.01); 110 Assert.AreEqual(0.394, ranks[2], 0.01); 111 Assert.AreEqual(0.037, ranks[3], 0.01); 112 } 113 using (var graph = new Graph(4, new[] { 112 114 Tuple.Create(0, 1), 113 115 Tuple.Create(1, 2), 114 116 Tuple.Create(2, 3), 115 117 Tuple.Create(3, 0), 116 }, directed: true) ;117 ranks = graph.PageRank();118 Assert.AreEqual(4, ranks.Length);119 Assert.AreEqual(0.250, ranks[0], 0.01);120 Assert.AreEqual(0.250, ranks[1], 0.01);121 Assert.AreEqual(0.250, ranks[2], 0.01);122 Assert.AreEqual(0.250, ranks[3], 0.01);123 124 graph = new Graph(4, new[] {118 }, directed: true)) { 119 var ranks = graph.PageRank(); 120 Assert.AreEqual(4, ranks.Length); 121 Assert.AreEqual(0.250, ranks[0], 0.01); 122 Assert.AreEqual(0.250, ranks[1], 0.01); 123 Assert.AreEqual(0.250, ranks[2], 0.01); 124 Assert.AreEqual(0.250, ranks[3], 0.01); 125 } 126 using (var graph = new Graph(4, new[] { 125 127 Tuple.Create(0, 1), 126 128 Tuple.Create(0, 2), … … 129 131 Tuple.Create(2, 0), 130 132 Tuple.Create(3, 0), 131 }, directed: true); 132 ranks = graph.PageRank(); 133 Assert.AreEqual(4, ranks.Length); 134 Assert.AreEqual(0.480, ranks[0], 0.01); 135 Assert.AreEqual(0.173, ranks[1], 0.01); 136 Assert.AreEqual(0.173, ranks[2], 0.01); 137 Assert.AreEqual(0.173, ranks[3], 0.01); 133 }, directed: true)) { 134 var ranks = graph.PageRank(); 135 Assert.AreEqual(4, ranks.Length); 136 Assert.AreEqual(0.480, ranks[0], 0.01); 137 Assert.AreEqual(0.173, ranks[1], 0.01); 138 Assert.AreEqual(0.173, ranks[2], 0.01); 139 Assert.AreEqual(0.173, ranks[3], 0.01); 140 } 138 141 } 139 142 … … 143 146 [TestProperty("Time", "short")] 144 147 public void IGraphWrappersGraphBreadthFirstWalkTest() { 145 var graph = new Graph(4, new[] {148 using (var graph = new Graph(4, new[] { 146 149 Tuple.Create(0, 1), 147 150 Tuple.Create(0, 2), … … 149 152 Tuple.Create(2, 0), 150 153 Tuple.Create(3, 2), 151 }, directed: true); 152 var visited = new HashSet<int>(); 153 BreadthFirstHandler handler = (graph1, currentVertexId, previousVertexId, nextVertexId, rank, distance, tag) => { 154 visited.Add(currentVertexId); 155 return false; 156 }; 157 graph.BreadthFirstWalk(handler, 0, DirectedWalkMode.All, true, null); 158 Assert.AreEqual(4, visited.Count); 159 Assert.IsTrue(visited.Contains(0)); 160 Assert.IsTrue(visited.Contains(1)); 161 Assert.IsTrue(visited.Contains(2)); 162 Assert.IsTrue(visited.Contains(3)); 154 }, directed: true)) { 155 var visited = new HashSet<int>(); 156 BreadthFirstHandler handler = (graph1, currentVertexId, previousVertexId, nextVertexId, rank, distance, tag) => { 157 visited.Add(currentVertexId); 158 return false; 159 }; 160 graph.BreadthFirstWalk(handler, 0, DirectedWalkMode.All, true, null); 161 Assert.AreEqual(4, visited.Count); 162 Assert.IsTrue(visited.Contains(0)); 163 Assert.IsTrue(visited.Contains(1)); 164 Assert.IsTrue(visited.Contains(2)); 165 Assert.IsTrue(visited.Contains(3)); 166 } 163 167 } 164 168 … … 168 172 [TestProperty("Time", "short")] 169 173 public void IGraphWrappersGraphDepthFirstWalkTest() { 170 var graph = new Graph(4, new[] {174 using (var graph = new Graph(4, new[] { 171 175 Tuple.Create(0, 1), 172 176 Tuple.Create(0, 2), … … 174 178 Tuple.Create(2, 0), 175 179 Tuple.Create(3, 2), 176 }, directed: true); 177 var visited = new HashSet<int>(); 178 DepthFirstHandler handler = (graph1, vertexId, distance, tag) => { 179 visited.Add(vertexId); 180 return false; 181 }; 182 graph.DepthFirstWalk(handler, handler, 0, DirectedWalkMode.All, true, null); 183 Assert.AreEqual(4, visited.Count); 184 Assert.IsTrue(visited.Contains(0)); 185 Assert.IsTrue(visited.Contains(1)); 186 Assert.IsTrue(visited.Contains(2)); 187 Assert.IsTrue(visited.Contains(3)); 180 }, directed: true)) { 181 var visited = new HashSet<int>(); 182 DepthFirstHandler handler = (graph1, vertexId, distance, tag) => { 183 visited.Add(vertexId); 184 return false; 185 }; 186 graph.DepthFirstWalk(handler, handler, 0, DirectedWalkMode.All, true, null); 187 Assert.AreEqual(4, visited.Count); 188 Assert.IsTrue(visited.Contains(0)); 189 Assert.IsTrue(visited.Contains(1)); 190 Assert.IsTrue(visited.Contains(2)); 191 Assert.IsTrue(visited.Contains(3)); 192 } 188 193 } 189 194 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersMatrixTest.cs
r14277 r15191 31 31 [TestProperty("Time", "short")] 32 32 public void IGraphWrappersMatrixConstructionAndFinalizationTest() { 33 var matrix = new Matrix(3, 2);34 Assert.AreEqual(3, matrix.Rows);35 Assert.AreEqual(2, matrix.Columns);36 Assert.AreEqual(0, matrix[0, 0]);37 matrix[0, 0] = 4;38 var other = new Matrix(matrix);39 Assert.AreEqual(3, other.Rows);40 Assert.AreEqual(2, other.Columns);41 Assert.AreEqual(4, other[0, 0]);42 33 using (var matrix = new Matrix(3, 2)) { 34 Assert.AreEqual(3, matrix.Rows); 35 Assert.AreEqual(2, matrix.Columns); 36 Assert.AreEqual(0, matrix[0, 0]); 37 matrix[0, 0] = 4; 38 var other = new Matrix(matrix); 39 Assert.AreEqual(3, other.Rows); 40 Assert.AreEqual(2, other.Columns); 41 Assert.AreEqual(4, other[0, 0]); 42 } 43 43 var mat = new double[,] { 44 44 { 1, 2, 3 }, 45 45 { 4, 5, 6} 46 46 }; 47 matrix = new Matrix(mat); 48 Assert.AreEqual(2, matrix.Rows); 49 Assert.AreEqual(3, matrix.Columns); 50 var test = matrix.ToMatrix(); 51 for (var i = 0; i < matrix.Rows; i++) 52 for (var j = 0; j < matrix.Columns; j++) { 53 Assert.AreEqual(mat[i, j], matrix[i, j]); 54 Assert.AreEqual(mat[i, j], test[i, j]); 55 } 47 using (var matrix = new Matrix(mat)) { 48 Assert.AreEqual(2, matrix.Rows); 49 Assert.AreEqual(3, matrix.Columns); 50 var test = matrix.ToMatrix(); 51 for (var i = 0; i < matrix.Rows; i++) 52 for (var j = 0; j < matrix.Columns; j++) { 53 Assert.AreEqual(mat[i, j], matrix[i, j]); 54 Assert.AreEqual(mat[i, j], test[i, j]); 55 } 56 } 56 57 } 57 58 … … 61 62 [TestProperty("Time", "short")] 62 63 public void IGraphWrappersMatrixGetSetTest() { 63 var matrix = new Matrix(3, 2);64 matrix[0, 0] = matrix[0, 1] = 4;65 matrix[1, 0] = 3;66 matrix[1, 1] = 2;67 matrix[2, 0] = 1.5;68 matrix[2, 1] = -0.5;69 Assert.AreEqual(4, matrix[0, 0]);70 Assert.AreEqual(4, matrix[0, 1]);71 Assert.AreEqual(3, matrix[1, 0]);72 Assert.AreEqual(2, matrix[1, 1]);73 Assert.AreEqual(1.5, matrix[2, 0]);74 Assert.AreEqual(-0.5, matrix[2, 1]);64 using (var matrix = new Matrix(3, 2)) { 65 matrix[0, 0] = matrix[0, 1] = 4; 66 matrix[1, 0] = 3; 67 matrix[1, 1] = 2; 68 matrix[2, 0] = 1.5; 69 matrix[2, 1] = -0.5; 70 Assert.AreEqual(4, matrix[0, 0]); 71 Assert.AreEqual(4, matrix[0, 1]); 72 Assert.AreEqual(3, matrix[1, 0]); 73 Assert.AreEqual(2, matrix[1, 1]); 74 Assert.AreEqual(1.5, matrix[2, 0]); 75 Assert.AreEqual(-0.5, matrix[2, 1]); 75 76 76 var netmat = matrix.ToMatrix(); 77 Assert.AreEqual(3, netmat.GetLength(0)); 78 Assert.AreEqual(2, netmat.GetLength(1)); 79 for (var i = 0; i < netmat.GetLength(0); i++) 80 for (var j = 0; j < netmat.GetLength(1); j++) 81 Assert.AreEqual(matrix[i, j], netmat[i, j]); 77 var netmat = matrix.ToMatrix(); 78 Assert.AreEqual(3, netmat.GetLength(0)); 79 Assert.AreEqual(2, netmat.GetLength(1)); 80 for (var i = 0; i < netmat.GetLength(0); i++) 81 for (var j = 0; j < netmat.GetLength(1); j++) 82 Assert.AreEqual(matrix[i, j], netmat[i, j]); 83 } 82 84 } 83 85 … … 87 89 [TestProperty("Time", "short")] 88 90 public void IGraphWrappersMatrixFillTest() { 89 var matrix = new Matrix(3, 2); 90 matrix.Fill(2.6); 91 Assert.AreEqual(2.6, matrix[0, 0]); 92 Assert.AreEqual(2.6, matrix[0, 1]); 93 Assert.AreEqual(2.6, matrix[1, 0]); 94 Assert.AreEqual(2.6, matrix[1, 1]); 95 Assert.AreEqual(2.6, matrix[2, 0]); 96 Assert.AreEqual(2.6, matrix[2, 1]); 91 using (var matrix = new Matrix(3, 2)) { 92 matrix.Fill(2.6); 93 Assert.AreEqual(2.6, matrix[0, 0]); 94 Assert.AreEqual(2.6, matrix[0, 1]); 95 Assert.AreEqual(2.6, matrix[1, 0]); 96 Assert.AreEqual(2.6, matrix[1, 1]); 97 Assert.AreEqual(2.6, matrix[2, 0]); 98 Assert.AreEqual(2.6, matrix[2, 1]); 99 } 97 100 } 98 101 … … 102 105 [TestProperty("Time", "short")] 103 106 public void IGraphWrappersMatrixTransposeTest() { 104 var matrix = new Matrix(3, 2); 105 matrix.Transpose(); 106 Assert.AreEqual(2, matrix.Rows); 107 Assert.AreEqual(3, matrix.Columns); 107 using (var matrix = new Matrix(3, 2)) { 108 matrix.Transpose(); 109 Assert.AreEqual(2, matrix.Rows); 110 Assert.AreEqual(3, matrix.Columns); 111 } 108 112 } 109 113 … … 113 117 [TestProperty("Time", "short")] 114 118 public void IGraphWrappersMatrixScaleTest() { 115 var matrix = new Matrix(3, 2); 116 matrix[0, 0] = matrix[0, 1] = 4; 117 matrix[1, 0] = 3; 118 matrix[1, 1] = 2; 119 matrix[2, 0] = 1.5; 120 matrix[2, 1] = -0.5; 121 matrix.Scale(2); 122 Assert.AreEqual(8, matrix[0, 0]); 123 Assert.AreEqual(8, matrix[0, 1]); 124 Assert.AreEqual(6, matrix[1, 0]); 125 Assert.AreEqual(4, matrix[1, 1]); 126 Assert.AreEqual(3, matrix[2, 0]); 127 Assert.AreEqual(-1, matrix[2, 1]); 119 using (var matrix = new Matrix(3, 2)) { 120 matrix[0, 0] = matrix[0, 1] = 4; 121 matrix[1, 0] = 3; 122 matrix[1, 1] = 2; 123 matrix[2, 0] = 1.5; 124 matrix[2, 1] = -0.5; 125 matrix.Scale(2); 126 Assert.AreEqual(8, matrix[0, 0]); 127 Assert.AreEqual(8, matrix[0, 1]); 128 Assert.AreEqual(6, matrix[1, 0]); 129 Assert.AreEqual(4, matrix[1, 1]); 130 Assert.AreEqual(3, matrix[2, 0]); 131 Assert.AreEqual(-1, matrix[2, 1]); 132 } 128 133 } 129 134 } -
trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersVectorTest.cs
r14277 r15191 32 32 [TestProperty("Time", "short")] 33 33 public void IGraphWrappersVectorConstructionAndFinalizationTest() { 34 var vector = new Vector(7); 35 Assert.AreEqual(7, vector.Length); 36 Assert.AreEqual(0, vector[0]); 37 vector[0] = 4; 38 var other = new Vector(vector); 39 Assert.AreEqual(7, other.Length); 40 Assert.AreEqual(4, other[0]); 41 34 using (var vector = new Vector(7)) { 35 Assert.AreEqual(7, vector.Length); 36 Assert.AreEqual(0, vector[0]); 37 vector[0] = 4; 38 using (var other = new Vector(vector)) { 39 Assert.AreEqual(7, other.Length); 40 Assert.AreEqual(4, other[0]); 41 } 42 } 42 43 var myvec = new double[] { 1, 2, 3 }; 43 vector = new Vector(myvec); 44 Assert.AreEqual(3, vector.Length); 45 Assert.AreEqual(myvec[0], vector[0]); 46 Assert.AreEqual(myvec[1], vector[1]); 47 Assert.AreEqual(myvec[2], vector[2]); 44 using (var vector = new Vector(myvec)) { 45 Assert.AreEqual(3, vector.Length); 46 Assert.AreEqual(myvec[0], vector[0]); 47 Assert.AreEqual(myvec[1], vector[1]); 48 Assert.AreEqual(myvec[2], vector[2]); 49 } 48 50 } 49 51 … … 53 55 [TestProperty("Time", "short")] 54 56 public void IGraphWrappersVectorGetSetTest() { 55 var vector = new Vector(5);56 vector[0] = vector[1] = 4;57 vector[2] = 3;58 vector[3] = 1.5;59 vector[4] = -0.5;60 Assert.AreEqual(4, vector[0]);61 Assert.AreEqual(4, vector[1]);62 Assert.AreEqual(3, vector[2]);63 Assert.AreEqual(1.5, vector[3]);64 Assert.AreEqual(-0.5, vector[4]);57 using (var vector = new Vector(5)) { 58 vector[0] = vector[1] = 4; 59 vector[2] = 3; 60 vector[3] = 1.5; 61 vector[4] = -0.5; 62 Assert.AreEqual(4, vector[0]); 63 Assert.AreEqual(4, vector[1]); 64 Assert.AreEqual(3, vector[2]); 65 Assert.AreEqual(1.5, vector[3]); 66 Assert.AreEqual(-0.5, vector[4]); 65 67 66 var netmat = vector.ToArray(); 67 Assert.AreEqual(5, netmat.Length); 68 for (var i = 0; i < netmat.Length; i++) 69 Assert.AreEqual(vector[i], netmat[i]); 68 var netmat = vector.ToArray(); 69 Assert.AreEqual(5, netmat.Length); 70 for (var i = 0; i < netmat.Length; i++) 71 Assert.AreEqual(vector[i], netmat[i]); 72 } 70 73 } 71 74 … … 75 78 [TestProperty("Time", "short")] 76 79 public void IGraphWrappersVectorFillTest() { 77 var vector = new Vector(5); 78 vector.Fill(2.3); 79 Assert.IsTrue(new[] { 2.3, 2.3, 2.3, 2.3, 2.3 }.SequenceEqual(vector.ToArray())); 80 using (var vector = new Vector(5)) { 81 vector.Fill(2.3); 82 Assert.IsTrue(new[] { 2.3, 2.3, 2.3, 2.3, 2.3 }.SequenceEqual(vector.ToArray())); 83 } 80 84 } 81 85 … … 85 89 [TestProperty("Time", "short")] 86 90 public void IGraphWrappersVectorReverseTest() { 87 var vector = new Vector(5); 88 vector[0] = vector[1] = 4; 89 vector[2] = 3; 90 vector[3] = 1.5; 91 vector[4] = -0.5; 92 vector.Reverse(); 93 Assert.IsTrue(new[] { -0.5, 1.5, 3, 4, 4 }.SequenceEqual(vector.ToArray())); 91 using (var vector = new Vector(5)) { 92 vector[0] = vector[1] = 4; 93 vector[2] = 3; 94 vector[3] = 1.5; 95 vector[4] = -0.5; 96 vector.Reverse(); 97 Assert.IsTrue(new[] { -0.5, 1.5, 3, 4, 4 }.SequenceEqual(vector.ToArray())); 98 } 94 99 } 95 100 … … 99 104 [TestProperty("Time", "short")] 100 105 public void IGraphWrappersVectorShuffleTest() { 101 var vector = new Vector(5); 102 vector[0] = vector[1] = 4; 103 vector[2] = 3; 104 vector[3] = 1.5; 105 vector[4] = -0.5; 106 vector.Shuffle(); 107 Assert.IsFalse(new[] { -0.5, 1.5, 3, 4, 4 }.SequenceEqual(vector.ToArray())); 108 Assert.IsFalse(new[] { 4, 4, 3, 1.5, -0.5 }.SequenceEqual(vector.ToArray())); 106 var different = 0; 107 for (var i = 0; i < 100; i++) { 108 using (var vector = new Vector(5)) { 109 vector[0] = vector[1] = 4; 110 vector[2] = 3; 111 vector[3] = 1.5; 112 vector[4] = -0.5; 113 vector.Shuffle(); 114 var result = vector.ToArray(); 115 if (!result.SequenceEqual(new double[] { 4, 4, 3, 1.5, -0.5 })) 116 different++; 117 Assert.AreEqual(2, result.Count(x => x == 4)); 118 Assert.AreEqual(1, result.Count(x => x == 3)); 119 Assert.AreEqual(1, result.Count(x => x == 1.5)); 120 Assert.AreEqual(1, result.Count(x => x == -0.5)); 121 } 122 } 123 // There should be reasonable low probability that all 100 shuffles result in exactly the same vector 124 Assert.IsTrue(different > 0); 109 125 } 110 126 … … 114 130 [TestProperty("Time", "short")] 115 131 public void IGraphWrappersVectorScaleTest() { 116 var vector = new Vector(5); 117 vector[0] = vector[1] = 4; 118 vector[2] = 3; 119 vector[3] = 1.5; 120 vector[4] = -0.5; 121 vector.Scale(2); 122 Assert.IsTrue(new double[] { 8, 8, 6, 3, -1 }.SequenceEqual(vector.ToArray())); 132 using (var vector = new Vector(5)) { 133 vector[0] = vector[1] = 4; 134 vector[2] = 3; 135 vector[3] = 1.5; 136 vector[4] = -0.5; 137 vector.Scale(2); 138 Assert.IsTrue(new double[] { 8, 8, 6, 3, -1 }.SequenceEqual(vector.ToArray())); 139 } 123 140 } 124 141 }
Note: See TracChangeset
for help on using the changeset viewer.