<?php

namespace loyalsoft;

include_once __DIR__ . '/AppServer.php';
require_once __DIR__ . '/../process/ActiveProc/CipheredBase32.php';              # 算法库

/**
 * Description of TestServer
 * UT 测试
 * @author jgao
 */
class TestServer {

    /**
     * 入口函数专用测试
     * @param type $req
     */
    static public function testApi($req) {
        DebugHelper::debug($req);
        $app = new AppServer();
        new Req($req);
        $ret = $app->api();
        DebugHelper::debug($ret);
    }

    /**
     * 运行环境自检:
     *   PHP 版本,扩展模块
     *   redis操作性
     *   sqldb操作性
     */
    static function selfTest() {
        echoLine("phpver: " . PHP_VERSION . PHP_EOL);                           # 打印下PHP版本 
        SelfChecker::CheckConfig();                                             # 进行配置环境检测
    }

//----------------------------------------------------


    public function testRedisLua($key, $value) {
        $mem = gMem();
        $script = <<<SCR
if redis.call("get",KEYS[1]) == ARGV[1]
then
    return redis.call("del",KEYS[1])
else
    return 0
end
SCR;
        DebugHelper:: var_dump($script);

        $ret = $mem->redis->eval($script, 1, $key, $value);
        DebugHelper:: var_dump($ret);
        $mem->close();
    }

    public function testBinSearch($v) {
        $arr = array(1, 2, 3, 4, 4, 11, 12, 124);
        $start = 0;
        $end = count($arr) - 1;
        while ($start <= $end) {
            $index = intval(($start + $end) / 2);
            if ($v < $arr[$index]) {
                $end = $index - 1;
            } elseif ($v > $arr[$index]) {
                $start = $index + 1;
            } else {
                echo($index);
                return;
            }
        }
        echo($index);
    }

}
