簡體   English   中英

我正在創建一個Codeigniter庫,如何測試代碼?

[英]I am creating a Codeigniter library, how do i test the code?

我正在codeigniter中為輪盤系統建立一個庫。

我希望使代碼非常有效且防黑客攻擊。

  1. 有什么工具可以用來測試代碼,例如功能測試等?
  2. 代碼高效嗎? 如果沒有,如何測試/提高效率?

這是代碼

    <?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 

庫中的函數1.用於檢查數字是否為有效數字的函數(輸入:數字|輸出:是/否)[錨定:1num]

  1. 根據給定數字返回顏色的函數(輸入:數字|輸出:顏色)[錨點:2color]
  2. 在給定數字的右邊查找鄰居的函數(輸入:數字,鄰居數|輸出:數字數組)[錨:neirside]
  3. 查找給定數字左側的鄰居的功能(輸入:數字,鄰居數|輸出:數字數組)[錨:neiLside]
  4. 返回游戲的利潤n payoutAmount的函數(輸入:數組(被投資的數字),數組(被投資的數量),獲勝數|輸出:數組(amountReceivedAtTheEnd,利潤,lossAmount,investedAmount)))
  5. 查找右邊第x個鄰居的函數(輸入:數字,distanceToXthNumber |輸出:數字)[錨定:XthNeiRside]
  6. 查找左側第x個鄰居的函數(輸入:數字,distanceToXthNumber |輸出:數字)[錨定:XthNeiLside]

  7. 查找右邊距離(順時針)的功能(輸入:number1,number2 |輸出:中間的總數)

  8. 查找左側鄰居距離的功能(逆時針方向)(輸入:number1,number2 |輸出:中間的總數)
  9. 知道數字屬於哪個打的功能(輸入:數字|輸出:打的數字或0)
  10. 查看數字是偶數還是奇數的函數(輸入:數字|輸出:偶數r奇數r 0)
  11. 查找Low1to18OrHigh19to36的函數(輸入:中獎號碼|輸出:低或高或0)
  12. 查找第一,第二或第三列號的功能(輸入:中獎號碼|輸出:列號或0)
  13. 返回給定數字的finals(1,11,21,31,2,22,32 ..)的函數(輸入:獲勝數字|輸出:決賽數字)
  14. 返回車輪扇形的功能(輸入:獲勝編號|輸出:扇形名稱[零游戲,零鄰居..])

您應該執行以下操作以將其用作庫
1.將您的庫文件放置在application / third_party文件夾中。

在此處輸入圖片說明

2.在application / libraries文件夾中創建另一個文件,並擴展類,如下所示。

在此處輸入圖片說明

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 require_once APPPATH."/third_party/Prad_roulette.php"; 

class Roulette extends Prad_roulette { 

}

注意:如果需要在類中使用構造函數,請確保擴展父構造函數:

class Roulette extends Prad_roulette {
    public function __construct()
    {
        parent::__construct();
    }
}

3.最后加載您的庫。

$this->load->library('roulette');

要檢查庫是否已加載,可以執行如下的method_exits函數

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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM