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