Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersVectorTest.cs @ 16662

Last change on this file since 16662 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.IGraph.Wrappers;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Tests {
30  [TestClass]
31  public class IGraphWrappersVectorTest {
32    [TestMethod]
33    [TestCategory("ExtLibs")]
34    [TestCategory("ExtLibs.igraph")]
35    [TestProperty("Time", "short")]
36    public void IGraphWrappersVectorConstructionAndFinalizationTest() {
37      using (var vector = new Vector(7)) {
38        Assert.AreEqual(7, vector.Length);
39        Assert.AreEqual(0, vector[0]);
40        vector[0] = 4;
41        using (var other = new Vector(vector)) {
42          Assert.AreEqual(7, other.Length);
43          Assert.AreEqual(4, other[0]);
44        }
45      }
46      var myvec = new double[] { 1, 2, 3 };
47      using (var vector = new Vector(myvec)) {
48        Assert.AreEqual(3, vector.Length);
49        Assert.AreEqual(myvec[0], vector[0]);
50        Assert.AreEqual(myvec[1], vector[1]);
51        Assert.AreEqual(myvec[2], vector[2]);
52      }
53    }
54
55    [TestMethod]
56    [TestCategory("ExtLibs")]
57    [TestCategory("ExtLibs.igraph")]
58    [TestProperty("Time", "short")]
59    public void IGraphWrappersVectorGetSetTest() {
60      using (var vector = new Vector(5)) {
61        vector[0] = vector[1] = 4;
62        vector[2] = 3;
63        vector[3] = 1.5;
64        vector[4] = -0.5;
65        Assert.AreEqual(4, vector[0]);
66        Assert.AreEqual(4, vector[1]);
67        Assert.AreEqual(3, vector[2]);
68        Assert.AreEqual(1.5, vector[3]);
69        Assert.AreEqual(-0.5, vector[4]);
70
71        var netmat = vector.ToArray();
72        Assert.AreEqual(5, netmat.Length);
73        for (var i = 0; i < netmat.Length; i++)
74          Assert.AreEqual(vector[i], netmat[i]);
75      }
76    }
77
78    [TestMethod]
79    [TestCategory("ExtLibs")]
80    [TestCategory("ExtLibs.igraph")]
81    [TestProperty("Time", "short")]
82    public void IGraphWrappersVectorFillTest() {
83      using (var vector = new Vector(5)) {
84        vector.Fill(2.3);
85        Assert.IsTrue(new[] { 2.3, 2.3, 2.3, 2.3, 2.3 }.SequenceEqual(vector.ToArray()));
86      }
87    }
88
89    [TestMethod]
90    [TestCategory("ExtLibs")]
91    [TestCategory("ExtLibs.igraph")]
92    [TestProperty("Time", "short")]
93    public void IGraphWrappersVectorReverseTest() {
94      using (var vector = new Vector(5)) {
95        vector[0] = vector[1] = 4;
96        vector[2] = 3;
97        vector[3] = 1.5;
98        vector[4] = -0.5;
99        vector.Reverse();
100        Assert.IsTrue(new[] { -0.5, 1.5, 3, 4, 4 }.SequenceEqual(vector.ToArray()));
101      }
102    }
103
104    [TestMethod]
105    [TestCategory("ExtLibs")]
106    [TestCategory("ExtLibs.igraph")]
107    [TestProperty("Time", "short")]
108    public void IGraphWrappersVectorShuffleTest() {
109      var different = new HashSet<RealVector>(new RealVectorEqualityComparer());
110      for (var i = 0; i < 100; i++) {
111        using (var vector = new Vector(5)) {
112          vector[0] = vector[1] = 4;
113          vector[2] = 3;
114          vector[3] = 1.5;
115          vector[4] = -0.5;
116          vector.Shuffle();
117          var result = vector.ToArray();
118          different.Add(new RealVector(result));
119          Assert.AreEqual(2, result.Count(x => x == 4));
120          Assert.AreEqual(1, result.Count(x => x == 3));
121          Assert.AreEqual(1, result.Count(x => x == 1.5));
122          Assert.AreEqual(1, result.Count(x => x == -0.5));
123        }
124      }
125      // There should be reasonable low probability that all 100 shuffles result in exactly the same vector
126      Assert.IsTrue(different.Count > 1);
127      Assert.IsTrue(different.Count <= 60); // there are a total of 60 different shuffles 5! / 2!
128      Console.WriteLine("Shuffle produced " + different.Count + " unique vectors");
129    }
130
131    [TestMethod]
132    [TestCategory("ExtLibs")]
133    [TestCategory("ExtLibs.igraph")]
134    [TestProperty("Time", "short")]
135    public void IGraphWrappersVectorScaleTest() {
136      using (var vector = new Vector(5)) {
137        vector[0] = vector[1] = 4;
138        vector[2] = 3;
139        vector[3] = 1.5;
140        vector[4] = -0.5;
141        vector.Scale(2);
142        Assert.IsTrue(new double[] { 8, 8, 6, 3, -1 }.SequenceEqual(vector.ToArray()));
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.