Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Tests/QAPLIBInstancesTest.cs @ 6875

Last change on this file since 6875 was 6875, checked in by abeham, 13 years ago

#1619

  • Moved QAPLIB files from the problem plugin to the test plugin
  • Updated unit test
  • Moved service reference to the problem plugin (from the views plugin)
  • Created a simple test list for our most common tests
File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text;
28using HeuristicLab.Problems.QuadraticAssignment;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace Tests {
32  [TestClass]
33  public class QAPLIBInstancesTest {
34    private static string InstancePrefix = "HeuristicLab.Problems.QuadraticAssignment.Tests_33.QAPLIB.";
35
36    private IEnumerable<string> EmbeddedInstances {
37      get {
38        return Assembly.GetExecutingAssembly()
39          .GetManifestResourceNames()
40          .Where(x => x.EndsWith(".dat"))
41          .OrderBy(x => x)
42          .Select(x => x.Replace(".dat", String.Empty))
43          .Select(x => x.Replace(InstancePrefix, String.Empty));
44      }
45    }
46
47    [TestMethod]
48    public void TestQAPLIBInstances() {
49      QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
50      StringBuilder failedInstances = new StringBuilder();
51      string tempPath = Path.GetTempPath();
52
53      foreach (string instance in EmbeddedInstances) {
54        WriteEmbeddedResourceToFile(InstancePrefix + instance + ".dat", File.Create(Path.Combine(tempPath, "instance.dat")));
55
56        bool solutionExists = Assembly.GetExecutingAssembly().GetManifestResourceNames().Any(x => x == InstancePrefix + instance + ".sln");
57        if (solutionExists)
58          WriteEmbeddedResourceToFile(InstancePrefix + instance + ".sln", File.Create(Path.Combine(tempPath, "instance.sln")));
59
60        try {
61          qap.LoadInstanceFromFile(Path.Combine(tempPath, "instance.dat"));
62        } catch (Exception ex) {
63          failedInstances.AppendLine(instance + ": " + ex.Message);
64          solutionExists = false; // not necessary to test solution as well
65        }
66
67        if (solutionExists) {
68          try {
69            qap.LoadInstanceFromFile(Path.Combine(tempPath, "instance.dat"), Path.Combine(tempPath, "instance.sln"));
70            if (qap.BestKnownSolution == null)
71              failedInstances.AppendLine(instance + " (sln): Given solution and reported quality cannot be reproduced.");
72          } catch (Exception ex) {
73            failedInstances.AppendLine(instance + " (+sln):" + ex.Message);
74          }
75        }
76      }
77      Assert.IsTrue(failedInstances.Length == 0, "Following instances failed: " + Environment.NewLine + failedInstances.ToString());
78    }
79
80    private void WriteEmbeddedResourceToFile(string resource, FileStream file) {
81      try {
82        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)) {
83          int token;
84          while ((token = stream.ReadByte()) >= 0) {
85            file.WriteByte((byte)token);
86          }
87        }
88      } finally { file.Close(); }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.