<?php

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

final class OIDTest extends GitTestCase
{

	public function testToStrS(): void
	{
		$oid = new git_oid;
		$this->assertSame('0000000000000000000000000000000000000000', git::oid_tostr_s($oid));
	}

	public function testCmp(): void
	{
		$this->assertOK(git::oid_fromstr($a, '0000000000000000000000000000000000000000'));
		$this->assertOK(git::oid_fromstr($b, '0000000000000000000000000000000000000001'));
		$this->assertSame(-1, git::oid_cmp($a, $b));
		$this->assertSame(1, git::oid_cmp($b, $a));
		$this->assertSame(0, git::oid_cmp($a, $a));
	}

	public function testFromStr(): void
	{
		$this->assertOK(git::oid_fromstr($oid, '4630303030303030303030303030303030303046'));
		$this->assertSame('F000000000000000000F', $oid->id);
	}

	public function testFromStrN(): void
	{
		$this->assertOK(git::oid_fromstrn($oid, '4639', 4));
		$this->assertSame('4639000000000000000000000000000000000000', git::oid_tostr_s($oid));
	}

	public function testFromRaw(): void
	{
		$this->assertOK(git::oid_fromraw($oid, 'F000000000000000000F'));
		$this->assertSame('4630303030303030303030303030303030303046', git::oid_tostr_s($oid));
	}

	public function testCpy(): void
	{
		$this->assertOK(git::oid_fromstr($oid, '4630303030303030303030303030303030303046'));
		$this->assertOK(git::oid_cpy($cpy, $oid));
		$this->assertSame('4630303030303030303030303030303030303046', git::oid_tostr_s($cpy));
	}

	public function testEqual(): void
	{
		$this->assertOK(git::oid_fromstr($a, '4630303030303030303030303030303030303046'));
		$this->assertOK(git::oid_fromstr($b, '4630303030303030303030303030303030303046'));
		$this->assertOK(git::oid_fromstr($c, '0000000000000000000000000000000000000000'));
		$this->assertTrue(git::oid_equal($a, $b));
		$this->assertFalse(git::oid_equal($a, $c));
	}

	public function testFmt(): void
	{
		$this->assertOK(git::oid_fromstr($oid, '4630303030303030303030303030303030303046'));
		$this->assertOK(git::oid_fmt($fmt, $oid));
		$this->assertSame('4630303030303030303030303030303030303046', $fmt);
	}

	public function testIsZero(): void
	{
		$this->assertOK(git::oid_fromstr($n, '4630303030303030303030303030303030303046'));
		$this->assertFalse(git::oid_is_zero($n));
		$this->assertOK(git::oid_fromstr($z, '0000000000000000000000000000000000000000'));
		$this->assertTrue(git::oid_is_zero($z));
	}

	public function testNcmp(): void
	{
		$this->assertOK(git::oid_fromstr($a, '0001000000000000000000000000000000000000'));
		$this->assertOK(git::oid_fromstr($b, '0000000000000000000000000000000000000000'));
		$this->assertSame(0, git::oid_ncmp($a, $b, 3));
		$this->assertSame(1, git::oid_ncmp($a, $b, 4));
	}

	public function testNfmt(): void
	{
		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertOK(git::oid_nfmt($out, 4, $oid));
		$this->assertSame('a678', $out);

		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertOK(git::oid_nfmt($out, git_oid::HEXSZ, $oid));
		$this->assertSame('a67865f88d959014e2618f4fc15d135ddb1f87f5', $out);

		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertOK(git::oid_nfmt($out, git_oid::HEXSZ + 3, $oid));
		$this->assertSame("a67865f88d959014e2618f4fc15d135ddb1f87f5\0\0\0", $out);
	}

	public function testPathfmt(): void
	{
		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertOK(git::oid_pathfmt($out, $oid));
		$this->assertSame('a6/7865f88d959014e2618f4fc15d135ddb1f87f5', $out);
	}

	public function testStrcmp(): void
	{
		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertSame(0, git::oid_strcmp($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5'));
		$this->assertSame(-1, git::oid_strcmp($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f6'));
		$this->assertSame(1, git::oid_strcmp($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f4'));
	}

	public function testStreq(): void
	{
		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertTrue(git::oid_streq($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5'));
		$this->assertFalse(git::oid_streq($oid, '4630303030303030303030303030303030303046'));
	}

	public function testToStr(): void
	{
		git::oid_fromstr($oid, 'a67865f88d959014e2618f4fc15d135ddb1f87f5');
		$this->assertSame('a678', git::oid_tostr($out, 4, $oid));
		$this->assertSame('a678', $out);
		$this->assertNull(git::oid_tostr($out, 0, $oid));
		$this->assertSame('a678', $out);
	}

}