. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Tests\Encodings; use Fisharebest\Webtrees\Encodings\UTF16BE; use Fisharebest\Webtrees\Encodings\UTF8; use PHPUnit\Framework\TestCase; use function chr; use function dechex; use function iconv; use function intdiv; use function range; /** * Tests for class UTF16BE. */ class UTF16BETest extends TestCase { /** * @covers \Fisharebest\Webtrees\Encodings\AbstractEncoding * @covers \Fisharebest\Webtrees\Encodings\UTF16BE * * @return void */ public function testToUtf8(): void { $encoding = new UTF16BE(); foreach (range(0, 0x7F) as $code) { $char = chr(intdiv($code, 256)) . chr($code % 256); $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); $actual = $encoding->toUtf8($char); static::assertSame($expected, $actual, 'U+' . dechex($code)); } foreach (range(0x80, 0xFF) as $code) { $char = chr(intdiv($code, 256)) . chr($code % 256); $actual = $encoding->toUtf8($char); static::assertSame(UTF8::REPLACEMENT_CHARACTER, $actual, 'U+' . dechex($code)); } foreach (range(0x100, 0xD7FF) as $code) { $char = chr(intdiv($code, 256)) . chr($code % 256); $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); $actual = $encoding->toUtf8($char); static::assertSame($expected, $actual, 'U+' . dechex($code)); } foreach (range(0xD800, 0xDFFF) as $code) { $char = chr(intdiv($code, 256)) . chr($code % 256); $actual = $encoding->toUtf8($char); static::assertSame(UTF8::REPLACEMENT_CHARACTER, $actual, 'U+' . dechex($code)); } foreach (range(0xE000, 0xFFFF) as $code) { $char = chr(intdiv($code, 256)) . chr($code % 256); $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); $actual = $encoding->toUtf8($char); static::assertSame($expected, $actual, 'U+' . dechex($code)); } } }