AdventOfCode2023/tests/UPolynomialRootsTestCases.pas

89 lines
2.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 UPolynomialRootsTestCases;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, UPolynomial, UPolynomialRoots, UBigInt;
type
{ TPolynomialRootsTestCase }
TPolynomialRootsTestCase = class(TTestCase)
protected
FRootIsolation: TRootIsolation;
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestBisectionRootIsolation;
end;
implementation
{ TPolynomialRootsTestCase }
procedure TPolynomialRootsTestCase.SetUp;
begin
inherited SetUp;
FRootIsolation := TRootIsolation.Create;
end;
procedure TPolynomialRootsTestCase.TearDown;
begin
FRootIsolation.Free;
inherited TearDown;
end;
procedure TPolynomialRootsTestCase.TestBisectionRootIsolation;
const
expRoots: array of Cardinal = (34000, 23017, 5);
var
exp: Cardinal;
a: TBigIntPolynomial;
r: TIsolatingIntervals;
ri: TIsolatingInterval;
found: Boolean;
begin
// y = 3 * (x - 34000) * (x - 23017) * (x - 5) * (x^2 - 19) * (x + 112)
// = 3 * x^6 - 170730 * x^5 + 2329429920 * x^4 + 251300082690 * x^3 - 1270471872603 * x^2 + 4774763204640 * x - 24979889760000
a := TBigIntPolynomial.Create([-24979889760000, 4774763204640, -1270471872603, 251300082690, 2329429920, -170730, 3]);
r := FRootIsolation.Bisect(a);
AssertEquals(Length(expRoots), r.Count);
for exp in expRoots do
begin
found := False;
for ri in r do
if (ri.A <= exp) and (exp <= ri.B) then
begin
found := True;
Break;
end;
AssertTrue('No isolating interval for expected root ' + IntToStr(exp) + ' found.', found);
end;
end;
initialization
RegisterTest(TPolynomialRootsTestCase);
end.