Skip to content

Commit d94fac1

Browse files
committed
Merge branch 'feature/anotherCloningVariant' into develop
2 parents f0cd989 + 9ad9417 commit d94fac1

24 files changed

+472
-122
lines changed

DeepCloner.Tests/ArraysSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
namespace Force.DeepCloner.Tests
77
{
8-
[TestFixture]
9-
public class ArraysSpec
8+
[TestFixture(false)]
9+
[TestFixture(true)]
10+
public class ArraysSpec : BaseTest
1011
{
12+
public ArraysSpec(object isSafeInit)
13+
: base((bool)isSafeInit)
14+
{
15+
}
16+
1117
[Test]
1218
public void IntArray_Should_Be_Cloned()
1319
{

DeepCloner.Tests/BaseTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
3+
using Force.DeepCloner.Helpers;
4+
5+
namespace Force.DeepCloner.Tests
6+
{
7+
public class BaseTest
8+
{
9+
public BaseTest(bool isSafeInit)
10+
{
11+
SwitchTo(isSafeInit);
12+
}
13+
14+
public static void SwitchTo(bool isSafeInit)
15+
{
16+
typeof(ShallowObjectCloner).GetMethod("SwitchTo", BindingFlags.NonPublic | BindingFlags.Static)
17+
.Invoke(null, new object[] { isSafeInit });
18+
}
19+
}
20+
}

DeepCloner.Tests/CloneExtensionsSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
namespace Force.DeepCloner.Tests
66
{
7-
[TestFixture]
8-
public class CloneExtensionsSpec
7+
[TestFixture(false)]
8+
[TestFixture(true)]
9+
public class CloneExtensionsSpec : BaseTest
910
{
11+
public CloneExtensionsSpec(bool isSafeInit)
12+
: base(isSafeInit)
13+
{
14+
}
15+
1016
public class C1
1117
{
1218
public int X { get; set; }

DeepCloner.Tests/ConstructorsSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
namespace Force.DeepCloner.Tests
66
{
7-
[TestFixture]
8-
public class ConstructorsSpec
7+
[TestFixture(false)]
8+
[TestFixture(true)]
9+
public class ConstructorsSpec : BaseTest
910
{
11+
public ConstructorsSpec(bool isSafeInit)
12+
: base(isSafeInit)
13+
{
14+
}
15+
1016
public class T1
1117
{
1218
private T1()

DeepCloner.Tests/DeepCloner.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
</ItemGroup>
5050
<ItemGroup>
5151
<Compile Include="ArraysSpec.cs" />
52+
<Compile Include="BaseTest.cs" />
5253
<Compile Include="CloneExtensionsSpec.cs" />
5354
<Compile Include="ConstructorsSpec.cs" />
5455
<Compile Include="GenericsSpec.cs" />

DeepCloner.Tests/GenericsSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
namespace Force.DeepCloner.Tests
66
{
7-
[TestFixture]
8-
public class GenericsSpec
7+
[TestFixture(false)]
8+
[TestFixture(true)]
9+
public class GenericsSpec : BaseTest
910
{
11+
public GenericsSpec(bool isSafeInit)
12+
: base(isSafeInit)
13+
{
14+
}
15+
1016
[Test]
1117
public void Tuple_Should_Be_Cloned()
1218
{

DeepCloner.Tests/InheritanceSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
namespace Force.DeepCloner.Tests
77
{
8-
[TestFixture]
9-
public class InheritanceSpec
8+
[TestFixture(false)]
9+
[TestFixture(true)]
10+
public class InheritanceSpec : BaseTest
1011
{
12+
public InheritanceSpec(bool isSafeInit)
13+
: base(isSafeInit)
14+
{
15+
}
16+
1117
public class C1 : IDisposable
1218
{
1319
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Reviewed. Suppression is OK here.")]

DeepCloner.Tests/LoopCheckSpec.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace Force.DeepCloner.Tests
44
{
5-
[TestFixture]
6-
public class LoopCheckSpec
5+
[TestFixture(false)]
6+
[TestFixture(true)]
7+
public class LoopCheckSpec : BaseTest
78
{
9+
public LoopCheckSpec(bool isSafeInit)
10+
: base(isSafeInit)
11+
{
12+
}
13+
814
public class C1
915
{
1016
public int F { get; set; }

DeepCloner.Tests/PerformanceSpec.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ private T CloneViaFormatter<T>(T obj)
5959
}
6060

6161
[Test, Ignore("Manual")]
62-
public void Test_Construct_Variants()
62+
[TestCase(false)]
63+
[TestCase(true)]
64+
public void Test_Construct_Variants(bool isSafe)
6365
{
66+
// we cache cloners for type, so, this only variant with separate run
67+
BaseTest.SwitchTo(isSafe);
6468
var c1 = new C1 { V1 = 1 };
6569
// warm up
6670
for (var i = 0; i < 1000; i++) ManualDeepClone(c1);
@@ -137,6 +141,9 @@ public void Test_Shallow_Variants()
137141
// warm up
138142
for (var i = 0; i < 1000; i++) ManualShallowClone(c1);
139143
for (var i = 0; i < 1000; i++) c1.Clone();
144+
BaseTest.SwitchTo(false);
145+
for (var i = 0; i < 1000; i++) c1.ShallowClone();
146+
BaseTest.SwitchTo(true);
140147
for (var i = 0; i < 1000; i++) c1.ShallowClone();
141148
for (var i = 0; i < 1000; i++) c1.GetClone();
142149

@@ -150,10 +157,17 @@ public void Test_Shallow_Variants()
150157

151158
for (var i = 0; i < 1000000; i++) c1.Clone();
152159
Console.WriteLine("Auto Internal: " + sw.ElapsedMilliseconds);
153-
sw.Restart();
160+
sw.Reset();
161+
BaseTest.SwitchTo(false);
162+
sw.Start();
163+
for (var i = 0; i < 1000000; i++) c1.ShallowClone();
164+
Console.WriteLine("Shallow Unsafe: " + sw.ElapsedMilliseconds);
165+
sw.Reset();
154166

167+
BaseTest.SwitchTo(true);
168+
sw.Start();
155169
for (var i = 0; i < 1000000; i++) c1.ShallowClone();
156-
Console.WriteLine("Shallow: " + sw.ElapsedMilliseconds);
170+
Console.WriteLine("Shallow Safe: " + sw.ElapsedMilliseconds);
157171
sw.Restart();
158172

159173
for (var i = 0; i < 1000000; i++) c1.GetClone(CloningFlags.Shallow);

DeepCloner.Tests/PermissionSpec.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ public void EnsurePermission()
2525
// assembly execute
2626
permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
2727

28-
permissions.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
29-
30-
permissions.AddPermission(new SecurityPermission(PermissionState.Unrestricted));
28+
permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.MemberAccess));
29+
// permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess));
3130

3231
var test = AppDomain.CreateDomain("sandbox", null, setup, permissions);
3332

3433
var instance = (Executor)test.CreateInstanceFromAndUnwrap(this.GetType().Assembly.Location, typeof(Executor).FullName);
3534
instance.DoShallowClone();
35+
instance.DoDeepClone();
36+
}
37+
38+
public class Test
39+
{
40+
public int X { get; set; }
3641
}
3742

3843
public class Executor : MarshalByRefObject
@@ -44,7 +49,7 @@ public void DoDeepClone()
4449

4550
public void DoShallowClone()
4651
{
47-
new List<int> { 1, 2, 3 }.ShallowClone();
52+
new List<int> { 1, 2, 3 }.ShallowClone();
4853
}
4954
}
5055
}

0 commit comments

Comments
 (0)