Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.QuadraticAssignment-3.3/QAPLIBInstancesTest.cs @ 7077

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

#1619, #1628

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