<?php
-// $Id: SpamTest.php,v 1.21 2007/08/18 09:10:35 henoheno Exp $
+// $Id: SpamTest.php,v 1.22 2008/12/27 11:20:06 henoheno Exp $
// Copyright (C) 2007 heno
//
// Design test case for spam.php (called from runner.php)
class SpamTest extends PHPUnit_TestCase
{
+ function testFunc_delimiter_reverse()
+ {
+ // Simple
+ $this->assertEquals('com.example.bar.foo',
+ delimiter_reverse('foo.bar.example.com'));
+
+ // A vector (an simple array)
+ $array = array('foo.ba2r', 'foo.bar2');
+ $this->assertEquals(array('ba2r|foo', 'bar2|foo'),
+ delimiter_reverse($array, '.', '|'));
+
+ // Note: array_map() vanishes all keys
+ $array = array('FB' => 'foo.ba2r', 'FB2' => 'foo.bar2');
+ $this->assertEquals(array('ba2r|foo', 'bar2|foo'),
+ delimiter_reverse($array, '.', '|'));
+
+ // A tree (recurse)
+ $array = array('foo.ba2r', 'foo.bar2', array('john.doe', 'bob.dude'));
+ $this->assertEquals(array('ba2r|foo', 'bar2|foo', array('doe|john', 'dude|bob')),
+ delimiter_reverse($array, '.', '|'));
+
+ // Nothing changes
+ $this->assertEquals('100', delimiter_reverse('100'));
+ $this->assertEquals(array(), delimiter_reverse(array()));
+
+ // Invalid cases
+ $this->assertEquals(FALSE, delimiter_reverse(TRUE));
+ $this->assertEquals(FALSE, delimiter_reverse(FALSE));
+ $this->assertEquals(FALSE, delimiter_reverse(NULL));
+ $this->assertEquals(FALSE, delimiter_reverse(100));
+ $this->assertEquals(FALSE, delimiter_reverse('100', FALSE));
+ $this->assertEquals(FALSE, delimiter_reverse('100', 0));
+ $this->assertEquals(FALSE, delimiter_reverse('100', '0', 0));
+ }
+
function setup_string_null()
{
return array(
<?php
-// $Id: spam.php,v 1.202 2007/08/18 09:10:58 henoheno Exp $
+// $Id: spam.php,v 1.203 2008/12/27 11:20:06 henoheno Exp $
// Copyright (C) 2006-2007 PukiWiki Developers Team
// License: GPL v2 or (at your option) any later version
//
}
}
+// Data structure: Create an array they _refer_only_one_ value
+function one_value_array($num = 0, $value = NULL)
+{
+ $num = max(0, intval($num));
+ $array = array();
+
+ for ($i = 0; $i < $num; $i++) {
+ $array[] = & $value;
+ }
+
+ return $array;
+}
+
// Reverse $string with specified delimiter
-function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
+function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL)
{
- if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
- return $string;
+ $to_null = ($to_delim === NULL);
- // com.example.bar.foo
+ if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) {
+ return FALSE;
+ }
+ if (is_array($string)) {
+ // Map, Recurse
+ $count = count($string);
+ $from = one_value_array($count, $from_delim);
+ if ($to_null) {
+ // Note: array_map() vanishes all keys
+ return array_map('delimiter_reverse', $string, $from);
+ } else {
+ $to = one_value_array($count, $to_delim);
+ // Note: array_map() vanishes all keys
+ return array_map('delimiter_reverse', $string, $from, $to);
+ }
+ }
+ if (! is_string($string)) {
+ return FALSE;
+ }
+
+ // Returns com.example.bar.foo
+ if ($to_null) $to_delim = & $from_delim;
return implode($to_delim, array_reverse(explode($from_delim, $string)));
}
$sort[delimiter_reverse($key)] = $key;
}
ksort($sort, SORT_STRING);
+
$result = array();
foreach($sort as $key) {
$result[$key] = & $array[$key];
}
+
$array = $result;
}