<commit_before><?php

date_default_timezone_set('UTC');

require_once dirname(__FILE__) . '/../util.php';

class VarDumpExperiment
{
    public $public = 1;
    protected $protected = 2;
    private $private = 3;
    public $data;

    public function __construct()
    {
        $this->data = (object)array('a', 'b', 'c');
    }
}

/**
 * PHPUnit test case for the util.php library
 *
 * @author  <NAME> <<EMAIL>>
 * @since   1.0.000
 */
class UtilityPHPTest extends PHPUnit_Framework_TestCase
{
    /**
     * Allows for the testing of private and protected methods.
     *
     * @param $name
     * @return \ReflectionMethod
     */
    protected static function getMethod($name)
    {
        $class = new \ReflectionClass('util');
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method;
    }

    public function test_array_get()
    {
        $_GET = array();
        $_GET['abc'] = 'def';
        $_GET['nested'] = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3' );

        // Looks for $array['abc']
        $this->assertEquals( 'def', util::array_get( $_GET['abc'] ) );

        // Looks for $array['nested']['key2']
        $this->assertEquals( 'val2', util::array_get( $_GET['nested']['key2'] ) );

        // Looks for $array['doesnotexist']
        $this->assertEquals( 'defaultval', util::array_get( $_GET['doesnotexist'], 'defaultval' ) );
    }

    public function test_slugify()
    {
        $this->assertEquals( 'a-simple-title', util::slugify( 'A simple title' ) );
        $this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash' ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251' ) );
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );

        $this->assertEquals( 'a-simple-title', util::slugify( 'A simple title', '-' ) );
        $this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash', '-' ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251', '-' ) );
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );

        $this->assertEquals( 'a_simple_title', util::slugify( 'A simple title', '_' ) );
        $this->assertEquals( 'this_post_it_has_a_dash', util::slugify( 'This post -- it has a dash', '_' ) );
        $this->assertEquals( '123_1251251', util::slugify( '123----1251251', '_' ) );
        $this->assertEquals( 'one23_1251251', util::slugify( '123----1251251', '_', true ) );

        // Blank seperator test
        $this->assertEquals( 'asimpletitle', util::slugify( 'A simple title', '' ) );
        $this->assertEquals( 'thispostithasadash', util::slugify( 'This post -- it has a dash', '' ) );
        $this->assertEquals( '1231251251', util::slugify( '123----1251251', '' ) );
        $this->assertEquals( 'one231251251', util::slugify( '123----1251251', '', true ) );

        // Test old parameter ordering for backwards compatability
        error_reporting(E_ALL ^ E_USER_DEPRECATED);
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', true ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251', false ) );
    }

    public function test_seems_utf8()
    {
        // Test a valid UTF-8 sequence: "ÜTF-8 Fµñ".
        $validUTF8 = "\xC3\x9CTF-8 F\xC2\xB5\xC3\xB1";
        $this->assertTrue( util::seems_utf8( $validUTF8 ) );

        $this->assertTrue( util::seems_utf8( "\xEF\xBF\xBD this has \xEF\xBF\xBD\xEF\xBF\xBD some invalid utf8 \xEF\xBF\xBD" ) );

        // Test invalid UTF-8 sequences
        $invalidUTF8 = "\xc3 this has \xe6\x9d some invalid utf8 \xe6";
        $this->assertFalse( util::seems_utf8( $invalidUTF8 ) );

        // And test some plain ASCII
        $this->assertTrue( util::seems_utf8( 'The quick brown fox jumps over the lazy dog' ) );

        // Test an invalid non-UTF-8 string.
        if (function_exists('mb_convert_encoding')) {
            mb_internal_encoding('UTF-8');
            // Converts the 'ç' UTF-8 character to UCS-2LE
            $utf8Char = pack('n', 50087);
            $ucsChar = mb_convert_encoding($utf8Char, 'UCS-2LE', 'UTF-8');

            // Ensure that PHP's internal encoding system isn't malconfigured.
            $this->assertEquals( $utf8Char, 'ç', 'This PHP system\'s internal character set is not properly set as UTF-8.' );
            $this->assertEquals( $utf8Char, pack('n', 50087), 'Something is wrong with your ICU unicode library.' );

            // Test for not UTF-8.
            $this->assertFalse( util::seems_utf8( $ucsChar) );

            // Test the worker method.
            $method = self::getMethod('seemsUtf8Regex');
            $this->assertFalse($method->invoke(null, $invalidUTF8), 'util::seemsUtf8Regex did not properly detect invalid UTF-8.');
            $this->assertTrue($method->invoke(null, $validUTF8), 'util::seemsUtf8Regex did not properly detect valid UTF-8.');
        }
    }

    public function test_size_format()
    {
        $size = util::size_format( 512, 0 );
        $this->assertEquals( '512 B', $size );

        $size = util::size_format( 2048, 1 );
        $this->assertEquals( '2.0 KiB', $size );

        $size = util::size_format( 25151251, 2 );
        $this->assertEquals( '23.99 MiB', $size );

        $size = util::size_format( 19971597926, 2 );
        $this->assertEquals( '18.60 GiB', $size );

        $size = util::size_format( 2748779069440, 1 );
        $this->assertEquals( '2.5 TiB', $size );

        $size = util::size_format( 2.81475e15, 1 );
        $this->assertEquals( '2.5 PiB', $size );

        $size = util::size_format( 2.81475e19, 1 );
        $this->assertEquals( '25000.0 PiB', $size );
    }

    public function test_maybe_serialize()
    {
        $obj = new stdClass();
        $obj->prop1 = 'Hello';
        $obj->prop2 = 'World';

        $this->assertEquals( 'This is a string', util::maybe_serialize( 'This is a string' ) );
        $this->assertEquals( 5.81, util::maybe_serialize( 5.81 ) );
        $this->assertEquals( 'a:0:{}', util::maybe_serialize( array() ) );
        $this->assertEquals( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}', util::maybe_serialize( $obj ) );
        $this->assertEquals( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}', util::maybe_serialize( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ) ) );
    }

    public function test_maybe_unserialize()
    {
        $obj = new stdClass();
        $obj->prop1 = 'Hello';
        $obj->prop2 = 'World';

        $this->assertEquals( 'This is a string', util::maybe_unserialize( 'This is a string' ) );
        $this->assertEquals( 5.81, util::maybe_unserialize( 5.81 ) );
        $this->assertEquals( array(), util::maybe_unserialize( 'a:0:{}' ) );
        $this->assertEquals( $obj, util::maybe_unserialize( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
        $this->assertEquals( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ), util::maybe_unserialize( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}' ) );
    }

    public function test_is_serialized()
    {
        $this->assertFalse( util::is_serialized( 's:4:"test;' ) );
        $this->assertFalse( util::is_serialized( 'a:0:{}!' ) );
        $this->assertFalse( util::is_serialized( 'a:0' ) );
        $this->assertFalse( util::is_serialized( 'This is a string' ) );
        $this->assertFalse( util::is_serialized( 'a string' ) );
        $this->assertFalse( util::is_serialized( 'z:0;' ) );
        $this->assertTrue( util::is_serialized( 'N;' ) );
        $this->assertTrue( util::is_serialized( 'b:1;' ) );<commit_msg>Achieved 100% test code coverage for is_serialized().<commit_after><?php

date_default_timezone_set('UTC');

require_once dirname(__FILE__) . '/../util.php';

class VarDumpExperiment
{
    public $public = 1;
    protected $protected = 2;
    private $private = 3;
    public $data;

    public function __construct()
    {
        $this->data = (object)array('a', 'b', 'c');
    }
}

/**
 * PHPUnit test case for the util.php library
 *
 * @author  <NAME> <<EMAIL>>
 * @since   1.0.000
 */
class UtilityPHPTest extends PHPUnit_Framework_TestCase
{
    /**
     * Allows for the testing of private and protected methods.
     *
     * @param $name
     * @return \ReflectionMethod
     */
    protected static function getMethod($name)
    {
        $class = new \ReflectionClass('util');
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method;
    }

    public function test_array_get()
    {
        $_GET = array();
        $_GET['abc'] = 'def';
        $_GET['nested'] = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3' );

        // Looks for $array['abc']
        $this->assertEquals( 'def', util::array_get( $_GET['abc'] ) );

        // Looks for $array['nested']['key2']
        $this->assertEquals( 'val2', util::array_get( $_GET['nested']['key2'] ) );

        // Looks for $array['doesnotexist']
        $this->assertEquals( 'defaultval', util::array_get( $_GET['doesnotexist'], 'defaultval' ) );
    }

    public function test_slugify()
    {
        $this->assertEquals( 'a-simple-title', util::slugify( 'A simple title' ) );
        $this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash' ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251' ) );
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );

        $this->assertEquals( 'a-simple-title', util::slugify( 'A simple title', '-' ) );
        $this->assertEquals( 'this-post-it-has-a-dash', util::slugify( 'This post -- it has a dash', '-' ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251', '-' ) );
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', '-', true ) );

        $this->assertEquals( 'a_simple_title', util::slugify( 'A simple title', '_' ) );
        $this->assertEquals( 'this_post_it_has_a_dash', util::slugify( 'This post -- it has a dash', '_' ) );
        $this->assertEquals( '123_1251251', util::slugify( '123----1251251', '_' ) );
        $this->assertEquals( 'one23_1251251', util::slugify( '123----1251251', '_', true ) );

        // Blank seperator test
        $this->assertEquals( 'asimpletitle', util::slugify( 'A simple title', '' ) );
        $this->assertEquals( 'thispostithasadash', util::slugify( 'This post -- it has a dash', '' ) );
        $this->assertEquals( '1231251251', util::slugify( '123----1251251', '' ) );
        $this->assertEquals( 'one231251251', util::slugify( '123----1251251', '', true ) );

        // Test old parameter ordering for backwards compatability
        error_reporting(E_ALL ^ E_USER_DEPRECATED);
        $this->assertEquals( 'one23-1251251', util::slugify( '123----1251251', true ) );
        $this->assertEquals( '123-1251251', util::slugify( '123----1251251', false ) );
    }

    public function test_seems_utf8()
    {
        // Test a valid UTF-8 sequence: "ÜTF-8 Fµñ".
        $validUTF8 = "\xC3\x9CTF-8 F\xC2\xB5\xC3\xB1";
        $this->assertTrue( util::seems_utf8( $validUTF8 ) );

        $this->assertTrue( util::seems_utf8( "\xEF\xBF\xBD this has \xEF\xBF\xBD\xEF\xBF\xBD some invalid utf8 \xEF\xBF\xBD" ) );

        // Test invalid UTF-8 sequences
        $invalidUTF8 = "\xc3 this has \xe6\x9d some invalid utf8 \xe6";
        $this->assertFalse( util::seems_utf8( $invalidUTF8 ) );

        // And test some plain ASCII
        $this->assertTrue( util::seems_utf8( 'The quick brown fox jumps over the lazy dog' ) );

        // Test an invalid non-UTF-8 string.
        if (function_exists('mb_convert_encoding')) {
            mb_internal_encoding('UTF-8');
            // Converts the 'ç' UTF-8 character to UCS-2LE
            $utf8Char = pack('n', 50087);
            $ucsChar = mb_convert_encoding($utf8Char, 'UCS-2LE', 'UTF-8');

            // Ensure that PHP's internal encoding system isn't malconfigured.
            $this->assertEquals( $utf8Char, 'ç', 'This PHP system\'s internal character set is not properly set as UTF-8.' );
            $this->assertEquals( $utf8Char, pack('n', 50087), 'Something is wrong with your ICU unicode library.' );

            // Test for not UTF-8.
            $this->assertFalse( util::seems_utf8( $ucsChar) );

            // Test the worker method.
            $method = self::getMethod('seemsUtf8Regex');
            $this->assertFalse($method->invoke(null, $invalidUTF8), 'util::seemsUtf8Regex did not properly detect invalid UTF-8.');
            $this->assertTrue($method->invoke(null, $validUTF8), 'util::seemsUtf8Regex did not properly detect valid UTF-8.');
        }
    }

    public function test_size_format()
    {
        $size = util::size_format( 512, 0 );
        $this->assertEquals( '512 B', $size );

        $size = util::size_format( 2048, 1 );
        $this->assertEquals( '2.0 KiB', $size );

        $size = util::size_format( 25151251, 2 );
        $this->assertEquals( '23.99 MiB', $size );

        $size = util::size_format( 19971597926, 2 );
        $this->assertEquals( '18.60 GiB', $size );

        $size = util::size_format( 2748779069440, 1 );
        $this->assertEquals( '2.5 TiB', $size );

        $size = util::size_format( 2.81475e15, 1 );
        $this->assertEquals( '2.5 PiB', $size );

        $size = util::size_format( 2.81475e19, 1 );
        $this->assertEquals( '25000.0 PiB', $size );
    }

    public function test_maybe_serialize()
    {
        $obj = new stdClass();
        $obj->prop1 = 'Hello';
        $obj->prop2 = 'World';

        $this->assertEquals( 'This is a string', util::maybe_serialize( 'This is a string' ) );
        $this->assertEquals( 5.81, util::maybe_serialize( 5.81 ) );
        $this->assertEquals( 'a:0:{}', util::maybe_serialize( array() ) );
        $this->assertEquals( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}', util::maybe_serialize( $obj ) );
        $this->assertEquals( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}', util::maybe_serialize( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ) ) );
    }

    public function test_maybe_unserialize()
    {
        $obj = new stdClass();
        $obj->prop1 = 'Hello';
        $obj->prop2 = 'World';

        $this->assertEquals( 'This is a string', util::maybe_unserialize( 'This is a string' ) );
        $this->assertEquals( 5.81, util::maybe_unserialize( 5.81 ) );
        $this->assertEquals( array(), util::maybe_unserialize( 'a:0:{}' ) );
        $this->assertEquals( $obj, util::maybe_unserialize( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
        $this->assertEquals( array( 'test', 'blah', 'hello' => 'world', 'array' => $obj ), util::maybe_unserialize( 'a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}' ) );
    }

    public function test_is_serialized()
    {
        $this->assertFalse( util::is_serialized(1) );
        $this->assertFalse( util::is_serialized(null) );
        $this->assertFalse( util::is_serialized( 's:4:"test;' ) );
        $this->assertFalse( util::is_serialized( 'a:0:{}!' ) );
        $this->assertFalse( util::is_serialized( 'a:0' ) );
        $this->assertFalse( util::is_serialized( 'This is a string' ) );
        $this->assertFalse( util::is_serialized( 'a string' ) );
        $this->assertFalse( util::is_serialized( 'z:0;' ) );
        $this->assertTrue( util::is_serialized( 'N;' ) );
        $this->assertTrue( util::is_serialized( 'b:1;' ) );
{'fraction_non_alphanumeric': 0.19618325355877417, 'fraction_numerical': 0.052837695033256665, 'mean_word_length': 2.96256157635468, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 153, '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': '7086504', 'n_tokens_mistral': 5855, 'n_tokens_neox': 5435, 'n_words': 1098}