Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15191 for trunk/sources


Ignore:
Timestamp:
07/11/17 11:19:37 (7 years ago)
Author:
abeham
Message:

#2803: fixed tests

Location:
trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphLayoutTest.cs

    r14277 r15191  
    3333    [TestProperty("Time", "short")]
    3434    public void IGraphWrappersLayoutFruchtermanReingoldTest() {
    35       var graph = new Graph(5, new[] {
     35      using (var graph = new Graph(5, new[] {
    3636        Tuple.Create(0, 1),
    3737        Tuple.Create(0, 2),
     
    4040        Tuple.Create(2, 4),
    4141        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."); }
    5050
    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) { }
    5757
    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) { }
    6464
    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      }
    8384    }
    8485  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersGraphTest.cs

    r14277 r15191  
    3636    [TestProperty("Time", "short")]
    3737    public void IGraphWrappersGraphConstructionAndFinalizationTest() {
    38       var graph = new Graph(5, new[] {
     38      using (var graph = new Graph(5, new[] {
    3939        Tuple.Create(0, 1),
    4040        Tuple.Create(0, 2),
     
    4343        Tuple.Create(2, 4),
    4444        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      }
    4849
    49       graph = new Graph(3, new[] {
     50      using (var graph = new Graph(3, new[] {
    5051        Tuple.Create(0, 1),
    5152        Tuple.Create(0, 2),
    5253        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      }
    5658    }
    5759
     
    6163    [TestProperty("Time", "short")]
    6264    public void IGraphWrappersGraphDensityTest() {
    63       var graph = new Graph(5, new[] {
     65      using (var graph = new Graph(5, new[] {
    6466        Tuple.Create(0, 1),
    6567        Tuple.Create(0, 2),
     
    6870        Tuple.Create(2, 4),
    6971        Tuple.Create(3, 4),
    70       });
     72      })) {
    7173
    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      }
    7578
    76       graph.Dispose();
    77 
    78       graph = new Graph(5, new[] {
     79      using (var graph = new Graph(5, new[] {
    7980        Tuple.Create(0, 1),
    8081        Tuple.Create(0, 2),
     
    8384        Tuple.Create(2, 4),
    8485        Tuple.Create(3, 4),
    85       }, directed: true);
     86      }, directed: true)) {
    8687
    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      }
    9092    }
    9193
     
    9597    [TestProperty("Time", "short")]
    9698    public void IGraphWrappersGraphPageRankTest() {
    97       var graph = new Graph(4, new[] {
     99      using (var graph = new Graph(4, new[] {
    98100        Tuple.Create(0, 1),
    99101        Tuple.Create(0, 2),
     
    101103        Tuple.Create(2, 0),
    102104        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[] {
    112114        Tuple.Create(0, 1),
    113115        Tuple.Create(1, 2),
    114116        Tuple.Create(2, 3),
    115117        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[] {
    125127        Tuple.Create(0, 1),
    126128        Tuple.Create(0, 2),
     
    129131        Tuple.Create(2, 0),
    130132        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      }
    138141    }
    139142
     
    143146    [TestProperty("Time", "short")]
    144147    public void IGraphWrappersGraphBreadthFirstWalkTest() {
    145       var graph = new Graph(4, new[] {
     148      using (var graph = new Graph(4, new[] {
    146149        Tuple.Create(0, 1),
    147150        Tuple.Create(0, 2),
     
    149152        Tuple.Create(2, 0),
    150153        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      }
    163167    }
    164168
     
    168172    [TestProperty("Time", "short")]
    169173    public void IGraphWrappersGraphDepthFirstWalkTest() {
    170       var graph = new Graph(4, new[] {
     174      using (var graph = new Graph(4, new[] {
    171175        Tuple.Create(0, 1),
    172176        Tuple.Create(0, 2),
     
    174178        Tuple.Create(2, 0),
    175179        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      }
    188193    }
    189194  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersMatrixTest.cs

    r14277 r15191  
    3131    [TestProperty("Time", "short")]
    3232    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      }
    4343      var mat = new double[,] {
    4444        { 1, 2, 3 },
    4545        { 4, 5, 6}
    4646      };
    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      }
    5657    }
    5758
     
    6162    [TestProperty("Time", "short")]
    6263    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]);
    7576
    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      }
    8284    }
    8385
     
    8789    [TestProperty("Time", "short")]
    8890    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      }
    97100    }
    98101
     
    102105    [TestProperty("Time", "short")]
    103106    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      }
    108112    }
    109113
     
    113117    [TestProperty("Time", "short")]
    114118    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      }
    128133    }
    129134  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersVectorTest.cs

    r14277 r15191  
    3232    [TestProperty("Time", "short")]
    3333    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      }
    4243      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      }
    4850    }
    4951
     
    5355    [TestProperty("Time", "short")]
    5456    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]);
    6567
    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      }
    7073    }
    7174
     
    7578    [TestProperty("Time", "short")]
    7679    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      }
    8084    }
    8185
     
    8589    [TestProperty("Time", "short")]
    8690    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      }
    9499    }
    95100
     
    99104    [TestProperty("Time", "short")]
    100105    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);
    109125    }
    110126
     
    114130    [TestProperty("Time", "short")]
    115131    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      }
    123140    }
    124141  }
Note: See TracChangeset for help on using the changeset viewer.