<?php

declare(strict_types=1);
namespace Tests\git2;
use git2\git;
use git2\git_buf;
use git2\LogicException;

final class BufTest extends GitTestCase
{

	public function testBuf(): void
	{
		$this->assertOK(git::buf_set($buf, 'foo'));
		$this->assertSame(3, $buf->size);
		$this->assertSame('foo', (string) $buf);
		git::buf_dispose($buf);
	}

	public function testReadPastBuffer(): void
	{
		$this->expectException(LogicException::class);
		git::buf_set($buf, 'foo');
		$buf->slice(0, 4);
	}

	public function testReadAheadBuffer(): void
	{
		$this->expectException(LogicException::class);
		git::buf_set($buf, 'foo');
		$buf->slice(-1, 1);
	}

	public function testReadEmptyBuffer(): void
	{
		git::buf_set($buf, '');
		$this->assertSame('', $buf->slice(0, 0));
	}

	public function testReadEndStart(): void
	{
		$this->expectException(LogicException::class);
		git::buf_set($buf, 'foo');
		$buf->slice(2, 1);
	}

	public function testIsBinary(): void
	{
		git::buf_set($ascii, 'foo');
		git::buf_set($binary, "f\0o");
		$this->assertFalse(git::buf_is_binary($ascii));
		$this->assertTrue(git::buf_is_binary($binary));
	}

	public function testContainsNul(): void
	{
		git::buf_set($ascii, 'foo');
		git::buf_set($binary, "f\0o");
		$this->assertFalse(git::buf_contains_nul($ascii));
		$this->assertTrue(git::buf_contains_nul($binary));
	}

	public function testGrow(): void
	{
		$buf = new git_buf;
		$this->assertSame(0, $buf->size);
		$this->assertSame(0, $buf->asize);
		$this->assertOK(git::buf_grow($buf, 3));
		$this->assertSame(0, $buf->size);
		$this->assertGreaterThanOrEqual(3, $buf->asize);
	}

}