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.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.IO;
|
---|
25 | using System.Threading.Tasks;
|
---|
26 | using HEAL.Fossil;
|
---|
27 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
28 | using HeuristicLab.Persistence.Core;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Persistence.Fossil.Tests {
|
---|
32 | [TestClass]
|
---|
33 | public class UseCases {
|
---|
34 |
|
---|
35 | private string tempFile;
|
---|
36 |
|
---|
37 | [TestInitialize()]
|
---|
38 | public void CreateTempFile() {
|
---|
39 | tempFile = Path.GetTempFileName();
|
---|
40 | }
|
---|
41 |
|
---|
42 | [TestCleanup()]
|
---|
43 | public void ClearTempFile() {
|
---|
44 | StreamReader reader = new StreamReader(tempFile);
|
---|
45 | string s = reader.ReadToEnd();
|
---|
46 | reader.Close();
|
---|
47 | File.Delete(tempFile);
|
---|
48 | }
|
---|
49 |
|
---|
50 | [TestMethod]
|
---|
51 | [TestCategory("Persistence.Fossil")]
|
---|
52 | [TestProperty("Time", "short")]
|
---|
53 | public void BitmapTest() {
|
---|
54 | Icon icon = System.Drawing.SystemIcons.Hand;
|
---|
55 | Bitmap bitmap = icon.ToBitmap();
|
---|
56 | new ProtoBufSerializer().Serialize(bitmap, tempFile);
|
---|
57 | Bitmap newBitmap = (Bitmap)new ProtoBufSerializer().Deserialize(tempFile);
|
---|
58 |
|
---|
59 | Assert.AreEqual(bitmap.Size, newBitmap.Size);
|
---|
60 | for (int i = 0; i < bitmap.Size.Width; i++)
|
---|
61 | for (int j = 0; j < bitmap.Size.Height; j++)
|
---|
62 | Assert.AreEqual(bitmap.GetPixel(i, j), newBitmap.GetPixel(i, j));
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | [TestMethod]
|
---|
67 | [TestCategory("Persistence.Fossil")]
|
---|
68 | [TestProperty("Time", "short")]
|
---|
69 | public void FontTest() {
|
---|
70 | List<Font> fonts = new List<Font>() {
|
---|
71 | new Font(FontFamily.GenericSansSerif, 12),
|
---|
72 | new Font("Times New Roman", 21, FontStyle.Bold, GraphicsUnit.Pixel),
|
---|
73 | new Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Document),
|
---|
74 | new Font("Helvetica", 21, FontStyle.Strikeout, GraphicsUnit.Inch, 0, true),
|
---|
75 | };
|
---|
76 | new ProtoBufSerializer().Serialize(fonts, tempFile);
|
---|
77 | var newFonts = (List<Font>)new ProtoBufSerializer().Deserialize(tempFile);
|
---|
78 | Assert.AreEqual(fonts[0], newFonts[0]);
|
---|
79 | Assert.AreEqual(fonts[1], newFonts[1]);
|
---|
80 | Assert.AreEqual(fonts[2], newFonts[2]);
|
---|
81 | Assert.AreEqual(fonts[3], newFonts[3]);
|
---|
82 | }
|
---|
83 |
|
---|
84 | [TestMethod]
|
---|
85 | [TestCategory("Persistence.Fossil")]
|
---|
86 | [TestProperty("Time", "medium")]
|
---|
87 | public void ConcurrencyTest() {
|
---|
88 | int n = 20;
|
---|
89 | Task[] tasks = new Task[n];
|
---|
90 | for (int i = 0; i < n; i++) {
|
---|
91 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
92 | byte[] data;
|
---|
93 | using (var stream = new MemoryStream()) {
|
---|
94 | new ProtoBufSerializer().Serialize(new GeneticAlgorithm(), stream);
|
---|
95 | data = stream.ToArray();
|
---|
96 | }
|
---|
97 | }, i);
|
---|
98 | }
|
---|
99 | Task.WaitAll(tasks);
|
---|
100 | }
|
---|
101 |
|
---|
102 | [TestMethod]
|
---|
103 | [TestCategory("Persistence.Fossil")]
|
---|
104 | [TestProperty("Time", "medium")]
|
---|
105 | public void ConcurrentBitmapTest() {
|
---|
106 | Bitmap b = new Bitmap(300, 300);
|
---|
107 | System.Random r = new System.Random();
|
---|
108 | for (int x = 0; x < b.Height; x++) {
|
---|
109 | for (int y = 0; y < b.Width; y++) {
|
---|
110 | b.SetPixel(x, y, Color.FromArgb(r.Next()));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | Task[] tasks = new Task[20];
|
---|
114 | byte[][] datas = new byte[tasks.Length][];
|
---|
115 | for (int i = 0; i < tasks.Length; i++) {
|
---|
116 | tasks[i] = Task.Factory.StartNew((idx) => {
|
---|
117 | using (var stream = new MemoryStream()) {
|
---|
118 | new ProtoBufSerializer().Serialize(b, stream);
|
---|
119 | datas[(int)idx] = stream.ToArray();
|
---|
120 | }
|
---|
121 | }, i);
|
---|
122 | }
|
---|
123 | Task.WaitAll(tasks);
|
---|
124 | }
|
---|
125 |
|
---|
126 | [ClassInitialize]
|
---|
127 | public static void Initialize(TestContext testContext) {
|
---|
128 | ConfigurationService.Instance.Reset();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|