Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Tests/HeuristicLab.Persistence.Fossil/UseCases.cs @ 16485

Last change on this file since 16485 was 16485, checked in by gkronber, 5 years ago

#2520 replaced static type registration in HL.Persistence plugin with the KnownStorableTypesMap which is discovered and used by HEAL.Fossil.
As a consequence we do not need to explicitly call type registration for unit tests.
This change has been necessary because on the first access of StaticCache all StoraleTypes are registered. However, when the HL.Persistence plugin is loaded we have not yet loaded all plugins and as a consequence we would miss StorableTypes.

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Drawing;
25using System.IO;
26using System.Linq;
27using System.Reflection;
28using System.Threading.Tasks;
29using HEAL.Fossil;
30using HeuristicLab.Algorithms.GeneticAlgorithm;
31using HeuristicLab.Analysis;
32using HeuristicLab.Common;
33using HeuristicLab.Data;
34using HeuristicLab.Persistence.Core;
35using HeuristicLab.Persistence.Default.Xml;
36using Microsoft.VisualStudio.TestTools.UnitTesting;
37
38namespace HeuristicLab.Persistence.Fossil.Tests {
39  [TestClass]
40  public class UseCases {
41
42    private string tempFile;
43
44    [TestInitialize()]
45    public void CreateTempFile() {
46      tempFile = Path.GetTempFileName();
47    }
48
49    [TestCleanup()]
50    public void ClearTempFile() {
51      StreamReader reader = new StreamReader(tempFile);
52      string s = reader.ReadToEnd();
53      reader.Close();
54      File.Delete(tempFile);
55    }
56
57    [TestMethod]
58    [TestCategory("Persistence.Fossil")]
59    [TestProperty("Time", "short")]
60    public void BitmapTest() {
61      Icon icon = System.Drawing.SystemIcons.Hand;
62      Bitmap bitmap = icon.ToBitmap();
63      new ProtoBufSerializer().Serialize(bitmap, tempFile);
64      Bitmap newBitmap = (Bitmap)new ProtoBufSerializer().Deserialize(tempFile);
65
66      Assert.AreEqual(bitmap.Size, newBitmap.Size);
67      for (int i = 0; i < bitmap.Size.Width; i++)
68        for (int j = 0; j < bitmap.Size.Height; j++)
69          Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
70    }
71
72
73    [TestMethod]
74    [TestCategory("Persistence.Fossil")]
75    [TestProperty("Time", "short")]
76    public void FontTest() {
77      List<Font> fonts = new List<Font>() {
78        new Font(FontFamily.GenericSansSerif, 12),
79        new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
80        new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
81        new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
82      };
83      new ProtoBufSerializer().Serialize(fonts, tempFile);
84      var newFonts = (List<Font>)new ProtoBufSerializer().Deserialize(tempFile);
85      Assert.AreEqual(fonts[0], newFonts[0]);
86      Assert.AreEqual(fonts[1], newFonts[1]);
87      Assert.AreEqual(fonts[2], newFonts[2]);
88      Assert.AreEqual(fonts[3], newFonts[3]);
89    }
90
91    [TestMethod]
92    [TestCategory("Persistence.Fossil")]
93    [TestProperty("Time", "medium")]
94    public void ConcurrencyTest() {
95      int n = 20;
96      Task[] tasks = new Task[n];
97      for (int i = 0; i < n; i++) {
98        tasks[i] = Task.Factory.StartNew((idx) => {
99          byte[] data;
100          using (var stream = new MemoryStream()) {
101            new ProtoBufSerializer().Serialize(new GeneticAlgorithm(), stream);
102            data = stream.ToArray();
103          }
104        }, i);
105      }
106      Task.WaitAll(tasks);
107    }
108
109    [TestMethod]
110    [TestCategory("Persistence.Fossil")]
111    [TestProperty("Time", "medium")]
112    public void ConcurrentBitmapTest() {
113      Bitmap b = new Bitmap(300, 300);
114      System.Random r = new System.Random();
115      for (int x = 0; x < b.Height; x++) {
116        for (int y = 0; y < b.Width; y++) {
117          b.SetPixel(x, y, Color.FromArgb(r.Next()));
118        }
119      }
120      Task[] tasks = new Task[20];
121      byte[][] datas = new byte[tasks.Length][];
122      for (int i = 0; i < tasks.Length; i++) {
123        tasks[i] = Task.Factory.StartNew((idx) => {
124          using (var stream = new MemoryStream()) {
125            new ProtoBufSerializer().Serialize(b, stream);
126            datas[(int)idx] = stream.ToArray();
127          }
128        }, i);
129      }
130      Task.WaitAll(tasks);
131    }
132
133
134    [TestMethod]
135    [TestCategory("Persistence.Fossil")]
136    [TestProperty("Time", "short")]
137    public void TestLoadingSamples() {
138      var path = @"C:\reps\hl-core\branches\2520_PersistenceReintegration\HeuristicLab.Optimizer\3.3\Documents";
139      var serializer = new ProtoBufSerializer();
140      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
141        var original = XmlParser.Deserialize(fileName);
142        var ok = true;
143        foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {
144          if (
145            t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
146              .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) {
147            try {
148              if (t.IsGenericType) {
149                var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition());
150              } else {
151                var g = Mapper.StaticCache.GetGuid(t);
152              }
153            } catch (Exception e) {
154              Console.WriteLine($"type {t.FullName} in {fileName} is not registered with a GUID in HEAL.Fossil");
155              ok = false;
156            }
157          }
158        }
159        if (ok) {
160          serializer.Serialize(original, fileName + ".proto");
161          var newVersion = serializer.Deserialize(fileName + ".proto");
162          Console.WriteLine("File: " + fileName);
163        }
164      }
165    }
166
167    [TestMethod]
168    [TestCategory("Persistence.Fossil")]
169    [TestProperty("Time", "short")]
170    public void TestIndexedDataTable() {
171      var dt = new IndexedDataTable<int>("test", "test description");
172      var dr = new IndexedDataRow<int>("test row");
173      dr.Values.Add(Tuple.Create(1, 1.0));
174      dr.Values.Add(Tuple.Create(2, 2.0));
175      dr.Values.Add(Tuple.Create(3, 3.0));
176      dt.Rows.Add(dr);
177      var ser = new ProtoBufSerializer();
178      ser.Serialize(dt, tempFile);
179      var dt2 = (IndexedDataTable<int>)ser.Deserialize(tempFile);
180      Assert.AreEqual(dt.Rows["test row"].Values[0], dt2.Rows["test row"].Values[0]);
181      Assert.AreEqual(dt.Rows["test row"].Values[1], dt2.Rows["test row"].Values[1]);
182      Assert.AreEqual(dt.Rows["test row"].Values[2], dt2.Rows["test row"].Values[2]);
183    }
184
185    [TestMethod]
186    [TestCategory("Persistence.Fossil")]
187    [TestProperty("Time", "short")]
188    public void TestPoint2d() {
189      var tag = new IntValue(10);
190      var p = new Point2D<double>(1.0, 2.0, tag);
191      var ser = new ProtoBufSerializer();
192      ser.Serialize(p, tempFile);
193      var p2 = (Point2D<double>)ser.Deserialize(tempFile);
194      Assert.AreEqual(p.X, p2.X);
195      Assert.AreEqual(p.Y, p2.Y);
196      var tag2 = (IntValue)p2.Tag;
197      Assert.AreEqual(tag.Value, tag2.Value);
198    }
199
200    [ClassInitialize]
201    public static void Initialize(TestContext testContext) {
202      ConfigurationService.Instance.Reset();
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.