Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Tests/HeuristicLab.Problems.QuadraticAssignment-3.3/QAPLIBInstancesTest.cs @ 10217

Last change on this file since 10217 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Problems.QuadraticAssignment.Tests_33 {
31  [TestClass]
32  public class QAPLIBInstancesTest {
33    private static string InstancePrefix = "HeuristicLab.Tests.HeuristicLab.Problems.QuadraticAssignment_3._3.QAPLIB.";
34
35    private IEnumerable<string> EmbeddedInstances {
36      get {
37        return Assembly.GetExecutingAssembly()
38          .GetManifestResourceNames()
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));
43      }
44    }
45
46    [TestMethod]
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
61        try {
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
66        }
67
68        if (solutionExists) {
69          try {
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);
75          }
76        }
77      }
78      Assert.IsTrue(failedInstances.Length == 0, "Following instances failed: " + Environment.NewLine + failedInstances.ToString());
79    }
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    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.