From 222db6da2ed56bd656b593331cbf26569a8c54e3 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 24 Jan 2008 11:24:51 -0500 Subject: [PATCH] Do not allow random() to take an array of high or low, just an optional fixed length (otherwise it will randomly select one) * 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 | 8 ++------ UnitTest/FunctionsTest.php | 14 +++++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Functions.php b/Functions.php index fe89f43..18e19e8 100644 --- a/Functions.php +++ b/Functions.php @@ -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); } diff --git a/UnitTest/FunctionsTest.php b/UnitTest/FunctionsTest.php index 8e676bc..570ae03 100644 --- a/UnitTest/FunctionsTest.php +++ b/UnitTest/FunctionsTest.php @@ -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() -- 2.43.5