|
| 1 | +using QuadrupleLib.Accelerators; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace QuadrupleLib.Tests.Utilities |
| 5 | +{ |
| 6 | + public abstract class AcceleratorTests<TAccelerator> |
| 7 | + where TAccelerator : IAccelerator |
| 8 | + { |
| 9 | + [Theory] |
| 10 | + [InlineData(1UL, 0UL, 0UL, 0UL)] |
| 11 | + [InlineData(1UL, 1UL, 1UL, 0UL)] |
| 12 | + [InlineData(1UL, 2UL, 2UL, 0UL)] |
| 13 | + [InlineData(4UL, 1UL << 63, 0UL, 2UL)] |
| 14 | + public void IsBigMulCorrect(ulong a, ulong b, ulong expectedLo, ulong expectedHi) |
| 15 | + { |
| 16 | + ulong actualHi = TAccelerator.BigMul(a, b, out ulong actualLo); |
| 17 | + Assert.Equal((expectedLo, expectedHi), (actualLo, actualHi)); |
| 18 | + } |
| 19 | + |
| 20 | + [Theory] |
| 21 | + [InlineData(2UL, 0UL, 1UL, 0UL, 2UL, 0UL)] |
| 22 | + [InlineData(2UL, 0UL, 2UL, 0UL, 1UL, 0UL)] |
| 23 | + [InlineData(0UL, 4UL, 2UL, 0UL, 0UL, 2UL)] |
| 24 | + [InlineData(0UL, 4UL, 3UL, 0UL, 6148914691236517205UL, 1UL)] |
| 25 | + public void IsDivRemQuotientCorrect(ulong aLo, ulong aHi, ulong bLo, ulong bHi, ulong expectedLo, ulong expectedHi) |
| 26 | + { |
| 27 | + UInt128 a = aLo | ((UInt128)aHi << 64); |
| 28 | + UInt128 b = bLo | ((UInt128)bHi << 64); |
| 29 | + UInt128 expected = expectedLo | ((UInt128)expectedHi << 64); |
| 30 | + |
| 31 | + (UInt128 actual, UInt128 _) = TAccelerator.DivRem(a, b); |
| 32 | + Assert.Equal(expected, actual); |
| 33 | + } |
| 34 | + |
| 35 | + [Theory] |
| 36 | + [InlineData(2UL, 0UL, 1UL, 0UL, 0UL, 0UL)] |
| 37 | + [InlineData(2UL, 0UL, 2UL, 0UL, 0UL, 0UL)] |
| 38 | + [InlineData(0UL, 4UL, 2UL, 0UL, 0UL, 0UL)] |
| 39 | + [InlineData(0UL, 4UL, 3UL, 0UL, 1UL, 0UL)] |
| 40 | + public void IsDivRemRemainderCorrect(ulong aLo, ulong aHi, ulong bLo, ulong bHi, ulong expectedLo, ulong expectedHi) |
| 41 | + { |
| 42 | + UInt128 a = aLo | ((UInt128)aHi << 64); |
| 43 | + UInt128 b = bLo | ((UInt128)bHi << 64); |
| 44 | + UInt128 expected = expectedLo | ((UInt128)expectedHi << 64); |
| 45 | + |
| 46 | + (UInt128 _, UInt128 actual) = TAccelerator.DivRem(a, b); |
| 47 | + Assert.Equal(expected, actual); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public class AcceleratorTests_DefaultAccelerator : |
| 52 | + AcceleratorTests<DefaultAccelerator> { } |
| 53 | + |
| 54 | + public class AcceleratorTests_SoftwareAccelerator : |
| 55 | + AcceleratorTests<SoftwareAccelerator> |
| 56 | + { } |
| 57 | +} |
0 commit comments