summaryrefslogtreecommitdiff
path: root/ext/mbstring/tests/sjismac_encoding.phpt
blob: 5803f0dc022c886a26d4ef77741685e5ae27f217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
--TEST--
Exhaustive test of MacJapanese encoding verification and conversion
--SKIPIF--
<?php
extension_loaded('mbstring') or die('skip mbstring not available');
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
?>
--FILE--
<?php
srand(300); /* Make results consistent */
include('encoding_tests.inc');
mb_substitute_character(0x25); // '%'

/* Read in the table of all characters in MacJapanese */
$validChars = array(); /* MacJapanese string -> UTF-32BE string */
$fromUnicode = array(); /* UTF-16BE -> MacJapanese */
$fp = fopen(__DIR__ . '/data/MacJapanese-SJIS.txt', 'r+');
while ($line = fgets($fp, 256)) {
	if ($line[0] == '#')
		continue;

	$cp1 = $cp2 = $cp3 = $cp4 = $cp5 = null;
	if (sscanf($line, "0x%x\t0x%x+0x%x+0x%x+0x%x+0x%x", $bytes, $cp1, $cp2, $cp3, $cp4, $cp5) >= 2) {
		if ($bytes < 256) {
			$macJap = chr($bytes);
		} else {
			$macJap = pack('n', $bytes);
		}

		if ($cp5) {
			$validChars[$macJap] = pack('NNNNN', $cp1, $cp2, $cp3, $cp4, $cp5);
			$fromUnicode[pack('nnnnn', $cp1, $cp2, $cp3, $cp4, $cp5)] = $macJap;
		} else if ($cp4) {
			$validChars[$macJap] = pack('NNNN', $cp1, $cp2, $cp3, $cp4);
			$fromUnicode[pack('nnnn', $cp1, $cp2, $cp3, $cp4)] = $macJap;
		} else if ($cp3) {
			$validChars[$macJap] = pack('NNN', $cp1, $cp2, $cp3);
			$fromUnicode[pack('nnn', $cp1, $cp2, $cp3)] = $macJap;
		} else if ($cp2) {
			$validChars[$macJap] = pack('NN', $cp1, $cp2);
			$fromUnicode[pack('nn', $cp1, $cp2)] = $macJap;
		} else {
			$validChars[$macJap] = pack('N', $cp1);
			$fromUnicode[pack('n', $cp1)] = $macJap;
		}
	}
}
/* Although not included in the table, 0x0-0x1F and 0x7F are valid;
 * these are 'control characters' */
for ($i = 0; $i < 0x20; $i++) {
	$validChars[chr($i)] = pack('N', $i);
	$fromUnicode[pack('n', $i)] = chr($i);
}
$validChars["\x7F"] = pack('N', 0x7F);
$fromUnicode["\x00\x7F"] = "\x7F";

/* While Shift-JIS 0x815C normally corresponds to U+2015 (HORIZONTAL BAR),
 * for MacJapanese we convert 0x815C to U+2014 (EM DASH)
 * (See recommendations in JAPANESE.txt from the Unicode Consortium, under
 * 'Unicode mapping issues', point 3)
 * However, when converting Unicode -> MacJapanese, we accept both U+2014
 * and U+2015 */
$fromUnicode["\x20\x15"] = "\x81\x5C";

/* Convert U+203E (OVERLINE) to 0x8150 (FULLWIDTH MACRON) */
$fromUnicode["\x20\x3E"] = "\x81\x50";
/* And also U+00AF (MACRON) */
$fromUnicode["\x00\xAF"] = "\x81\x50";

/* Convert U+FF5E (FULLWIDTH TILDE) to 0x8160 (WAVE DASH) */
$fromUnicode["\xFF\x5E"] = "\x81\x60";

testAllValidChars($validChars, 'SJIS-mac', 'UTF-32BE');
echo "MacJapanese verification and conversion works on all valid characters\n";

findInvalidChars($validChars, $invalidChars, $truncated,
	array_fill_keys(range(0x81, 0x9F), 2) + array_fill_keys(range(0xE0, 0xED), 2));
testAllInvalidChars($invalidChars, $validChars, 'SJIS-mac', 'UTF-32BE', "\x00\x00\x00%");
testTruncatedChars($truncated, 'SJIS-mac', 'UTF-32BE', "\x00\x00\x00%");
echo "MacJapanese verification and conversion rejects all invalid characters\n";

testAllValidChars($fromUnicode, 'UTF-16BE', 'SJIS-mac', false);
echo "Unicode -> SJIS-mac conversion works on all valid characters\n";

findInvalidChars($fromUnicode, $invalidChars, $unused, array_fill_keys(range(0, 0xFF), 2));
convertAllInvalidChars($invalidChars, $fromUnicode, 'UTF-16BE', 'SJIS-mac', '%');
echo "Unicode -> SJIS-mac conversion works on all invalid characters\n";
?>
--EXPECT--
MacJapanese verification and conversion works on all valid characters
MacJapanese verification and conversion rejects all invalid characters
Unicode -> SJIS-mac conversion works on all valid characters
Unicode -> SJIS-mac conversion works on all invalid characters