11c6adce8SGreg Roach<?php 21c6adce8SGreg Roach 31c6adce8SGreg Roach/** 41c6adce8SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 61c6adce8SGreg Roach * This program is free software: you can redistribute it and/or modify 71c6adce8SGreg Roach * it under the terms of the GNU General Public License as published by 81c6adce8SGreg Roach * the Free Software Foundation, either version 3 of the License, or 91c6adce8SGreg Roach * (at your option) any later version. 101c6adce8SGreg Roach * This program is distributed in the hope that it will be useful, 111c6adce8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121c6adce8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131c6adce8SGreg Roach * GNU General Public License for more details. 141c6adce8SGreg Roach * You should have received a copy of the GNU General Public License 151c6adce8SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 161c6adce8SGreg Roach */ 171c6adce8SGreg Roach 181c6adce8SGreg Roachdeclare(strict_types=1); 191c6adce8SGreg Roach 201c6adce8SGreg Roachnamespace Fisharebest\Webtrees\Tests\Encodings; 211c6adce8SGreg Roach 221c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF16BE; 231c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF8; 241c6adce8SGreg Roachuse PHPUnit\Framework\TestCase; 251c6adce8SGreg Roach 261c6adce8SGreg Roachuse function chr; 271c6adce8SGreg Roachuse function dechex; 281c6adce8SGreg Roachuse function iconv; 291c6adce8SGreg Roachuse function intdiv; 301c6adce8SGreg Roachuse function range; 311c6adce8SGreg Roach 321c6adce8SGreg Roach/** 331c6adce8SGreg Roach * Tests for class UTF16BE. 341c6adce8SGreg Roach */ 351c6adce8SGreg Roachclass UTF16BETest extends TestCase 361c6adce8SGreg Roach{ 371c6adce8SGreg Roach /** 381c6adce8SGreg Roach * @covers \Fisharebest\Webtrees\Encodings\AbstractEncoding 391c6adce8SGreg Roach * @covers \Fisharebest\Webtrees\Encodings\UTF16BE 401c6adce8SGreg Roach * 411c6adce8SGreg Roach * @return void 421c6adce8SGreg Roach */ 431c6adce8SGreg Roach public function testToUtf8(): void 441c6adce8SGreg Roach { 451c6adce8SGreg Roach $encoding = new UTF16BE(); 461c6adce8SGreg Roach 471c6adce8SGreg Roach foreach (range(0, 0x7F) as $code) { 481c6adce8SGreg Roach $char = chr(intdiv($code, 256)) . chr($code % 256); 491c6adce8SGreg Roach $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); 501c6adce8SGreg Roach $actual = $encoding->toUtf8($char); 511c6adce8SGreg Roach 52f01ab4acSGreg Roach static::assertSame($expected, $actual, 'U+' . dechex($code)); 531c6adce8SGreg Roach } 541c6adce8SGreg Roach 551c6adce8SGreg Roach foreach (range(0x80, 0xFF) as $code) { 561c6adce8SGreg Roach $char = chr(intdiv($code, 256)) . chr($code % 256); 571c6adce8SGreg Roach $actual = $encoding->toUtf8($char); 581c6adce8SGreg Roach 59f01ab4acSGreg Roach static::assertSame(UTF8::REPLACEMENT_CHARACTER, $actual, 'U+' . dechex($code)); 601c6adce8SGreg Roach } 611c6adce8SGreg Roach 621c6adce8SGreg Roach foreach (range(0x100, 0xD7FF) as $code) { 631c6adce8SGreg Roach $char = chr(intdiv($code, 256)) . chr($code % 256); 641c6adce8SGreg Roach $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); 651c6adce8SGreg Roach $actual = $encoding->toUtf8($char); 661c6adce8SGreg Roach 67f01ab4acSGreg Roach static::assertSame($expected, $actual, 'U+' . dechex($code)); 681c6adce8SGreg Roach } 691c6adce8SGreg Roach 701c6adce8SGreg Roach foreach (range(0xD800, 0xDFFF) as $code) { 711c6adce8SGreg Roach $char = chr(intdiv($code, 256)) . chr($code % 256); 721c6adce8SGreg Roach $actual = $encoding->toUtf8($char); 731c6adce8SGreg Roach 74f01ab4acSGreg Roach static::assertSame(UTF8::REPLACEMENT_CHARACTER, $actual, 'U+' . dechex($code)); 751c6adce8SGreg Roach } 761c6adce8SGreg Roach 771c6adce8SGreg Roach foreach (range(0xE000, 0xFFFF) as $code) { 781c6adce8SGreg Roach $char = chr(intdiv($code, 256)) . chr($code % 256); 791c6adce8SGreg Roach $expected = iconv(UTF16BE::NAME, UTF8::NAME, $char); 801c6adce8SGreg Roach $actual = $encoding->toUtf8($char); 811c6adce8SGreg Roach 82f01ab4acSGreg Roach static::assertSame($expected, $actual, 'U+' . dechex($code)); 831c6adce8SGreg Roach } 841c6adce8SGreg Roach } 851c6adce8SGreg Roach} 86