<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002 The PHP Group                                     |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Jeroen Derks <jeroen@derks.it>                              |
// +----------------------------------------------------------------------+
//
// $Id: Xtea2Test.php,v 1.2 2004/09/08 19:21:08 jeroend Exp $

/** Xtea2 class */
require_once 'Crypt/Xtea2.php';
/** PHPUnit class */
require_once 'PHPUnit.php';


/**
 *  Tester class for Xtea2 class.
 *
 *  @package    Crypt_Xtea2_Test
 *  @access     public
 *
 *  @version    $Revision: 1.2 $
 *  @since      2004/Sep/06
 *  @author     Jeroen Derks <jeroen@derks.it>
 */
class Crypt_Xtea2Test extends PHPUnit_TestCase
{
    var 
$obj;
    var 
$data;
    var 
$key;

    function 
Crypt_Xtea2Test($method) {
        global 
$profiling;

        
$this->profiling $profiling;
        
$this->PHPUnit_TestCase($method);
    }

    function 
setUp() {
        
$this->obj = new Crypt_Xtea2();
        
$this->key '0123456789abcdeffedcba9876543210';

        if (!
$this->profiling$this->startTimer('data');
        
//$this->data = '1'; return;
        //$this->data = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return;
        
$this->data '';
        for (
$i 0$i 256; ++$i) {
            
$this->data .= chr($i 0xff);
        }
        if (!
$this->profiling$this->endTimer('data');
    }

    function 
testIter() {
        
$this->obj->setIter(36);
        
$this->assertEquals(36$this->obj->getIter());
    }

    function 
testCrypt() {
        
$start = IsSet($this->profiling) && $this->profiling strlen($this->data) - 0;
        if (
== $start) {
            
$walker '-\|/';
            echo 
"Testing... ";
            
flush();
        }

        for (
$i $start$i strlen($this->data); ++$i)
        {
            if (
== $start) {
                echo 
$walker{$i 4} . sprintf(" %4u"$i) . str_repeat(""6);
                
flush();
            }

            if (!
$this->profiling$this->startTimer('data');
            
$data substr($this->data0$i);
            if (!
$this->profiling$this->endTimer('data');

            if (!
$this->profiling$this->startTimer('encrypt');
            
$encrypted  $this->obj->encrypt($data$this->key);
            if (!
$this->profiling$this->endTimer('encrypt');
            if (!
$this->profiling$this->startTimer('decrypt');
            
$decrypted  $this->obj->decrypt($encrypted$this->key);
            if (!
$this->profiling$this->endTimer('decrypt');

            if (!
$this->profiling$this->startTimer('assert');
            
$this->assertEquals(strlen($data), strlen($decrypted));
            
$this->assertEquals($data$decrypted"run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
            if (!
$this->profiling$this->endTimer('assert');
        }

        if (
== $start) {
            echo 
"                          ";
            
flush();
        }
    }

    function 
_testHuge() {
        
set_time_limit(99999);

        if (!
$this->profiling$this->startTimer('data');
        
$data '';
        for(
$i 0$i 1024 1024; ++$i)
            
$data .= chr($i 0xff);
        if (!
$this->profiling$this->endTimer('data');

        if (!
$this->profiling$this->startTimer('encrypt');
        
$encrypted $this->obj->encrypt($data$this->key);
        if (!
$this->profiling$this->endTimer('encrypt');
        if (!
$this->profiling$this->startTimer('decrypt');
        
$decrypted $this->obj->decrypt($encrypted$this->key);
        if (!
$this->profiling$this->endTimer('decrypt');

        if (!
$this->profiling$this->startTimer('assert');
        
$this->assertEquals(strlen($data), strlen($decrypted));
        
$this->assertEquals($data$decrypted"run $i failed: expected '***' (".strlen($data)."), actual '***' (".strlen($decrypted).")");
        if (!
$this->profiling$this->endTimer('assert');
    }

    function 
tearDown() {
        
$this->obj NULL;
    }

    function 
startTimer($label) {
        
$time0 strtok(microtime(), ' ');
        
$time1 strtok(' ');

        
$timer              =& $this->_getTimer();
        
$timer[$label][]    = (string) $time1 substr($time01);
    }

    function 
endTimer($label) {
        
$time0 strtok(microtime(), ' ');
        
$time1 strtok(' ');

        
$timer              =& $this->_getTimer();
        
$timer[$label][]    = (string) $time1 substr($time01);
    }

    
/**
     *  @static
     */
    
function &_getTimer() {
        static 
$timing NULL;

        if (!IsSet(
$timing))
        {
            
$time0 strtok(microtime(), ' ');
            
$time1 strtok(' ');

            
// start _global
            
$timing = array('_global' => array((string) $time1 substr($time01)));
        }

        return 
$timing;
    }

    
/**
     *  @static
     */
    
function getTimings() {
        
$time0 strtok(microtime(), ' ');
        
$time1 strtok(' ');

        
$timer                  =& Crypt_Xtea2Test::_getTimer();
        
$timer['_global'][1]    = (string) $time1 substr($time01);
        
$labels                 =  array_keys($timer);
        
$results                =  array();

        
// calculate times and calls
        
sort($labels);
        foreach (
$labels as $label) {
            
$results[$label]['time']    = 0.0;
            
$results[$label]['calls']   = 0;

            
$n count($timer[$label]);
            for (
$i 0$i $n$i += 2) {
                
$results[$label]['time'] += (float) $timer[$label][$i 1] - $timer[$label][$i];
                ++
$results[$label]['calls'];
            }
        }

        
// calculate percentages
        
foreach ($labels as $label) {
            
$results[$label]['perc'] = ( $results[$label]['time'] * 100.0 ) / $results['_global']['time'];
        }

        
// output results
        
echo "Timing results:\n" .
            
sprintf("%-20s %13s %8s %10s%%\n"'Label''time''#calls''perc') .
            
str_repeat('-'57) . "\n";
        foreach (
$labels as $label) {
            
printf("%-20s %13s %8lu %10s%%\n",
                    
$label,
                    
sprintf("%.8f"$results[$label]['time']),
                    
$results[$label]['calls'],
                    
sprintf("%.6f"$results[$label]['perc']));
        }
    }
}

?>