Convert most libs/ tests to use PHPUnit_Framework_TestCase
[mediawiki.git] / tests / phpunit / includes / libs / RunningStatTest.php
blobedfaf162c76d41eea8b4c46bb82cfdf2a16a5a84
1 <?php
2 /**
3 * PHP Unit tests for RunningStat class.
4 * @covers RunningStat
5 */
6 class RunningStatTest extends PHPUnit_Framework_TestCase {
8 public $points = array(
9 49.7168, 74.3804, 7.0115, 96.5769, 34.9458,
10 36.9947, 33.8926, 89.0774, 23.7745, 73.5154,
11 86.1322, 53.2124, 16.2046, 73.5130, 10.4209,
12 42.7299, 49.3330, 47.0215, 34.9950, 18.2914,
15 /**
16 * Verify that the statistical moments and extrema computed by RunningStat
17 * match expected values.
18 * @covers RunningStat::push
19 * @covers RunningStat::count
20 * @covers RunningStat::getMean
21 * @covers RunningStat::getVariance
22 * @covers RunningStat::getStdDev
24 public function testRunningStatAccuracy() {
25 $rstat = new RunningStat();
26 foreach( $this->points as $point ) {
27 $rstat->push( $point );
30 $mean = array_sum( $this->points ) / count( $this->points );
31 $variance = array_sum( array_map( function ( $x ) use ( $mean ) {
32 return pow( $mean - $x, 2 );
33 }, $this->points ) ) / ( count( $rstat ) - 1 );
34 $stddev = sqrt( $variance );
36 $this->assertEquals( count( $rstat ), count( $this->points ) );
37 $this->assertEquals( $rstat->min, min( $this->points ) );
38 $this->assertEquals( $rstat->max, max( $this->points ) );
39 $this->assertEquals( $rstat->getMean(), $mean );
40 $this->assertEquals( $rstat->getVariance(), $variance );
41 $this->assertEquals( $rstat->getStdDev(), $stddev );
44 /**
45 * When one RunningStat instance is merged into another, the state of the
46 * target RunningInstance should have the state that it would have had if
47 * all the data had been accumulated by it alone.
48 * @covers RunningStat::merge
49 * @covers RunningStat::count
51 public function testRunningStatMerge() {
52 $expected = new RunningStat();
54 foreach( $this->points as $point ) {
55 $expected->push( $point );
58 // Split the data into two sets
59 $sets = array_chunk( $this->points, floor( count( $this->points ) / 2 ) );
61 // Accumulate the first half into one RunningStat object
62 $first = new RunningStat();
63 foreach( $sets[0] as $point ) {
64 $first->push( $point );
67 // Accumulate the second half into another RunningStat object
68 $second = new RunningStat();
69 foreach( $sets[1] as $point ) {
70 $second->push( $point );
73 // Merge the second RunningStat object into the first
74 $first->merge( $second );
76 $this->assertEquals( count( $first ), count( $this->points ) );
77 $this->assertEquals( $first, $expected );