Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.PermutationEncoding-3.3/PermutationManipulationTest.cs @ 14662

Last change on this file since 14662 was 14662, checked in by abeham, 7 years ago

#2731: Added better performing manipulation methods to Permutation and adapted the manipulators to use them

  • Added unit tests
File size: 9.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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 Microsoft.VisualStudio.TestTools.UnitTesting;
23
24namespace HeuristicLab.Encodings.PermutationEncoding.Tests {
25  [TestClass]
26  public class PermutationManipulationTest {
27    [TestMethod]
28    [TestCategory("Encodings.Permutation")]
29    [TestProperty("Time", "short")]
30    public void TestPermutationSwap() {
31      var permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
32      permutation.Swap(0, 0);
33      Assert.IsTrue(permutation.Validate());
34      var expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
35      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
36
37      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
38      permutation.Swap(0, 9);
39      Assert.IsTrue(permutation.Validate());
40      expected = new Permutation(PermutationTypes.Absolute, new[] { 9, 1, 2, 3, 4, 5, 6, 7, 8, 0 });
41      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
42
43      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
44      permutation.Swap(1, 3);
45      permutation.Swap(3, 5);
46      Assert.IsTrue(permutation.Validate());
47      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 3, 2, 5, 4, 1, 6, 7, 8, 9 });
48      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
49    }
50
51    [TestMethod]
52    [TestCategory("Encodings.Permutation")]
53    [TestProperty("Time", "short")]
54    public void TestPermutationReverse() {
55      var permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
56      permutation.Reverse(0, 0);
57      Assert.IsTrue(permutation.Validate());
58      var expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
59      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
60
61      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
62      permutation.Reverse(9, 1);
63      Assert.IsTrue(permutation.Validate());
64      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
65      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
66
67      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
68      permutation.Reverse(0, 10);
69      Assert.IsTrue(permutation.Validate());
70      expected = new Permutation(PermutationTypes.Absolute, new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
71      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
72
73      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
74      permutation.Reverse(1, 4);
75      Assert.IsTrue(permutation.Validate());
76      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 });
77      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
78    }
79
80    [TestMethod]
81    [TestCategory("Encodings.Permutation")]
82    [TestProperty("Time", "short")]
83    public void TestPermutationMove() {
84      var permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
85      permutation.Move(0, 0, 0);
86      Assert.IsTrue(permutation.Validate());
87      var expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
88      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
89
90      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
91      permutation.Move(0, 0, 1);
92      Assert.IsTrue(permutation.Validate());
93      expected = new Permutation(PermutationTypes.Absolute, new[] { 1, 0, 2, 3, 4, 5, 6, 7, 8, 9 });
94      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
95
96      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
97      permutation.Move(0, 2, 3);
98      Assert.IsTrue(permutation.Validate());
99      expected = new Permutation(PermutationTypes.Absolute, new[] { 3, 4, 5, 0, 1, 2, 6, 7, 8, 9 });
100      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
101
102      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
103      permutation.Move(3, 5, 0);
104      Assert.IsTrue(permutation.Validate());
105      expected = new Permutation(PermutationTypes.Absolute, new[] { 3, 4, 5, 0, 1, 2, 6, 7, 8, 9 });
106      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
107
108      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
109      permutation.Move(3, 5, 2);
110      Assert.IsTrue(permutation.Validate());
111      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 3, 4, 5, 2, 6, 7, 8, 9 });
112      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
113
114      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
115      permutation.Move(5, 6, 8);
116      Assert.IsTrue(permutation.Validate());
117      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 7, 8, 9, 5, 6 });
118      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
119
120      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
121      permutation.Move(5, 7, 7);
122      Assert.IsTrue(permutation.Validate());
123      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 8, 9, 5, 6, 7 });
124      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
125
126      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
127      permutation.Move(2, 6, 5);
128      Assert.IsTrue(permutation.Validate());
129      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 7, 8, 9, 2, 3, 4, 5, 6 });
130      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
131
132      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
133      permutation.Move(3, 5, 4);
134      Assert.IsTrue(permutation.Validate());
135      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 6, 3, 4, 5, 7, 8, 9 });
136      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
137
138      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
139      var exception = false;
140      try {
141        permutation.Move(0, 5, 7);
142      } catch { exception = true; }
143      Assert.IsTrue(exception);
144    }
145
146    [TestMethod]
147    [TestCategory("Encodings.Permutation")]
148    [TestProperty("Time", "short")]
149    public void TestPermutationReplace() {
150      var permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
151      permutation.Replace(0, new int[0]);
152      Assert.IsTrue(permutation.Validate());
153      var expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
154      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
155
156      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
157      permutation.Replace(3, new [] { 3 });
158      Assert.IsTrue(permutation.Validate());
159      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
160      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
161
162      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
163      permutation.Replace(5, new [] { 8, 6, 5, 7 });
164      Assert.IsTrue(permutation.Validate());
165      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 8, 6, 5, 7, 9 });
166      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
167
168      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
169      permutation.Replace(0, new [] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
170      Assert.IsTrue(permutation.Validate());
171      expected = new Permutation(PermutationTypes.Absolute, new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });
172      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
173
174      permutation = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
175      permutation.Replace(3, new[] { 5, 6, 7 });
176      Assert.IsFalse(permutation.Validate());
177      expected = new Permutation(PermutationTypes.Absolute, new[] { 0, 1, 2, 5, 6, 7, 6, 7, 8, 9 });
178      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, permutation));
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.