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

Last change on this file since 16482 was 16482, checked in by gkronber, 8 months ago

#2520: added two custom transformers (for Fonts and Bitmaps) and registered types that are available in .NET Framework but not in .NET Standard 2.0 and added.

File size: 6.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      // in HL this is not necessary because RegisterStorableTypes is called when the plugin is loaded
48      HeuristicLabPersistencePlugin.RegisterStorableTypes();
49    }
50
51    [TestCleanup()]
52    public void ClearTempFile() {
53      StreamReader reader = new StreamReader(tempFile);
54      string s = reader.ReadToEnd();
55      reader.Close();
56      File.Delete(tempFile);
57    }
58
59    [TestMethod]
60    [TestCategory("Persistence.Fossil")]
61    [TestProperty("Time", "short")]
62    public void BitmapTest() {
63      Icon icon = System.Drawing.SystemIcons.Hand;
64      Bitmap bitmap = icon.ToBitmap();
65      new ProtoBufSerializer().Serialize(bitmap, tempFile);
66      Bitmap newBitmap = (Bitmap)new ProtoBufSerializer().Deserialize(tempFile);
67
68      Assert.AreEqual(bitmap.Size, newBitmap.Size);
69      for (int i = 0; i < bitmap.Size.Width; i++)
70        for (int j = 0; j < bitmap.Size.Height; j++)
71          Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
72    }
73
74
75    [TestMethod]
76    [TestCategory("Persistence.Fossil")]
77    [TestProperty("Time", "short")]
78    public void FontTest() {
79      List<Font> fonts = new List<Font>() {
80        new Font(FontFamily.GenericSansSerif, 12),
81        new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
82        new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
83        new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
84      };
85      new ProtoBufSerializer().Serialize(fonts, tempFile);
86      var newFonts = (List<Font>)new ProtoBufSerializer().Deserialize(tempFile);
87      Assert.AreEqual(fonts[0], newFonts[0]);
88      Assert.AreEqual(fonts[1], newFonts[1]);
89      Assert.AreEqual(fonts[2], newFonts[2]);
90      Assert.AreEqual(fonts[3], newFonts[3]);
91    }
92
93    [TestMethod]
94    [TestCategory("Persistence.Fossil")]
95    [TestProperty("Time", "medium")]
96    public void ConcurrencyTest() {
97      int n = 20;
98      Task[] tasks = new Task[n];
99      for (int i = 0; i < n; i++) {
100        tasks[i] = Task.Factory.StartNew((idx) => {
101          byte[] data;
102          using (var stream = new MemoryStream()) {
103            new ProtoBufSerializer().Serialize(new GeneticAlgorithm(), stream);
104            data = stream.ToArray();
105          }
106        }, i);
107      }
108      Task.WaitAll(tasks);
109    }
110
111    [TestMethod]
112    [TestCategory("Persistence.Fossil")]
113    [TestProperty("Time", "medium")]
114    public void ConcurrentBitmapTest() {
115      Bitmap b = new Bitmap(300, 300);
116      System.Random r = new System.Random();
117      for (int x = 0; x < b.Height; x++) {
118        for (int y = 0; y < b.Width; y++) {
119          b.SetPixel(x, y, Color.FromArgb(r.Next()));
120        }
121      }
122      Task[] tasks = new Task[20];
123      byte[][] datas = new byte[tasks.Length][];
124      for (int i = 0; i < tasks.Length; i++) {
125        tasks[i] = Task.Factory.StartNew((idx) => {
126          using (var stream = new MemoryStream()) {
127            new ProtoBufSerializer().Serialize(b, stream);
128            datas[(int)idx] = stream.ToArray();
129          }
130        }, i);
131      }
132      Task.WaitAll(tasks);
133    }
134
135
136    [TestMethod]
137    [TestCategory("Persistence.Fossil")]
138    [TestProperty("Time", "short")]
139    public void TestLoadingSamples() {
140      var path = @"C:\reps\hl-core\branches\2520_PersistenceReintegration\HeuristicLab.Optimizer\3.3\Documents";
141      var serializer = new ProtoBufSerializer();
142      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
143        var original = XmlParser.Deserialize(fileName);
144        var ok = true;
145        foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {
146          if (
147            t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
148              .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) {
149            try {
150              if (t.IsGenericType) {
151                var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition());
152              } else {
153                var g = Mapper.StaticCache.GetGuid(t);
154              }
155            } catch (Exception e) {
156              Console.WriteLine($"type {t.FullName} in {fileName} is not registered with a GUID in HEAL.Fossil");
157              ok = false;
158            }
159          }
160        }
161        if (ok) {
162          serializer.Serialize(original, fileName + ".proto");
163          var newVersion = serializer.Deserialize(fileName + ".proto");
164          Console.WriteLine("File: " + fileName);
165        }
166      }
167    }
168   
169    // TODO:
170    // - IndexedItem and IndexedDataTable
171    // - Point2D from HL.Common
172
173    [ClassInitialize]
174    public static void Initialize(TestContext testContext) {
175      ConfigurationService.Instance.Reset();
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.