From c3ecaf59fa5d702d4c8b575c3397af2704d06587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 23 Dec 2023 20:16:18 +0100 Subject: [PATCH] Added custom constructors for TCosmicExpansion and TStepCounter to simplify test case setup --- solvers/UCosmicExpansion.pas | 16 ++++++--------- solvers/UStepCounter.pas | 7 +++---- tests/UCosmicExpansionTestCases.pas | 31 ++--------------------------- tests/UStepCounterTestCases.pas | 6 +----- 4 files changed, 12 insertions(+), 48 deletions(-) diff --git a/solvers/UCosmicExpansion.pas b/solvers/UCosmicExpansion.pas index 522c74c..5f77003 100644 --- a/solvers/UCosmicExpansion.pas +++ b/solvers/UCosmicExpansion.pas @@ -35,17 +35,17 @@ type TCosmicExpansion = class(TSolver) private + FExpansionFactor: Integer; FColumnExpansion, FRowExpansion: specialize TList; FGalaxies: specialize TList; procedure InitColumnExpansion(const ASize: Integer); public - constructor Create; + constructor Create(const AExpansionFactor: Integer = 999999); destructor Destroy; override; procedure ProcessDataLine(const ALine: string); override; procedure Finish; override; function GetDataFileName: string; override; function GetPuzzleName: string; override; - function GetExpansionFactor: Integer; virtual; end; implementation @@ -64,8 +64,9 @@ begin end; end; -constructor TCosmicExpansion.Create; +constructor TCosmicExpansion.Create(const AExpansionFactor: Integer); begin + FExpansionFactor := AExpansionFactor; FColumnExpansion := specialize TList.Create; FRowExpansion := specialize TList.Create; FGalaxies := specialize TList.Create; @@ -110,12 +111,12 @@ begin for k := Min(FGalaxies[i].X, FGalaxies[j].X) to Max(FGalaxies[i].X, FGalaxies[j].X) - 1 do begin Inc(FPart1, 1 + FColumnExpansion[k]); - Inc(FPart2, 1 + FColumnExpansion[k] * GetExpansionFactor); + Inc(FPart2, 1 + FColumnExpansion[k] * FExpansionFactor); end; for k := Min(FGalaxies[i].Y, FGalaxies[j].Y) + 1 to Max(FGalaxies[i].Y, FGalaxies[j].Y) do begin Inc(FPart1, 1 + FRowExpansion[k]); - Inc(FPart2, 1 + FRowExpansion[k] * GetExpansionFactor); + Inc(FPart2, 1 + FRowExpansion[k] * FExpansionFactor); end; end; end; @@ -130,10 +131,5 @@ begin Result := 'Day 11: Cosmic Expansion'; end; -function TCosmicExpansion.GetExpansionFactor: Integer; -begin - Result := 999999; -end; - end. diff --git a/solvers/UStepCounter.pas b/solvers/UStepCounter.pas index 6772509..887cdb7 100644 --- a/solvers/UStepCounter.pas +++ b/solvers/UStepCounter.pas @@ -38,8 +38,7 @@ type function GetPosition(constref APoint: TPoint): Char; procedure SetPosition(constref APoint: TPoint; const AValue: Char); public - property MaxSteps: Integer read FMaxSteps write FMaxSteps; - constructor Create; + constructor Create(const AMaxSteps: Integer = 64); destructor Destroy; override; procedure ProcessDataLine(const ALine: string); override; procedure Finish; override; @@ -90,9 +89,9 @@ begin FLines[APoint.Y] := s; end; -constructor TStepCounter.Create; +constructor TStepCounter.Create(const AMaxSteps: Integer); begin - FMaxSteps := 64; + FMaxSteps := AMaxSteps; FLines := TStringList.Create; end; diff --git a/tests/UCosmicExpansionTestCases.pas b/tests/UCosmicExpansionTestCases.pas index e2690b5..34171d6 100644 --- a/tests/UCosmicExpansionTestCases.pas +++ b/tests/UCosmicExpansionTestCases.pas @@ -45,13 +45,6 @@ type procedure TestPart1; end; - { TFactor10CosmicExpansion } - - TFactor10CosmicExpansion = class(TCosmicExpansion) - public - function GetExpansionFactor: Integer; override; - end; - { TCosmicExpansionExampleFactor10TestCase } TCosmicExpansionExampleFactor10TestCase = class(TExampleEngineBaseTest) @@ -61,12 +54,6 @@ type procedure TestPart2; end; - { TFactor100CosmicExpansion } - - TFactor100CosmicExpansion = class(TCosmicExpansion) - function GetExpansionFactor: Integer; override; - end; - { TCosmicExpansionExampleFactor100TestCase } TCosmicExpansionExampleFactor100TestCase = class(TExampleEngineBaseTest) @@ -107,18 +94,11 @@ begin AssertEquals(374, FSolver.GetResultPart1); end; -{ TFactor10CosmicExpansion } - -function TFactor10CosmicExpansion.GetExpansionFactor: Integer; -begin - Result := 9; -end; - { TCosmicExpansionExampleFactor10TestCase } function TCosmicExpansionExampleFactor10TestCase.CreateSolver: ISolver; begin - Result := TFactor10CosmicExpansion.Create; + Result := TCosmicExpansion.Create(9); end; procedure TCosmicExpansionExampleFactor10TestCase.TestPart2; @@ -126,18 +106,11 @@ begin AssertEquals(1030, FSolver.GetResultPart2); end; -{ TFactor100CosmicExpansion } - -function TFactor100CosmicExpansion.GetExpansionFactor: Integer; -begin - Result := 99; -end; - { TCosmicExpansionExampleFactor100TestCase } function TCosmicExpansionExampleFactor100TestCase.CreateSolver: ISolver; begin - Result := TFactor100CosmicExpansion.Create; + Result := TCosmicExpansion.Create(99); end; procedure TCosmicExpansionExampleFactor100TestCase.TestPart2; diff --git a/tests/UStepCounterTestCases.pas b/tests/UStepCounterTestCases.pas index 802b7ad..12264e0 100644 --- a/tests/UStepCounterTestCases.pas +++ b/tests/UStepCounterTestCases.pas @@ -61,12 +61,8 @@ end; { TStepCounterMax6ExampleTestCase } function TStepCounterMax6ExampleTestCase.CreateSolver: ISolver; -var - solver: TStepCounter; begin - solver := TStepCounter.Create; - solver.MaxSteps := 6; - Result := solver; + Result := TStepCounter.Create(6); end; procedure TStepCounterMax6ExampleTestCase.TestPart1;