A set of dependency-free basic internationalization tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
102 KiB

<?php
/** @license MIT
* Copyright 2018 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace MensBeam\Intl\TestCase\Encoding;
use MensBeam\Intl\Encoding\ShiftJIS;
use MensBeam\Intl\Encoding\Coder;
use MensBeam\Intl\Encoding\EncoderException;
class TestShiftJIS extends \MensBeam\Intl\Test\CoderDecoderTest {
protected $testedClass = ShiftJIS::class;
/*
Char 0 U+007A (1 byte) Offset 0
Char 1 U+FF96 (1 byte) Offset 1
Char 2 U+3088 (2 bytes) Offset 2
Char 3 U+FF0D (2 bytes) Offset 4
Char 4 U+005C (1 byte) Offset 6
Char 5 U+FF9B (1 byte) Offset 7
Char 6 U+E000 (2 bytes) Offset 8
End of string at char 7, offset 10
*/
protected $seekString = "7A D6 82E6 817C 5C DB F040";
protected $seekCodes = [0x7A, 0xFF96, 0x3088, 0xFF0D, 0x5C, 0xFF9B, 0xE000];
protected $seekOffsets = [0, 1, 2, 4, 6, 7, 8, 10];
/* This string contains an invalid character sequence sandwiched between two null characters */
protected $brokenChar = "00 FF 00";
/* This string conatins the ASCII characters "A" and "Z" followed by two arbitrary non-ASCII characters, followed by the two ASCII characters "0" and "9" */
protected $spanString = "41 5A D6 82E6 30 39";
public function provideCodePoints() {
return [
'U+0064 (HTML)' => [false, 0x64, "64"],
'U+0064 (fatal)' => [true, 0x64, "64"],
'U+00A5 (HTML)' => [false, 0xA5, "5C"],
'U+00A5 (fatal)' => [true, 0xA5, "5C"],
'U+203E (HTML)' => [false, 0x203E, "7E"],
'U+203E (fatal)' => [true, 0x203E, "7E"],
'U+3088 (HTML)' => [false, 0x3088, "82 E6"],
'U+3088 (fatal)' => [true, 0x3088, "82 E6"],
'U+FF96 (HTML)' => [false, 0xFF96, "D6"],
'U+FF96 (fatal)' => [true, 0xFF96, "D6"],
'U+2212 (HTML)' => [false, 0x2212, "81 7C"],
'U+2212 (fatal)' => [true, 0x2212, "81 7C"],
'U+00E6 (HTML)' => [false, 0xE6, bin2hex("&#230;")],
'U+00E6 (fatal)' => [true, 0xE6, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'U+FFE2 (HTML)' => [false, 0xFFE2, "81 CA"],
'U+FFE2 (fatal)' => [true, 0xFFE2, "81 CA"],
'U+2116 (HTML)' => [false, 0x2116, "87 82"],
'U+2116 (fatal)' => [true, 0x2116, "87 82"],
'U+E000 (HTML)' => [false, 0xE000, bin2hex("&#57344;")],
'U+E000 (fatal)' => [true, 0xE000, new EncoderException("", Coder::E_UNAVAILABLE_CODE_POINT)],
'-1 (HTML)' => [false, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'-1 (fatal)' => [true, -1, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (HTML)' => [false, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
'0x110000 (fatal)' => [true, 0x110000, new EncoderException("", Coder::E_INVALID_CODE_POINT)],
];
}
public function provideStrings() {
return [
'empty string' => ["", []],
'sanity check' => ["40", [64]],
'invalid byte' => ["FF", [65533]],
'former ASCII deviations' => ["5C 7E", [92, 126]],
'JIS X 0201 range' => ["A1 DF", [65377, 65439]],
'EUDC range' => ["F040 F9FC", [57344, 59223]],
'JIS X 0208 assigned range' => ["8140 FC4B", [12288, 40657]],
'JIS X 0208 total range' => ["8140 FCFC", [12288, 65533]],
'JIS X 0208 truncated character 1' => ["81", [65533]],
'JIS X 0208 truncated character 2' => ["81 20", [65533, 32]],
'JIS X 0208 truncated character 3' => ["81 FF", [65533]],
];
}
/**
* @dataProvider provideCodePoints
* @covers MensBeam\Intl\Encoding\Encoder
* @covers MensBeam\Intl\Encoding\ShiftJIS::encode
4 years ago
* @covers MensBeam\Intl\Encoding\ShiftJIS::errEnc
4 years ago
*/
public function testEncodeCodePoints(bool $fatal, $input, $exp) {
return parent::testEncodeCodePoints($fatal, $input, $exp);
}
/**
* @dataProvider provideCodePoints
* @covers MensBeam\Intl\Encoding\ShiftJIS::encode
* @covers MensBeam\Intl\Encoding\ShiftJIS::errEnc
*/
public function testEncodeCodePointsStatically(bool $fatal, $input, $exp) {
return parent::testEncodeCodePointsStatically($fatal, $input, $exp);
}
/**
* @dataProvider provideStrings
* @covers MensBeam\Intl\Encoding\ShiftJIS::__construct
* @covers MensBeam\Intl\Encoding\ShiftJIS::nextCode
4 years ago
*/
public function testDecodeMultipleCharactersAsCodePoints(string $input, array $exp) {
return parent::testDecodeMultipleCharactersAsCodePoints($input, $exp);
}
/**
* @dataProvider provideStrings
* @covers MensBeam\Intl\Encoding\ShiftJIS::__construct
* @covers MensBeam\Intl\Encoding\ShiftJIS::nextChar
4 years ago
*/
public function testDecodeMultipleCharactersAsStrings(string $input, array $exp) {
return parent::testDecodeMultipleCharactersAsStrings($input, $exp);
}
/**
* @dataProvider provideStrings
* @covers MensBeam\Intl\Encoding\ShiftJIS::seekBack
4 years ago
*/
public function testSTepBackThroughAString(string $input, array $exp) {
return parent::testSTepBackThroughAString($input, $exp);
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::seek
* @covers MensBeam\Intl\Encoding\ShiftJIS::posChar
* @covers MensBeam\Intl\Encoding\ShiftJIS::posByte
* @covers MensBeam\Intl\Encoding\ShiftJIS::rewind
4 years ago
*/
public function testSeekThroughAString() {
return parent::testSeekThroughAString();
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::posChar
* @covers MensBeam\Intl\Encoding\ShiftJIS::posByte
* @covers MensBeam\Intl\Encoding\ShiftJIS::eof
4 years ago
*/
public function testTraversePastTheEndOfAString() {
return parent::testTraversePastTheEndOfAString();
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::peekChar
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateSave
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateApply
4 years ago
*/
public function testPeekAtCharacters() {
return parent::testPeekAtCharacters();
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::peekCode
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateSave
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateApply
4 years ago
*/
public function testPeekAtCodePoints() {
return parent::testPeekAtCodePoints();
}
/**
* @dataProvider provideStrings
* @covers MensBeam\Intl\Encoding\ShiftJIS::lenChar
* @covers MensBeam\Intl\Encoding\ShiftJIS::lenByte
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateSave
* @covers MensBeam\Intl\Encoding\ShiftJIS::stateApply
4 years ago
*/
public function testGetStringLength(string $input, array $points) {
return parent::testGetStringLength($input, $points);
}
/**
4 years ago
* @covers MensBeam\Intl\Encoding\ShiftJIS::errDec
4 years ago
*/
public function testReplacementModes() {
return parent::testReplacementModes();
}
/**
* @dataProvider provideStrings
* @covers MensBeam\Intl\Encoding\ShiftJIS::rewind
* @covers MensBeam\Intl\Encoding\ShiftJIS::chars
* @covers MensBeam\Intl\Encoding\ShiftJIS::codes
4 years ago
*/
public function testIterateThroughAString(string $input, array $exp) {
return parent::testIterateThroughAString($input, $exp);
}
/**
* @dataProvider provideStrings
* @coversNothing
4 years ago
*/
public function testIterateThroughAStringAllowingSurrogates(string $input, array $strictExp, array $relaxedExp = null) {
return parent::testIterateThroughAStringAllowingSurrogates($input, $strictExp, $relaxedExp);
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::seekBack
*/
public function testSeekBackOverRandomData() {
return parent::testSeekBackOverRandomData();
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::asciiSpan
*/
public function testExtractAsciiSpans() {
parent::testExtractAsciiSpans();
}
/**
* @covers MensBeam\Intl\Encoding\ShiftJIS::asciiSpanNot
*/
public function testExtractNegativeAsciiSpans() {
parent::testExtractNegativeAsciiSpans();
}
/**
* @group optional
4 years ago
*/
public function testPedanticallyDecodeSingleCharactersAsCodePoint() {
$series = [
'characters' => [["80","5C","8198","814E","818B","817D","814C","81F7","817E","8180","839F","83A0","83A1","83A2","83A3","83A4","83A5","83A6","83A7","83A8","83A9","83AA","83AB","83AC","83AD","83AE","83AF","83B0","83B1","83B2","83B3","83B4","83B5","83B6","83BF","83C0","83C1","83C2","83C3","83C4","83C5","83C6","83C7","83C8","83C9","83CA","83CB","83CC","83CD","83CE","83CF","83D0","83D1","83D2","83D3","83D4","83D5","83D6","8446","8440","8441","8442","8443","8444","8445","8447","8448","8449","844A","844B","844C","844D","844E","844F","8450","8451","8452","8453","8454","8455","8456","8457","8458","8459","845A","845B","845C","845D","845E","845F","8460","8470","8471","8472","8473","8474","8475","8477","8478","8479","847A","847B","847C","847D","847E","8480","8481","8482","8483","8484","8485","8486","8487","8488","8489","848A","848B","848C","848D","848E","848F","8490","8491","8476","815D","815C","8165","8166","8167","8168","81F5","81F6","8164","8163","81F1","818C","818D","81A6","7E","818E","8782","8784","81F0","8754","8755","8756","8757","8758","8759","875A","875B","875C","875D","FA40","FA41","FA42","FA43","FA44","FA45","FA46","FA47","FA48","FA49","81A9","81AA","81A8","81AB","81CB","81CC","81CD","81DD","81CE","81DE","81B8","81B9","8794","817C","81E3","81E5","8187","8798","81DA","8161","81C8","81C9","81BF","81BE","81E7","81E8","8793","8188","81E6","81E4","81E0","8182","81DF","8185","8186","81E1","81E2","81BC","81BD","81BA","81BB","81DB","8799","81DC","8740","8741","8742","8743","8744","8745","8746","8747","8748","8749","874A","874B","874C","874D","874E","874F","8750","8751","8752","8753","849F","84AA","84A0","84AB","84A1","84AC","84A2","84AD","84A4","84AF","84A3","84AE","84A5","84BA","84B5","84B0","84A7","84BC","84B7","84B2","84A6","84B6","84BB","84B1","84A8","84B8","84BD","84B3","84A9","84B9","84BE","84B4","81A1","81A0","81A3","81A2","81A5","81A4","819F","819E","819B","819D","819C","81FC","819A","8199","818A","8189","81F4","81F3","81F2","8140","8141","8142","8156","8158","8159","815A","8171","8172","8173","8174","8175","8176","8177","8178","8179","817A","81A7","81AC","816B","816C","8780","8781","829F","82A0","82A1","82A2","82A3","82A4","82A5","82A6","82A7","82A8","82A9","82AA","82AB","82AC","82AD","82AE","82AF","82B0","82B1","82B2","82B3","82B4","82B5","82B6","82B7","82B8","82B9","82BA","82BB","82BC","82BD","82BE","82BF","82C0","82C1","82C2","82C3","82C4","82C5","82C6","82C7","82C8","82C9","82CA","82CB","82CC","82CD","82CE","82CF","82D0","82D1","82D2","82D3","82D4","82D5","82D6","82D7","82D8","82D9","82DA","82DB","82DC","82DD","82DE","82DF","82E0","82E1","82E2","82E3","82E4","82E5","82E6","82E7","82E8","82E9","82EA","82EB","82EC","82ED","82EE","82EF","82F0","82F1","814A","814B","8154","8155","8340","8341","8342","8343","8344","8345","8346","8347","8348","8349","834A","834B","834C","834D","834E","834F","8350","8351","8352","8353","8354","8355","8356","8357","8358","8359","835A","835B","835C","835D","835E","835F","8360","8361","8362","8363","8364","8365","8366","8367","8368","8369","836A","836B","836C","836D","836E","836F","8370","8371","8372","8373","8374","8375","8376","8377","8378","8379","837A","837B","837C","837D","837E","8380","8381","8382","8383","8384","8385","8386","8387","8388","8389","838A","838B","838C","838D","838E","838F","8390","8391","8392","8393","8394","8395","8396","8145","815B","8152","8153","878A","878B","878C","8785","8786","8787","8788","8789","8765","8769","8760","8763","8761","876B","876A","8764","876C","8766","876E","875F","876D","8762","8767","8768","877E","878F","878E","878D","8772","8773","876F","8770","8771","8775","8774","8783","88EA","929A","8EB5","969C","8FE4","8E4F","8FE3","89BA","9573","975E","98A0","894E","8A8E","98A1","90A2","99C0","8B75","95B8","8FE5","97BC","95C0","FA68","98A2","9286","98A3","8BF8","98A4","8ADB","924F","8EE5","98A5","98A6","98A7","9454","8B76","9456","93E1","8CC1","9652","E568","98A8","8FE6","98A9","89B3","8BE3","8CEE","96E7","9BA4","9790","93FB","8AA3","8B54","98AA","98AB","97B9","975C","9188","98AD","8E96","93F1","98B0","895D","8CDD","8CDC","88E4","986A","9869","8DB
];
foreach ($series as $test) {
foreach ($test[0] as $a => $input) {
$class = $this->testedClass;
$char = hex2bin($input);
$exp = $test[1][$a];
$s = new $class($char);
$this->assertSame($exp, $s->nextCode(), "Sequence $input did not decode to $exp.");
$this->assertFalse($s->nextCode(), "Sequence $input did not end after one character");
}
}
}
}