-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypothesis.Examples.Tests.pas
More file actions
139 lines (110 loc) · 3.58 KB
/
Hypothesis.Examples.Tests.pas
File metadata and controls
139 lines (110 loc) · 3.58 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
unit Hypothesis.Examples.Tests;
interface
uses
DUnitX.TestFramework,
System.SysUtils,
Hypothesis.Attributes,
Hypothesis.Runner,
Hypothesis.Examples;
type
[TestFixture]
TStringUtilsPropertyTests = class
public
[Test]
procedure RunTestReverseIsInvolutive;
[ForAll(100)]
procedure TestReverseIsInvolutive([StringAlpha(0, 50)] const Text: string);
[Test]
procedure RunTestReverseLengthPreserved;
[ForAll(100)]
procedure TestReverseLengthPreserved([StringGen(0, 100)] const Text: string);
end;
[TestFixture]
TMathUtilsPropertyTests = class
public
[Test]
procedure RunTestAdditionIsCommutative;
[ForAll(100)]
procedure TestAdditionIsCommutative([IntRange(-1000, 1000)] const A: Integer;
[IntRange(-1000, 1000)] const B: Integer);
[Test]
procedure RunTestAdditionIsAssociative;
[ForAll(100)]
procedure TestAdditionIsAssociative([IntRange(-100, 100)] const A: Integer;
[IntRange(-100, 100)] const B: Integer;
[IntRange(-100, 100)] const C: Integer);
[Test]
procedure RunTestIsEvenConsistency;
[ForAll(100)]
procedure TestIsEvenConsistency([IntRange(-10000, 10000)] const Value: Integer);
end;
implementation
procedure TStringUtilsPropertyTests.RunTestReverseIsInvolutive;
begin
THypothesis.Run(Self, 'TestReverseIsInvolutive');
end;
procedure TStringUtilsPropertyTests.RunTestReverseLengthPreserved;
begin
THypothesis.Run(Self, 'TestReverseLengthPreserved');
end;
procedure TStringUtilsPropertyTests.TestReverseIsInvolutive(const Text: string);
var
Reversed: string;
DoubleReversed: string;
begin
Reversed := TStringUtils.Reverse(Text);
DoubleReversed := TStringUtils.Reverse(Reversed);
Assert.AreEqual(Text, DoubleReversed,
'Reversing a string twice should yield the original string');
end;
procedure TStringUtilsPropertyTests.TestReverseLengthPreserved(const Text: string);
var
Reversed: string;
begin
Reversed := TStringUtils.Reverse(Text);
Assert.AreEqual(Text.Length, Reversed.Length,
'Reversing should preserve string length');
end;
procedure TMathUtilsPropertyTests.TestAdditionIsCommutative(const A: Integer; const B: Integer);
var
Sum1: Int64;
Sum2: Int64;
begin
Sum1 := TMathUtils.Add(A, B);
Sum2 := TMathUtils.Add(B, A);
Assert.AreEqual(Sum1, Sum2,
Format('Addition should be commutative: %d + %d = %d + %d', [A, B, B, A]));
end;
procedure TMathUtilsPropertyTests.TestAdditionIsAssociative(const A: Integer; const B: Integer; const C: Integer);
var
Result1: Int64;
Result2: Int64;
begin
Result1 := TMathUtils.Add(TMathUtils.Add(A, B), C);
Result2 := TMathUtils.Add(A, TMathUtils.Add(B, C));
Assert.AreEqual(Result1, Result2,
Format('Addition should be associative: (%d + %d) + %d = %d + (%d + %d)', [A, B, C, A, B, C]));
end;
procedure TMathUtilsPropertyTests.RunTestAdditionIsCommutative;
begin
THypothesis.Run(Self, 'TestAdditionIsCommutative');
end;
procedure TMathUtilsPropertyTests.RunTestAdditionIsAssociative;
begin
THypothesis.Run(Self, 'TestAdditionIsAssociative');
end;
procedure TMathUtilsPropertyTests.RunTestIsEvenConsistency;
begin
THypothesis.Run(Self, 'TestIsEvenConsistency');
end;
procedure TMathUtilsPropertyTests.TestIsEvenConsistency(const Value: Integer);
var
IsEven: Boolean;
ExpectedEven: Boolean;
begin
IsEven := TMathUtils.IsEven(Value);
ExpectedEven := (Value mod 2) = 0;
Assert.AreEqual(ExpectedEven, IsEven,
Format('IsEven(%d) should return %s', [Value, BoolToStr(ExpectedEven, True)]));
end;
end.