188 lines
5.3 KiB
Plaintext
188 lines
5.3 KiB
Plaintext
{
|
|
Solutions to the Advent Of Code.
|
|
Copyright (C) 2024 Stefan Müller
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
}
|
|
|
|
unit UPolynomialTestCases;
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, fpcunit, testregistry, UPolynomial, UBigInt;
|
|
|
|
type
|
|
|
|
{ TBigIntPolynomialTestCase }
|
|
|
|
TBigIntPolynomialTestCase = class(TTestCase)
|
|
private
|
|
procedure TestCreateWithDegree(const ACoefficients: array of TBigInt; const ADegree: Integer);
|
|
published
|
|
procedure TestCreate;
|
|
procedure TestCreateDegreeOne;
|
|
procedure TestCreateDegreeZero;
|
|
procedure TestCreateDegreeZeroTrim;
|
|
procedure TestEqual;
|
|
procedure TestUnequalSameLength;
|
|
procedure TestUnequalDifferentLength;
|
|
procedure TestTrimLeadingZeros;
|
|
procedure TestCalcValueAt;
|
|
procedure TestSignVariations;
|
|
procedure TestScaleByPowerOfTwo;
|
|
procedure TestScaleVariable;
|
|
procedure TestTranslateVariableByOne;
|
|
procedure TestRevertOrderOfCoefficients;
|
|
procedure TestDivideByVariable;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TBigIntPolynomialTestCase }
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCreateWithDegree(const ACoefficients: array of TBigInt; const ADegree: Integer);
|
|
var
|
|
a: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create(ACoefficients);
|
|
AssertEquals('Degree of created polynomial incorrect.', ADegree, a.Degree);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCreate;
|
|
begin
|
|
TestCreateWithDegree([992123, 7, 20, 4550022], 3);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCreateDegreeOne;
|
|
begin
|
|
TestCreateWithDegree([4007], 0);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCreateDegreeZero;
|
|
begin
|
|
TestCreateWithDegree([], -1);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCreateDegreeZeroTrim;
|
|
begin
|
|
TestCreateWithDegree([0], -1);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestEqual;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([10, 7, 5, 1034]);
|
|
b := TBigIntPolynomial.Create([10, 7, 5, 1034]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestUnequalSameLength;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([103, 7, 5, 10]);
|
|
b := TBigIntPolynomial.Create([1034, 7, 5, 10]);
|
|
AssertTrue('Polynomials are equal.', a <> b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestUnequalDifferentLength;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([40000, 10, 7, 5, 1034]);
|
|
b := TBigIntPolynomial.Create([10, 7, 5, 1034]);
|
|
AssertTrue('Polynomials are equal.', a <> b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestTrimLeadingZeros;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([10, 7, 5, 1034, 0, 0]);
|
|
b := TBigIntPolynomial.Create([10, 7, 5, 1034]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestCalcValueAt;
|
|
var
|
|
a: TBigIntPolynomial;
|
|
exp: TBigInt;
|
|
begin
|
|
a := TBigIntPolynomial.Create([80, 892477222, 0, 921556, 7303]);
|
|
exp:= TBigInt.FromInt64(16867124285);
|
|
AssertTrue('Polynomial evaluation unexpected.', a.CalcValueAt(15) = exp);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestSignVariations;
|
|
var
|
|
a: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([-10, 15, 0, 10, -20, -15, 0, 0, 5, 10, -10]);
|
|
AssertEquals(4, a.CalcSignVariations);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestScaleByPowerOfTwo;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([10, 7, 5, 1034]).ScaleByPowerOfTwo(7);
|
|
b := TBigIntPolynomial.Create([128 * 10, 128 * 7, 128 * 5, 128 * 1034]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestScaleVariable;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([10, 7, 5, 1034]).ScaleVariable(TBigInt.FromInt64(10));
|
|
b := TBigIntPolynomial.Create([10, 70, 500, 1034000]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestTranslateVariableByOne;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([10, 7, 5, 1034]).TranslateVariableByOne;
|
|
b := TBigIntPolynomial.Create([1056, 3119, 3107, 1034]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestRevertOrderOfCoefficients;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([0, 10, 7, 5, 1034]).RevertOrderOfCoefficients;
|
|
b := TBigIntPolynomial.Create([1034, 5, 7, 10]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
procedure TBigIntPolynomialTestCase.TestDivideByVariable;
|
|
var
|
|
a, b: TBigIntPolynomial;
|
|
begin
|
|
a := TBigIntPolynomial.Create([0, 10, 7, 5, 1034]).DivideByVariable;
|
|
b := TBigIntPolynomial.Create([10, 7, 5, 1034]);
|
|
AssertTrue('Polynomials are not equal.', a = b);
|
|
end;
|
|
|
|
initialization
|
|
|
|
RegisterTest(TBigIntPolynomialTestCase);
|
|
end.
|
|
|