This repository has been archived on 2020-06-07. You can view files and clone it, but cannot push or open issues or pull requests.
php-armadillo/tests/includes/assert.php

56 lines
1.3 KiB
PHP

<?php
/**
* Batch assert for one or more cases with the given comparator.
*
* Print message if test fail.
*
* @param string $test
* @param callable $comp
* @param array $cases
* @return bool
*/
function batch_assert_ex($test, $comp, ...$cases) {
foreach ($cases as $case) {
[$expected, $got] = $case;
if ($comp($expected, $got)) {
continue;
}
echo "Test for $test failed.\n";
$expected = var_export($expected, true);
$got = var_export($got, true);
echo "Expected:\n$expected\n";
echo "Got:\n$got\n";
return false;
}
return true;
}
/**
* Batch assert with default comparator.
*
* @param string $test
* @param array $cases
* @return bool
*/
function batch_assert($test, ...$cases) {
return batch_assert_ex($test, function ($expected, $got) {
return $expected == $got;
}, ...$cases);
}
/**
* Batch assert for matrix equality.
*
* @param $test
* @param array $cases
* @return bool
*/
function batch_assert_mat($test, ...$cases) {
$comp = function ($expected, $got) {
$compare_result = $expected->equals($got);
return $compare_result->max() == 1 && $compare_result->min() == 1;
};
return batch_assert_ex($test, $comp, ...$cases);
}