<commit_before><?php
#session_start();
require_once dirname(__FILE__) . '/../util.php';

/**
 * PHPUnit test case for the util.php library
 *
 * @author  <NAME> <<EMAIL>>
 * @since   1.0.000
 */
class UtilityPHPTest extends PHPUnit_Framework_TestCase
{
    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 ) );
    }

    public function test_seems_utf8()
    {
        // Test a valid UTF-8 sequence
        $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
        $this->assertFalse( util::seems_utf8( "\xc3 this has \xe6\x9d some invalid utf8 \xe6" ) );

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

    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;' ) );
        $this->assertTrue( util::is_serialized( 'a:0:{}' ) );
        $this->assertTrue( util::is_serialized( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
    }

    public function test_fix_broken_serialization()
    {
        $expectedData = array(
            'Normal',
            'High-value Char: '.chr(231).'a-va?',   // High-value Char:  ça-va? [in ISO-8859-1]
        );

        $brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}';

        // Temporarily override error handling to ensure that this is, in fact, [still] a broken serialization.
        {
            $expectedError = array(
                'errorno' => 8,
                'errstr' => 'unserialize(): Error at offset 55 of 60 bytes'
            );

            $reportedError = array();
            set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$reportedError) {
                $reportedError = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
            });

            unserialize($brokenSerialization);
            $this->assertEquals($expectedError, array_intersect($expectedError, $reportedError));
            restore_error_handler();
        }

        $fixedSerialization = util::fix_broken_serialization($brokenSerialization);
        $unserializedData = unserialize($fixedSerialization);
        $this->assertEquals($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.');
        $this->assertEquals(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.');
    }

    public function test_is_https()<commit_msg>Skipped an unserialization assertion in HHVM.<commit_after><?php
#session_start();
require_once dirname(__FILE__) . '/../util.php';

/**
 * PHPUnit test case for the util.php library
 *
 * @author  <NAME> <<EMAIL>>
 * @since   1.0.000
 */
class UtilityPHPTest extends PHPUnit_Framework_TestCase
{
    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 ) );
    }

    public function test_seems_utf8()
    {
        // Test a valid UTF-8 sequence
        $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
        $this->assertFalse( util::seems_utf8( "\xc3 this has \xe6\x9d some invalid utf8 \xe6" ) );

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

    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;' ) );
        $this->assertTrue( util::is_serialized( 'a:0:{}' ) );
        $this->assertTrue( util::is_serialized( 'O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}' ) );
    }

    public function test_fix_broken_serialization()
    {
        $expectedData = array(
            'Normal',
            'High-value Char: '.chr(231).'a-va?',   // High-value Char:  ça-va? [in ISO-8859-1]
        );

        $brokenSerialization = 'a:2:{i:0;s:6:"Normal";i:1;s:23:"High-value Char: ▒a-va?";}';

        // Temporarily override error handling to ensure that this is, in fact, [still] a broken serialization.
        {
            $expectedError = array(
                'errno' => 8,
                'errstr' => 'unserialize(): Error at offset 55 of 60 bytes'
            );

            $reportedError = array();
            set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$reportedError) {
                $reportedError = compact('errno', 'errstr');
            });

            unserialize($brokenSerialization);

            $this->assertEquals($expectedError['errno'], $reportedError['errno']);
            // Because HHVM's unserialize() error message does not contain enough info to properly test.
            if (!defined('HHVM_VERSION')) {
                $this->assertEquals($expectedError['errstr'], $reportedError['errstr']);
            }
            restore_error_handler();
        }

        $fixedSerialization = util::fix_broken_serialization($brokenSerialization);
        $unserializedData = unserialize($fixedSerialization);
        $this->assertEquals($expectedData[0], $unserializedData[0], 'Did not properly fix the broken serialized data.');

        $this->assertEquals(substr($expectedData[1], 0, 10), substr($unserializedData[1], 0, 10), 'Did not properly fix the broken serialized data.');
    }

    public function test_is_https()
{'fraction_non_alphanumeric': 0.19664008670743982, 'fraction_numerical': 0.04010219091120229, 'mean_word_length': 3.063541994337842, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 116, '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': '2085587', 'n_tokens_mistral': 4585, 'n_tokens_neox': 4317, 'n_words': 839}