|
| 1 | +using System; |
| 2 | +using NFluent; |
| 3 | +using ReClassNET.MemoryScanner; |
| 4 | +using ReClassNET.MemoryScanner.Comparer; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace ReClass.NET_Tests.MemoryScanner.Comparer |
| 8 | +{ |
| 9 | + public class FloatMemoryComparerTest |
| 10 | + { |
| 11 | + [Theory] |
| 12 | + [InlineData(ScanCompareType.Equal, ScanRoundMode.Normal, 0.0f, 0.0f)] |
| 13 | + [InlineData(ScanCompareType.Equal, ScanRoundMode.Strict, 1.0f, 2.0f)] |
| 14 | + [InlineData(ScanCompareType.Equal, ScanRoundMode.Truncate, 2.0f, 1.0f)] |
| 15 | + [InlineData(ScanCompareType.Between, ScanRoundMode.Normal, 2.0f, 4.0f)] |
| 16 | + [InlineData(ScanCompareType.BetweenOrEqual, ScanRoundMode.Strict, 4.0f, 2.0f)] |
| 17 | + [InlineData(ScanCompareType.NotEqual, ScanRoundMode.Truncate, 0.0f, 0.0f)] |
| 18 | + public void TestConstructor(ScanCompareType compareType, ScanRoundMode roundMode, float value1, float value2) |
| 19 | + { |
| 20 | + var sut = new FloatMemoryComparer(compareType, roundMode, 1, value1, value2); |
| 21 | + |
| 22 | + Check.That(sut.CompareType).IsEqualTo(compareType); |
| 23 | + Check.That(sut.RoundType).IsEqualTo(roundMode); |
| 24 | + Check.That(sut.ValueSize).IsEqualTo(sizeof(float)); |
| 25 | + Check.That(sut.Value1).IsOneOf(value1, value2); |
| 26 | + Check.That(sut.Value2).IsOneOf(value1, value2); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData(1)] |
| 31 | + [InlineData(2)] |
| 32 | + [InlineData(3)] |
| 33 | + public void TestConstructorSignificantDigits(int significantDigits) |
| 34 | + { |
| 35 | + const float Value1 = 1.234567f; |
| 36 | + const float Value2 = 7.654321f; |
| 37 | + |
| 38 | + var sut = new FloatMemoryComparer(ScanCompareType.Equal, ScanRoundMode.Normal, significantDigits, Value1, Value2); |
| 39 | + |
| 40 | + Check.That(sut.Value1).IsEqualTo((float)Math.Round(Value1, significantDigits, MidpointRounding.AwayFromZero)); |
| 41 | + Check.That(sut.Value2).IsEqualTo((float)Math.Round(Value2, significantDigits, MidpointRounding.AwayFromZero)); |
| 42 | + } |
| 43 | + |
| 44 | + public static TheoryData<ScanCompareType, float, float, byte[], bool, ScanResult> GetTestCompareBasicData() => new TheoryData<ScanCompareType, float, float, byte[], bool, ScanResult> |
| 45 | + { |
| 46 | + { ScanCompareType.GreaterThan, 1.0f, 0.0f, BitConverter.GetBytes(0.0f), false, null }, |
| 47 | + { ScanCompareType.GreaterThan, 1.0f, 0.0f, BitConverter.GetBytes(2.0f), true, new FloatScanResult(2.0f) }, |
| 48 | + { ScanCompareType.GreaterThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(0.0f), false, null }, |
| 49 | + { ScanCompareType.GreaterThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(1.0f), true, new FloatScanResult(1.0f) }, |
| 50 | + { ScanCompareType.GreaterThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(2.0f), true, new FloatScanResult(2.0f) }, |
| 51 | + { ScanCompareType.LessThan, 1.0f, 0.0f, BitConverter.GetBytes(1.0f), false, null }, |
| 52 | + { ScanCompareType.LessThan, 1.0f, 0.0f, BitConverter.GetBytes(0.0f), true, new FloatScanResult(0.0f) }, |
| 53 | + { ScanCompareType.LessThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(2.0f), false, null }, |
| 54 | + { ScanCompareType.LessThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(1.0f), true, new FloatScanResult(1.0f) }, |
| 55 | + { ScanCompareType.LessThanOrEqual, 1.0f, 0.0f, BitConverter.GetBytes(0.0f), true, new FloatScanResult(0.0f) }, |
| 56 | + { ScanCompareType.Between, 1.0f, 2.0f, BitConverter.GetBytes(0.0f), false, null }, |
| 57 | + { ScanCompareType.Between, 1.0f, 2.0f, BitConverter.GetBytes(1.0f), false, null }, |
| 58 | + { ScanCompareType.Between, 1.0f, 2.0f, BitConverter.GetBytes(2.0f), false, null }, |
| 59 | + { ScanCompareType.Between, 1.0f, 2.0f, BitConverter.GetBytes(3.0f), false, null }, |
| 60 | + { ScanCompareType.BetweenOrEqual, 1.0f, 2.0f, BitConverter.GetBytes(0.0f), false, null }, |
| 61 | + { ScanCompareType.BetweenOrEqual, 1.0f, 2.0f, BitConverter.GetBytes(1.0f), true, new FloatScanResult(1.0f) }, |
| 62 | + { ScanCompareType.BetweenOrEqual, 1.0f, 2.0f, BitConverter.GetBytes(2.0f), true, new FloatScanResult(2.0f) }, |
| 63 | + { ScanCompareType.BetweenOrEqual, 1.0f, 2.0f, BitConverter.GetBytes(3.0f), false, null } |
| 64 | + }; |
| 65 | + |
| 66 | + public static TheoryData<ScanCompareType, float, float, byte[], bool, ScanResult> GetTestCompareScanCompareTypeUnknownData() => new TheoryData<ScanCompareType, float, float, byte[], bool, ScanResult> |
| 67 | + { |
| 68 | + { ScanCompareType.Unknown, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), true, new FloatScanResult(0.0f) }, |
| 69 | + { ScanCompareType.Unknown, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), true, new FloatScanResult(1.0f) } |
| 70 | + }; |
| 71 | + |
| 72 | + [Theory] |
| 73 | + [MemberData(nameof(GetTestCompareBasicData))] |
| 74 | + [MemberData(nameof(GetTestCompareScanCompareTypeUnknownData))] |
| 75 | + public void TestCompare(ScanCompareType compareType, float value1, float value2, byte[] data, bool expectedResult, ScanResult expectedScanResult) |
| 76 | + { |
| 77 | + var sut = new FloatMemoryComparer(compareType, ScanRoundMode.Normal, 1, value1, value2); |
| 78 | + |
| 79 | + Check.That(sut.Compare(data, 0, out var scanResult)).IsEqualTo(expectedResult); |
| 80 | + Check.That(scanResult).IsEqualTo(expectedScanResult); |
| 81 | + if (scanResult != null) |
| 82 | + { |
| 83 | + Check.That(scanResult).IsInstanceOf<FloatScanResult>(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [Theory] |
| 88 | + [InlineData(ScanCompareType.Changed)] |
| 89 | + [InlineData(ScanCompareType.NotChanged)] |
| 90 | + [InlineData(ScanCompareType.Decreased)] |
| 91 | + [InlineData(ScanCompareType.DecreasedOrEqual)] |
| 92 | + [InlineData(ScanCompareType.Increased)] |
| 93 | + [InlineData(ScanCompareType.IncreasedOrEqual)] |
| 94 | + public void TestCompareInvalidCompareTypeThrows(ScanCompareType compareType) |
| 95 | + { |
| 96 | + var sut = new FloatMemoryComparer(compareType, ScanRoundMode.Normal, 1, 0.0f, 0.0f); |
| 97 | + |
| 98 | + Check.ThatCode(() => sut.Compare(BitConverter.GetBytes(0.0f), 0, out _)).Throws<InvalidCompareTypeException>(); |
| 99 | + } |
| 100 | + |
| 101 | + public static TheoryData<byte[], int, Type> GetTestCompareThrowsData() => new TheoryData<byte[], int, Type> |
| 102 | + { |
| 103 | + { null, 0, typeof(ArgumentNullException) }, |
| 104 | + { new byte[0], 0, typeof(ArgumentOutOfRangeException) }, |
| 105 | + { new byte[1], 1, typeof(ArgumentOutOfRangeException) } |
| 106 | + }; |
| 107 | + |
| 108 | + [Theory] |
| 109 | + [MemberData(nameof(GetTestCompareThrowsData))] |
| 110 | + public void TestCompareInvalidDataThrows(byte[] data, int index, Type expectedExceptionType) |
| 111 | + { |
| 112 | + var sut = new FloatMemoryComparer(ScanCompareType.Unknown, ScanRoundMode.Normal, 1, 0.0f, 0.0f); |
| 113 | + |
| 114 | + Check.ThatCode(() => sut.Compare(data, index, out _)).ThrowsType(expectedExceptionType); |
| 115 | + } |
| 116 | + |
| 117 | + public static TheoryData<ScanCompareType, float, float, byte[], ScanResult, bool, ScanResult> GetTestCompareWithPreviousData() |
| 118 | + { |
| 119 | + var data = new TheoryData<ScanCompareType, float, float, byte[], ScanResult, bool, ScanResult> |
| 120 | + { |
| 121 | + { ScanCompareType.Changed, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), true, new FloatScanResult(0.0f) }, |
| 122 | + { ScanCompareType.Changed, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), false, null }, |
| 123 | + { ScanCompareType.NotChanged, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), true, new FloatScanResult(1.0f) }, |
| 124 | + { ScanCompareType.NotChanged, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), false, null }, |
| 125 | + { ScanCompareType.Increased, 0.0f, 0.0f, BitConverter.GetBytes(2.0f), new FloatScanResult(1.0f), true, new FloatScanResult(2.0f) }, |
| 126 | + { ScanCompareType.Increased, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), false, null }, |
| 127 | + { ScanCompareType.Increased, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), false, null }, |
| 128 | + { ScanCompareType.IncreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(2.0f), new FloatScanResult(1.0f), true, new FloatScanResult(2.0f) }, |
| 129 | + { ScanCompareType.IncreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), true, new FloatScanResult(1.0f) }, |
| 130 | + { ScanCompareType.IncreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), false, null }, |
| 131 | + { ScanCompareType.Decreased, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), true, new FloatScanResult(0.0f) }, |
| 132 | + { ScanCompareType.Decreased, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), false, null }, |
| 133 | + { ScanCompareType.Decreased, 0.0f, 0.0f, BitConverter.GetBytes(2.0f), new FloatScanResult(1.0f), false, null }, |
| 134 | + { ScanCompareType.DecreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(0.0f), new FloatScanResult(1.0f), true, new FloatScanResult(0.0f) }, |
| 135 | + { ScanCompareType.DecreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(1.0f), new FloatScanResult(1.0f), true, new FloatScanResult(1.0f) }, |
| 136 | + { ScanCompareType.DecreasedOrEqual, 0.0f, 0.0f, BitConverter.GetBytes(2.0f), new FloatScanResult(1.0f), false, null } |
| 137 | + }; |
| 138 | + |
| 139 | + var basicData = GetTestCompareBasicData(); |
| 140 | + foreach (var x in basicData) |
| 141 | + { |
| 142 | + data.Add((ScanCompareType)x[0], (float)x[1], (float)x[2], (byte[])x[3], new FloatScanResult(1.0f), (bool)x[4], (ScanResult)x[5]); |
| 143 | + } |
| 144 | + |
| 145 | + return data; |
| 146 | + } |
| 147 | + |
| 148 | + [Theory] |
| 149 | + [MemberData(nameof(GetTestCompareWithPreviousData))] |
| 150 | + public void TestCompareWithPrevious(ScanCompareType compareType, float value1, float value2, byte[] data, ScanResult previousScanResult, bool expectedResult, ScanResult expectedScanResult) |
| 151 | + { |
| 152 | + var sut = new FloatMemoryComparer(compareType, ScanRoundMode.Normal, 1, value1, value2); |
| 153 | + |
| 154 | + Check.That(sut.Compare(data, 0, previousScanResult, out var scanResult)).IsEqualTo(expectedResult); |
| 155 | + Check.That(scanResult).IsEqualTo(expectedScanResult); |
| 156 | + if (scanResult != null) |
| 157 | + { |
| 158 | + Check.That(scanResult).IsInstanceOf<FloatScanResult>(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void TestCompareWithPreviousThrows() |
| 164 | + { |
| 165 | + var sut = new FloatMemoryComparer(ScanCompareType.Unknown, ScanRoundMode.Normal, 1, 0, 0); |
| 166 | + |
| 167 | + Check.ThatCode(() => sut.Compare(BitConverter.GetBytes(0.0f), 0, new FloatScanResult(0.0f), out _)).Throws<InvalidCompareTypeException>(); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments