Skip to content

Commit 18a4eb5

Browse files
committed
Fisher Yates with PHP thest with phpunit
1 parent b2e88ad commit 18a4eb5

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
function fisher_yates($a = []){
3+
$n = count($a);
4+
for($i = 0; $i < $n; $i++){
5+
$j = mt_rand(0,$i);
6+
$tmp = $a[$j];
7+
$a[$j] = $a[$i];
8+
$a[$i] = $tmp;
9+
}
10+
return $a;
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require 'fisher_yates.php';
3+
class FisherYates_Test extends PHPUnit_Framework_TestCase{
4+
private $test = [1,2,3,4,5,6,7,8,9];
5+
6+
public function testEmptyArray(){
7+
$this->assertEquals([], fisher_yates([]));
8+
}
9+
10+
public function testShuffleArray(){
11+
$tmp = $this->test;
12+
$this->assertNotEquals($this->test, fisher_yates($tmp));
13+
}
14+
}

0 commit comments

Comments
 (0)