-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
TestSerialization.cs
306 lines (276 loc) · 11.9 KB
/
TestSerialization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using Stride.Core.Serialization;
using Xunit;
namespace Stride.Core.Tests
{
public partial class TestSerialization
{
[DataContract]
public struct SerializeStructTest
{
public int A { get; set; }
public int B;
}
[DataContract]
public class SerializeClassTest
{
public int A { get; set; }
public int B;
}
public enum SerializeEnum
{
Enum1,
Enum2,
Enum3,
}
[DataContract]
public class SerializeBaseTypeTest
{
public bool Bool;
public float F;
public double D;
public ushort U16;
public short I16;
public uint U32;
public int I32;
public ulong U64;
public long I64;
public string String;
public string StringNull;
public char C;
public byte B;
public sbyte SB;
public int? Nullable1;
public int? Nullable2;
public SerializeEnum Enum;
public Enum Enum2;
public Enum EnumNull;
public byte[] Data;
public byte[] DataNull;
public Guid Guid;
//public decimal Decimal; Not implemented
}
[DataContract(Inherited = true)]
public class A {}
public class B : A { }
[DataContract]
public class SerializeTypeTest
{
public object A { get; set; }
}
[DataContract]
public class SerializeCollectionTest
{
public List<int> List;
public List<int> ListNull;
public IList<int> ListInterface;
public IList<int> ListInterfaceNull;
public List<SerializeClassTest> ListClass;
public Dictionary<string, string> Dictionary;
public Dictionary<string, string> DictionaryNull;
public readonly List<int> ReadOnlyList = new List<int>();
}
[DataContract]
public struct S
{
public int A { get; set; }
}
// TODO change binary serialization due to microsoft security concerns.
#pragma warning disable SYSLIB0011
public byte[] SerializeDotNet<T>(T obj)
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
var memoryStream = new MemoryStream();
bformatter.Serialize(memoryStream, obj);
return memoryStream.ToArray();
}
public enum SerializationBackend
{
Binary,
Xml,
}
public byte[] SerializeBinary<T>(T obj)
{
var memoryStream = new MemoryStream();
var writer = new BinarySerializationWriter(memoryStream);
writer.Write(obj);
writer.Flush();
return memoryStream.ToArray();
}
public T CopyBySerialization<T>(T obj, SerializationBackend serializationBackend)
{
var result = default(T);
if (serializationBackend == SerializationBackend.Binary)
{
var memoryStream = new MemoryStream();
var writer = new BinarySerializationWriter(memoryStream);
writer.Write(obj);
writer.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
var reader = new BinarySerializationReader(memoryStream);
reader.Serialize(ref result, ArchiveMode.Deserialize);
}
//else if (serializationBackend == SerializationBackend.Xml)
//{
// var xmlDoc = new XmlDocument();
// var xmlElement = xmlDoc.CreateElement("object");
// var writer = new XmlSerializationWriter(xmlElement);
// writer.Write(obj);
// writer.Flush();
// var reader = new XmlSerializationReader(xmlElement);
// reader.Serialize(ref result, ArchiveMode.Deserialize);
//}
return result;
}
//[InlineData(SerializationBackend.Binary)]
//[InlineData(SerializationBackend.Xml)]
//[Description("Test struct serialization of field and properties (using ComplexTypeSerializer)")]
//public void TestSerializationComplexTypeStruct(SerializationBackend serializationBackend)
//{
// Serializer.Default.RegisterSerializer(new ComplexTypeSerializer<SerializeStructTest> { Flags = ComplexTypeSerializerFlags.SerializePublicProperties | ComplexTypeSerializerFlags.SerializePublicFields });
// var source = new SerializeStructTest { A = 32, B = 123 };
// var copy = CopyBySerialization(source, serializationBackend);
// Assert.Equal(source.A, copy.A);
// Assert.Equal(source.B, copy.B);
//}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test class serialization of field and properties (using ComplexTypeSerializer)")]
public void TestSerializationComplexTypeClass(SerializationBackend serializationBackend)
{
var source = new SerializeClassTest { A = 32, B = 123 };
var copy = CopyBySerialization(source, serializationBackend);
Assert.Equal(source.A, copy.A);
Assert.Equal(source.B, copy.B);
}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test class serialization of field and properties (using ComplexTypeSerializer)")]
public void TestSerializationList(SerializationBackend serializationBackend)
{
var source = new List<int> { 32, 12, 15 };
var copy = CopyBySerialization(source, serializationBackend);
Assert.Equal(source.Count, copy.Count);
for (int i = 0; i < source.Count; ++i)
Assert.Equal(source[i], copy[i]);
}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test serialization for every base types")]
public void TestSerializationBaseTypes(SerializationBackend serializationBackend)
{
var source = new SerializeBaseTypeTest
{
Nullable1 = 546,
Bool = true,
B = 12,
SB = 14,
C = 'a',
F = 12.0f,
D = 13.0,
Enum = SerializeEnum.Enum2,
Enum2 = SerializeEnum.Enum3,
I16 = -8,
I32 = -12,
I64 = -16,
U16 = 8,
U32 = 12,
U64 = 16,
//Decimal = 12300,
String = "Tat\"asd\\a",
Data = new byte[] { 12, 31, 11 },
Guid = Guid.NewGuid(),
};
var copy = CopyBySerialization(source, serializationBackend);
Assert.Equal(source.Nullable1, copy.Nullable1);
Assert.Equal(source.Nullable2, copy.Nullable2);
Assert.Equal(source.Bool, copy.Bool);
Assert.Equal(source.C, copy.C);
Assert.Equal(source.B, copy.B);
Assert.Equal(source.SB, copy.SB);
Assert.Equal(source.F, copy.F);
Assert.Equal(source.D, copy.D);
Assert.Equal(source.U16, copy.U16);
Assert.Equal(source.I16, copy.I16);
Assert.Equal(source.U32, copy.U32);
Assert.Equal(source.I32, copy.I32);
Assert.Equal(source.U64, copy.U64);
Assert.Equal(source.I64, copy.I64);
Assert.Equal(source.Enum, copy.Enum);
Assert.Equal(source.Enum2, copy.Enum2);
Assert.Equal(source.EnumNull, copy.EnumNull);
Assert.Equal(source.String, copy.String);
Assert.Equal(source.StringNull, copy.StringNull);
Assert.Equal(source.Data, copy.Data);
Assert.Equal(source.DataNull, copy.DataNull);
Assert.Equal(source.Guid, copy.Guid);
}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test serialization for collection types")]
public void TestSerializationCollectionTypes(SerializationBackend serializationBackend)
{
var source = new SerializeCollectionTest
{
List = new List<int> { 3112, 123 },
ListInterface = new List<int> { 5112, 623 },
ListClass = new List<SerializeClassTest> { new SerializeClassTest { A = 1, B = 2 }, new SerializeClassTest { A = 3, B = 4 } },
ReadOnlyList = { 345, 567 },
Dictionary = new Dictionary<string, string> { { "a", "b" }, { "c", "d" } },
};
var copy = CopyBySerialization(source, serializationBackend);
Assert.Equal(source.List, copy.List);
Assert.Null(copy.ListNull);
Assert.Equal(source.ListInterface, copy.ListInterface);
Assert.Null(copy.ListInterfaceNull);
Assert.Equal(source.ListClass.Count, copy.ListClass.Count);
Assert.Equal(source.ReadOnlyList.Count, copy.ReadOnlyList.Count);
for (int i = 0; i < source.ListClass.Count; ++i)
{
Assert.Equal(source.ListClass[i].A, copy.ListClass[i].A);
Assert.Equal(source.ListClass[i].B, copy.ListClass[i].B);
}
Assert.Equal(source.Dictionary, copy.Dictionary);
Assert.Null(copy.DictionaryNull);
}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test serialization with type information")]
public void TestSerializationType(SerializationBackend serializationBackend)
{
var source = new SerializeTypeTest { A = new B() };
var copy = CopyBySerialization(source, serializationBackend);
Assert.True(copy.A is B);
}
[Theory]
[InlineData(SerializationBackend.Binary)]
[Description("Test serialization with type information")]
public void TestSerializationStructType(SerializationBackend serializationBackend)
{
var source = new SerializeTypeTest { A = new S { A = 32 } };
var copy = CopyBySerialization(source, serializationBackend);
Assert.True(copy.A is S);
Assert.Equal(((S)source.A).A, ((S)copy.A).A);
}
struct StructWithRef
{
public S A { get; set; }
}
//[InlineData(SerializationBackend.Binary)]
//[InlineData(SerializationBackend.Xml)]
//[Description("Test serialization with type information")]
//public void TestSerializationPropertyStructType(SerializationBackend serializationBackend)
//{
// Serializer.Default.RegisterSerializer(new ComplexTypeSerializer<StructWithRef>());
// var source = new StructWithRef { A = new S { A = 32 } };
// var copy = CopyBySerialization(source, serializationBackend);
// Assert.IsInstanceOf<S>(copy.A);
// Assert.Equal(((S)source.A).A, ((S)copy.A).A);
//}
}
}