1BEGIN { 2 if ($ENV{'PERL_CORE'}){ 3 chdir 't'; 4 unshift @INC, '../lib'; 5 } 6 require Config; Config->import(); 7 if ($Config{'extensions'} !~ /\bEncode\b/) { 8 print "1..0 # Skip: Encode was not built\n"; 9 exit 0; 10 } 11 if (ord("A") == 193) { 12 print "1..0 # Skip: EBCDIC\n"; 13 exit 0; 14 } 15 $| = 1; 16} 17 18use strict; 19use Test::More tests => 29; 20use Encode; 21 22no utf8; # we have raw Chinese encodings here 23 24BEGIN { 25 use_ok('Encode::CN'); 26} 27 28# Since JP.t already tests basic file IO, we will just focus on 29# internal encode / decode test here. Unfortunately, to test 30# against all the UniHan characters will take a huge disk space, 31# not to mention the time it will take, and the fact that Perl 32# did not bundle UniHan.txt anyway. 33 34# So, here we just test a typical snippet spanning multiple Unicode 35# blocks, and hope it can point out obvious errors. 36 37run_tests('Simplified Chinese only', { 38 'utf' => ( 3912298.26131.32463.12299.31532.19968.21350. 4024406.26352.65306. 4122823.21705.20094.20803.65292.19975.29289.36164.22987.65292. 4220035.32479.22825.12290. 4320113.34892.38632.26045.65292.21697.29289.27969.24418.12290. 4422823.26126.22987.32456.65292.20845.20301.26102.25104.65292. 4526102.20056.20845.40857.20197.24481.22825.12290. 4620094.36947.21464.21270.65292.21508.27491.24615.21629.65292. 4720445.21512.22823.21644.65292.20035.21033.36126.12290. 4839318.20986.24246.29289.65292.19975.22269.21688.23425.12290 49 ), 50 51 'euc-cn' => join('', 52'��������һ��', 53'��Ի��', 54'����ǬԪ��������ʼ��', 55'��ͳ�졣', 56'������ʩ��Ʒ�����Ρ�', 57'����ʼ�գ���λʱ�ɣ�', 58'ʱ�����������졣', 59'Ǭ���仯������������', 60'���ϴ�ͣ������ꡣ', 61'�׳�������������', 62 ), 63 64 'gb2312-raw' => join('', 65'!6RW>-!75ZR;XT', 66'ehT;#:', 67'4sTUG,T*#,MrNoWJJ<#,', 68'DKM3Ll!#', 69'TFPPSjJ)#,F7NoAwPN!#', 70'4sCwJ<VU#,AyN;J13I#,', 71'J13KAyAzRTSyLl!#', 72'G,5@1d;/#,8wU}PTC|#,', 73'1#:O4s:M#,DK@{Uj!#', 74'JW3vJ|No#,Mr9zOLD~!#' 75 ), 76 77 'iso-ir-165'=> join('', 78'!6RW>-!75ZR;XT', 79'ehT;#:', 80'4sTUG,T*#,MrNoWJJ<#,', 81'DKM3Ll!#', 82'TFPPSjJ)#,F7NoAwPN!#', 83'4sCwJ<VU#,AyN;J13I#,', 84'J13KAyAzRTSyLl!#', 85'G,5@1d;/#,8wU}PTC|#,', 86'1#:O4s:M#,DK@{Uj!#', 87'JW3vJ|No#,Mr9zOLD~!#' 88 ), 89}); 90 91run_tests('Simplified Chinese + ASCII', { 92 'utf' => ( 9335937.26352.65306.10. 9422825.34892.20581.65292.21531.23376.20197.33258.24378.19981.24687.12290.10. 9528508.40857.21247.29992.65292.38451.22312.19979.20063.12290.32. 9635265.40857.22312.30000.65292.24503.26045.26222.20063.12290.32. 9732456.26085.20094.20094.65292.21453.22797.36947.20063.12290.10. 9825110.36291.22312.28170.65292.36827.26080.21646.20063.12290.39134. 9940857.22312.22825.65292.22823.20154.36896.20063.12290.32. 10020130.40857.26377.24724.65292.30408.19981.21487.20037.20063.12290.10. 10129992.20061.65292.22825.24503.19981.21487.20026.39318.20063.12290 102 ), 103 104 'cp936' => join(chr(10), 105'��Ի��', 106'���н�����������ǿ��Ϣ��', 107'DZ�����ã�������Ҳ�� ���������ʩ��Ҳ�� ����ǬǬ��������Ҳ��', 108'��Ծ��Ԩ������Ҳ���������죬������Ҳ�� �����лڣ�ӯ���ɾ�Ҳ��', 109'�þţ���²���Ϊ��Ҳ��', 110 ), 111 112 'hz' => join(chr(10), 113'~{OsT;#:~}', 114'~{LlPP=!#,>}WSRTWTG?2;O"!#~}', 115'~{G1AzNpSC#,QtTZOBR2!#~} ~{<{AzTZLo#,5BJ)FUR2!#~} ~{VUHUG,G,#,74845@R2!#~}', 116'~{;rT>TZT(#,=xN^>LR2!#7IAzTZLl#,4sHKTlR2!#~} ~{?:AzSP;Z#,S/2;?I>CR2!#~}', 117'~{SC>E#,Ll5B2;?IN*JWR2!#~}', 118 ), 119}); 120 121run_tests('Traditional Chinese', { 122 'utf', => 20094.65306.20803.12289.20136.12289.21033.12289.35998, 123 'gb12345-raw' => 'G,#:T*!":`!"@{!"Uj', 124 'gbk' => 'Ǭ��Ԫ���ࡢ����ؑ', 125}); 126 127sub run_tests { 128 my ($title, $tests) = @_; 129 my $utf = delete $tests->{'utf'}; 130 131 # $enc = encoding, $str = content 132 foreach my $enc (sort keys %{$tests}) { 133 my $str = $tests->{$enc}; 134 135 is(Encode::decode($enc, $str), $utf, "[$enc] decode - $title"); 136 is(Encode::encode($enc, $utf), $str, "[$enc] encode - $title"); 137 138 my $str2 = $str; 139 my $utf8 = Encode::encode('utf-8', $utf); 140 141 Encode::from_to($str2, $enc, 'utf-8'); 142 is($str2, $utf8, "[$enc] from_to => utf8 - $title"); 143 144 Encode::from_to($utf8, 'utf-8', $enc); # convert $utf8 as $enc 145 is($utf8, $str, "[$enc] utf8 => from_to - $title"); 146 } 147} 148