[14276] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14276] | 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 |
|
---|
[15192] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[14277] | 24 | using System.Linq;
|
---|
[15192] | 25 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[14276] | 26 | using HeuristicLab.IGraph.Wrappers;
|
---|
| 27 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Tests {
|
---|
| 30 | [TestClass]
|
---|
| 31 | public class IGraphWrappersVectorTest {
|
---|
| 32 | [TestMethod]
|
---|
[14277] | 33 | [TestCategory("ExtLibs")]
|
---|
| 34 | [TestCategory("ExtLibs.igraph")]
|
---|
| 35 | [TestProperty("Time", "short")]
|
---|
| 36 | public void IGraphWrappersVectorConstructionAndFinalizationTest() {
|
---|
[15191] | 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 | }
|
---|
[14277] | 46 | var myvec = new double[] { 1, 2, 3 };
|
---|
[15191] | 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 | }
|
---|
[14276] | 53 | }
|
---|
| 54 |
|
---|
| 55 | [TestMethod]
|
---|
[14277] | 56 | [TestCategory("ExtLibs")]
|
---|
| 57 | [TestCategory("ExtLibs.igraph")]
|
---|
| 58 | [TestProperty("Time", "short")]
|
---|
[14276] | 59 | public void IGraphWrappersVectorGetSetTest() {
|
---|
[15191] | 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]);
|
---|
[14276] | 70 |
|
---|
[15191] | 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 | }
|
---|
[14276] | 76 | }
|
---|
[14277] | 77 |
|
---|
| 78 | [TestMethod]
|
---|
| 79 | [TestCategory("ExtLibs")]
|
---|
| 80 | [TestCategory("ExtLibs.igraph")]
|
---|
| 81 | [TestProperty("Time", "short")]
|
---|
| 82 | public void IGraphWrappersVectorFillTest() {
|
---|
[15191] | 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 | }
|
---|
[14277] | 87 | }
|
---|
| 88 |
|
---|
| 89 | [TestMethod]
|
---|
| 90 | [TestCategory("ExtLibs")]
|
---|
| 91 | [TestCategory("ExtLibs.igraph")]
|
---|
| 92 | [TestProperty("Time", "short")]
|
---|
| 93 | public void IGraphWrappersVectorReverseTest() {
|
---|
[15191] | 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 | }
|
---|
[14277] | 102 | }
|
---|
| 103 |
|
---|
| 104 | [TestMethod]
|
---|
| 105 | [TestCategory("ExtLibs")]
|
---|
| 106 | [TestCategory("ExtLibs.igraph")]
|
---|
| 107 | [TestProperty("Time", "short")]
|
---|
| 108 | public void IGraphWrappersVectorShuffleTest() {
|
---|
[15192] | 109 | var different = new HashSet<RealVector>(new RealVectorEqualityComparer());
|
---|
[15191] | 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();
|
---|
[15192] | 118 | different.Add(new RealVector(result));
|
---|
[15191] | 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
|
---|
[15192] | 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");
|
---|
[14277] | 129 | }
|
---|
| 130 |
|
---|
| 131 | [TestMethod]
|
---|
| 132 | [TestCategory("ExtLibs")]
|
---|
| 133 | [TestCategory("ExtLibs.igraph")]
|
---|
| 134 | [TestProperty("Time", "short")]
|
---|
| 135 | public void IGraphWrappersVectorScaleTest() {
|
---|
[15191] | 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 | }
|
---|
[14277] | 144 | }
|
---|
[14276] | 145 | }
|
---|
| 146 | }
|
---|