ext/nkf/test.rb


DEFINITIONS

This source file includes following functions.


   1  $counter = 0
   2  def result(result, message = nil)
   3    $counter += 1
   4    printf("%s %d%s\n",
   5           result ? 'ok' : 'no', 
   6           $counter, 
   7           message ? ' ... ' + message : '')
   8  end
   9  
  10  begin
  11    require 'nkf'
  12    include NKF
  13  rescue LoadError
  14    result(false)
  15  end
  16  result(true)
  17  
  18  if nkf('-me', '1')
  19    result(true);
  20  else
  21    result(false);
  22  end
  23  
  24  output = nkf('-e', "\033\$@#1#3#2%B")
  25  if output
  26    # print output, "\n"
  27    result(true, output)
  28  else
  29    result(false)
  30  end
  31  
  32  output = nkf('-Zj', "\033\$@#1#3#2%B")
  33  if output
  34    # print output, "\n"
  35    result(true, output)
  36  else
  37    result(false)
  38  end
  39  
  40  output = "\244\306 " * 1024
  41  old =  output.length
  42  output = nkf("-j", output)
  43  if output
  44    # print output, "\n"
  45    result(true, "#{old} #{output.length}")
  46  else
  47      result(false)
  48  end
  49  
  50  
  51  $detail = false
  52  def test(opt, input, expect)
  53    print "\nINPUT:\n", input if $detail
  54    print "\nEXPECT:\n", expect if $detail
  55    result = nkf(opt, input)
  56    print "\nGOT:\n", result if $detail
  57  
  58    print result == expect ? "Ok\n" : "Fail\n"
  59    return result
  60  end
  61  
  62  # Basic Conversion
  63  print "\nBasic Conversion test\n\n"
  64  
  65  example = {}
  66  example['jis'] = <<'eofeof'.unpack('u')[0]
  67  M1FER<W0@4W1A9V4@&R1"(3DQ(3%^2R%+?D]3&RA"(%-E8V]N9"!3=&%G92`;
  68  M)$)0)TU:&RA"($AI<F%G86YA(!LD0B0B)"0D)B0H)"HD;R1R)',;*$(*2V%T
  69  M86MA;F$@&R1")2(E)"4F)2@E*B5O)7(E<QLH0B!+:6=O=2`;)$(A)B%G(S`C
  70  /029!)E@G(B=!*$`;*$(*
  71  eofeof
  72  #'
  73  
  74  example['sjis'] = <<'eofeof'.unpack('u')[0]
  75  M1FER<W0@4W1A9V4@@5B)0(F>ED"6GIAR(%-E8V]N9"!3=&%G92"8I9=Y($AI
  76  M<F%G86YA((*@@J*"I(*F@JB"[8+P@O$*2V%T86MA;F$@@T&#0X-%@T>#28./
  77  >@Y*#DR!+:6=O=2"!18&'@D^"8(._@]:$081@A+X*
  78  eofeof
  79  #'
  80  
  81  example['euc'] = <<'eofeof'.unpack('u')[0]
  82  M1FER<W0@4W1A9V4@H;FQH;'^RZ'+_L_3(%-E8V]N9"!3=&%G92#0I\W:($AI
  83  M<F%G86YA(*2BI*2DIJ2HI*JD[Z3RI/,*2V%T86MA;F$@I:*EI*6FI:BEJJ7O
  84  >I?*E\R!+:6=O=2"AIJ'GH["CP:;!IMBGHJ?!J,`*
  85  eofeof
  86  #'
  87  
  88  example['amb'] = <<'eofeof'.unpack('u')[0]
  89  MI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&E
  90  MPK"QI<*PL:7"L+&EPK"QI<(*I<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*P
  91  ML:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<(*I<*PL:7"L+&E
  92  MPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"
  93  ML+&EPK"QI<(*I<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"Q
  94  MI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<(*I<*PL:7"L+&EPK"QI<*PL:7"
  95  ML+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<*PL:7"L+&EPK"QI<(*
  96  eofeof
  97  
  98  example['amb.euc'] = <<'eofeof'.unpack('u')[0]
  99  M&R1")4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25"
 100  M,#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*&R1")4(P,25",#$E0C`Q)4(P,25"
 101  M,#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;
 102  M*$(*&R1")4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P
 103  M,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*&R1")4(P,25",#$E0C`Q)4(P
 104  M,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q
 105  M)4(;*$(*&R1")4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q
 106  >)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*
 107  eofeof
 108  
 109  example['amb.sjis'] = <<'eofeof'.unpack('u')[0]
 110  M&RA))4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25"
 111  M,#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*&RA))4(P,25",#$E0C`Q)4(P,25"
 112  M,#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;
 113  M*$(*&RA))4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P
 114  M,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*&RA))4(P,25",#$E0C`Q)4(P
 115  M,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q
 116  M)4(;*$(*&RA))4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q
 117  >)4(P,25",#$E0C`Q)4(P,25",#$E0C`Q)4(;*$(*
 118  eofeof
 119  
 120  example['x0201.sjis'] = <<'eofeof'.unpack('u')[0]
 121  MD5.*<(-*@TR#3H-0@U*#2X--@T^#48-3"I%3B7""8()A@F*"8X)D@F6"9H*!
 122  M@H*"@X*$@H6"AH*'"I%3BTR-AH%)@9>!E(&0@9.!3X&5@9:!:8%J@7R!>X&!
 123  M@6V!;H%O@7"!CPJ4O(IPMK>X/;FZMMZWWKC>N=ZZWH+&"I2\BG#*W\O?S-_-
 124  MW\[?M]^QW@K*W\O?S`IH86YK86MU(,K?R]_,I`K*W\O?S-VA"I2\BG""S(SC
 125  !"@!"
 126  eofeof
 127  #'
 128  
 129  example['x0201.euc'] = <<'eofeof'.unpack('u')[0]
 130  MP;2ST:6KI:VEKZ6QI;.EK*6NI;"ELJ6T"L&TL=&CP:/"H\.CQ*/%H\:CQZ/A
 131  MH^*CXZ/DH^6CYJ/G"L&TM:VYYJ&JH?>A]*'PH?.AL*'UH?:ARJ'+H=VAW*'A
 132  MH<ZASZ'0H=&A[PK(OK/1CK:.MXZX/8ZYCKJ.MH[>CK>.WHZXCMZ.N8[>CKJ.
 133  MWJ3("LB^L]&.RH[?CLN.WX[,CM^.S8[?CLZ.WXZWCM^.L8[>"H[*CM^.RX[?
 134  MCLP*:&%N:V%K=2".RH[?CLN.WX[,CJ0*CLJ.WX[+CM^.S([=CJ$*R+ZST:3.
 135  #N.4*
 136  eofeof
 137  #'
 138  
 139  example['x0201.jis'] = <<'eofeof'.unpack('u')[0]
 140  M&R1"030S424K)2TE+R4Q)3,E+"4N)3`E,B4T&RA""ALD0D$T,5$C02-"(T,C
 141  M1"-%(T8C1R-A(V(C8R-D(V4C9B-G&RA""ALD0D$T-2TY9B$J(7<A="%P(7,A
 142  M,"%U(78A2B%+(5TA7"%A(4XA3R%0(5$A;QLH0@H;)$)(/C-1&RA)-C<X&RA"
 143  M/1LH23DZ-EXW7CA>.5XZ7ALD0B1(&RA""ALD0D@^,U$;*$E*7TM?3%]-7TY?
 144  M-U\Q7ALH0@H;*$E*7TM?3!LH0@IH86YK86MU(!LH24I?2U],)!LH0@H;*$E*
 145  97TM?3%TA&RA""ALD0D@^,U$D3CAE&RA""@``
 146  eofeof
 147  #`
 148  
 149  example['x0201.sosi'] = <<'eofeof'.unpack('u')[0]
 150  M&R1"030S424K)2TE+R4Q)3,E+"4N)3`E,B4T&RA*"ALD0D$T,5$C02-"(T,C
 151  M1"-%(T8C1R-A(V(C8R-D(V4C9B-G&RA*"ALD0D$T-2TY9B$J(7<A="%P(7,A
 152  M,"%U(78A2B%+(5TA7"%A(4XA3R%0(5$A;QLH2@H;)$)(/C-1&RA*#C8W.`\;
 153  M*$H]#CDZ-EXW7CA>.5XZ7@\;)$(D2!LH2@H;)$)(/C-1&RA*#DI?2U],7TU?
 154  M3E\W7S%>#PH.2E]+7TP/&RA*"FAA;FMA:W4@#DI?2U],)`\;*$H*#DI?2U],
 155  672$/&RA*"ALD0D@^,U$D3CAE&RA""@``
 156  eofeof
 157  #"
 158  
 159  example['x0201.x0208'] = <<'eofeof'.unpack('u')[0]
 160  M&R1"030S424K)2TE+R4Q)3,E+"4N)3`E,B4T&RA""ALD0D$T,5$;*$)!0D-$
 161  M149'86)C9&5F9PH;)$)!-#4M.68;*$(A0",D)5XF*B@I+2L]6UU[?1LD0B%O
 162  M&RA""ALD0D@^,U$E*R4M)2\;*$(]&R1")3$E,R4L)2XE,"4R)30D2!LH0@H;
 163  M)$)(/C-1)5$E5"57)5HE724M(2PE(B$K&RA""ALD0B51)50E51LH0@IH86YK
 164  M86MU(!LD0B51)50E52$B&RA""ALD0B51)50E525S(2,;*$(*&R1"2#XS421.
 165  &.&4;*$(*
 166  eofeof
 167  #`
 168  
 169  example['mime.iso2022'] = <<'eofeof'.unpack('u')[0]
 170  M/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23TI566Q/4U9)1WEH2S\]"CT_
 171  M:7-O+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]*55EL3U-624=Y:$L_/0H]/VES
 172  M;RTR,#(R+4I0/U$_/3%")$(D1B11/3%"*$)?96YD/ST*&R1`)#TD)B0K)$H;
 173  M*$H@/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D:'-O4V<]/3\]
 174  M(&5N9"!O9B!L:6YE"CT_25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]0>6LW
 175  M9&AS;U-G/3T_/2`]/TE33RTR,#(R+4I0/T(_1WE204Y%13=E:5)/4'EK-V1H
 176  M<V]39ST]/ST*0G)O:V5N(&-A<V4*/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W
 177  M96E23U!Y:S=D"FAS;U-G/3T_/2`]/TE33RTR,`HR,BU*4#]"/T=Y4D%.144W
 178  M96E23U!Y:S=D:'-O4V<]/3\]"CT_25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I
 179  44D]*55EL3QM;2U-624=Y:$L_/0H_
 180  eofeof
 181  #'
 182  
 183  example['mime.ans.strict'] = <<'eofeof'.unpack('u')[0]
 184  M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
 185  M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
 186  M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
 187  M/3])4T\M,C`R,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D"FAS;U-G/3T_/2`]
 188  M/TE33RTR,`HR,BU*4#]"/T=Y4D%.144W96E23U!Y:S=D:'-O4V<]/3\]"CT_
 189  L25-/+3(P,C(M2E`_0C]'>5)!3D5%-V5I4D]*55EL3QM;2U-624=Y:$L_/0H_
 190  eofeof
 191  #'
 192  
 193  example['mime.unbuf.strict'] = <<'eofeof'.unpack('u')[0]
 194  M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
 195  M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
 196  M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
 197  M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@/3])4T\M,C`*,C(M2E`_0C]'>5)!
 198  M3D5%-V5I4D]0>6LW9&AS;U-G/3T_/0H;)$(T03MZ)$XE1ALH0EM+4U9)1WEH
 199  $2S\]"F5I
 200  eofeof
 201  
 202  example['mime.ans'] = <<'eofeof'.unpack('u')[0]
 203  M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
 204  M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
 205  M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
 206  M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@&R1"-$$[>B1./RD[=ALH0@H;)$(T
 207  603MZ)$XE1ALH0EM+4U9)1WEH2S\]"@`*
 208  eofeof
 209  #"
 210  
 211  example['mime.unbuf'] = <<'eofeof'.unpack('u')[0]
 212  M&R1"-$$[>B1.)48E.25(&RA""ALD0C1!.WHD3B5&)3DE2!LH0@H;)$(D1B11
 213  M&RA"(&5N9`H;)$(D/20F)"LD2ALH0B`;)$(T03MZ)$X_*3MV&RA"96YD(&]F
 214  M(&QI;F4*&R1"-$$[>B1./RD[=C1!.WHD3C\I.W8;*$(*0G)O:V5N(&-A<V4*
 215  M&R1"-$$[>B1./RD;*$)H<V]39ST]/ST@&R1"-$$[>B1./RD[=ALH0@H;)$(T
 216  603MZ)$XE1ALH0EM+4U9)1WEH2S\]"@`*
 217  eofeof
 218  #"
 219  
 220  example['mime.base64'] = <<'eofeof'.unpack('u')[0]
 221  M9W-M5"])3&YG<FU#>$I+-&=Q=4,S24LS9W%Q0E%:3TUI-39,,S0Q-&=S5T)1
 222  M43!+9VUA1%9O3T@*9S)+1%1O3'=K8C)1;$E+;V=Q2T-X24MG9W5M0W%*3EEG
 223  <<T=#>$E+9V=U;4,X64Q&9W)70S592VMG<6U""F=Q
 224  eofeof
 225  #"
 226  
 227  example['mime.base64.ans'] = <<'eofeof'.unpack('u')[0]
 228  M&R1")$M&?B1I)#LD1D0Z)"TD7B0Y)"PA(D5L-7XV83E9)$<A(ALH0@T*&R1"
 229  M(T<E-R5G)4,E+R1R0C\_="0J)"0D1B0B)&LD*D4Y)$,D1B0B)&LD<R1')#<D
 230  (9R0F)"L;*$(E
 231  eofeof
 232  #'
 233  
 234  example['mime.is8859'] = <<'eofeof'.unpack('u')[0]
 235  M/3])4T\M.#@U.2TQ/U$_*CU#-V%V83\_/2`*4&5E<B!4]G)N9W)E;@I,87-S
 236  M92!(:6QL97+X92!0971E<G-E;B`@7"`B36EN(&MA97!H97-T(&AA<B!F86%E
 237  M="!E="!F;V5L(2(*06%R:'5S(%5N:79E<G-I='DL($1%3DU!4DL@(%P@(DUI
 238  <;B!KYG!H97-T(&AA<B!FY65T(&5T(&;X;"$B"@!K
 239  eofeof
 240  
 241  example['mime.is8859.ans'] = <<'eofeof'.unpack('u')[0]
 242  M*L=A=F$_(`I0965R(%3V<FYG<F5N"DQA<W-E($AI;&QE<OAE(%!E=&5R<V5N
 243  M("!<(")-:6X@:V%E<&AE<W0@:&%R(&9A865T(&5T(&9O96PA(@I!87)H=7,@
 244  M56YI=F5R<VET>2P@1$5.34%22R`@7"`B36EN(&OF<&AE<W0@:&%R(&;E970@
 245  )970@9OAL(2(*
 246  eofeof
 247  #"
 248  
 249  print 'JIS  to JIS ... '; test('  ', example['jis'], example['jis'])
 250  print 'JIS  to SJIS... '; test('-s', example['jis'], example['sjis'])
 251  print 'JIS  to EUC ... '; test('-e', example['jis'], example['euc'])
 252  
 253  print 'SJIS to JIS ... '; test('-j', example['sjis'], example['jis'])
 254  print 'SJIS to SJIS... '; test('-s', example['sjis'], example['sjis'])
 255  print 'SJIS to EUC ... '; test('-e', example['sjis'], example['euc'])
 256  
 257  print 'EUC  to JIS ... '; test('  ', example['euc'], example['jis'])
 258  print 'EUC  to SJIS... '; test('-s', example['euc'], example['sjis'])
 259  print 'EUC  to EUC ... '; test('-e', example['euc'], example['euc'])
 260  
 261  
 262  # Ambigous Case
 263  print 'Ambiguous Case. '; test(''  , example['amb'], example['amb.euc'])
 264  
 265  # Input assumption
 266  print 'SJIS  Input assumption '
 267  test('-Sx', example['amb'], example['amb.sjis'])
 268  
 269  # X0201 仮名
 270  # X0201->X0208 conversion
 271  # X0208 aphabet -> ASCII
 272  # X0201 相互変換
 273  
 274  print "\nX0201 test\n\n"
 275  
 276  # -X is necessary to allow X0201 in SJIS
 277  # -Z convert X0208 alphabet to ASCII
 278  print 'X0201 conversion: SJIS '
 279  test('-XZ', example['x0201.sjis'], example['x0201.x0208'])
 280  print 'X0201 conversion: JIS  '
 281  test('-Z',  example['x0201.jis'],  example['x0201.x0208'])
 282  print 'X0201 conversion:SI/SO '
 283  test('-Z',  example['x0201.sosi'], example['x0201.x0208'])
 284  print 'X0201 conversion: EUC  '
 285  test('-Z',  example['x0201.euc'],  example['x0201.x0208'])
 286  # -x means X0201 output
 287  print 'X0201 output: SJIS     '
 288  test('-xs', example['x0201.euc'],  example['x0201.sjis'])
 289  print 'X0201 output: JIS      '
 290  test('-xj', example['x0201.sjis'], example['x0201.jis'])
 291  print 'X0201 output: EUC      '
 292  test('-xe', example['x0201.jis'],  example['x0201.euc'])
 293  
 294  # MIME decode
 295  
 296  print "\nMIME test\n\n"
 297  
 298  # MIME ISO-2022-JP
 299  
 300  print "Next test is expeced to Fail.\n"
 301  
 302  print 'MIME decode (strict)   '
 303  tmp = test('-m', example['mime.iso2022'], example['mime.ans.strict'])
 304  print 'MIME decode (nonstrict)'
 305  tmp = test('-m', example['mime.iso2022'], example['mime.ans'])
 306  #    open(OUT,'>tmp1');print OUT pack('u',$tmp);close(OUT);
 307  # unbuf mode implies more pessimistic decode
 308  print 'MIME decode (unbuf)    '
 309  test('-mu', example['mime.iso2022'], example['mime.unbuf'])
 310  print 'MIME decode (base64)   '
 311  t = test('-mB', example['mime.base64'],  example['mime.base64.ans'])
 312  
 313  # MIME ISO-8859-1
 314  
 315  # Without -l, ISO-8859-1 was handled as X0201.
 316  
 317  print 'MIME ISO-8859-1 (Q)    '
 318  test('-ml', example['mime.is8859'], example['mime.is8859.ans'])