Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.IGraph/IGraphWrappersVectorTest.cs @ 16413

Last change on this file since 16413 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.1 KB
RevLine 
[14244]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[14244]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
[15218]22using System;
23using System.Collections.Generic;
[14245]24using System.Linq;
[15218]25using HeuristicLab.Encodings.RealVectorEncoding;
[14244]26using HeuristicLab.IGraph.Wrappers;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Tests {
30  [TestClass]
31  public class IGraphWrappersVectorTest {
32    [TestMethod]
[14245]33    [TestCategory("ExtLibs")]
[15130]34    [TestCategory("ExtLibs.igraph")]
[14245]35    [TestProperty("Time", "short")]
[14247]36    public void IGraphWrappersVectorConstructionAndFinalizationTest() {
[15218]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      }
[14245]46      var myvec = new double[] { 1, 2, 3 };
[15218]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      }
[14244]53    }
54
55    [TestMethod]
[14245]56    [TestCategory("ExtLibs")]
[15130]57    [TestCategory("ExtLibs.igraph")]
[14245]58    [TestProperty("Time", "short")]
[14244]59    public void IGraphWrappersVectorGetSetTest() {
[15218]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]);
[14244]70
[15218]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      }
[14244]76    }
[14245]77
78    [TestMethod]
79    [TestCategory("ExtLibs")]
[15130]80    [TestCategory("ExtLibs.igraph")]
[14245]81    [TestProperty("Time", "short")]
82    public void IGraphWrappersVectorFillTest() {
[15218]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      }
[14245]87    }
88
89    [TestMethod]
90    [TestCategory("ExtLibs")]
[15130]91    [TestCategory("ExtLibs.igraph")]
[14245]92    [TestProperty("Time", "short")]
93    public void IGraphWrappersVectorReverseTest() {
[15218]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      }
[14245]102    }
103
104    [TestMethod]
105    [TestCategory("ExtLibs")]
[15130]106    [TestCategory("ExtLibs.igraph")]
[14245]107    [TestProperty("Time", "short")]
108    public void IGraphWrappersVectorShuffleTest() {
[15218]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");
[14245]129    }
130
131    [TestMethod]
132    [TestCategory("ExtLibs")]
[15130]133    [TestCategory("ExtLibs.igraph")]
[14245]134    [TestProperty("Time", "short")]
135    public void IGraphWrappersVectorScaleTest() {
[15218]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      }
[14245]144    }
[14244]145  }
146}
Note: See TracBrowser for help on using the repository browser.