<commit_before><?php

/*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Tests "filesystem functions" as defined by
* http://www.php.net/manual/en/ref.filesystem.php
*/
class FilesystemTest extends PHPUnit_Framework_TestCase
{
    protected $file;
    protected $filesize;
    protected $sha256sum;

    public function setUp()
    {
        if (!isset($_SERVER['FILE'])) {
            $this->markTestSkipped(
                'You must provide some environment variables'
            );
        }
        $this->file = $_SERVER['FILE'];
        $this->filesize = $_SERVER['FILESIZE'];
        $this->sha256sum = $_SERVER['SHA256SUM'];
    }

    public function tearDown()
    {
        if ($this->filesize && $this->file) {
            exec(sprintf(
                'truncate --size %s %s',
                escapeshellarg($this->filesize),
                escapeshellarg($this->file)
            ));
        }
    }

    public function testFilesize()
    {
        $this->assertSame(
            $this->filesize,
            sprintf('%u', filesize($this->file))
        );
    }

    public function testHashFile()
    {
        $this->assertSame($this->sha256sum, hash_file('sha256', $this->file));
    }

    public function testFopenReadFeofFreadFclose()
    {
        $hash = hash_init('sha256');
        $fp = fopen($this->file, 'rb');
        while (!feof($fp)) {
            hash_update($hash, fread($fp, 4096));
        }
        fclose($fp);
        $this->assertSame($this->sha256sum, hash_final($hash));
    }

    /**
    * @depends testHashFile
    * @depends testFopenReadFeofFreadFclose
    */
    public function testFopenAppendFwriteFclose()
    {
        $data = str_repeat('ABC', 1024 * 1024);

        $hash = $this->getOpenHash($this->file);
        hash_update($hash, $data);
        $expected = hash_final($hash);

        $fp = fopen($this->file, 'ab');
        fwrite($fp, $data);
        fclose($fp);

        $this->assertSame($expected, hash_file('sha256', $this->file));
    }

    protected function getOpenHash($filename, $algo = 'sha256')
    {
        $hash = hash_init($algo);<commit_msg>Add test case for file_put_contents().<commit_after><?php

/*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Tests "filesystem functions" as defined by
* http://www.php.net/manual/en/ref.filesystem.php
*/
class FilesystemTest extends PHPUnit_Framework_TestCase
{
    protected $file;
    protected $filesize;
    protected $sha256sum;

    public function setUp()
    {
        if (!isset($_SERVER['FILE'])) {
            $this->markTestSkipped(
                'You must provide some environment variables'
            );
        }
        $this->file = $_SERVER['FILE'];
        $this->filesize = $_SERVER['FILESIZE'];
        $this->sha256sum = $_SERVER['SHA256SUM'];
    }

    public function tearDown()
    {
        if ($this->filesize && $this->file) {
            exec(sprintf(
                'truncate --size %s %s',
                escapeshellarg($this->filesize),
                escapeshellarg($this->file)
            ));
        }
    }

    public function testFilesize()
    {
        $this->assertSame(
            $this->filesize,
            sprintf('%u', filesize($this->file))
        );
    }

    public function testHashFile()
    {
        $this->assertSame($this->sha256sum, hash_file('sha256', $this->file));
    }

    public function testFopenReadFeofFreadFclose()
    {
        $hash = hash_init('sha256');
        $fp = fopen($this->file, 'rb');
        while (!feof($fp)) {
            hash_update($hash, fread($fp, 4096));
        }
        fclose($fp);
        $this->assertSame($this->sha256sum, hash_final($hash));
    }

    /**
    * @depends testHashFile
    * @depends testFopenReadFeofFreadFclose
    */
    public function testFopenAppendFwriteFclose()
    {
        $data = str_repeat('ABC', 1024 * 1024);

        $hash = $this->getOpenHash($this->file);
        hash_update($hash, $data);
        $expected = hash_final($hash);

        $fp = fopen($this->file, 'ab');
        fwrite($fp, $data);
        fclose($fp);

        $this->assertSame($expected, hash_file('sha256', $this->file));
    }

    /**
    * @depends testHashFile
    */
    public function testFopenFilePutContents()
    {
        $fp = fopen($this->file, 'rb');
        $filename = $this->getTempfile();
        file_put_contents($filename, $fp);
        $actual = hash_file('sha256', $filename);
        unlink($filename);
        $this->assertSame($this->sha256sum, $actual);
    }

    protected function getTempfile()
    {
        return tempnam(__DIR__ . '/..', 'data-php-large-file-test');
    }

    protected function getOpenHash($filename, $algo = 'sha256')
    {
        $hash = hash_init($algo);
{'fraction_non_alphanumeric': 0.1541708542713568, 'fraction_numerical': 0.016884422110552764, 'mean_word_length': 2.2522875816993464, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1970688', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1511, 'n_words': 293}