Merge branch 'bigint' into day24-analytical

This commit is contained in:
2024-05-23 22:03:41 +02:00
2 changed files with 181 additions and 55 deletions

View File

@@ -55,7 +55,6 @@ type
// TODO: TBigIntConversionTestCase
// TODO: TBigIntIncrementDecrementTestCase
// TODO: TBigIntQuotientTestCase
// TODO: TBigIntShiftRightTestCase
{ TBigIntSignTestCase }
@@ -210,6 +209,21 @@ type
procedure TestZero;
end;
{ TBigIntShiftRightTestCase }
TBigIntShiftRightTestCase = class(TTestCase)
private
procedure Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult: string);
published
procedure TestShort;
procedure TestShortWithCarry;
procedure TestLongWithCarry;
procedure TestLongWithMultiDigitCarry;
procedure TestWithAlignedDigits;
procedure TestShiftToZero;
procedure TestZero;
end;
{ TBigIntEqualityTestCase }
TBigIntEqualityTestCase = class(TTestCase)
@@ -718,7 +732,8 @@ end;
{ TBigIntShiftLeftTestCase }
procedure TBigIntShiftLeftTestCase.Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult: string);
procedure TBigIntShiftLeftTestCase.Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult:
string);
var
a, s: TBigInt;
begin
@@ -767,6 +782,63 @@ begin
Test('0', 119, '0');
end;
{ TBigIntShiftRightTestCase }
procedure TBigIntShiftRightTestCase.Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult:
string);
var
a, s: TBigInt;
begin
a := TBigInt.FromHexadecimalString(AHexValueOperand);
s := TBigInt.FromHexadecimalString(AHexValueResult);
AssertTrue('BigInt from hexadecimal string ''' + AHexValueOperand + ''' shifted right by ''' + IntToStr(AShift)
+ ''' was not equal to BigInt from hexadecimal string ''' + AHexValueResult + '''.',
s = (a >> AShift));
end;
procedure TBigIntShiftRightTestCase.TestShort;
begin
// BIN 100110101
// BIN 10011
Test('135', 4, '13');
end;
procedure TBigIntShiftRightTestCase.TestShortWithCarry;
begin
// BIN 1 1101 1010 1110 1001 1000 0111 0000 0000 0000 1111
// BIN 11 1011 0101 1101 0011 0000 1110
Test('1DAE987000F', 15, '3B5D30E');
end;
procedure TBigIntShiftRightTestCase.TestLongWithCarry;
begin
// BIN 10 0110 0001 0110 0100 0111 1100 1001 1001 1111 0010 1010 1000 1000 1010 0010 0010 1101 1101
// BIN 100 1100 0010 1100 1000 1111 1001 0011 0011 1110 0101 0101 0001 0001 0100 0100
Test('261647C99F2A88A22DD', 11, '4C2C8F933E551144');
end;
procedure TBigIntShiftRightTestCase.TestLongWithMultiDigitCarry;
begin
Test('647C99F12A088A22FF6DD02187345A3B839401BFB9272', 104, '647C99F12A088A22FF6');
end;
// Shifts the left operand by a multiple of the length of full TBigInt digits, so the digits will be shifted, but not
// changed.
procedure TBigIntShiftRightTestCase.TestWithAlignedDigits;
begin
Test('C5E10F0F39000AA2000C020000010000000000000F00000007', 32 * 5, 'C5E10F0F39');
end;
procedure TBigIntShiftRightTestCase.TestShiftToZero;
begin
Test('B5D10F0F39882F', 150, '0');
end;
procedure TBigIntShiftRightTestCase.TestZero;
begin
Test('0', 3, '0');
end;
{ TBigIntEqualityTestCase }
procedure TBigIntEqualityTestCase.TestEqual(const AValue: Int64);
@@ -954,6 +1026,7 @@ initialization
RegisterTest(TBigIntDifferenceTestCase);
RegisterTest(TBigIntProductTestCase);
RegisterTest(TBigIntShiftLeftTestCase);
RegisterTest(TBigIntShiftRightTestCase);
RegisterTest(TBigIntEqualityTestCase);
RegisterTest(TBigIntComparisonTestCase);
end.