首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在创建一个Codeigniter库,如何测试代码?

我正在创建一个Codeigniter库,如何测试代码?
EN

Stack Overflow用户
提问于 2014-11-03 13:10:14
回答 1查看 80关注 0票数 0

我正在为轮盘赌系统建立一个代码点火器库。

我希望使代码非常有效和防黑客。

  1. 有什么工具可以用来测试代码,比如功能测试等等?
  2. 代码有效吗?如果不是,如何测试/提高效率?

这是密码

代码语言:javascript
复制
    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Prad Roulette
*
* Version: 0.1
*
* Author: Pradyummna Reddy
*         prad@hireprad.co.uk
*         
*
* Location: https://github.com/pradyummna/prad_roulette
*
* Created:  21/10/2014
*
* Description:  The library can be used for roulette system developers
*
* Requirements: PHP5 or above
*
*/

class Prad_roulette
{

    /**
     *
     * Function to find if the number is a valid roulette number
     *
     * @param  integer  $number The number to which has to be checked
     *
     * @return boolean 
     *
     * Anchor: 1num
     */
     function isValidRouletteNumber($number)
    {
        if(ctype_digit($number) && $number < 37 && $number >= 0 ){return true;}else{return false;}
    }


    /**
     * Function to finds the number's color` in a roulette table
     *
     * @param  integer  $number The number to which the colour is to be determined
     *
     * @return string
     *
     * Anchor : 2color
     */
    function findColour($number)
    {

        // checking the input numbers
        if(!($this->isValidRouletteNumber($number))){return 'InValidInput';}

        $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');

        $numberInArray = array_keys($wheel, $number);

        if(isset($numberInArray[0]))
        {
            if($numberInArray[0] == '0')
            {$blackRred='ZERO';}
            else{
                    if($numberInArray[0] % 2 == 0)
                    {
                        //its black!!!
                        $blackRred='Black';
                    }
                    else{ $blackRred = 'Red'; }
                }
        }
        else
        {
            $blackRred = 'OUT';
        }

    return $blackRred;  
    }

    /**
     * Function to finds the number's neighbours left to them
     *
     * @param  integer  $number The number to which the left neighbours is to be determined
     * @param  integer  $howManyNeighbours The total neighbours to be found
     * @return array
     *
     * Anchor: neiLside
     */
    function neighboursLeft($number,$howManyNeighbours)
    {   
        // checking the input numbers
        if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}

        $x = $howManyNeighbours;
        $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');

        $numberInArray = array_keys($wheel, $number);

        $neighboursLeft = null;
        if(isset($numberInArray[0]))
        {
            $currentNumber = $numberInArray[0];

            //finding the end of the array
            $startValueInArray = reset($wheel);
            //$endArrayKey = key($wheel);               
            //reset($wheel);

            //moving to the current position value position in the array
            while (key($wheel) !== $numberInArray[0]) next($wheel);

            $neighboursLeft[0] = 'In';
            for($i=0;$i<$x;$i++)
            {
                if(current($wheel) != $startValueInArray)
                {
                    //echo next($wheel);echo '::'.$i; echo '<br/>';
                    $neighboursLeft[$i+1] =  prev($wheel);
                }
                else
                {
                    end($wheel);
                    //echo current($wheel);echo '::'.$i;echo '<br/>';
                    $neighboursLeft[$i+1] =  current($wheel);
                }
            }
        }
        else
        {
            $neighboursLeft = array('OUT');
        }
        //$neighboursLeft[0] can be OUT or In 
        return $neighboursLeft;

    }

    /**
     * Function to finds the number's neighbours right to them
     *
     * @param  integer  $number The number to which the right neighbours is to be determined
     * @param  integer  $howManyNeighbours The total neighbours to be found
     * @return array
     *
     * Anchor: neirside
     */
    function neighboursRight($number,$howManyNeighbours)
    {

        // checking the input numbers
        if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}


        $x = $howManyNeighbours;
        $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');

        $numberInArray = array_keys($wheel, $number);

        $neighboursRight = null;
        if(isset($numberInArray[0]))
        {
            $currentNumber = $numberInArray[0];

            //finding the end of the array
            $endValueInArray = end($wheel);
            //$endArrayKey = key($wheel);               
            reset($wheel);

            //moving to the current position value position in the array
            while (key($wheel) !== $numberInArray[0]) next($wheel);

            $neighboursRight[0] = 'In';
            for($i=0;$i<$x;$i++)
            {
                if(current($wheel) != $endValueInArray)
                {
                    //echo next($wheel);echo '::'.$i; echo '<br/>';
                    $neighboursRight[$i+1] =  next($wheel);
                }
                else
                {
                    reset($wheel);
                    //echo current($wheel);echo '::'.$i;echo '<br/>';
                    $neighboursRight[$i+1] =  current($wheel);
                }
            }

        }
        else
        {
            $neighboursRight = array('OUT');
        }

        // $neighboursRight[0] can be OUT or In
        return $neighboursRight;

    }

    /**
     * Function to finds the number's neighbours right to them
     *
     * @param  array  $numbers The number to which the right neighbours is to be determined
     * @param  array  $moneyOnNumbers The total neighbours to be found
     * @param  integer $winningNumber The winning number
     * @return array payoutAmount, Profit, Invested amount
     *
     * Anchor: xPayoutProfit
     */
    function payoutProfit($numbers,$moneyOnNumbers,$winningNumber)
    {
        // checking the input numbers
        if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($winningNumber))){return array('InValidInput');}

        //find the payout

            if(in_array($winningNumber,$numbers))
            {
                $amountOnNumber = $moneyOnNumbers[array_search($winningNumber)];
                $payoutAmount = $amountOnNumber * 36;
            }
            else
            {
                $payoutAmount = 0;
            }           
            $totalInvested = 0;

            // find the amount invested on it
            foreach($moneyOnNumbers as $money)
            {
                $totalInvested = $totalInvested + $money;
            }           

            //calculate profit
            $profit = $payoutAmount - $totalInvested;

            //return
            return array($payoutAmount,$totalInvested,$profit);
    }


    /**
     * Function to finds the number's neighbour right to it
     *
     * @param  integer  $number The number to which the right xth neighbour is to be determined
     * @param  integer  $distanceToXthNumber Distance to the xth neighbour
     *
     * @return integer number in the xth position
     *
     * Anchor: XthNeiRside
     */
    function findXthNumberOnRight($number,$distanceToXthNumber)
    {
        // checking the input numbers
        if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
        if($distanceToXthNumber != '0')
        {
            $xthNumber = $this->neighboursRight($number,$distanceToXthNumber);
            $xthNumber = $xthNumber[$distanceToXthNumber];
        }
        else
        {
            return $number;
        }

        //find the payout
        return $xthNumber;
    }

    /**
     * Function to finds the number's xth neighbour left to it
     *
     * @param  integer  $number The number to which the left xth neighbour is to be determined
     * @param  integer  $distanceToXthNumber Distance to the xth neighbour
     *
     * @return integer number in the xth position
     *
     * Anchor: XthNeiLside
     */
    function findXthNumberOnLeft($number,$distanceToXthNumber)
    {
        // checking the input numbers
        if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
        if($distanceToXthNumber != '0')
        {
            $xthNumber = $this->neighboursLeft($number,$distanceToXthNumber);
            $xthNumber = $xthNumber[$distanceToXthNumber];
        }
        else
        {
            return $number;
        }

        //find the payout
        return $xthNumber;
    }
}





//1000861505 

函数检查数字是否为有效数字(输入: number : true/false)锚点: 1num。

  1. 函数返回基于给定数字的颜色(输入:数字:输出:颜色)锚点:2颜色
  2. 函数在给定数字的右边查找邻居(输入: number,output:数组数字)锚点: neirside
  3. 函数在给定数字的左边查找邻居(输入: number,output:数组数字)锚点: neiLside
  4. 函数为游戏返回利润n payoutAmount (输入:数组(投资数字)、数组(投资额)、获胜数字(输出):数组( amountReceivedAtTheEnd、R溢、lossAmount、investedAmount))锚点: xPayoutProfit。
  5. 函数以在右侧查找xth邻域(输入: number,distanceToXthNumber \x输出: number)锚点: XthNeiRside
  6. 函数以查找左侧的xth邻域(输入: number,distanceToXthNumber \x输出: number)锚点: XthNeiLside
  7. 函数以查找相邻距离(按时钟排列)(输入: number1,number2 =输出:中间的总数字)
  8. 函数以求邻接距离左(反时钟)(输入: number1,number2的输出:中间的总数字)
  9. 函数来知道该数字属于哪一打(输入: number : number :十几个数字或0)
  10. 函数,以查看该数字是偶数还是奇数(输入: number :偶数r奇数r0)。
  11. 函数以查找Low1to18OrHigh19to36 (输入:中奖号码:输出:低或高或0)
  12. 函数查找列号1、第2或第3(输入:中奖号码:输出:列号或0)。
  13. 返回决赛的职能(1,11,21,31,22,32.)对于给定的数字(输入:获胜号码:输出:决赛号码)
  14. 函数返回轮盘的扇区(输入:中奖号:输出:扇区名称为零的游戏,邻接为零.)
EN

回答 1

Stack Overflow用户

发布于 2014-11-03 14:58:48

您应该做以下事情来使用它作为一个库

1.将库文件放在应用程序/第三方文件夹中.

2.在应用程序/库文件夹中创建另一个文件,并像下面这样扩展类

代码语言:javascript
复制
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 require_once APPPATH."/third_party/Prad_roulette.php"; 

class Roulette extends Prad_roulette { 

}

注意:如果需要在类中使用构造函数,请确保扩展父构造函数:

代码语言:javascript
复制
class Roulette extends Prad_roulette {
    public function __construct()
    {
        parent::__construct();
    }
}

3.最后加载库.

代码语言:javascript
复制
$this->load->library('roulette');

若要检查库是否已加载,可以执行以下method_exits函数

代码语言:javascript
复制
if(method_exists($this->roulette,'isValidRouletteNumber')){ /* isValidRouletteNumber is the method of Prad_roulette class. */
        echo "Library is loaded successfully";
}else{
        echo "Couldn't load the library";
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26715094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档