Skip to content

Commit af38087

Browse files
committed
Added tests for IntPtrExtension.
1 parent 0be7a43 commit af38087

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

ReClass.NET/Extensions/IntPtrExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,15 @@ public static long ToInt64Bits(this IntPtr ptr)
156156
#endif
157157
}
158158

159+
[Pure]
160+
[DebuggerStepThrough]
159161
public static IntPtr From(int value)
160162
{
161163
return (IntPtr)value;
162164
}
163165

166+
[Pure]
167+
[DebuggerStepThrough]
164168
public static IntPtr From(long value)
165169
{
166170
#if RECLASSNET64
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using NFluent;
3+
using ReClassNET.Extensions;
4+
using Xunit;
5+
6+
namespace ReClass.NET_Tests.Extensions
7+
{
8+
public class IntPtrExtensionTest
9+
{
10+
public static TheoryData<IntPtr, bool> GetTestIsNullData => new TheoryData<IntPtr, bool>
11+
{
12+
{ IntPtr.Zero, true },
13+
{ (IntPtr)1, false }
14+
};
15+
16+
[Theory]
17+
[MemberData(nameof(GetTestIsNullData))]
18+
public void TestIsNull(IntPtr ptr, bool expected)
19+
{
20+
Check.That(ptr.IsNull()).IsEqualTo(expected);
21+
}
22+
23+
public static TheoryData<IntPtr, bool> GetTestMayBeValidData => new TheoryData<IntPtr, bool>
24+
{
25+
{ IntPtr.Zero, false },
26+
{ (IntPtr)1, false },
27+
{ (IntPtr)0x10000, true },
28+
{ (IntPtr)int.MaxValue, true },
29+
#if RECLASSNET64
30+
{ (IntPtr)long.MaxValue + 1, false }
31+
#else
32+
{ (IntPtr)int.MaxValue + 1, false }
33+
#endif
34+
};
35+
36+
[Theory]
37+
[MemberData(nameof(GetTestMayBeValidData))]
38+
public void TestMayBeValid(IntPtr ptr, bool expected)
39+
{
40+
Check.That(ptr.MayBeValid()).IsEqualTo(expected);
41+
}
42+
43+
public static TheoryData<IntPtr, IntPtr, IntPtr, bool> GetTestIsInRangeData => new TheoryData<IntPtr, IntPtr, IntPtr, bool>
44+
{
45+
{ (IntPtr)10, (IntPtr)100, (IntPtr)1000, false },
46+
{ (IntPtr)100, (IntPtr)100, (IntPtr)1000, true },
47+
{ (IntPtr)500, (IntPtr)100, (IntPtr)1000, true },
48+
{ (IntPtr)1000, (IntPtr)100, (IntPtr)1000, true },
49+
{ (IntPtr)1500, (IntPtr)100, (IntPtr)1000, false }
50+
};
51+
52+
[Theory]
53+
[MemberData(nameof(GetTestIsInRangeData))]
54+
public void TestIsInRange(IntPtr ptr, IntPtr start, IntPtr end, bool expected)
55+
{
56+
Check.That(ptr.IsInRange(start, end)).IsEqualTo(expected);
57+
}
58+
59+
public static TheoryData<IntPtr, IntPtr, int> GetTestCompareToData => new TheoryData<IntPtr, IntPtr, int>
60+
{
61+
{ (IntPtr)10, (IntPtr)100, -1 },
62+
{ (IntPtr)100, (IntPtr)100, 0 },
63+
{ (IntPtr)500, (IntPtr)100, 1 }
64+
};
65+
66+
[Theory]
67+
[MemberData(nameof(GetTestCompareToData))]
68+
public void TestCompareTo(IntPtr ptr, IntPtr other, int expected)
69+
{
70+
Check.That(ptr.CompareTo(other)).IsEqualTo(expected);
71+
}
72+
73+
public static TheoryData<IntPtr, IntPtr, IntPtr, int> GetTestCompareToRangeData => new TheoryData<IntPtr, IntPtr, IntPtr, int>
74+
{
75+
{ (IntPtr)10, (IntPtr)100, (IntPtr)1000, -1 },
76+
{ (IntPtr)100, (IntPtr)100, (IntPtr)1000, 0 },
77+
{ (IntPtr)500, (IntPtr)100, (IntPtr)1000, 0 },
78+
{ (IntPtr)1000, (IntPtr)100, (IntPtr)1000, 0 },
79+
{ (IntPtr)1500, (IntPtr)100, (IntPtr)1000, 1 }
80+
};
81+
82+
[Theory]
83+
[MemberData(nameof(GetTestCompareToRangeData))]
84+
public void TestCompareToRange(IntPtr ptr, IntPtr start, IntPtr end, int expected)
85+
{
86+
Check.That(ptr.CompareToRange(start, end)).IsEqualTo(expected);
87+
}
88+
89+
public static TheoryData<IntPtr, long> GetTestToInt64BitsData => new TheoryData<IntPtr, long>
90+
{
91+
{ (IntPtr)0x10, 0x10L },
92+
{ (IntPtr)int.MaxValue, 0x7FFF_FFFFL },
93+
{ (IntPtr)int.MaxValue + 1, 0x8000_0000L }
94+
};
95+
96+
[Theory]
97+
[MemberData(nameof(GetTestToInt64BitsData))]
98+
public void TestToInt64Bits(IntPtr ptr, long expected)
99+
{
100+
Check.That(ptr.ToInt64Bits()).IsEqualTo(expected);
101+
}
102+
}
103+
}

ReClass.NET_Tests/ReClass.NET_Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Extensions\EncodingExtensionTest.cs" />
7474
<Compile Include="Extensions\FloatingPointExtensionTest.cs" />
7575
<Compile Include="Extensions\EnumerableExtensionTests.cs" />
76+
<Compile Include="Extensions\IntPtrExtensionTest.cs" />
7677
<Compile Include="Extensions\ListExtensionTest.cs" />
7778
<Compile Include="Extensions\PointExtensionTest.cs" />
7879
<Compile Include="Extensions\StringBuilderExtensionTest.cs" />

0 commit comments

Comments
 (0)