Login   Register  
PHP Classes
elePHPant
Icontem

File: ExternalSortTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Philipp Strazny  >  External Sort  >  ExternalSortTest.php  >  Download  
File: ExternalSortTest.php
Role: Unit test script
Content type: text/plain
Description: unit tests for ExternalSort
Class: External Sort
Sort large files similar to the UNIX sort command
Author: By
Last change:
Date: 2 years ago
Size: 12,630 bytes
 

Contents

Class file image Download
<?php

/*
* ExternalSortTest is copyright 2011. Philipp Strazny.
* Licensed under the Academic Free License version 3.0
*
* ExternalSortTest implements unit tests for ExternalSort.
*  
* @author Philipp Strazny
* @version 1.0
* @copyright Philipp Strazny
* @license Academic Free License v. 3.0
* @package ExternalSortTest
* filename: ExternalSortTest.php
* date: 2011-11-04
* 
* 
* command-line usage:
* 	phpunit ExternalSortTest.php
*/


require('ExternalSort.php');

/**
 * Test class for ExternalSort.
 * Generated by PHPUnit on 2011-05-24 at 21:03:54.
 */
class ExternalSortTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var ExternalSort
     */
    protected $object;
    /**
     * @var ExternalSort ReflectionClass
     */
    protected $testclass;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = new ExternalSort();//'dummyarg');
		$this->testclass = new ReflectionClass('ExternalSort');
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * tests that 
     * tmpdir and first bucket file are created
     * and then removed after ExternalSort is set to null 
     * (implicit call do __destruct)
     */
    public function test__destruct()
    {
		$arr = array('a','b','c','d');
		$tmpfile = 'tmp1.txt';
		file_put_contents($tmpfile, implode("\n", $arr));
 		$this->object->setFile($tmpfile);
		$fp = fopen($tmpfile, 'r');
        $cnt = $this->object->initExternalBuckets($fp);
		$tmpdir = $this->getProperty('tmpdir');
		$this->assertTrue(file_exists($tmpdir.'/0'));
		$this->object=null;
		$this->assertFalse(file_exists($tmpdir));
		@unlink($tmpfile); //input file
		@unlink($tmpfile.'.sorted'); //output file
    }

    /**
     * @todo Implement testRun().
     */
    public function testRun()
    {
		$arr = array('a','b','c','d');
		$tmpfile = 'tmp1.txt';
		file_put_contents($tmpfile, implode("\n", $arr));
 		$sortedFile = $tmpfile.'.sorted';
		$this->assertFalse(file_exists($sortedFile), 'making sure that sorted file does not exist yet');
 		$this->object->setFile($tmpfile);
 		$this->object->run();
		$this->assertTrue(file_exists($sortedFile), 'making sure that sorted file exists after sorting');
		unlink($sortedFile);
		unlink($tmpfile);
    }

    /**
     * @todo Implement testSetVerify().
     */
    public function testSetVerify()
    {
 		$this->object->setVerify();
		$verifyOutput = $this->getProperty('verifyOutput');
		$this->assertFalse($verifyOutput);
 		$this->object->setVerify(true);
		$verifyOutput = $this->getProperty('verifyOutput');
		$this->assertTrue($verifyOutput);
   }

    /**
     * @todo Implement testSetVerifyInput().
     */
    public function testSetVerifyInput()
    {
 		$this->object->setVerifyInput();
		$verifyInput = $this->getProperty('verifyInput');
		$this->assertFalse($verifyInput);
 		$this->object->setVerifyInput(true);
		$verifyInput = $this->getProperty('verifyInput');
		$this->assertTrue($verifyInput);
    }

    /**
     * @todo Implement testSetTrim().
     */
    public function testSetTrim()
    {
 		$this->object->setTrim();
		$trim = $this->getProperty('trim');
		$this->assertFalse($trim);
 		$this->object->setTrim(true);
		$trim = $this->getProperty('trim');
		$this->assertTrue($trim);
    }

    /**
     * @todo Implement testSetFile().
     */
    public function testSetFile()
    {
		$file = 'abc';
		$outputfile = 'abc.sorted';
		@touch($file);
		$this->assertFalse(file_exists($outputfile));
 		$this->object->setFile($file);
		$fileProp = $this->getProperty('file');
		$outputfileProp = $this->getProperty('outputfile');
		$this->assertTrue(file_exists($outputfile));
		$this->assertEquals($fileProp, $file);
		$this->assertEquals($outputfileProp, $outputfile);
		@unlink($file);
		@unlink($outputfile);
    }

    /**
     * @todo Implement testSetOutputFile().
     */
    public function testSetOutputFile()
    {
		$file = 'abc';
		$this->assertFalse(file_exists($file));
 		$this->object->setOutputFile($file);
		$outputfile = $this->getProperty('outputfile');
		$this->assertTrue(file_exists($file));
		$this->assertEquals($outputfile, $file);
		@unlink($file); //input file
		@unlink($file.'.sorted'); //output file
    }

    /**
     * @todo Implement testSetLinebreak().
     */
    public function testSetLinebreak()
    {
 		$this->object->setLinebreak();
		$linebreak = $this->getProperty('linebreak');
		$this->assertEquals($linebreak, "\n");
 		$this->object->setLinebreak(true);
		$linebreak = $this->getProperty('linebreak');
		$this->assertEquals($linebreak, "\r\n");
    }


    /**
     * @todo Implement testSetTrackingOutput().
     */
    public function testSetTrackingOutput()
    {
 		$this->object->setTrackingOutput();
		$trackingOutput = $this->getProperty('trackingOutput');
		$this->assertFalse($trackingOutput);
 		$this->object->setTrackingOutput(true);
		$trackingOutput = $this->getProperty('trackingOutput');
		$this->assertTrue($trackingOutput);
    }

    /**
     * @todo Implement testSetMaxBucketSize().
     */
    public function testSetMaxBucketSize()
    {
 		$this->object->setMaxBucketSize(2345);
		$maxsize = $this->getProperty('maxsize');
		$this->assertEquals($maxsize, 2345);
    }

    /**
     * @todo Implement testExternalSort().
     */
    public function testExternalSort()
    {
 		$this->object->setFile('ExternalSort.php');
 		$this->object->setMaxBucketSize(1000); // 
    }

    /**
     * @todo Implement testInitExternalBuckets().
     */
    public function testInitExternalBuckets()
    {
		$arr = array('a','b','c','d');
		$tmpfile = 'tmp1.txt';
		file_put_contents($tmpfile, implode("\n", $arr));
 		$this->object->setFile($tmpfile);
		$tmpdir = $this->getProperty('tmpdir');
		$this->assertFalse(file_exists($tmpdir.'/0'));
		$fp = fopen($tmpfile, 'r');
		$this->object->initExternalBuckets($fp);
		// first bucket should exist now
		$this->assertTrue(file_exists($tmpdir.'/0'));
		$extProp = $this->getProperty('externalBuckets');
		$b =  $extProp->getBucket(0);
		// first value should be smallest and largest of first bucket
		$this->assertEquals('a', $b['smallest']);
		$this->assertEquals('a', $b['biggest']);
		unlink($tmpfile);
		@unlink($tmpfile.'.sorted'); //output file
    }


    /**
     * @todo Implement testFillExternalBuckets().
     */
    public function testFillExternalBuckets()
    {
		$arr = array('a','b','c','d');
		$tmpfile = 'tmp1.txt';
		file_put_contents($tmpfile, implode("\n", $arr));
 		$this->object->setFile($tmpfile);
		$fp = fopen($tmpfile, 'r');
        $cnt = $this->object->initExternalBuckets($fp);
        $this->object->fillExternalBuckets($fp, $cnt);
        // first bucket should now have all items in first array
        // smallest item is 'a'
        // biggest item is 'd'
		$extProp = $this->getProperty('externalBuckets');
		$b =  $extProp->getBucket(0);
		// first value should be smallest and largest of first bucket
		$this->assertEquals('a', $b['smallest']);
		$this->assertEquals('d', $b['biggest']);
		$tmpdir = $this->getProperty('tmpdir');
		$flushedVals = file($tmpdir.'/0', FILE_IGNORE_NEW_LINES);
		$this->assertEquals($arr, $flushedVals);
		unlink($tmpfile);
		unlink($tmpfile.'.sorted'); //output file
    }

    /**
     * @todo Implement testBucketSanityCheck().
     */
    public function testBucketSanityCheck()
    {
		$tmpinput = 'tmpinputfile.txt';
		touch($tmpinput);
        $fp = fopen($tmpinput, 'r');
        $this->object->setFile($tmpinput);
        $this->object->initExternalBuckets($fp);
		$externalBuckets = $this->getProperty('externalBuckets');
		$tmpdir = 'testBucketSanityCheck';
		mkdir ($tmpdir);
		$fname = $tmpdir.'/0';
		$fname1 = $tmpdir.'/1';
		//var_dump($externalBuckets);
		$externalBuckets->addBucket('d', $fname);
		$externalBuckets->addBucket('p', $fname1);
		file_put_contents($fname, "n\ne\nm\n");
		file_put_contents($fname1, "z\nv\nx\n");
		$this->assertTrue($this->object->bucketSanityCheck('test'));
		file_put_contents($fname1, 'a', FILE_APPEND);
		$exceptioncaught=false;
		try{
			$this->object->bucketSanityCheck('test');
		}
		catch(Exception $e){
			$exceptioncaught=true;
		}
		if ( !$exceptioncaught){
			$this->fail('An expected exception has not been raised.');
		}
		unlink($fname);
		unlink($fname1);
		rmdir($tmpdir);
    }

    /**
     * @todo Implement testSortAndConcatExternalBuckets().
     */
    public function testSortAndConcatExternalBuckets()
    {	
		$tmpinput = 'tmpinputfile.txt';
		touch($tmpinput);
        $fp = fopen($tmpinput, 'r');
        $this->object->setFile($tmpinput);
        $this->object->initExternalBuckets($fp);
		$externalBuckets = $this->getProperty('externalBuckets');
		$tmpdir = 'testSortAndConcatExternalBuckets';
		mkdir ($tmpdir);
		$fname = $tmpdir.'/0';
		$fname1 = $tmpdir.'/1';
		//var_dump($externalBuckets);
		$externalBuckets->addBucket('d', $fname);
		$externalBuckets->addBucket('p', $fname1);
		$items = array('e', 'n', 'm', 'x', 'v', 'z');
		$externalBuckets->sortIntoBuckets($items);

		$this->bucketCollectionClass = new ReflectionClass('BucketCollection');
		$buckCollProperty = $this->bucketCollectionClass->getProperty('values');
		$buckCollProperty->setAccessible(true);
		$values = $buckCollProperty->getValue($externalBuckets);	  
		//print_r($values);

		$first = implode(',', $values[1]);
		$second = implode(',', $values[2]);
		$this->assertEquals($first, 'e,n,m', 'e,n,m should be sorted into "d" array');
		$this->assertEquals($second, 'x,v,z', 'x,v,z should be sorted into "p" array');
		
		//$tmpdir = $this->getProperty('tmpdir');
		$externalBuckets->flushBuckets(100, false, $tmpdir); // write contents to files
		$contents1 = implode(',', file($fname, FILE_IGNORE_NEW_LINES));
		$contents2 = implode(',', file($fname1, FILE_IGNORE_NEW_LINES));
		$this->assertEquals($contents1, 'e,n,m', 'e,n,m should be in first file');
		$this->assertEquals($contents2, 'x,v,z', 'x,v,z should be in second file');
		
		$items = array('g', 'f');
		$externalBuckets->sortIntoBuckets($items);
		$externalBuckets->flushBuckets(100, false, $tmpdir); // write contents to files
		$contents1 = implode(',', file($fname, FILE_IGNORE_NEW_LINES));
		$this->assertEquals($contents1, 'e,n,m,g,f', 'e,n,m,g,f should be in first file');
		
		
		$outputfile = "tmpoutput.txt";
		$this->object->sortAndConcatExternalBuckets();
		$output = implode(',', file($tmpinput.'.sorted', FILE_IGNORE_NEW_LINES));
		$this->assertEquals($output, 'e,f,g,m,n,v,x,z', 'e,f,g,m,n,v,x,z should be in output file');
		unlink($fname);
		unlink($fname1);
		unlink($tmpinput);		
		unlink($tmpinput.'.sorted');
		rmdir($tmpdir);
    }

    /**
     * @todo Implement testCheckFileSort().
     */
    public function testCheckFileSort()
    {
		$linenumber = -1;
		$line='';
		$prevline = '';
		$arr = array('a','b','c','d');
		$tmpfile = 'tmp1.txt';
		file_put_contents($tmpfile, implode("\n", $arr));
		$this->assertTrue(ExternalSort::checkFileSort($tmpfile));
		$arr = array('a','c','b','d');
		file_put_contents($tmpfile, implode("\n", $arr));
		$this->assertFalse(ExternalSort::checkFileSort($tmpfile, $linenumber, $line, $prevline));
		$this->assertEquals($linenumber, 2);
		$this->assertEquals($line, 'b');
		$this->assertEquals($prevline, 'c');	
		$linenumber = -1;
		$line='';
		$prevline = '';
		$this->assertFalse(ExternalSort::checkFileSort($tmpfile, $linenumber, $line, $prevline, 2));
		$this->assertEquals($linenumber, 2);
		$this->assertEquals($line, 'b');
		$this->assertEquals($prevline, 'c');	
		unlink($tmpfile);
    }

    /**
     * @todo Implement testCheckArraySort().
     */
    public function testCheckArraySort()
    {
		$linenumber = -1;
		$line='';
		$prevline = '';
		$arr = array('a','b','c','d');
		$this->assertTrue(ExternalSort::checkArraySort($arr));
		$arr = array('a','c','b','d');
		$this->assertFalse(ExternalSort::checkArraySort($arr, $linenumber, $line, $prevline));
		$this->assertEquals($linenumber, 2);
		$this->assertEquals($line, 'b');
		$this->assertEquals($prevline, 'c');	
    }
    protected function getMethod($name) {
	  $method = $this->testclass->getMethod($name);
	  $method->setAccessible(true);
	  return $method;
	}
    protected function getProperty($name) {
	  $property = $this->testclass->getProperty($name);
	  $property->setAccessible(true);
	  return $property->getValue($this->object);	  
	  //use $property->setValue
	}

}
?>