<commit_before><?php
define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false);

require_once 'PHPUnit/Framework.php';
require_once 'PredisShared.php';

class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
    public $redis;

    // TODO: instead of an boolean assertion against the return value 
    //       of RC::sameValuesInArrays, we should extend PHPUnit with 
    //       a new assertion, e.g. $this->assertSameValues();
    // TODO: an option to skip certain tests such as testFlushDatabases
    //       should be provided.
    // TODO: missing test with float values for a few commands

    protected function setUp() { 
        $this->redis = RC::getConnection();
        $this->redis->flushDatabase();
    }

    protected function tearDown() { 
    }

    protected function onNotSuccessfulTest($exception) {
        // drops and reconnect to a redis server on uncaught exceptions
        RC::resetConnection();
        parent::onNotSuccessfulTest($exception);
    }


    /* miscellaneous commands */

    function testPing() {
        $this->assertTrue($this->redis->ping());
    }

    function testEcho() {
        $string = 'This is an echo test!';
        $this->assertEquals($string, $this->redis->echo($string));
    }

    function testQuit() {
        $this->redis->quit();
        $this->assertFalse($this->redis->isConnected());
    }

    function testMultiExec() {
        // NOTE: due to a limitation in the current implementation of Predis\Client, 
        //       the replies returned by Predis\Command\Exec are not parsed by their 
        //       respective Predis\Command::parseResponse methods. If you need that 
        //       kind of behaviour, you should use an instance of Predis\MultiExecBlock.
        $this->assertTrue($this->redis->multi());
        $this->assertType('Predis\ResponseQueued', $this->redis->ping());
        $this->assertType('Predis\ResponseQueued', $this->redis->echo('hello'));
        $this->assertType('Predis\ResponseQueued', $this->redis->echo('redis'));
        $this->assertEquals(array('PONG', 'hello', 'redis'), $this->redis->exec());

        $this->assertTrue($this->redis->multi());
        $this->assertEquals(array(), $this->redis->exec());

        // should throw an exception when trying to EXEC without having previously issued MULTI
        RC::testForServerException($this, RC::EXCEPTION_EXEC_NO_MULTI, function($test) {
            $test->redis->exec();
        });
    }

    /* commands operating on string values */

    function testSet() {
        $this->assertTrue($this->redis->set('foo', 'bar'));
        $this->assertEquals('bar', $this->redis->get('foo'));
    }

    function testGet() {
        $this->redis->set('foo', 'bar');

        $this->assertEquals('bar', $this->redis->get('foo'));
        $this->assertNull($this->redis->get('fooDoesNotExist'));

        // should throw an exception when trying to do a GET on non-string types
        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
            $test->redis->pushTail('metavars', 'foo');
            $test->redis->get('metavars');
        });
    }

    function testExists() {
        $this->redis->set('foo', 'bar');

        $this->assertTrue($this->redis->exists('foo'));
        $this->assertFalse($this->redis->exists('key_does_not_exist'));
    }

    function testSetPreserve() {
        $multi = RC::getKeyValueArray();

        $this->assertTrue($this->redis->setPreserve('foo', 'bar'));
        $this->assertFalse($this->redis->setPreserve('foo', 'rab'));
        $this->assertEquals('bar', $this->redis->get('foo'));
    }

    function testMultipleSetAndGet() {
        $multi = RC::getKeyValueArray();

        // key=>value pairs via array instance
        $this->assertTrue($this->redis->setMultiple($multi));
        $multiRet = $this->redis->getMultiple(array_keys($multi));
        $this->assertEquals($multi, array_combine(array_keys($multi), array_values($multiRet)));

        // key=>value pairs via function arguments
        $this->assertTrue($this->redis->setMultiple('a', 1, 'b', 2, 'c', 3));
        $this->assertEquals(array(1, 2, 3), $this->redis->getMultiple('a', 'b', 'c'));
    }

    function testSetMultiplePreserve() {
        $multi    = RC::getKeyValueArray();
        $newpair  = array('hogehoge' => 'piyopiyo');
        $hijacked = array('foo' => 'baz', 'hoge' => 'fuga');

        // successful set
        $expectedResult = array_merge($multi, $newpair);
        $this->redis->setMultiple($multi);
        $this->assertTrue($this->redis->setMultiplePreserve($newpair));
        $this->assertEquals(
            array_values($expectedResult), 
            $this->redis->getMultiple(array_keys($expectedResult))
        );

        $this->redis->flushDatabase();

        // unsuccessful set
        $expectedResult = array_merge($multi, array('hogehoge' => null));
        $this->redis->setMultiple($multi);
        $this->assertFalse($this->redis->setMultiplePreserve(array_merge($newpair, $hijacked)));
        $this->assertEquals(
            array_values($expectedResult), 
            $this->redis->getMultiple(array_keys($expectedResult))
        );
    }

    function testGetSet() {
        $this->assertNull($this->redis->getSet('foo', 'bar'));
        $this->assertEquals('bar', $this->redis->getSet('foo', 'barbar'));
        $this->assertEquals('barbar', $this->redis->getSet('foo', 'baz'));
    }

    function testIncrementAndIncrementBy() {
        // test subsequent increment commands
        $this->assertEquals(1, $this->redis->increment('foo'));
        $this->assertEquals(2, $this->redis->increment('foo'));

        // test subsequent incrementBy commands
        $this->assertEquals(22, $this->redis->incrementBy('foo', 20));
        $this->assertEquals(10, $this->redis->incrementBy('foo', -12));
        $this->assertEquals(-100, $this->redis->incrementBy('foo', -110));
    }

    function testDecrementAndDecrementBy() {
        // test subsequent decrement commands
        $this->assertEquals(-1, $this->redis->decrement('foo'));
        $this->assertEquals(-2, $this->redis->decrement('foo'));

        // test subsequent decrementBy commands
        $this->assertEquals(-22, $this->redis->decrementBy('foo', 20));
        $this->assertEquals(-10, $this->redis->decrementBy('foo', -12));
        $this->assertEquals(100, $this->redis->decrementBy('foo', -110));
    }

    function testDelete() {
        $this->redis->set('foo', 'bar');
        $this->assertTrue($this->redis->delete('foo'));
        $this->assertFalse($this->redis->exists('foo'));
        $this->assertFalse($this->redis->delete('foo'));
    }

    function testType() {
        $this->assertEquals('none', $this->redis->type('fooDoesNotExist'));

        $this->redis->set('fooString', 'bar');
        $this->assertEquals('string', $this->redis->type('fooString'));

        $this->redis->pushTail('fooList', 'bar');
        $this->assertEquals('list', $this->redis->type('fooList'));

        $this->redis->setAdd('fooSet', 'bar');
        $this->assertEquals('set', $this->redis->type('fooSet'));

        $this->redis->zsetAdd('fooZSet', 'bar', 0);
        $this->assertEquals('zset', $this->redis->type('fooZSet'));
    }

    function testAppend() {
        $this->redis->set('foo', 'bar');
        $this->assertEquals(5, $this->redis->append('foo', '__'));
        $this->assertEquals(8, $this->redis->append('foo', 'bar'));
        $this->assertEquals('bar__bar', $this->redis->get('foo'));

        $this->assertEquals(4, $this->redis->append('hoge', 'piyo'));
        $this->assertEquals('piyo', $this->redis->get('hoge'));

        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
            $test->redis->pushTail('metavars', 'foo');
            $test->redis->append('metavars', 'bar');
        });
    }

    /* commands operating on the key space */

    function testKeys() {
        $keyValsNs     = RC::getNamespacedKeyValueArray();
        $keyValsOthers = array('aaa' => 1, 'aba' => 2, 'aca' => 3);
        $allKeyVals    = array_merge($keyValsNs, $keyValsOthers);

        $this->redis->setMultiple($allKeyVals);

        $this->assertEquals(array(), $this->redis->keys('nokeys:*'));

        $keysFromRedis = $this->redis->keys('metavar:*');
        $this->assertEquals(array(), array_diff(array_keys($keyValsNs), $keysFromRedis));

        $keysFromRedis = $this->redis->keys('*');
        $this->assertEquals(array(), array_diff(array_keys($allKeyVals), $keysFromRedis));

        $keysFromRedis = $this->redis->keys('a?a');
        $this->assertEquals(array(), array_diff(array_keys($keyValsOthers), $keysFromRedis));
    }

    function testRandomKey() {
        $keyvals = RC::getKeyValueArray();

        $this->assertNull($this->redis->randomKey());

        $this->redis->setMultiple($keyvals);
        $this->assertTrue(in_array($this->redis->randomKey(), array_keys($keyvals)));
    }
<commit_msg>Added tests for SUBSTR.<commit_after><?php
define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false);

require_once 'PHPUnit/Framework.php';
require_once 'PredisShared.php';

class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
    public $redis;

    // TODO: instead of an boolean assertion against the return value 
    //       of RC::sameValuesInArrays, we should extend PHPUnit with 
    //       a new assertion, e.g. $this->assertSameValues();
    // TODO: an option to skip certain tests such as testFlushDatabases
    //       should be provided.
    // TODO: missing test with float values for a few commands

    protected function setUp() { 
        $this->redis = RC::getConnection();
        $this->redis->flushDatabase();
    }

    protected function tearDown() { 
    }

    protected function onNotSuccessfulTest($exception) {
        // drops and reconnect to a redis server on uncaught exceptions
        RC::resetConnection();
        parent::onNotSuccessfulTest($exception);
    }


    /* miscellaneous commands */

    function testPing() {
        $this->assertTrue($this->redis->ping());
    }

    function testEcho() {
        $string = 'This is an echo test!';
        $this->assertEquals($string, $this->redis->echo($string));
    }

    function testQuit() {
        $this->redis->quit();
        $this->assertFalse($this->redis->isConnected());
    }

    function testMultiExec() {
        // NOTE: due to a limitation in the current implementation of Predis\Client, 
        //       the replies returned by Predis\Command\Exec are not parsed by their 
        //       respective Predis\Command::parseResponse methods. If you need that 
        //       kind of behaviour, you should use an instance of Predis\MultiExecBlock.
        $this->assertTrue($this->redis->multi());
        $this->assertType('Predis\ResponseQueued', $this->redis->ping());
        $this->assertType('Predis\ResponseQueued', $this->redis->echo('hello'));
        $this->assertType('Predis\ResponseQueued', $this->redis->echo('redis'));
        $this->assertEquals(array('PONG', 'hello', 'redis'), $this->redis->exec());

        $this->assertTrue($this->redis->multi());
        $this->assertEquals(array(), $this->redis->exec());

        // should throw an exception when trying to EXEC without having previously issued MULTI
        RC::testForServerException($this, RC::EXCEPTION_EXEC_NO_MULTI, function($test) {
            $test->redis->exec();
        });
    }

    /* commands operating on string values */

    function testSet() {
        $this->assertTrue($this->redis->set('foo', 'bar'));
        $this->assertEquals('bar', $this->redis->get('foo'));
    }

    function testGet() {
        $this->redis->set('foo', 'bar');

        $this->assertEquals('bar', $this->redis->get('foo'));
        $this->assertNull($this->redis->get('fooDoesNotExist'));

        // should throw an exception when trying to do a GET on non-string types
        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
            $test->redis->pushTail('metavars', 'foo');
            $test->redis->get('metavars');
        });
    }

    function testExists() {
        $this->redis->set('foo', 'bar');

        $this->assertTrue($this->redis->exists('foo'));
        $this->assertFalse($this->redis->exists('key_does_not_exist'));
    }

    function testSetPreserve() {
        $multi = RC::getKeyValueArray();

        $this->assertTrue($this->redis->setPreserve('foo', 'bar'));
        $this->assertFalse($this->redis->setPreserve('foo', 'rab'));
        $this->assertEquals('bar', $this->redis->get('foo'));
    }

    function testMultipleSetAndGet() {
        $multi = RC::getKeyValueArray();

        // key=>value pairs via array instance
        $this->assertTrue($this->redis->setMultiple($multi));
        $multiRet = $this->redis->getMultiple(array_keys($multi));
        $this->assertEquals($multi, array_combine(array_keys($multi), array_values($multiRet)));

        // key=>value pairs via function arguments
        $this->assertTrue($this->redis->setMultiple('a', 1, 'b', 2, 'c', 3));
        $this->assertEquals(array(1, 2, 3), $this->redis->getMultiple('a', 'b', 'c'));
    }

    function testSetMultiplePreserve() {
        $multi    = RC::getKeyValueArray();
        $newpair  = array('hogehoge' => 'piyopiyo');
        $hijacked = array('foo' => 'baz', 'hoge' => 'fuga');

        // successful set
        $expectedResult = array_merge($multi, $newpair);
        $this->redis->setMultiple($multi);
        $this->assertTrue($this->redis->setMultiplePreserve($newpair));
        $this->assertEquals(
            array_values($expectedResult), 
            $this->redis->getMultiple(array_keys($expectedResult))
        );

        $this->redis->flushDatabase();

        // unsuccessful set
        $expectedResult = array_merge($multi, array('hogehoge' => null));
        $this->redis->setMultiple($multi);
        $this->assertFalse($this->redis->setMultiplePreserve(array_merge($newpair, $hijacked)));
        $this->assertEquals(
            array_values($expectedResult), 
            $this->redis->getMultiple(array_keys($expectedResult))
        );
    }

    function testGetSet() {
        $this->assertNull($this->redis->getSet('foo', 'bar'));
        $this->assertEquals('bar', $this->redis->getSet('foo', 'barbar'));
        $this->assertEquals('barbar', $this->redis->getSet('foo', 'baz'));
    }

    function testIncrementAndIncrementBy() {
        // test subsequent increment commands
        $this->assertEquals(1, $this->redis->increment('foo'));
        $this->assertEquals(2, $this->redis->increment('foo'));

        // test subsequent incrementBy commands
        $this->assertEquals(22, $this->redis->incrementBy('foo', 20));
        $this->assertEquals(10, $this->redis->incrementBy('foo', -12));
        $this->assertEquals(-100, $this->redis->incrementBy('foo', -110));
    }

    function testDecrementAndDecrementBy() {
        // test subsequent decrement commands
        $this->assertEquals(-1, $this->redis->decrement('foo'));
        $this->assertEquals(-2, $this->redis->decrement('foo'));

        // test subsequent decrementBy commands
        $this->assertEquals(-22, $this->redis->decrementBy('foo', 20));
        $this->assertEquals(-10, $this->redis->decrementBy('foo', -12));
        $this->assertEquals(100, $this->redis->decrementBy('foo', -110));
    }

    function testDelete() {
        $this->redis->set('foo', 'bar');
        $this->assertTrue($this->redis->delete('foo'));
        $this->assertFalse($this->redis->exists('foo'));
        $this->assertFalse($this->redis->delete('foo'));
    }

    function testType() {
        $this->assertEquals('none', $this->redis->type('fooDoesNotExist'));

        $this->redis->set('fooString', 'bar');
        $this->assertEquals('string', $this->redis->type('fooString'));

        $this->redis->pushTail('fooList', 'bar');
        $this->assertEquals('list', $this->redis->type('fooList'));

        $this->redis->setAdd('fooSet', 'bar');
        $this->assertEquals('set', $this->redis->type('fooSet'));

        $this->redis->zsetAdd('fooZSet', 'bar', 0);
        $this->assertEquals('zset', $this->redis->type('fooZSet'));
    }

    function testAppend() {
        $this->redis->set('foo', 'bar');
        $this->assertEquals(5, $this->redis->append('foo', '__'));
        $this->assertEquals(8, $this->redis->append('foo', 'bar'));
        $this->assertEquals('bar__bar', $this->redis->get('foo'));

        $this->assertEquals(4, $this->redis->append('hoge', 'piyo'));
        $this->assertEquals('piyo', $this->redis->get('hoge'));

        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
            $test->redis->pushTail('metavars', 'foo');
            $test->redis->append('metavars', 'bar');
        });
    }

    function testSubstr() {
        $this->redis->set('var', 'foobar');
        $this->assertEquals('foo', $this->redis->substr('var', 0, 2));
        $this->assertEquals('bar', $this->redis->substr('var', 3, 5));
        $this->assertEquals('bar', $this->redis->substr('var', -3, -1));
        $this->assertNull($this->redis->substr('var', 5, 0));

        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
            $test->redis->pushTail('metavars', 'foo');
            $test->redis->substr('metavars', 0, 3);
        });
    }

    /* commands operating on the key space */

    function testKeys() {
        $keyValsNs     = RC::getNamespacedKeyValueArray();
        $keyValsOthers = array('aaa' => 1, 'aba' => 2, 'aca' => 3);
        $allKeyVals    = array_merge($keyValsNs, $keyValsOthers);

        $this->redis->setMultiple($allKeyVals);

        $this->assertEquals(array(), $this->redis->keys('nokeys:*'));

        $keysFromRedis = $this->redis->keys('metavar:*');
        $this->assertEquals(array(), array_diff(array_keys($keyValsNs), $keysFromRedis));

        $keysFromRedis = $this->redis->keys('*');
        $this->assertEquals(array(), array_diff(array_keys($allKeyVals), $keysFromRedis));

        $keysFromRedis = $this->redis->keys('a?a');
        $this->assertEquals(array(), array_diff(array_keys($keyValsOthers), $keysFromRedis));
    }

    function testRandomKey() {
        $keyvals = RC::getKeyValueArray();

        $this->assertNull($this->redis->randomKey());

        $this->redis->setMultiple($keyvals);
        $this->assertTrue(in_array($this->redis->randomKey(), array_keys($keyvals)));
    }

{'fraction_non_alphanumeric': 0.1885078477746721, 'fraction_numerical': 0.0053751881315846056, 'mean_word_length': 3.10163139329806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 477, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3714116', 'n_tokens_mistral': 5732, 'n_tokens_neox': 5545, 'n_words': 920}