Snippets

Stefan Glienke DUnit with external test data

Created by Stefan Glienke
unit PrimeFactors.DUnit.Test;

interface

uses
  Spring,
  Spring.Testing,
  PrimeFactors,
  PrimeFactors.Utils;

type
  TPrimeFactor = record
    Input: Integer;
    Expected: TArray<Integer>;
  end;

  TPrimeFactorsTest = class(TTestCase)
  public
    class function FactorsCases: TArray<TPrimeFactor>; static;
  published
    [TestCaseSource('FactorsCases')]
    procedure CheckPrimeFactorsTest(const primeFactor: TPrimeFactor);
  end;

implementation

const
  // https://en.wikipedia.org/wiki/Prime_factor
  FactorsTable: array[0..9] of TPrimeFactor = (
   (Input: 1; Expected: []),
   (Input: 2; Expected: [2]),
   (Input: 3; Expected: [3]),
   (Input: 4; Expected: [2, 2]),
   (Input: 5; Expected: [5]),
   (Input: 6; Expected: [2, 3]),
   (Input: 7; Expected: [7]),
   (Input: 8; Expected: [2, 2, 2]),
   (Input: 9; Expected: [3, 3]),
   (Input: 2 * 2 * 3 * 3 * 5 * 7 * 11 * 11 * 13; Expected: [2, 2, 3, 3, 5, 7, 11, 11, 13])
  );

class function TPrimeFactorsTest.FactorsCases: TArray<TPrimeFactor>;
begin
  Result := TArray.Copy<TPrimeFactor>(FactorsTable);
end;

procedure TPrimeFactorsTest.CheckPrimeFactorsTest(const primeFactor: TPrimeFactor);
begin
  CheckEquals(primeFactor.Expected.ToString, TPrime.Factorization(primeFactor.Input).ToString);
end;

end.

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.