Skip to content

Instantly share code, notes, and snippets.

@colin-kiegel
Created March 22, 2015 11:47
Show Gist options
  • Save colin-kiegel/0cb327a0a716e2cd2c82 to your computer and use it in GitHub Desktop.
Save colin-kiegel/0cb327a0a716e2cd2c82 to your computer and use it in GitHub Desktop.
/usr/bin/php /www/pub/ilias-trunk/test.php
Testcase ID=Modules/Wiki/mediawiki/normal/UtfNormalTest.php:32
Array
(
[in] => ABCDEFG12345678910
[out1] => 414243444546473132333435363738393130
[out2] => 414243444546473132333435363738393130
)
OK
Array
(
[in] => Hello World, convert me!?#+
[out1] => 48656C6C6F20576F726C642C20636F6E76657274206D65213F232B
[out2] => 48656C6C6F20576F726C642C20636F6E76657274206D65213F232B
)
OK
Testcase ID=Modules/Wiki/mediawiki/normal/UtfNormalTest.php:41
Array
(
[in] => ABCDEFG12345678910
[out1] => 0041 0042 0043 0044 0045 0046 0047 0031 0032 0033 0034 0035 0036 0037 0038 0039 0031 0030
[out2] => 0041 0042 0043 0044 0045 0046 0047 0031 0032 0033 0034 0035 0036 0037 0038 0039 0031 0030
)
OK
Array
(
[in] => Hello World, convert me!?#+
[out1] => 0048 0065 006C 006C 006F 0020 0057 006F 0072 006C 0064 002C 0020 0063 006F 006E 0076 0065 0072 0074 0020 006D 0065 0021 003F 0023 002B
[out2] => 0048 0065 006C 006C 006F 0020 0057 006F 0072 006C 0064 002C 0020 0063 006F 006E 0076 0065 0072 0074 0020 006D 0065 0021 003F 0023 002B
)
OK
Testcase ID=Modules/Wiki/mediawiki/normal/UtfNormalUtil.php:81
Array
(
[in] => ABCDEFG12345678910
[out1] => 0041 0042 0043 0044 0045 0046 0047 0031 0032 0033 0034 0035 0036 0037 0038 0039 0031 0030
[out2] => 0041 0042 0043 0044 0045 0046 0047 0031 0032 0033 0034 0035 0036 0037 0038 0039 0031 0030
)
OK
Array
(
[in] => Hello World, convert me!?#+
[out1] => 0048 0065 006c 006c 006f 0020 0057 006f 0072 006c 0064 002c 0020 0063 006f 006e 0076 0065 0072 0074 0020 006d 0065 0021 003f 0023 002b
[out2] => 0048 0065 006c 006c 006f 0020 0057 006f 0072 006c 0064 002c 0020 0063 006f 006e 0076 0065 0072 0074 0020 006d 0065 0021 003f 0023 002b
)
OK
Testcase ID=Services/Math/classes/class.EvalMath.php:119
Array
(
[in] => 4e5
[out1] => 4*10^(5)
[out2] => 4*10^(5)
)
OK
Array
(
[in] => e-20
[out1] => 10^(-20)
[out2] => 10^(-20)
)
OK
Testcase ID=Services/OpenId/lib/Auth/Yadis/ParseHTML.php:85
Array
(
[in] => B
[out1] => B
[out2] => B
)
OK
Array
(
[in] => .
[out1] => .
[out2] => .
)
OK
Testcase ID=Services/OpenId/lib/Auth/Yadis/ParseHTML.php:86
Array
(
[in] => *
[out1] => *
[out2] => *
)
OK
Array
(
[in] => ^
[out1] => ^
[out2] => ^
)
OK
Testcase ID=Services/PEAR/lib/HTML/Template/IT.php:932
Array
(
[in] => <!-- INCLUDE BANANAS-WITH-CHOCOLATE -->
[out1] => FuncCall:getFile(BANANAS-WITH-CHOCOLATE)
[out2] => FuncCall:getFile(BANANAS-WITH-CHOCOLATE)
)
OK
Array
(
[in] => <!-- INCLUDE str4awb3rryp13 -->
[out1] => FuncCall:getFile(str4awb3rryp13)
[out2] => FuncCall:getFile(str4awb3rryp13)
)
OK
Testcase ID=Services/Utilities/classes/Parser.php:3567
Array
(
[in] => <!--LINK 123-->
[out1] => Hello
[out2] => Hello
)
OK
Array
(
[in] => <!--LINK 42-->
[out1] => World!
[out2] => World!
)
OK
Testcase ID=Services/Utilities/classes/Parser.php:3570
Array
(
[in] => <!--IWLINK 123-->
[out1] => Hello
[out2] => Hello
)
OK
Array
(
[in] => <!--IWLINK 42-->
[out1] => World!
[out2] => World!
)
OK
Testcase ID=Services/Utilities/classes/Sanitizer.php:622
Array
(
[in] => \00FF
[out1] => ÿ
[out2] => ÿ
)
OK
Array
(
[in] => \0042
[out1] => B
[out2] => B
)
OK
Testcase ID=Services/Utilities/classes/Sanitizer.php:1241
Array
(
[in] => ][ Hello! <>
[out1] => %5D%5B+Hello!+%3C%3E
[out2] => %5D%5B+Hello!+%3C%3E
)
OK
Array
(
[in] => special_treatment_for_quotes_"'
[out1] => special_treatment_for_quotes_%5C%22'
[out2] => special_treatment_for_quotes_%5C%22'
)
OK
Testcase ID=include/Unicode/UtfNormalUtil.php:80
Array
(
[in] => abcDEFG
[out1] => 0061 0062 0063 0044 0045 0046 0047
[out2] => 0061 0062 0063 0044 0045 0046 0047
)
OK
Array
(
[in] => 123!"§
[out1] => 0031 0032 0033 0021 0022 00a7
[out2] => 0031 0032 0033 0021 0022 00a7
)
OK
All Testcases OK
Process finished with exit code 0
<?php
include 'include/Unicode/UtfNormalUtil.php';
$testsuite = [
'Modules/Wiki/mediawiki/normal/UtfNormalTest.php:32' => [
'in' => ['ABCDEFG12345678910', 'Hello World, convert me!?#+'],
'f1' => function($string) {
return preg_replace( '/([\x00-\xff])/e',
'sprintf("%02X", ord("$1"))',
$string);
},
'f2' => function($string) {
return preg_replace_callback(
'/([\x00-\xff])/',
function($hit) {
return sprintf("%02X", ord($hit[1]));
},
$string
);
}
],
'Modules/Wiki/mediawiki/normal/UtfNormalTest.php:41' => [
'in' => ['ABCDEFG12345678910', 'Hello World, convert me!?#+'],
'f1' => function($string) {
return trim( preg_replace( '/(.)/use',
'sprintf("%04X ", utf8ToCodepoint("$1"))',
$string ) );
},
'f2' => function($string) {
return trim( preg_replace_callback(
'/(.)/us',
function($hit) {
return sprintf("%04X ", utf8ToCodepoint($hit[1]));
},
$string
));
}
],
'Modules/Wiki/mediawiki/normal/UtfNormalUtil.php:81' => [
'in' => ['ABCDEFG12345678910', 'Hello World, convert me!?#+'],
'f1' => function($str) {
return rtrim( preg_replace( '/(.)/uSe',
'sprintf("%04x ", utf8ToCodepoint("$1"))',
$str ) );
},
'f2' => function($str) {
return rtrim( preg_replace_callback(
'/(.)/uS',
function($hit) {
return sprintf("%04x ", utf8ToCodepoint($hit[1]));
},
$str
));
}
],
'Services/Math/classes/class.EvalMath.php:119' => [
'in' => ['4e5','e-20'],
'f1' => function($expr) {
return preg_replace("/(\\d{0,1})e(-{0,1}\\d+)/eis", "'\\1'.((strlen('\\1')) ? '*' : '').'10^(\\2)'", $expr);
},
'f2' => function($expr) {
return preg_replace_callback(
"/(\\d{0,1})e(-{0,1}\\d+)/is",
function($hit) {
return $hit[1].((strlen($hit[1])) ? '*' : '').'10^('.$hit[2].')';
},
$expr
);
}
],
'Services/OpenId/lib/Auth/Yadis/ParseHTML.php:85' => [
'in' => ['&#x42;','&#x2e;'],
'f1' => function($str) {
return preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $str);
},
'f2' => function($str) {
return preg_replace_callback(
'~&#x([0-9a-f]+);~i',
function($hit) {
return chr(hexdec($hit[1]));
},
$str
);
}
],
'Services/OpenId/lib/Auth/Yadis/ParseHTML.php:86' => [
'in' => ['&#42;','&#94;'],
'f1' => function($str) {
return preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $str);
},
'f2' => function($str) {
return preg_replace_callback(
'~&#([0-9]+);~',
function($hit) {
return chr($hit[1]);
},
$str
);
}
],
'Services/PEAR/lib/HTML/Template/IT.php:932' => [
'in' => ['<!-- INCLUDE BANANAS-WITH-CHOCOLATE -->', '<!-- INCLUDE str4awb3rryp13 -->'],
'f1' => function($content) {
class PearITFoo1 {
function getFile($name) { // original is implicitly public too
return 'FuncCall:' . __FUNCTION__ . "($name)";
}
public function bar($content) {
return preg_replace(
"#<!-- INCLUDE (.*) -->#ime", "\$this->getFile('\\1')", $content);
}
}
$foo = new PearITFoo1();
return $foo->bar($content);
},
'f2' => function($content) {
class PearITFoo2 {
function getFile($name) { // original is implicitly public too
return 'FuncCall:' . __FUNCTION__ . "($name)";
}
public function bar($content) {
return preg_replace_callback(
"#<!-- INCLUDE (.*) -->#im",
function ($hit) {
return $this->getFile($hit[1]);
},
$content);
}
}
$foo = new PearITFoo2();
return $foo->bar($content);
}
],
'Services/Utilities/classes/Parser.php:3567' => [
'in' => ['<!--LINK 123-->', '<!--LINK 42-->'],
'f1' => function($canonized_headline) {
class ServicesParserFoo1 {
public $mLinkHolders = [
'texts' => [
'123' => 'Hello ',
'42' => 'World!'
]];
public function bar($canonized_headline) {
return preg_replace( '/<!--LINK ([0-9]*)-->/e',
"\$this->mLinkHolders['texts'][\$1]",
$canonized_headline );
}
}
$foo = new ServicesParserFoo1();
return $foo->bar($canonized_headline);
},
'f2' => function($canonized_headline) {
class ServicesParserFoo2 {
public $mLinkHolders = [
'texts' => [
'123' => 'Hello ',
'42' => 'World!'
]];
public function bar($canonized_headline) {
return preg_replace_callback(
'/<!--LINK ([0-9]*)-->/',
function($hit) {
return $this->mLinkHolders['texts'][$hit[1]];
},
$canonized_headline
);
}
}
$foo = new ServicesParserFoo2();
return $foo->bar($canonized_headline);
}
],
'Services/Utilities/classes/Parser.php:3570' => [
'in' => ['<!--IWLINK 123-->', '<!--IWLINK 42-->'],
'f1' => function($canonized_headline) {
class ServicesParserFoo1b {
public $mInterwikiLinkHolders = [
'texts' => [
'123' => 'Hello ',
'42' => 'World!'
]];
public function bar($canonized_headline) {
return preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
"\$this->mInterwikiLinkHolders['texts'][\$1]",
$canonized_headline );
}
}
$foo = new ServicesParserFoo1b();
return $foo->bar($canonized_headline);
},
'f2' => function($canonized_headline) {
class ServicesParserFoo2b {
public $mInterwikiLinkHolders = [
'texts' => [
'123' => 'Hello ',
'42' => 'World!'
]];
public function bar($canonized_headline) {
return preg_replace_callback(
'/<!--IWLINK ([0-9]*)-->/',
function($hit) {
return $this->mInterwikiLinkHolders['texts'][$hit[1]];
},
$canonized_headline
);
}
}
$foo = new ServicesParserFoo2b();
return $foo->bar($canonized_headline);
}
],
'Services/Utilities/classes/Sanitizer.php:622' => [
'in' => ['\\00FF', '\\0042'],
'f1' => function($stripped) {
return preg_replace( '!\\\\([0-9A-Fa-f]{1,6})[ \\n\\r\\t\\f]?!e',
'codepointToUtf8(hexdec("$1"))', $stripped );
},
'f2' => function($stripped) {
return preg_replace_callback(
'!\\\\([0-9A-Fa-f]{1,6})[ \\n\\r\\t\\f]?!',
function($hit){
return codepointToUtf8(hexdec($hit[1]));
},
$stripped
);
}
],
'Services/Utilities/classes/Sanitizer.php:1241' => [
'in' => ['][ Hello! <>', 'special_treatment_for_quotes_"\''],
'f1' => function($url) {
return preg_replace( '/[\][<>"\\x00-\\x20\\x7F]/e', "urlencode('\\0')", $url );
},
'f2' => function($url) {
return preg_replace_callback(
'/[\][<>"\\x00-\\x20\\x7F]/',
function($hit) {
if($hit[0] === '"') {
return urlencode ('\\"');
} else {
return urlencode($hit[0]);
}
},
$url
);
}
],
'include/Unicode/UtfNormalUtil.php:80' => [
'in' => ['abcDEFG', '123!"§'],
'f1' => function($str) {
return rtrim( preg_replace( '/(.)/uSe',
'sprintf("%04x ", utf8ToCodepoint("$1"))',
$str ) );
},
'f2' => function($str) {
return rtrim(preg_replace_callback(
'/(.)/uS',
function($hit) {
return sprintf("%04x ", utf8ToCodepoint($hit[1]));
},
$str));
}
]
];
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);
}
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