File: AssertHelpers.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (120 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Authors: Alexander Klenin

}
unit AssertHelpers;

{$mode objfpc}

interface

uses
  FPCUnit;

type
  TAssertHelper = class helper for TAssert
  public
    class procedure AssertEquals(
      const AMessage: String;
      const AExpected, AActual: array of Double; ADelta: Double = 0.0); overload;
    class procedure AssertEquals(
      const AExpected, AActual: array of Double; ADelta: Double = 0.0); overload;
    class procedure AssertEquals(
      const AMessage: String; const AExpected, AActual: array of Boolean); overload;
    class procedure AssertEquals(
      const AExpected, AActual: array of Boolean); overload;
  end;

implementation

uses
  SysUtils;

function BooleanArrayEqual(const AA, AB: array of Boolean): Boolean;
var
  len: Integer;
begin
  len := Length(AA);
  if len <> Length(AB) then exit(false);
  if len = 0 then exit(true);
  Result := CompareByte(AA[0], AB[0], len) = 0;
end;

function BooleanArrayToStr(const AData: array of Boolean): String;
var
  b: Boolean;
begin
  Result := '';
  for b in AData do
    Result += BoolToStr(b, 'true,', 'false,');
  Delete(Result, Length(Result), 1);
end;

function DoubleArrayEqual(const AA, AB: array of Double; ADelta: Double): Boolean;
var
  i: Integer;
begin
  if Length(AA) <> Length(AB) then exit(false);
  for i := 0 to High(AA) do
    if Abs(AA[i] - AB[i]) > ADelta then exit(false);
  Result := true;
end;

function DoubleArrayToStr(const AData: array of Double): String;
var
  a: Double;
begin
  Result := '';
  for a in AData do
    Result += Format('%g,', [a]);
  Delete(Result, Length(Result), 1);
end;

{ TAssertHelper }

class procedure TAssertHelper.AssertEquals(
  const AExpected, AActual: array of Boolean);
begin
  AssertEquals('', AExpected, AActual);
end;

class procedure TAssertHelper.AssertEquals(
  const AExpected, AActual: array of Double; ADelta: Double);
begin
  AssertEquals('', AExpected, AActual, ADelta);
end;

class procedure TAssertHelper.AssertEquals(
  const AMessage: String; const AExpected, AActual: array of Boolean);
var
  expectedStr, actualStr: String;
begin
  if BooleanArrayEqual(AExpected, AActual) then exit;
  expectedStr := BooleanArrayToStr(AExpected);
  actualStr := BooleanArrayToStr(AActual);
  Fail(AMessage + ComparisonMsg(expectedStr, actualStr));
end;

class procedure TAssertHelper.AssertEquals(
  const AMessage: String; const AExpected, AActual: array of Double;
  ADelta: Double);
var
  expectedStr, actualStr: String;
begin
  if DoubleArrayEqual(AExpected, AActual, ADelta) then exit;
  expectedStr := DoubleArrayToStr(AExpected);
  actualStr := DoubleArrayToStr(AActual);
  Fail(AMessage + ComparisonMsg(expectedStr, actualStr));
end;


initialization
  Random; // Workaround for issue #21808.

end.