-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactcheck.php
More file actions
125 lines (93 loc) · 2.93 KB
/
factcheck.php
File metadata and controls
125 lines (93 loc) · 2.93 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
<?php
namespace atst\factcheck;
require "vendor/autoload.php";
use iter;
const DEFAULT_NUMBER_OF_TEST_CASES=1000;
function ints($min = null, $max = null) {
return iter\chain(
iter\filter(iter\fn\operator("!==", null), [$min, $max]),
random_values("rand", $min ?: 0, $max ?: 1000000)
);
}
function always($value) {
return iter\repeat($value);
}
function unique($elements) {
$seen = array();
foreach ($elements as $element) {
if (!in_array($element, $seen)) {
$seen[] = $element;
yield $element;
}
}
}
function for_all(/** [numberOfTestCases], [generator[, generator[, generator...]]], [callable test] */) {
list($numberOfTestCases, $generators, $test) = parse_for_all_args(func_get_args());
$testCases = generate_test_cases($numberOfTestCases, $generators);
$testGenerator = function ($test) use ($testCases) {
return function($test) use ($testCases) {
$passed = 0;
foreach ($testCases as $testCase) {
if (!call_user_func_array($test, $testCase)) {
throw new TestFailure($test, $testCase);
}
$passed++;
}
return $passed;
};
};
return null !== $testGenerator ? $testGenerator($test) : $testGenerator;
}
function generate_test_cases($testCases, $generators) {
return iter\take($testCases, call_user_func_array("iter\zip", $generators));
}
function factcheck(/** test, [int numberOfTestCases], [generator[, generator[, generator...]]] */) {
$args = func_get_args();
$test = array_shift($args);
$numberOfTestCases = DEFAULT_NUMBER_OF_TEST_CASES;
if (is_int($args[0])) {
$numberOfTestCases = array_shift($args);
}
$generators = $args;
try {
$forAllArgs = array_merge([$numberOfTestCases], $generators, [$test]);
$testCheck = call_user_func_array('atst\factcheck\for_all', $forAllArgs);
$passes = $testCheck($test);
} catch (TestFailure $e) {
echo "*** Failed!\n" . var_export($e->test, true);
return false;
}
echo "+++ OK, passed $passes tests.\n";
return true;
}
class TestFailure extends \Exception
{
public $test;
public $testCase;
public function __construct($test, $testCase)
{
$this->test = $test;
$this->testCase = $testCase;
}
}
/** @api private */
function parse_for_all_args($args)
{
$numberOfTestCases = DEFAULT_NUMBER_OF_TEST_CASES;
if (is_int($args[0])) {
$numberOfTestCases = array_shift($args);
}
$test = null;
if (is_callable(end($args))) {
$test = array_pop($args);
}
$generators = $args;
return [$numberOfTestCases, $generators, $test];
}
/** @api private */
function random_values(/* $fn, args */) {
$args = func_get_args();
$fn = array_shift($args);
while (true)
yield call_user_func_array($fn, $args);
}