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