Repository navigation
Expand file tree
/
Copy pathrule_machine.php
More file actions
153 lines (124 loc) · 4.03 KB
/
Copy pathrule_machine.php
File metadata and controls
153 lines (124 loc) · 4.03 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Rasuvaeff\PropertyTesting\Gen;
use Rasuvaeff\PropertyTesting\Runner\CallableTrialExecutor;
use Rasuvaeff\PropertyTesting\Runner\Falsified;
use Rasuvaeff\PropertyTesting\Runner\Passed;
use Rasuvaeff\PropertyTesting\Runner\PropertyConfig;
use Rasuvaeff\PropertyTesting\Runner\PropertyDefinition;
use Rasuvaeff\PropertyTesting\Runner\PropertyRunner;
use Rasuvaeff\PropertyTesting\StateMachine\Invariant;
use Rasuvaeff\PropertyTesting\StateMachine\Precondition;
use Rasuvaeff\PropertyTesting\StateMachine\Rule;
use Rasuvaeff\PropertyTesting\StateMachine\RuleSequence;
/**
* Stateful testing with the rule-based façade: one class, whose #[Rule]
* methods are the steps, whose #[Invariant] holds after every step, and
* whose own fields are the model. Against a correct stack the property
* passes; against one that pops in FIFO order it is falsified and the
* sequence shrinks to the shortest witness.
*/
interface Stack
{
public function push(int $value): void;
public function pop(): int;
public function size(): int;
}
final class LifoStack implements Stack
{
/** @var list<int> */
private array $items = [];
public function push(int $value): void
{
$this->items[] = $value;
}
public function pop(): int
{
return array_pop($this->items) ?? throw new UnderflowException('empty');
}
public function size(): int
{
return count($this->items);
}
}
final class FifoStack implements Stack
{
/** @var list<int> */
private array $items = [];
public function push(int $value): void
{
$this->items[] = $value;
}
public function pop(): int
{
// BUG: oldest first.
return array_shift($this->items) ?? throw new UnderflowException('empty');
}
public function size(): int
{
return count($this->items);
}
}
final class StackMachine
{
/** @var list<int> */
private array $expected = [];
public function __construct(private readonly Stack $sut) {}
#[Rule]
public function push(int $value): void
{
$this->sut->push($value);
$this->expected[] = $value;
}
/** @return array<string, \Rasuvaeff\PropertyTesting\ArbitraryInterface> */
public static function pushGenerators(): array
{
return ['value' => Gen::intBetween(0, 9)];
}
#[Rule]
#[Precondition('notEmpty')]
public function pop(): void
{
$expected = array_pop($this->expected);
$actual = $this->sut->pop();
if ($actual !== $expected) {
throw new RuntimeException(sprintf('popped %d, expected %d', $actual, $expected));
}
}
public function notEmpty(): bool
{
return $this->expected !== [];
}
#[Invariant]
public function sizeMatches(): void
{
if ($this->sut->size() !== count($this->expected)) {
throw new RuntimeException('size mismatch');
}
}
}
$definition = new PropertyDefinition(
id: 'examples::stackBehavesLikeItsModel',
name: 'stackBehavesLikeItsModel',
generators: ['sequence' => Gen::rules(StackMachine::class, maxLength: 20)],
parameterNames: ['sequence'],
config: new PropertyConfig(runs: 100, seed: 42),
);
$runner = new PropertyRunner();
foreach (['LifoStack' => LifoStack::class, 'FifoStack' => FifoStack::class] as $name => $class) {
$result = $runner->run($definition, new CallableTrialExecutor(
static function (RuleSequence $sequence) use ($class): void {
$sequence->run(static fn(): StackMachine => new StackMachine(new $class()));
},
));
echo "== {$name} ==\n";
if ($result instanceof Passed) {
printf("passed %d sequences\n", $result->statistics->checks);
} elseif ($result instanceof Falsified) {
$example = $result->counterExample();
printf("falsified; shrunk sequence: %s\n", (string) $example->shrunkArguments['sequence']);
printf("failure: %s\n", $example->failure?->getMessage());
}
echo "\n";
}