-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObjTests.cs
364 lines (337 loc) · 12 KB
/
ObjTests.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace EP.Ex.Tests
{
[TestClass()]
public class ObjTests
{
#region Public Methods
[TestMethod()]
public void NewPerformanceTest()
{
const int count = 1000000;
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < count; ++i)
{
var obj = new TestClass();
}
stopwatch.Stop();
Console.WriteLine("new object: {0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = Obj<TestClass>.New();
}
stopwatch.Stop();
Console.WriteLine("Obj<TestClass>.New : {0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = Obj.New<TestClass>();
}
stopwatch.Stop();
Console.WriteLine("Obj.New<TestClass> : {0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = Obj.New(typeof(TestClass));
}
stopwatch.Stop();
Console.WriteLine("Obj.New typeof(TestClass) : {0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = Activator.CreateInstance<TestClass>();
}
stopwatch.Stop();
Console.WriteLine("Activator<TestClass> : {0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = Activator.CreateInstance(typeof(TestClass));
}
stopwatch.Stop();
Console.WriteLine("Activator.CreateInstance(typeof(TestClass)) : {0} ms", stopwatch.ElapsedMilliseconds);
}
[TestMethod()]
public void NewTest()
{
const string str = "231231";
TestClass m = (TestClass)Obj.New(typeof(TestClass));
TestClass c = Obj.New<TestClass>();
TestClass k = (TestClass)Obj.New(typeof(TestClass));
TestClass t = Obj<TestClass>.New();
st1 s = Obj<st1>.New();
st1 s1 = (st1)Obj.New<st1>();
st1 s2 = (st1)Obj.New(typeof(st1));
Console.WriteLine(s2.s);
s2.s = str;
Console.WriteLine(s2.s);
Assert.IsNotNull(m);
Assert.IsNotNull(t);
Assert.IsNotNull(c);
Assert.IsInstanceOfType(m, typeof(TestClass));
Assert.IsInstanceOfType(c, typeof(TestClass));
Assert.IsInstanceOfType(t, typeof(TestClass));
Assert.IsInstanceOfType(s, typeof(st1));
Assert.IsInstanceOfType(s1, typeof(st1));
Assert.IsInstanceOfType(s2, typeof(st1));
Assert.AreEqual(s2.s, str);
}
[TestMethod()]
public void NewVsDeepCopyPerformanceTest()
{
const int count = 100000;
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < count; ++i)
{
var obj = new DeepTestClass();
}
stopwatch.Stop();
Console.WriteLine("new object: {0} ms", stopwatch.ElapsedMilliseconds);
var co = new DeepTestClass();
var p = co.DeepCopy();
stopwatch.Restart();
for (int i = 0; i < count; ++i)
{
var obj = (object)Obj.DeepCopy((object)co);
}
stopwatch.Stop();
Console.WriteLine("Obj.DeepCopy : {0} ms", stopwatch.ElapsedMilliseconds);
}
[TestMethod()]
public void NewWONoArgConstructorTest()
{
var obj = new { test = 1, other = "string" };
var dup = obj.DeepCopy();
Assert.AreEqual(obj.test, dup.test);
Assert.AreEqual(obj.other, dup.other);
Assert.IsFalse(Object.ReferenceEquals(obj, dup));
}
[TestMethod()]
public void ShallowCopyTest()
{
const string str = "231231";
TestClass t = Obj<TestClass>.New();
t.Str = str + str;
st1 s = Obj<st1>.New();
s.s = str;
Console.WriteLine(t.Str);
Console.WriteLine(s.s);
K ss = Obj<K>.New();
ss.Field = 1;
ss.t = t;
var t1 = t.ShallowCopy();
var five = (5).ShallowCopy();
var s1 = Obj<st1>.ShallowCopy(s);
var ss1 = Obj<K>.ShallowCopy(ss);
var s2 = s1;
Console.WriteLine(t1.Str);
Console.WriteLine(s1.s);
Console.WriteLine(s2.s);
Assert.IsNotNull(t);
Assert.IsNotNull(t1);
Assert.IsInstanceOfType(t1, typeof(TestClass));
Assert.IsInstanceOfType(s1, typeof(st1));
Assert.IsInstanceOfType(ss1, typeof(K));
Assert.AreEqual(t1.Str, t.Str);
Assert.AreEqual(s1.s, s.s);
Assert.IsFalse(Assert.ReferenceEquals(t, t1));
Assert.IsTrue(Assert.ReferenceEquals(ss.t, ss1.t));
Assert.AreEqual(five, 5);
}
#endregion Public Methods
#region Private Structs
private struct K
{
#region Public Fields
public int Field;
public TestClass t;
#endregion Public Fields
}
#endregion Private Structs
#region Private Fields
private const string str = "231231";
private object[,,,,] MulDimArr = new object[1, 1, 2, 2, 2] {
{
{
{
{ "test", 1 },
{ 2, 2 }
},
{
{ 3, 4 },
{ "rer", 5 }
}
}
}
};
private int[,,,,] MulDimArrValued = new int[1, 1, 2, 2, 2] {
{
{
{
{ 5, 1 },
{ 2, 2 }
},
{
{ 3, 4 },
{ 7, 5 }
}
}
}
};
#endregion Private Fields
public static Dictionary<object, object> CloneDict(Dictionary<object, object> src, Dictionary<object, object> dict)
{
var dst = new Dictionary<object, object>();
object key;
object value;
foreach (var p in src)
{
if (!dict.TryGetValue(p.Key, out key))
{
key = dict[p.Key] = p.Key.DeepCopy();
}
if (!dict.TryGetValue(p.Value, out value))
{
value = dict[p.Value] = p.Value.DeepCopy();
}
dst[key] = value;
}
return dst;
}
[TestMethod()]
public void DeepCopyHSTest()
{
var ht1 = new HashSet<object>(new object[] { "test", 1, 2, 3, 4, 5 });
var ht2 = ht1.DeepCopy();
Assert.IsFalse(Assert.ReferenceEquals(ht1, ht2));
}
[TestMethod()]
public void DateTimeTest()
{
var dt = DateTime.Now;
var dt1 = dt.DeepCopy();
Assert.AreEqual(dt, dt1);
}
[TestMethod]
public void GenericTest()
{
var sn = new TestNode();
sn.fld = Obj.DeepCopy(DateTime.Now);
var sn1 = sn.DeepCopy();
Assert.AreEqual(sn.fld, sn1.fld);
Assert.IsFalse(Object.ReferenceEquals(sn, sn1));
}
[TestMethod()]
public void DeepCopyMultiArrayTest()
{
var ma = MulDimArr;
ma[0, 0, 1, 1, 1] = ma;
var ma2 = ma.DeepCopy();
var mav = MulDimArrValued;
var mav2 = mav.DeepCopy();
Assert.IsFalse(Assert.ReferenceEquals(ma, ma2));
Assert.IsFalse(Assert.ReferenceEquals(mav, mav2));
Assert.IsFalse(Assert.ReferenceEquals(ma[0, 0, 1, 1, 1], ma2[0, 0, 1, 1, 1]));
Assert.IsTrue(Assert.ReferenceEquals(ma[0, 0, 1, 1, 1], ma));
Assert.IsTrue(Assert.ReferenceEquals(ma2[0, 0, 1, 1, 1], ma2));
}
[TestMethod()]
public void DeepCopyTest()
{
object[] arr = new object[2] { 1, "str" };
var obj1 = arr.ShallowCopy();
var p = Obj<object[]>.ShallowCopy(arr);
DeepTestClass t = Obj<DeepTestClass>.New();
t.Linked = new DeepTestClass();
t.Linked.Linked = t;
t.Str = str + str;
var arr2 = arr.DeepCopy();
K s;
s.Field = 1;
s.t = new TestClass();
var t1 = Obj.DeepCopy(t);
var s1 = Obj.DeepCopy(s);
Dictionary<object, object> d = new Dictionary<object, object>() { { t, t } };
//Obj<Dictionary<object, object>>.SetDeepCopyFn(CloneDict);
var d2 = d.DeepCopy();
var k = d2.Keys.First();
var k1 = d2[k];
HashSet<DeepTestClass> hs = new HashSet<DeepTestClass>();
hs.Add(t);
hs.Add(t1);
Hashtable ht = new Hashtable();
ht[t] = t;
ht[t1] = t1;
var htc = ht.DeepCopy();
var hsc = hs.DeepCopy();
Assert.IsTrue(k == k1);
Assert.IsNotNull(t);
Assert.IsNotNull(t1);
Assert.IsInstanceOfType(t1, typeof(DeepTestClass));
Assert.IsInstanceOfType(s1, typeof(K));
Assert.AreEqual(t1.Str, t.Str);
Assert.AreEqual(s1.Field, s.Field);
Assert.AreEqual(hs.Count, hsc.Count);
Assert.IsFalse(Object.ReferenceEquals(hs.ElementAt(0), hsc.ElementAt(0)));
Assert.AreEqual(ht.Count, htc.Count);
Assert.IsFalse(Object.ReferenceEquals(ht, htc));
foreach (var h in htc.Keys)
{
Assert.IsTrue(Object.ReferenceEquals(htc[h], h));
}
Assert.IsFalse(Assert.ReferenceEquals(t, t1));
Assert.IsFalse(Assert.ReferenceEquals(s.t, s1.t));
}
[TestMethod()]
public void InheritenseLinkedPropertyCopyTest()
{
var a = Obj<A>.New();
var b = Obj<B>.New();
a.other = b;
b.other = a;
var c = a.DeepCopy();
Assert.IsTrue(Object.ReferenceEquals(c, c.other.other));
}
[TestMethod()]
public void LinkedDeepCopyTest()
{
DeepTestClass t = Obj<DeepTestClass>.New();
t.Linked = new DeepTestClass();
t.Linked.Linked = t;
t.Str = str + str;
var t1 = Obj.DeepCopy(t);
Assert.IsTrue(Object.ReferenceEquals(t1, t1.Linked.Linked));
}
[TestMethod()]
public void ShallowMultiArrayTest()
{
var ma2 = MulDimArr.ShallowCopy();
Assert.IsFalse(Assert.ReferenceEquals(MulDimArr, ma2));
}
[TestMethod()]
public void ArrayStructCopyTest()
{
DateTime[] s = new DateTime[] { DateTime.Now, DateTime.UtcNow };
var s1 = Obj.DeepCopy(s);
Assert.IsInstanceOfType(s1, typeof(DateTime[]));
Assert.AreEqual(s1[0], s[0]);
}
[TestMethod()]
public void StructCopyTest()
{
K s;
s.Field = 1;
s.t = new TestClass();
var s1 = Obj.DeepCopy(s);
Assert.IsInstanceOfType(s1, typeof(K));
Assert.AreEqual(s1.Field, s.Field);
Assert.IsFalse(Assert.ReferenceEquals(s.t, s1.t));
}
}
}