Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Tests/HeuristicLab.Encodings.LinearLinkageEncoding-3.4/ConversionsTest.cs @ 17717

Last change on this file since 17717 was 14475, checked in by sraggl, 8 years ago

#2666
Updated LinearLinkageEncoding by:

  • Speeding up GroupCrossover and SetGroups and GetGroups
  • making Constructor private adding static initializer Methods
File size: 3.8 KB
Line 
1using System;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4namespace HeuristicLab.Encodings.LinearLinkageEncoding.Tests {
5  [TestClass()]
6  public class ConversionsTest {
7    private readonly int[][] validForwardEncoding = new[]{
8      new int[0], new[] {0},
9      new[] {0, 1}, new[] {1, 1},
10      new[] {0, 1, 2}, new[] {1, 1, 2}, new[] {2, 1, 2},
11      new[] {0, 2, 2}, new[] {1, 2, 2},
12      new[] {0, 1, 2, 3}, new[] {1, 1, 2, 3}, new[] {2, 1, 2, 3}, new[] {3, 1, 2, 3},
13      new[] {0, 1, 3, 3}, new[] {1, 2, 2, 3}, new[] {2, 1, 3, 3}, new[] {3, 2, 2, 3},
14      new[] {0, 2, 2, 3}, new[] {1, 3, 2, 3},
15      new[] {0, 2, 3, 3}
16    };
17    private readonly int[][] invalidForwardEncoding = new[]{
18      new [] {-1}, new [] {1}, new [] {1, 0},
19      new [] {0, 0, 0}, new [] {0, 0, 1}, new [] {0, 0, 2},
20      new [] {0, 1, 0},
21      new [] {0, 2, 0}, new [] {0, 2, 1},
22      new [] {1, 0, 0}, new [] {1, 0, 1}, new [] {1, 0, 2},
23      new [] {1, 1, 0}, new [] {1, 1, 1},
24      new [] {1, 2, 0}, new [] {1, 2, 1},
25      new [] {2, 0, 0}, new [] {2, 0, 1}, new [] {2, 0, 2},
26      new [] {2, 1, 0}, new [] {2, 1, 1},
27      new [] {2, 2, 0}, new [] {2, 2, 1}, new [] {2, 2, 2},
28    };
29    private readonly int[][] invalidEndEncoding = new[]{
30      new [] {-1}, new [] {1}, new [] {1, 0},
31      new [] {0, 0, 1}, new [] {0, 0, 2},
32      new [] {0, 1, 0},
33      new [] {0, 2, 0}, new [] {0, 2, 1},
34      new [] {1, 0, 0}, new [] {1, 0, 1}, new [] {1, 0, 2},
35      new [] {1, 1, 0}, new [] {1, 1, 1},
36      new [] {1, 2, 0}, new [] {1, 2, 1},
37      new [] {2, 0, 0}, new [] {2, 0, 1}, new [] {2, 0, 2},
38      new [] {2, 1, 0}, new [] {2, 1, 1},
39      new [] {2, 2, 0}, new [] {2, 2, 1},
40    };
41
42    [TestMethod()]
43    [TestCategory("Encodings.LinearLinkage")]
44    [TestProperty("Time", "short")]
45    public void EndLinksRoundtripTest() {
46      foreach (var values in validForwardEncoding) {
47        var expected = LinearLinkage.FromForwardLinks(values);
48        var llee = expected.ToEndLinks();
49        var actual = LinearLinkage.FromEndLinks(llee);
50        Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, actual),
51          "[{0}] did not roundtrip successfully!", string.Join(", ", values));
52      }
53    }
54
55    [TestMethod()]
56    [TestCategory("Encodings.LinearLinkage")]
57    [TestProperty("Time", "short")]
58    public void GroupsRoundtripTest() {
59      foreach (var values in validForwardEncoding) {
60        var expected = LinearLinkage.FromForwardLinks(values);
61        var groups = expected.GetGroups();
62        var actual = LinearLinkage.FromGroups(expected.Length, groups);
63        Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, actual),
64          "[{0}] did not roundtrip successfully!", string.Join(", ", values));
65      }
66    }
67
68    [TestMethod()]
69    [TestCategory("Encodings.LinearLinkage")]
70    [TestProperty("Time", "short")]
71    public void InvalidForwardLinksTest() {
72      foreach (var values in invalidForwardEncoding) {
73        var isValid = true;
74        try {
75          var lle = LinearLinkage.FromForwardLinks(values);
76        } catch (ArgumentException e) {
77          isValid = false;
78        }
79        Assert.IsFalse(isValid, "[{0}] is invalid and should throw ArgumentException!", string.Join(", ", values));
80      }
81    }
82
83    [TestMethod()]
84    [TestCategory("Encodings.LinearLinkage")]
85    [TestProperty("Time", "short")]
86    public void InvalidEndLinksTest() {
87      foreach (var values in invalidEndEncoding) {
88        var isValid = true;
89        try {
90          var lle = LinearLinkage.FromEndLinks(values);
91        } catch (ArgumentException e) {
92          isValid = false;
93        }
94        Assert.IsFalse(isValid, "{0} should be invalid", string.Join(", ", values));
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.