Skip to content

Instantly share code, notes, and snippets.

@colin-kiegel
Created March 23, 2015 22:07
Show Gist options
  • Save colin-kiegel/b8e1ba2c1a1c414c4379 to your computer and use it in GitHub Desktop.
Save colin-kiegel/b8e1ba2c1a1c414c4379 to your computer and use it in GitHub Desktop.
Testcase ID=/Modules/SurveyQuestionPool/phplot/phplot.php:672
Array
(
[in] => Array
(
[which_style] => 4-3
[which_ndxcol] => 0
)
[out1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => -6
[5] => -6
[6] => -6
)
[out2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => -6
[5] => -6
[6] => -6
)
)
OK
Array
(
[in] => Array
(
[which_style] => 2-3-1-2
[which_ndxcol] => blueish
)
[out1] => Array
(
[0] => blueish
[1] => blueish
[2] => -6
[3] => -6
[4] => -6
[5] => blueish
[6] => -6
[7] => -6
)
[out2] => Array
(
[0] => blueish
[1] => blueish
[2] => -6
[3] => -6
[4] => -6
[5] => blueish
[6] => -6
[7] => -6
)
)
OK
Testcase ID=/Services/Math/classes/class.EvalMath.php:344
Array
(
[in] => Array
(
[fnn] => ln
[op] => 42
)
[out1] => 3.7376696182834
[out2] => 3.7376696182834
)
OK
Array
(
[in] => Array
(
[fnn] => sin
[op] => 42
)
[out1] => -0.91652154791563
[out2] => -0.91652154791563
)
OK
Testcase ID=/Services/PEAR/lib/PEAR.php:917
Array
(
[in] => Catch me, I'm falling!
[out1] => Catch me, I'm falling!
[out2] => Catch me, I'm falling!
)
OK
Array
(
[in] => Boo!
[out1] => Boo!
[out2] => Boo!
)
OK
All Testcases OK
Process finished with exit code 0
<?php
include 'include/Unicode/UtfNormalUtil.php';
$testsuite = [
'/Modules/SurveyQuestionPool/phplot/phplot.php:672' => [
/*!
* Sets the default dashed style.
* \param which_style A string specifying order of colored and transparent dots,
* i.e: '4-3' means 4 colored, 3 transparent;
* '2-3-1-2' means 2 colored, 3 transparent, 1 colored, 2 transparent.
*/
'in' => [
[
'which_style' => '4-3',
'which_ndxcol' => 0
],
[
'which_style' => '2-3-1-2',
'which_ndxcol' => 'blueish'
],
],
'f1' => function($in) {
class Foo1 {
private $default_dashed_style;
function bar($which_style, $which_ndxcol) {
// String: "numcol-numtrans-numcol-numtrans..."
$asked = explode('-', $which_style);
if (count($asked) < 2) {
return $this->PrintError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
}
// Build the string to be eval()uated later by SetDashedStyle()
$this->default_dashed_style = 'array( ';
$t = 0;
foreach($asked as $s) {
if ($t % 2 == 0) {
$this->default_dashed_style .= str_repeat('$which_ndxcol,', $s);
} else {
$this->default_dashed_style .= str_repeat('IMG_COLOR_TRANSPARENT,', $s);
}
$t++;
}
// Remove trailing comma and add closing parenthesis
$this->default_dashed_style = substr($this->default_dashed_style, 0, -1);
$this->default_dashed_style .= ')';
eval ("\$style = $this->default_dashed_style;");
return $style;
}
}
$foo1 = new Foo1();
return $foo1->bar($in['which_style'], $in['which_ndxcol']);
},
'f2' => function($in) {
class Foo2 {
private $default_dashed_style;
function bar($which_style, $which_ndxcol) {
// String: "numcol-numtrans-numcol-numtrans..."
$asked = explode('-', $which_style);
if (count($asked) < 2) {
return $this->PrintError("SetDefaultDashedStyle(): Wrong parameter '$which_style'.");
}
// Build the string to be eval()uated later by SetDashedStyle()
$this->default_dashed_style = '[ '; // switching from PHP-Array to JSON-Notation
$t = 0;
foreach($asked as $s) {
if ($t % 2 == 0) {
$this->default_dashed_style .= str_repeat('$which_ndxcol,', $s);
} else {
$this->default_dashed_style .= str_repeat('IMG_COLOR_TRANSPARENT,', $s);
}
$t++;
}
// Remove trailing comma and add closing parenthesis
$this->default_dashed_style = substr($this->default_dashed_style, 0, -1);
$this->default_dashed_style .= ']'; // end of JSON
// $this->default_dashed_style is a JSON-string
// we json_decode, to avoid eval()ing an PHP array definition
// this way, we are still able to do replacements.. (still nasty)
$replace = [
'$which_ndxcol' => json_encode($which_ndxcol),
'IMG_COLOR_TRANSPARENT' => json_encode(IMG_COLOR_TRANSPARENT)
];
$style = json_decode(str_replace(array_keys($replace),array_values($replace),$this->default_dashed_style));
return $style;
}
}
$foo1 = new Foo2();
return $foo1->bar($in['which_style'], $in['which_ndxcol']);
}
],
'/Services/Math/classes/class.EvalMath.php:344' => [
'in' => [
[
'fnn' => 'ln',
'op' => '42'
],
[
'fnn' => 'sin',
'op' => '42'
]
],
'f1' => function($array) {
$fnn = $array['fnn'];
$op1 = $array['op'];
$stack = new SplStack();
if ($fnn == 'ln') $fnn = 'log';
eval('$stack->push(' . $fnn . '($op1));'); // perfectly safe eval()
return $stack->pop();
},
'f2' => function($array) {
$fnn = $array['fnn'];
$op1 = $array['op'];
$stack = new SplStack();
if ($fnn == 'ln') $fnn = 'log';
$stack->push($fnn($op1)); // 'eval()' can be easily avoided here
return $stack->pop();
}
],
'/Services/PEAR/lib/PEAR.php:917' => [
'in' => ['Catch me, I\'m falling!', 'Boo!'],
'f1' => function($string) {
class PEARFoo1 {
public $message = "";
public $code = 0;
public function bar($string) {
$this->message = $string;
$this->code = 0;
try {
eval('$e = new Exception($this->message, $this->code);throw($e);');
}
catch (Exception $e) {
return $e->getMessage();
}
}
}
$foo = new PEARFoo1();
return $foo->bar($string);
},
'f2' => function($string) {
class PEARFoo2 {
public $message = "";
public $code = 0;
public function bar($string) {
$this->message = $string;
$this->code = 0;
try {
$e = new Exception($this->message, $this->code);throw($e); // no eval() necessary to throw an exception
}
catch (Exception $e) {
return $e->getMessage();
}
}
}
$foo = new PEARFoo2($string);
return $foo->bar($string);
}
],
];
function cmp_fns($f1, $f2, $in) {
$r1 = $f1($in);
$r2 = $f2($in);
$resultset = [
'in' => $in,
'out1' => $r1,
'out2' => $r2
];
print_r($resultset);
return ($r1 === $r2); // even checks recursive identity and order - i.e. arrays must be perfectly identical
}
class Test {
public function run($testdefinitions) {
foreach ($testdefinitions as $label => $testcase) {
echo "Testcase ID=" . $label . PHP_EOL;
$f1 = $testcase['f1'];
$f2 = $testcase['f2'];
foreach ($testcase['in'] as $input) {
if (cmp_fns($f1, $f2, $input)) {
echo "OK" . PHP_EOL;
} else {
echo "FAIL - STOP" . PHP_EOL;
die();
}
}
}
echo "All Testcases OK" . PHP_EOL;
}
}
$test = new Test();
$test->run($testsuite);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment