Login   Register  
PHP Classes
elePHPant
Icontem

File: test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cesar D. Rodas  >  Binary serialization  >  test.php  >  Download  
File: test.php
Role: Unit test script
Content type: text/plain
Description: Class test (and also sample)
Class: Binary serialization
Serialize and unserialize values in binary format
Author: By
Last change: * Adding some extra check for PHP4, since it may fails when comparing to objects, so I compare its properties
Date: 2009-03-17 21:56
Size: 3,002 bytes
 

Contents

Class file image Download
<?php
/**
 * Copyright (c) 2009, PHPServer
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Cesar Rodas nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CESAR RODAS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL CESAR RODAS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
include("bserialize.php");

if (!
is_callable("file_put_contents")) {
    function 
file_put_contents($f,$v) {
        
$fp fopen($f,"wb");
        
fwrite($fp,$v);
        
fclose($fp);
    }
}


$c = new bserialize;
$data = array(
    
/* signed/unsigned 1-byte number */
    
1,-5,9,4,66,-99,565,
    
/* signed/unsigned 2-bytes number */
    
1024,-8363,-43433,-9463,17345,
    
/* signed/unsigned 4-bytes number */
    
18978897,-645590,4290392,
    
/* signed/unsigned float number */
    
5.99,6.88,-99787.456,
    
/* boolean */
    
true,false,
    
/* string */
    
" ","some weird test",str_repeat("php rules",100000/* large text */
);
/* object  */
$o->data $data/* array into object */
$data[] = $o;
$data[] = new bserialize;
/* array */
$data[] = $data
$data[] = array();
$data[] = array(array(array(1,array())));

/**/
foreach($data as $i=>$element) {
    
$test $c->unserialize($c->serialize($element));
    if(
$element == $test) {
        echo 
"Test $i (".gettype($element).") [PASSED]\n";
    } else {
       if (
is_object($element)) {
            
/* php4 fails while comparing objects, so I compares its properties */
            
$test array_diff(get_object_vars($element),get_object_vars($test));
            if (
$test==array()) {
                echo 
"Test $i (".gettype($element).") [PASSED]\n";
                continue;
            }

        }
        echo 
"Test $i (".gettype($element).") [FAILED]\n";
    }
}
                    
?>