PHP Classes

File: BucketCollectionTest.php

Recommend this page to a friend!
  Classes of Philipp Strazny   External Sort   BucketCollectionTest.php   Download  
File: BucketCollectionTest.php
Role: Unit test script
Content type: text/plain
Description: unit tests for BucketCollection utility class
Class: External Sort
Sort large files similar to the UNIX sort command
Author: By
Last change:
Date: 12 years ago
Size: 10,902 bytes
 

Contents

Class file image Download
<?php /* * BucketCollectionTest is copyright 2011. Philipp Strazny. * Licensed under the Academic Free License version 3.0 * * BucketCollectionTest implements unit tests for BucketCollection, a * utility class used for ExternalSort. * * @author Philipp Strazny * @version 1.0 * @copyright Philipp Strazny * @license Academic Free License v. 3.0 * @package BucketCollectionTest * filename: BucketCollectionTest.php * date: 2011-11-04 * * * command-line usage: * phpunit BucketCollectionTest.php */ require 'ExternalSort.php'; /** * Test class for BucketCollection. * Generated by PHPUnit on 2011-05-21 at 20:02:37. */ class BucketCollectionTest extends PHPUnit_Framework_TestCase { /** * @var BucketCollection */ protected $object; /** * @var BucketCollection 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 BucketCollection; $this->testclass = new ReflectionClass('BucketCollection'); } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @todo Implement testAddBucket(). */ public function testAddBucket() { $this->object->addBucket('value', 'filename'); $values = $this->getProperty('values'); $this->assertEquals($values[0], array()); $smallestitems = $this->getProperty('smallestitems'); $this->assertEquals($smallestitems[0], 'value'); $biggestitems = $this->getProperty('biggestitems'); $this->assertEquals($biggestitems[0], 'value'); $fnames = $this->getProperty('fnames'); $this->assertEquals($fnames[0], 'filename'); } /** * @todo Implement testSortBucketCollection(). */ public function testSortBucketCollection() { $this->object->addBucket('v', 'v1'); //0 $this->object->addBucket('v', 'v2'); //1 $this->object->addBucket('a', 'a'); //2 $this->object->addBucket('x', 'x'); //3 // add smallestitems $this->object->addToBucket(0, 'v'); $this->object->addToBucket(1, 'v'); $this->object->addToBucket(2, 'a'); $this->object->addToBucket(3, 'x'); // add biggestitems $this->object->addToBucket(0, 'vz'); $this->object->addToBucket(1, 'vx'); $this->object->addToBucket(2, 'aa'); $this->object->addToBucket(3, 'xx'); // now sort $fnames = $this->getProperty('fnames'); $fnamesBefore = implode(',', $fnames); $this->object->sortBucketCollection(); $fnames = $this->getProperty('fnames'); $fnamesAfter = implode(',', $fnames); $this->assertEquals('v1,v2,a,x', $fnamesBefore, 'fnames sorted in order of entry'); $this->assertEquals('a,v2,v1,x', $fnamesAfter, 'fnames sorted in order of smallestitems, then biggestitems'); } /** * @todo Implement testGetBucket(). */ public function testGetBucket() { $this->object->addBucket('value', 'filename'); $testbucket = <<<END array ( 'smallest' => 'value', 'biggest' => 'value', 'fname' => 'filename', 'values' => array ( ), ) END; $bucket=$this->object->getBucket(0); $exportedbucket = var_export($bucket, true); $this->assertEquals($testbucket, $exportedbucket); } /** * @todo Implement testGetSize(). */ public function testGetSize() { $this->assertEquals(0, $this->object->getSize()); $this->object->addBucket('v', 'f'); $this->assertEquals(1, $this->object->getSize()); } /** * @todo Implement testAddToBucket(). */ public function testAddToBucket() { $this->object->addBucket('v', 'f'); $this->object->addToBucket(0, 'v'); $values = $this->getProperty('values'); $this->assertEquals($values[0][0], 'v', 'checking that v has been added to values'); $this->object->addToBucket(0, 'x'); $values = $this->getProperty('values'); $this->assertEquals($values[0][1], 'x', 'checking that x has been added to end of values'); // v is smallestitem, x is biggestitem $smallestitems = $this->getProperty('smallestitems'); $this->assertEquals($smallestitems[0], 'v', 'checking that v is still smallestitem'); $biggestitems = $this->getProperty('biggestitems'); $this->assertEquals($biggestitems[0], 'x', 'checking that x is now biggestitem'); $this->object->addToBucket(0, 'a'); // a is now smallestitem $smallestitems = $this->getProperty('smallestitems'); $this->assertEquals($smallestitems[0], 'a', 'checking that a is now smallestitem'); $values = $this->getProperty('values'); $this->assertEquals($values[0], array('v', 'x', 'a'), 'checking that values array is as expected'); } /** * @todo Implement testFitsInBucket(). */ public function testFitsInBucket() { $this->object->addBucket('v', 'f'); $this->assertTrue($this->object->fitsInBucket(0, 'v'), 'checking that v fits in v bucket'); $this->assertTrue($this->object->fitsInBucket(0, 'x'), 'checking that x fits in v bucket'); $this->assertFalse($this->object->fitsInBucket(0, 'a'), 'checking that a does not fit in v bucket'); } /** * @todo Implement testSplitBucket(). */ public function testSplitBucket() { $tmpdir = microtime(true); mkdir ($tmpdir); $fname = $tmpdir.'/0'; file_put_contents($fname, "a\nb\nc\nd"); $this->object->addBucket(0, $fname); $this->object->addToBucket(0, 'a'); $this->object->addToBucket(0, 'b'); $this->object->addToBucket(0, 'c'); $this->object->addToBucket(0, 'd'); $this->object->splitBucket(0, false, $tmpdir, $fname, ''); $newfilename = $tmpdir.'/1'; // now check... $this->assertTrue(file_exists($fname), 'first file exists'); $this->assertTrue(file_exists($newfilename), 'new file exists'); $testbucket = <<<END array ( 'smallest' => 'a', 'biggest' => '', 'fname' => '$fname', 'values' => array ( ), ) END; $bucket=$this->object->getBucket(0); $exportedbucket = var_export($bucket, true); $this->assertEquals($testbucket, $exportedbucket, 'checking that firstbucket now contains first half'); $testbucket = <<<END array ( 'smallest' => 'c', 'biggest' => '', 'fname' => '$newfilename', 'values' => array ( ), ) END; $bucket=$this->object->getBucket(1); $exportedbucket = var_export($bucket, true); $this->assertEquals($testbucket, $exportedbucket, 'checking that new bucket now contains second half'); unlink($fname); unlink($newfilename); rmdir($tmpdir); } /** * @todo Implement testCheckBucketSizes(). */ public function testCheckBucketSizes() { $tmpdir = microtime(true); mkdir ($tmpdir); $fname = $tmpdir.'/0'; file_put_contents($fname, "a\nb\nc\nd"); $this->object->addBucket(0, $fname); $this->object->addToBucket(0, 'a'); $this->object->addToBucket(0, 'b'); $this->object->addToBucket(0, 'c'); $this->object->addToBucket(0, 'd'); $newfilename = $tmpdir.'/1'; // now check... $this->assertTrue(file_exists($fname), 'first file exists'); $this->assertFalse(file_exists($newfilename), 'new file does not exist'); $this->object->checkBucketSizes(100, false, $tmpdir); $this->assertTrue(file_exists($fname), 'first file still exists'); $this->assertFalse(file_exists($newfilename), 'new file still does not exist'); $this->object->checkBucketSizes(2, false, $tmpdir); $this->assertTrue(file_exists($fname), 'first file still exists'); $this->assertTrue(file_exists($newfilename), 'new file now does exist'); unlink($fname); unlink($newfilename); rmdir($tmpdir); } /** * @todo Implement testFlushBuckets(). */ public function testFlushBuckets() { $tmpdir = microtime(true); mkdir ($tmpdir); $fname = $tmpdir.'/0'; $this->object->addBucket(0, $fname); $this->object->addToBucket(0, 'a'); $this->object->addToBucket(0, 'b'); $fname1 = $tmpdir.'/1'; $fname2 = $tmpdir.'/2'; $this->object->addBucket(1, $fname1); $this->object->addToBucket(1, 'c'); $this->object->addToBucket(1, 'd'); $this->assertFalse(file_exists($fname), 'fname does not exist'); $this->assertFalse(file_exists($fname1), 'fname1 does not exist'); $this->object->flushBuckets(100, false, $tmpdir); $this->assertTrue(file_exists($fname), 'fname new does exist'); $this->assertTrue(file_exists($fname1), 'fname1 now does exist'); $this->assertFalse(file_exists($fname2), 'fname2 does not exist'); $this->object->addToBucket(0, 'x'); $this->object->addToBucket(0, 'y'); $this->object->flushBuckets(2, false, $tmpdir); $this->assertTrue(file_exists($fname), 'fname does exist'); $this->assertTrue(file_exists($fname1), 'fname1 does exist'); $this->assertTrue(file_exists($fname2), 'fname2 now does exist'); $a = file_get_contents($fname); $b = file_get_contents($fname1); $c = file_get_contents($fname2); //print "a: $a"; //print "b: $b"; //print "c: $c"; $this->assertEquals($a, "a\nb\n"); $this->assertEquals($b, "c\nd\n"); $this->assertEquals($c, "x\ny\n"); unlink($fname); unlink($fname1); unlink($fname2); rmdir($tmpdir); } /** * @todo Implement testSortIntoBuckets(). */ public function testSortIntoBuckets() { $tmpdir = microtime(true); //mkdir ($tmpdir); $fname = $tmpdir.'/0'; $this->object->addBucket('d', $fname); $this->object->addBucket('p', $fname); $items = array('a', 'g', 'x'); $this->object->sortIntoBuckets($items); $values = $this->getProperty('values'); $first = implode(',', $values[0]); $second = implode(',', $values[1]); $this->assertEquals($first, 'a,g', 'a,g should be sorted into "d" array'); $this->assertEquals($second, 'x', 'x should be sorted into "p" array'); //unlink($fname); //rmdir($tmpdir); } /** * @todo Implement testSplitArrayInHalf(). */ public function testSplitArrayInHalf() { $testarray = array('a', 'b', 'c', 'd'); $firsthalf = $this->object->splitArrayInHalf($testarray); $this->assertEquals($firsthalf, array('a', 'b'), 'check whether firsthalf is really first half'); $this->assertEquals($testarray, array('c', 'd'), 'check whether testarray is now second half'); } /** * @todo Implement testSplitArrayInHalf(). */ //public function testPregtrim() //{ // $s = " \t\r\nwhatever\t\r\n\t "; //print '>'.trim($s).'<'."\n"; //print '>'.BucketCollection::pregtrim($s).'<'."\n"; //} 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 } } ?>