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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Reflection;
|
---|
28 | using System.Threading.Tasks;
|
---|
29 | using HEAL.Fossil;
|
---|
30 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
31 | using HeuristicLab.Analysis;
|
---|
32 | using HeuristicLab.Common;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 | using HeuristicLab.Persistence.Core;
|
---|
35 | using HeuristicLab.Persistence.Default.Xml;
|
---|
36 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
37 |
|
---|
38 | namespace 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 | [TestMethod]
|
---|
170 | [TestCategory("Persistence.Fossil")]
|
---|
171 | [TestProperty("Time", "short")]
|
---|
172 | public void TestIndexedDataTable() {
|
---|
173 | var dt = new IndexedDataTable<int>("test", "test description");
|
---|
174 | var dr = new IndexedDataRow<int>("test row");
|
---|
175 | dr.Values.Add(Tuple.Create(1, 1.0));
|
---|
176 | dr.Values.Add(Tuple.Create(2, 2.0));
|
---|
177 | dr.Values.Add(Tuple.Create(3, 3.0));
|
---|
178 | dt.Rows.Add(dr);
|
---|
179 | var ser = new ProtoBufSerializer();
|
---|
180 | ser.Serialize(dt, tempFile);
|
---|
181 | var dt2 = (IndexedDataTable<int>)ser.Deserialize(tempFile);
|
---|
182 | Assert.AreEqual(dt.Rows["test row"].Values[0], dt2.Rows["test row"].Values[0]);
|
---|
183 | Assert.AreEqual(dt.Rows["test row"].Values[1], dt2.Rows["test row"].Values[1]);
|
---|
184 | Assert.AreEqual(dt.Rows["test row"].Values[2], dt2.Rows["test row"].Values[2]);
|
---|
185 | }
|
---|
186 |
|
---|
187 | [TestMethod]
|
---|
188 | [TestCategory("Persistence.Fossil")]
|
---|
189 | [TestProperty("Time", "short")]
|
---|
190 | public void TestPoint2d() {
|
---|
191 | var tag = new IntValue(10);
|
---|
192 | var p = new Point2D<double>(1.0, 2.0, tag);
|
---|
193 | var ser = new ProtoBufSerializer();
|
---|
194 | ser.Serialize(p, tempFile);
|
---|
195 | var p2 = (Point2D<double>)ser.Deserialize(tempFile);
|
---|
196 | Assert.AreEqual(p.X, p2.X);
|
---|
197 | Assert.AreEqual(p.Y, p2.Y);
|
---|
198 | var tag2 = (IntValue)p2.Tag;
|
---|
199 | Assert.AreEqual(tag.Value, tag2.Value);
|
---|
200 | }
|
---|
201 |
|
---|
202 | [ClassInitialize]
|
---|
203 | public static void Initialize(TestContext testContext) {
|
---|
204 | ConfigurationService.Instance.Reset();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|