PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/core/PHP_Related.test.php

https://github.com/quarkness/piwik
PHP | 421 lines | 308 code | 52 blank | 61 comment | 28 complexity | 07c9e82ddf8d7244821319634a86f23a MD5 | raw file
  1. <?php
  2. if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
  3. {
  4. require_once dirname(__FILE__)."/../../tests/config_test.php";
  5. }
  6. /**
  7. * Random tests about what is happening in php
  8. */
  9. require_once 'Timer.php';
  10. class Test_PHP_Related extends UnitTestCase
  11. {
  12. function test_binHex()
  13. {
  14. $various = array(
  15. 'ffffffffffffffff',
  16. '0000000000000000',
  17. '0000000000000001',
  18. );
  19. foreach($various as $id => $test)
  20. {
  21. $bin = pack("H*" , $test);
  22. $this->assertEqual(strlen($bin), 8);
  23. $this->assertEqual( $test, bin2hex($bin));
  24. }
  25. }
  26. function _test_hashStringToInt()
  27. {
  28. $tests = array(
  29. 1=>10000,
  30. 200000=>300000,
  31. 100000001 => 100100001,
  32. 200000001 => 200211111,
  33. 'inv' => 10000,
  34. 'INV-' => 10000
  35. );
  36. $alreadyHashed = array();
  37. $c = 0;
  38. $collisions = 0;
  39. foreach($tests as $start => $finish)
  40. {
  41. $prefix = false;
  42. if(is_string($start)) {
  43. $prefix = $start;
  44. $start = 0;
  45. }
  46. for($i = $start; $i < $finish; $i++)
  47. {
  48. $value = $i;
  49. if(!empty($prefix)) {
  50. $value = $prefix . $value;
  51. }
  52. $hash = Piwik_Common::hashStringToInt($value);
  53. if(isset($alreadyHashed[$hash])) {
  54. $collisions++;
  55. $diff = ($value-$alreadyHashed[$hash]);
  56. $this->fail("Hash of $value is the same as hash of ".$alreadyHashed[$hash] . " DIFF WAS ".$diff);
  57. }
  58. $alreadyHashed[$hash] = $value;
  59. $c++;
  60. }
  61. }
  62. // $interval = PHP_INT_MAX / 50;
  63. // foreach($alreadyHashed as $hash => $value)
  64. // {
  65. // $hash %
  66. // }
  67. echo "Collision = $collisions - Total hashed = $c - Collision rate = ". round(100*$collisions/$c,5) ."%";
  68. $this->pass();
  69. }
  70. // conclusion:
  71. // - it's ok to have big holes in your array index values
  72. // - obvious: it's not ok to index array by strings when you can do magic and index with int
  73. function oneShotTest_memoryUsageArrayIncreasingIndexOrJumps()
  74. {
  75. ini_set('memory_limit','200M');
  76. Piwik::createConfigObject();
  77. Zend_Registry::get('config')->setTestEnvironment();
  78. Piwik::createLogObject();
  79. //test array[0] array[1] array[2]
  80. //VS
  81. // test array[0] array[100] array[200]
  82. // same memory usage? hash
  83. echo "start ". __FUNCTION__ . "<br>";
  84. echo Piwik::getMemoryUsage();
  85. $timer = new Piwik_Timer();
  86. $testId = 2;
  87. if($testId == 1)
  88. {
  89. echo "start incrementing index<br>";
  90. $array = array();
  91. for($i = 0; $i<1000000;$i++) {
  92. $array[$i] = 1;
  93. }
  94. }
  95. elseif($testId == 2)
  96. {
  97. echo "start indexing by strings<br>";
  98. $array = array();
  99. for($i = 0; $i<1000000;$i++) {
  100. $array[$i."_".$i] = 1;
  101. }
  102. }
  103. elseif($testId == 3)
  104. {
  105. echo "start jumping index<br>";
  106. for($i = 0; $i<1000000;$i++) {
  107. $array[$i*100] = 1;
  108. }
  109. }
  110. echo $timer . "<br>";
  111. echo "size serialized:". Piwik::getPrettySizeFromBytes(strlen(serialize($array))). "<br>";
  112. echo Piwik::getMemoryUsage();
  113. echo "end ". __FUNCTION__ . "<br>";
  114. }
  115. function test_versionTrailingZero()
  116. {
  117. $this->assertTrue(version_compare('0.1','0.01') == 0);
  118. }
  119. function test_equal()
  120. {
  121. //aaaaaaaaaaaahhhhhhhhhhhh
  122. $this->assertTrue( "off" == true);
  123. }
  124. function test_listEach()
  125. {
  126. $array = array('key' => 'elem2');
  127. list($elem1,$elem2) = each($array);
  128. $this->assertEqual($elem1, 'key');
  129. $this->assertEqual($elem2, 'elem2');
  130. }
  131. public function testStringEqualszero()
  132. {
  133. $columnToSort = 'nb_hits';
  134. // it might seem strange. This was the reason of a bug I searched 1 hour for!!
  135. $this->assertTrue( $columnToSort == 0);
  136. }
  137. public function testMergeArray()
  138. {
  139. $a = array('label' => 'test');
  140. $b = array('test' => 1, 1 => 2100);
  141. $expected = array(
  142. 'label' => 'test',
  143. 'test' => 1, 1 => 2100
  144. );
  145. // $this->assertEqual( array_merge($a,$b), $expected);
  146. $this->assertEqual( $a+$b, $expected);
  147. }
  148. /**
  149. * test reading static attribute of a variable class
  150. */
  151. public function test_staticAttr()
  152. {
  153. // use this trick to read the static attribute of the class
  154. $vars = get_class_vars("test_staticAttr");
  155. $this->assertEqual( $vars['a'], 'testa' );
  156. }
  157. static $countSort=0;
  158. function test_usortcalledHowManyTimes()
  159. {
  160. $a=array();
  161. //generate fake 1000 elements access
  162. for($i=0;$i<1000;$i++)
  163. {
  164. $a[]=mt_rand();
  165. }
  166. $timer = new Piwik_Timer;
  167. function countSort($a,$b)
  168. {
  169. Test_PHP_Related::$countSort++;
  170. return $a < $b ? -1 : 1;
  171. }
  172. //sort using usort
  173. usort($a, "countSort");
  174. // in the function used count nb of times called
  175. // print("called ".self::$countSort." times to sort the 1000 elements array");
  176. // echo $timer;
  177. }
  178. /**
  179. * __get is not called when reading a static attribute from a class... snif
  180. */
  181. public function test_magicMethodStaticAttr()
  182. {
  183. $val = test_magicMethodStaticAttr::$test;
  184. $this->assertEqual( $val, "test" );
  185. }
  186. function test_array2XML()
  187. {
  188. function array_to_simplexml($array, $name="config" ,&$xml=null )
  189. {
  190. if(is_null($xml))
  191. {
  192. $xml = new SimpleXMLElement("<{$name}/>");
  193. }
  194. foreach($array as $key => $value)
  195. {
  196. if(is_array($value))
  197. {
  198. $xml->addChild($key);
  199. array_to_simplexml($value, $name, $xml->$key);
  200. }
  201. else
  202. {
  203. $xml->addChild($key, $value);
  204. }
  205. }
  206. return $xml;
  207. }
  208. $test=array("TEST"=>"nurso",
  209. "none"=>null,
  210. "a"=>"b",
  211. array(
  212. "c"=>"d",
  213. array("d"=>"e"))
  214. );
  215. $xml = array_to_simplexml($test);
  216. // print("<pre>START");print($xml);print("START2");
  217. // print_r($xml->asXML());
  218. // print("</pre>");
  219. }
  220. /**
  221. * misc tests for performance
  222. *
  223. * - we try to serialize data when data is a huge multidimensional array
  224. * - we then compare the results when data is the huge array
  225. * but split in hundreds of smaller arrays.
  226. *
  227. * clearly the best solution is to split the array in multiple small arrays
  228. */
  229. public function _test_serializeHugeTable()
  230. {
  231. $timer = new Piwik_Timer;
  232. $a=array();
  233. //generate table
  234. for($i=0;$i<100;$i++)
  235. {
  236. $category=array();
  237. for($j=0;$j<10;$j++)
  238. {
  239. for($k=0;$k<20;$k++)
  240. {
  241. $infoPage=array(10,50,1500,15000,1477,15669,15085,45454,87877,222);
  242. $a[$i][$j][] = $infoPage;
  243. }
  244. }
  245. for($k=0;$k<15;$k++)
  246. {
  247. $infoPage=array(154548781,10,50,10,1477,15669,15085);
  248. $a[$i][] = $infoPage;
  249. }
  250. }
  251. //echo "<br>after generation array = ". $timer;
  252. //echo "<br>count array = ". count($a,1);
  253. $serialized = serialize($a);
  254. $size = round(strlen($serialized)/1024/1024,3);
  255. //echo "<br>size serialized string = ". $size."mb";
  256. //echo "<br>after serialization array = ". $timer;
  257. $serialized = gzcompress($serialized);
  258. $size = round(strlen($serialized)/1024/1024,3);
  259. //echo "<br>size compressed string = ". $size."mb";
  260. //echo "<br>after compression array = ". $timer;
  261. $a = gzuncompress($serialized);
  262. //echo "<br>after uncompression array = ". $timer;
  263. $a = unserialize($a);
  264. //echo "<br>after unserialization array = ". $timer;
  265. }
  266. public function _test_serializeManySmallTable()
  267. {
  268. $timer = new Piwik_Timer;
  269. $a=array();
  270. //echo "<br>";
  271. //generate table
  272. for($i=0;$i<100;$i++)
  273. {
  274. $category=array();
  275. for($j=0;$j<10;$j++)
  276. {
  277. for($k=0;$k<20;$k++)
  278. {
  279. $infoPage=array(10,50,1500,15000,1477,15669,15085,45454,87877,222);
  280. $a[$i][$j][] = $infoPage;
  281. }
  282. }
  283. for($k=0;$k<15;$k++)
  284. {
  285. $infoPage=array(-1,10,50,10,1477,15669,15085);
  286. $a[$i][] = $infoPage;
  287. }
  288. }
  289. //echo "<br>after generation array = ". $timer;
  290. //echo "<br>count array = ". count($a,1);
  291. $allSerialized=array();
  292. for($i=0;$i<100;$i++)
  293. {
  294. for($j=0;$j<10;$j++)
  295. {
  296. $allSerialized[] = serialize($a[$i][$j]);
  297. }
  298. $allSerialized[] = serialize( array_slice($a[$i], 10,15));
  299. }
  300. //echo "<br>after serialize the subs-arrays = ". $timer;
  301. //echo "<br>count array = ". count($allSerialized,1);
  302. $size=0;
  303. foreach($allSerialized as $str)
  304. {
  305. $size+=strlen($str);
  306. }
  307. $size = round($size/1024/1024,3);
  308. //echo "<br>size serialized string = ". $size."mb";
  309. $acompressed=array();
  310. $size = 0;
  311. foreach($allSerialized as $str)
  312. {
  313. $compressed=gzcompress($str);
  314. $size+=strlen($compressed);
  315. $acompressed[] = $compressed;
  316. }
  317. $size = round($size/1024/1024,3);
  318. //echo "<br>size compressed string = ". $size."mb";
  319. //echo "<br>after compression all sub arrays = ". $timer;
  320. }
  321. function test_functionReturnNothing()
  322. {
  323. function givemenothing()
  324. {
  325. $a = 4;
  326. }
  327. $return = givemenothing();
  328. $this->assertFalse(isset($return));
  329. $this->assertTrue(empty($return));
  330. $this->assertTrue(!is_int($return));
  331. $this->assertTrue(!is_string($return));
  332. $this->assertTrue(!is_bool($return));
  333. }
  334. function test_isThisForReal()
  335. {
  336. // no error here??
  337. $bla = false;
  338. $bla = unserialize($bla);
  339. $blabla = $bla['test'];
  340. $blabis = null;
  341. $blabis = unserialize($blabis);
  342. $blablabis = $blabis['test'];
  343. $this->pass();
  344. }
  345. function test_string2boolCasting()
  346. {
  347. $this->assertEqual((bool)"false", true);
  348. $this->assertEqual((bool)"0", false);
  349. }
  350. function test_urldecode()
  351. {
  352. $this->assertEqual(urldecode('Zubeh%C3%B6rshop+Homepage+'), urldecode('Zubeh%C3%B6rshop%20Homepage%20'));
  353. $this->assertEqual(urldecode('+%20'), ' ');
  354. }
  355. }
  356. class test_staticAttr
  357. {
  358. static public $a = 'testa';
  359. public $b = 'testb';
  360. }
  361. class test_magicMethodStaticAttr
  362. {
  363. static $test = "test";
  364. public function __get($name)
  365. {
  366. print("reading static attr ; __get called");
  367. return 1;
  368. }
  369. }