Do not allow random() to take an array of high or low, just an optional fixed length...
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 24 Jan 2008 16:24:51 +0000 (11:24 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 24 Jan 2008 16:24:51 +0000 (11:24 -0500)
* Functions.php:
(BSFunctions::random): Do not accept an Array as a parameter, just an optional int
* UnitTest/FunctionsTest.php:
(FunctionsTest::testRandom): Test using a fixed length as well as the random length

Functions.php
UnitTest/FunctionsTest.php

index fe89f4379863cb3718bc882a743910cf1ac97d43..18e19e8efd5ac4e91d4f22684b75003b4ce63bad 100644 (file)
@@ -244,12 +244,8 @@ class BSFunctions
        */
        public static function random($length = 0)
        {
-               // custom high and lows
-               if (is_array($length))
-               {
-                       $length = rand($length[0], $length[1]);
-               }
-               else if (!$length)
+               // length wasn't provided, so create our own
+               if ($length < 1)
                {
                        $length = rand(20, 65);
                }
index 8e676bc7ef5ba735ce5fc4cff383e882e3d8482f..570ae03b64f3ec93261b6a5742953a5b1454b49d 100644 (file)
@@ -55,14 +55,26 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
        
        public function testRandom()
        {
+               // test fixed length
                $values = array();
                for ($i = 0; $i < 100; $i++)
                {
                        $random = BSFunctions::random(5);
                        $this->assertEquals(5, strlen($random));
-                       $this->assertFalse(in_array($random, $values));
                        $values[] = $random;
                }
+               $this->assertEquals(sizeof(array_unique($values)), sizeof($values));
+               
+               // test random length
+               $values = array();
+               for ($i = 0; $i < 100; $i++)
+               {
+                       $random = BSFunctions::random();
+                       $length = strlen($random);
+                       $this->assertTrue($length >= 20 && $length <= 65);
+                       $values[] = $random;
+               }
+               $this->assertEquals(sizeof(array_unique($values)), sizeof($values));
        }
        
        public function testArraySetCurrent()