[25] | 1 | # This file contains a collection of tests for tclEncoding.c |
---|
| 2 | # Sourcing this file into Tcl runs the tests and generates output for |
---|
| 3 | # errors. No output means no errors were found. |
---|
| 4 | # |
---|
| 5 | # Copyright (c) 1997 Sun Microsystems, Inc. |
---|
| 6 | # Copyright (c) 1998-1999 by Scriptics Corporation. |
---|
| 7 | # |
---|
| 8 | # See the file "license.terms" for information on usage and redistribution |
---|
| 9 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 10 | # |
---|
| 11 | # RCS: @(#) $Id: encoding.test,v 1.27 2007/05/04 14:59:06 kennykb Exp $ |
---|
| 12 | |
---|
| 13 | package require tcltest 2 |
---|
| 14 | |
---|
| 15 | namespace eval ::tcl::test::encoding { |
---|
| 16 | variable x |
---|
| 17 | |
---|
| 18 | namespace import -force ::tcltest::* |
---|
| 19 | |
---|
| 20 | proc toutf {args} { |
---|
| 21 | variable x |
---|
| 22 | lappend x "toutf $args" |
---|
| 23 | } |
---|
| 24 | proc fromutf {args} { |
---|
| 25 | variable x |
---|
| 26 | lappend x "fromutf $args" |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | proc runtests {} { |
---|
| 30 | |
---|
| 31 | variable x |
---|
| 32 | |
---|
| 33 | # Some tests require the testencoding command |
---|
| 34 | testConstraint testencoding [llength [info commands testencoding]] |
---|
| 35 | testConstraint exec [llength [info commands exec]] |
---|
| 36 | |
---|
| 37 | # TclInitEncodingSubsystem is tested by the rest of this file |
---|
| 38 | # TclFinalizeEncodingSubsystem is not currently tested |
---|
| 39 | |
---|
| 40 | test encoding-1.1 {Tcl_GetEncoding: system encoding} {testencoding} { |
---|
| 41 | testencoding create foo [namespace origin toutf] [namespace origin fromutf] |
---|
| 42 | set old [encoding system] |
---|
| 43 | encoding system foo |
---|
| 44 | set x {} |
---|
| 45 | encoding convertto abcd |
---|
| 46 | encoding system $old |
---|
| 47 | testencoding delete foo |
---|
| 48 | set x |
---|
| 49 | } {{fromutf }} |
---|
| 50 | test encoding-1.2 {Tcl_GetEncoding: existing encoding} {testencoding} { |
---|
| 51 | testencoding create foo [namespace origin toutf] [namespace origin fromutf] |
---|
| 52 | set x {} |
---|
| 53 | encoding convertto foo abcd |
---|
| 54 | testencoding delete foo |
---|
| 55 | set x |
---|
| 56 | } {{fromutf }} |
---|
| 57 | test encoding-1.3 {Tcl_GetEncoding: load encoding} { |
---|
| 58 | list [encoding convertto jis0208 \u4e4e] \ |
---|
| 59 | [encoding convertfrom jis0208 8C] |
---|
| 60 | } "8C \u4e4e" |
---|
| 61 | |
---|
| 62 | test encoding-2.1 {Tcl_FreeEncoding: refcount == 0} { |
---|
| 63 | encoding convertto jis0208 \u4e4e |
---|
| 64 | } {8C} |
---|
| 65 | test encoding-2.2 {Tcl_FreeEncoding: refcount != 0} {testencoding} { |
---|
| 66 | set system [encoding system] |
---|
| 67 | set path [encoding dirs] |
---|
| 68 | encoding system shiftjis ;# incr ref count |
---|
| 69 | encoding dirs [list [pwd]] |
---|
| 70 | set x [encoding convertto shiftjis \u4e4e] ;# old one found |
---|
| 71 | encoding system identity |
---|
| 72 | lappend x [catch {encoding convertto shiftjis \u4e4e} msg] $msg |
---|
| 73 | encoding system identity |
---|
| 74 | encoding dirs $path |
---|
| 75 | encoding system $system |
---|
| 76 | set x |
---|
| 77 | } "\u008c\u00c1 1 {unknown encoding \"shiftjis\"}" |
---|
| 78 | |
---|
| 79 | test encoding-3.1 {Tcl_GetEncodingName, NULL} { |
---|
| 80 | set old [encoding system] |
---|
| 81 | encoding system shiftjis |
---|
| 82 | set x [encoding system] |
---|
| 83 | encoding system $old |
---|
| 84 | set x |
---|
| 85 | } {shiftjis} |
---|
| 86 | test encoding-3.2 {Tcl_GetEncodingName, non-null} { |
---|
| 87 | set old [fconfigure stdout -encoding] |
---|
| 88 | fconfigure stdout -encoding jis0208 |
---|
| 89 | set x [fconfigure stdout -encoding] |
---|
| 90 | fconfigure stdout -encoding $old |
---|
| 91 | set x |
---|
| 92 | } {jis0208} |
---|
| 93 | |
---|
| 94 | test encoding-4.1 {Tcl_GetEncodingNames} {testencoding} { |
---|
| 95 | cd [makeDirectory tmp] |
---|
| 96 | makeDirectory [file join tmp encoding] |
---|
| 97 | makeFile {} [file join tmp encoding junk.enc] |
---|
| 98 | makeFile {} [file join tmp encoding junk2.enc] |
---|
| 99 | set path [encoding dirs] |
---|
| 100 | encoding dirs {} |
---|
| 101 | catch {unset encodings} |
---|
| 102 | catch {unset x} |
---|
| 103 | foreach encoding [encoding names] { |
---|
| 104 | set encodings($encoding) 1 |
---|
| 105 | } |
---|
| 106 | encoding dirs [list [file join [pwd] encoding]] |
---|
| 107 | foreach encoding [encoding names] { |
---|
| 108 | if {![info exists encodings($encoding)]} { |
---|
| 109 | lappend x $encoding |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | encoding dirs $path |
---|
| 113 | cd [workingDirectory] |
---|
| 114 | removeFile [file join tmp encoding junk2.enc] |
---|
| 115 | removeFile [file join tmp encoding junk.enc] |
---|
| 116 | removeDirectory [file join tmp encoding] |
---|
| 117 | removeDirectory tmp |
---|
| 118 | lsort $x |
---|
| 119 | } {junk junk2} |
---|
| 120 | |
---|
| 121 | test encoding-5.1 {Tcl_SetSystemEncoding} { |
---|
| 122 | set old [encoding system] |
---|
| 123 | encoding system jis0208 |
---|
| 124 | set x [encoding convertto \u4e4e] |
---|
| 125 | encoding system identity |
---|
| 126 | encoding system $old |
---|
| 127 | set x |
---|
| 128 | } {8C} |
---|
| 129 | test encoding-5.2 {Tcl_SetSystemEncoding: test ref count} { |
---|
| 130 | set old [encoding system] |
---|
| 131 | encoding system $old |
---|
| 132 | string compare $old [encoding system] |
---|
| 133 | } {0} |
---|
| 134 | |
---|
| 135 | test encoding-6.1 {Tcl_CreateEncoding: new} {testencoding} { |
---|
| 136 | testencoding create foo [namespace code {toutf 1}] \ |
---|
| 137 | [namespace code {fromutf 2}] |
---|
| 138 | set x {} |
---|
| 139 | encoding convertfrom foo abcd |
---|
| 140 | encoding convertto foo abcd |
---|
| 141 | testencoding delete foo |
---|
| 142 | set x |
---|
| 143 | } {{toutf 1} {fromutf 2}} |
---|
| 144 | test encoding-6.2 {Tcl_CreateEncoding: replace encoding} {testencoding} { |
---|
| 145 | testencoding create foo [namespace code {toutf a}] \ |
---|
| 146 | [namespace code {fromutf b}] |
---|
| 147 | set x {} |
---|
| 148 | encoding convertfrom foo abcd |
---|
| 149 | encoding convertto foo abcd |
---|
| 150 | testencoding delete foo |
---|
| 151 | set x |
---|
| 152 | } {{toutf a} {fromutf b}} |
---|
| 153 | |
---|
| 154 | test encoding-7.1 {Tcl_ExternalToUtfDString: small buffer} { |
---|
| 155 | encoding convertfrom jis0208 8c8c8c8c |
---|
| 156 | } "\u543e\u543e\u543e\u543e" |
---|
| 157 | test encoding-7.2 {Tcl_UtfToExternalDString: big buffer} { |
---|
| 158 | set a 8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C |
---|
| 159 | append a $a |
---|
| 160 | append a $a |
---|
| 161 | append a $a |
---|
| 162 | append a $a |
---|
| 163 | set x [encoding convertfrom jis0208 $a] |
---|
| 164 | list [string length $x] [string index $x 0] |
---|
| 165 | } "512 \u4e4e" |
---|
| 166 | |
---|
| 167 | test encoding-8.1 {Tcl_ExternalToUtf} { |
---|
| 168 | set f [open [file join [temporaryDirectory] dummy] w] |
---|
| 169 | fconfigure $f -translation binary -encoding iso8859-1 |
---|
| 170 | puts -nonewline $f "ab\x8c\xc1g" |
---|
| 171 | close $f |
---|
| 172 | set f [open [file join [temporaryDirectory] dummy] r] |
---|
| 173 | fconfigure $f -translation binary -encoding shiftjis |
---|
| 174 | set x [read $f] |
---|
| 175 | close $f |
---|
| 176 | file delete [file join [temporaryDirectory] dummy] |
---|
| 177 | set x |
---|
| 178 | } "ab\u4e4eg" |
---|
| 179 | |
---|
| 180 | test encoding-9.1 {Tcl_UtfToExternalDString: small buffer} { |
---|
| 181 | encoding convertto jis0208 "\u543e\u543e\u543e\u543e" |
---|
| 182 | } {8c8c8c8c} |
---|
| 183 | test encoding-9.2 {Tcl_UtfToExternalDString: big buffer} { |
---|
| 184 | set a \u4e4e\u4e4e\u4e4e\u4e4e\u4e4e\u4e4e\u4e4e\u4e4e |
---|
| 185 | append a $a |
---|
| 186 | append a $a |
---|
| 187 | append a $a |
---|
| 188 | append a $a |
---|
| 189 | append a $a |
---|
| 190 | append a $a |
---|
| 191 | set x [encoding convertto jis0208 $a] |
---|
| 192 | list [string length $x] [string range $x 0 1] |
---|
| 193 | } "1024 8C" |
---|
| 194 | |
---|
| 195 | test encoding-10.1 {Tcl_UtfToExternal} { |
---|
| 196 | set f [open [file join [temporaryDirectory] dummy] w] |
---|
| 197 | fconfigure $f -translation binary -encoding shiftjis |
---|
| 198 | puts -nonewline $f "ab\u4e4eg" |
---|
| 199 | close $f |
---|
| 200 | set f [open [file join [temporaryDirectory] dummy] r] |
---|
| 201 | fconfigure $f -translation binary -encoding iso8859-1 |
---|
| 202 | set x [read $f] |
---|
| 203 | close $f |
---|
| 204 | file delete [file join [temporaryDirectory] dummy] |
---|
| 205 | set x |
---|
| 206 | } "ab\x8c\xc1g" |
---|
| 207 | |
---|
| 208 | proc viewable {str} { |
---|
| 209 | set res "" |
---|
| 210 | foreach c [split $str {}] { |
---|
| 211 | if {[string is print $c] && [string is ascii $c]} { |
---|
| 212 | append res $c |
---|
| 213 | } else { |
---|
| 214 | append res "\\u[format %4.4x [scan $c %c]]" |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | return "$str ($res)" |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | test encoding-11.1 {LoadEncodingFile: unknown encoding} {testencoding} { |
---|
| 221 | set system [encoding system] |
---|
| 222 | set path [encoding dirs] |
---|
| 223 | encoding system iso8859-1 |
---|
| 224 | encoding dirs {} |
---|
| 225 | set x [list [catch {encoding convertto jis0208 \u4e4e} msg] $msg] |
---|
| 226 | encoding dirs $path |
---|
| 227 | encoding system $system |
---|
| 228 | lappend x [encoding convertto jis0208 \u4e4e] |
---|
| 229 | } {1 {unknown encoding "jis0208"} 8C} |
---|
| 230 | test encoding-11.2 {LoadEncodingFile: single-byte} { |
---|
| 231 | encoding convertfrom jis0201 \xa1 |
---|
| 232 | } "\uff61" |
---|
| 233 | test encoding-11.3 {LoadEncodingFile: double-byte} { |
---|
| 234 | encoding convertfrom jis0208 8C |
---|
| 235 | } "\u4e4e" |
---|
| 236 | test encoding-11.4 {LoadEncodingFile: multi-byte} { |
---|
| 237 | encoding convertfrom shiftjis \x8c\xc1 |
---|
| 238 | } "\u4e4e" |
---|
| 239 | test encoding-11.5 {LoadEncodingFile: escape file} { |
---|
| 240 | viewable [encoding convertto iso2022 \u4e4e] |
---|
| 241 | } [viewable "\x1b\$B8C\x1b(B"] |
---|
| 242 | test encoding-11.5.1 {LoadEncodingFile: escape file} { |
---|
| 243 | viewable [encoding convertto iso2022-jp \u4e4e] |
---|
| 244 | } [viewable "\x1b\$B8C\x1b(B"] |
---|
| 245 | test encoding-11.6 {LoadEncodingFile: invalid file} {testencoding} { |
---|
| 246 | set system [encoding system] |
---|
| 247 | set path [encoding dirs] |
---|
| 248 | encoding system identity |
---|
| 249 | cd [temporaryDirectory] |
---|
| 250 | encoding dirs [file join tmp encoding] |
---|
| 251 | makeDirectory tmp |
---|
| 252 | makeDirectory [file join tmp encoding] |
---|
| 253 | set f [open [file join tmp encoding splat.enc] w] |
---|
| 254 | fconfigure $f -translation binary |
---|
| 255 | puts $f "abcdefghijklmnop" |
---|
| 256 | close $f |
---|
| 257 | set x [list [catch {encoding convertto splat \u4e4e} msg] $msg] |
---|
| 258 | file delete [file join [temporaryDirectory] tmp encoding splat.enc] |
---|
| 259 | removeDirectory [file join tmp encoding] |
---|
| 260 | removeDirectory tmp |
---|
| 261 | cd [workingDirectory] |
---|
| 262 | encoding dirs $path |
---|
| 263 | encoding system $system |
---|
| 264 | set x |
---|
| 265 | } {1 {invalid encoding file "splat"}} |
---|
| 266 | |
---|
| 267 | # OpenEncodingFile is fully tested by the rest of the tests in this file. |
---|
| 268 | |
---|
| 269 | test encoding-12.1 {LoadTableEncoding: normal encoding} { |
---|
| 270 | set x [encoding convertto iso8859-3 \u120] |
---|
| 271 | append x [encoding convertto iso8859-3 \ud5] |
---|
| 272 | append x [encoding convertfrom iso8859-3 \xd5] |
---|
| 273 | } "\xd5?\u120" |
---|
| 274 | test encoding-12.2 {LoadTableEncoding: single-byte encoding} { |
---|
| 275 | set x [encoding convertto iso8859-3 ab\u0120g] |
---|
| 276 | append x [encoding convertfrom iso8859-3 ab\xd5g] |
---|
| 277 | } "ab\xd5gab\u120g" |
---|
| 278 | test encoding-12.3 {LoadTableEncoding: multi-byte encoding} { |
---|
| 279 | set x [encoding convertto shiftjis ab\u4e4eg] |
---|
| 280 | append x [encoding convertfrom shiftjis ab\x8c\xc1g] |
---|
| 281 | } "ab\x8c\xc1gab\u4e4eg" |
---|
| 282 | test encoding-12.4 {LoadTableEncoding: double-byte encoding} { |
---|
| 283 | set x [encoding convertto jis0208 \u4e4e\u3b1] |
---|
| 284 | append x [encoding convertfrom jis0208 8C&A] |
---|
| 285 | } "8C&A\u4e4e\u3b1" |
---|
| 286 | test encoding-12.5 {LoadTableEncoding: symbol encoding} { |
---|
| 287 | set x [encoding convertto symbol \u3b3] |
---|
| 288 | append x [encoding convertto symbol \u67] |
---|
| 289 | append x [encoding convertfrom symbol \x67] |
---|
| 290 | } "\x67\x67\u3b3" |
---|
| 291 | |
---|
| 292 | test encoding-13.1 {LoadEscapeTable} { |
---|
| 293 | viewable [set x [encoding convertto iso2022 ab\u4e4e\u68d9g]] |
---|
| 294 | } [viewable "ab\x1b\$B8C\x1b\$\(DD%\x1b(Bg"] |
---|
| 295 | |
---|
| 296 | test encoding-14.1 {BinaryProc} { |
---|
| 297 | encoding convertto identity \x12\x34\x56\xff\x69 |
---|
| 298 | } "\x12\x34\x56\xc3\xbf\x69" |
---|
| 299 | |
---|
| 300 | test encoding-15.1 {UtfToUtfProc} { |
---|
| 301 | encoding convertto utf-8 \xa3 |
---|
| 302 | } "\xc2\xa3" |
---|
| 303 | |
---|
| 304 | test encoding-15.2 {UtfToUtfProc null character output} { |
---|
| 305 | set x \u0000 |
---|
| 306 | set y [encoding convertto utf-8 \u0000] |
---|
| 307 | set y [encoding convertfrom identity $y] |
---|
| 308 | binary scan $y H* z |
---|
| 309 | list [string bytelength $x] [string bytelength $y] $z |
---|
| 310 | } {2 1 00} |
---|
| 311 | |
---|
| 312 | test encoding-15.3 {UtfToUtfProc null character input} { |
---|
| 313 | set x [encoding convertfrom identity \x00] |
---|
| 314 | set y [encoding convertfrom utf-8 $x] |
---|
| 315 | binary scan [encoding convertto identity $y] H* z |
---|
| 316 | list [string bytelength $x] [string bytelength $y] $z |
---|
| 317 | } {1 2 c080} |
---|
| 318 | |
---|
| 319 | test encoding-16.1 {UnicodeToUtfProc} { |
---|
| 320 | set val [encoding convertfrom unicode NN] |
---|
| 321 | list $val [format %x [scan $val %c]] |
---|
| 322 | } "\u4e4e 4e4e" |
---|
| 323 | |
---|
| 324 | test encoding-17.1 {UtfToUnicodeProc} { |
---|
| 325 | } {} |
---|
| 326 | |
---|
| 327 | test encoding-18.1 {TableToUtfProc} { |
---|
| 328 | } {} |
---|
| 329 | |
---|
| 330 | test encoding-19.1 {TableFromUtfProc} { |
---|
| 331 | } {} |
---|
| 332 | |
---|
| 333 | test encoding-20.1 {TableFreefProc} { |
---|
| 334 | } {} |
---|
| 335 | |
---|
| 336 | test encoding-21.1 {EscapeToUtfProc} { |
---|
| 337 | } {} |
---|
| 338 | |
---|
| 339 | test encoding-22.1 {EscapeFromUtfProc} { |
---|
| 340 | } {} |
---|
| 341 | |
---|
| 342 | set iso2022encData "\u001b\$B;d\$I\$b\$G\$O!\"%A%C%W\$49XF~;~\$K\$4EPO?\$\$\$?\$@\$\$\$?\$4=;=j\$r%-%c%C%7%e%\"%&%H\$N:]\$N\u001b(B |
---|
| 343 | \u001b\$B>.@Z<jAwIU@h\$H\$7\$F;HMQ\$7\$F\$*\$j\$^\$9!#62\$lF~\$j\$^\$9\$,!\"@5\$7\$\$=;=j\$r\$4EPO?\$7\$J\$*\u001b(B |
---|
| 344 | \u001b\$B\$*4j\$\$\$\$\$?\$7\$^\$9!#\$^\$?!\"BgJQ62=L\$G\$9\$,!\"=;=jJQ99\$N\$\"\$H!\"F|K\\8l%5!<%S%9It!J\u001b(B |
---|
| 345 | casino_japanese@___.com \u001b\$B!K\$^\$G\$4=;=jJQ99:Q\$NO\"Mm\$r\$\$\$?\$@\$1\$J\$\$\$G\u001b(B |
---|
| 346 | \u001b\$B\$7\$g\$&\$+!)\u001b(B" |
---|
| 347 | |
---|
| 348 | set iso2022uniData [encoding convertfrom iso2022-jp $iso2022encData] |
---|
| 349 | set iso2022uniData2 "\u79c1\u3069\u3082\u3067\u306f\u3001\u30c1\u30c3\u30d7\u3054\u8cfc\u5165\u6642\u306b\u3054\u767b\u9332\u3044\u305f\u3060\u3044\u305f\u3054\u4f4f\u6240\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u30a2\u30a6\u30c8\u306e\u969b\u306e |
---|
| 350 | \u5c0f\u5207\u624b\u9001\u4ed8\u5148\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u6050\u308c\u5165\u308a\u307e\u3059\u304c\u3001\u6b63\u3057\u3044\u4f4f\u6240\u3092\u3054\u767b\u9332\u3057\u306a\u304a |
---|
| 351 | \u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u5927\u5909\u6050\u7e2e\u3067\u3059\u304c\u3001\u4f4f\u6240\u5909\u66f4\u306e\u3042\u3068\u3001\u65e5\u672c\u8a9e\u30b5\u30fc\u30d3\u30b9\u90e8\uff08 |
---|
| 352 | \u0063\u0061\u0073\u0069\u006e\u006f\u005f\u006a\u0061\u0070\u0061\u006e\u0065\u0073\u0065\u0040\u005f\u005f\u005f\u002e\u0063\u006f\u006d\u0020\uff09\u307e\u3067\u3054\u4f4f\u6240\u5909\u66f4\u6e08\u306e\u9023\u7d61\u3092\u3044\u305f\u3060\u3051\u306a\u3044\u3067 |
---|
| 353 | \u3057\u3087\u3046\u304b\uff1f" |
---|
| 354 | |
---|
| 355 | cd [temporaryDirectory] |
---|
| 356 | set fid [open iso2022.txt w] |
---|
| 357 | fconfigure $fid -encoding binary |
---|
| 358 | puts -nonewline $fid $iso2022encData |
---|
| 359 | close $fid |
---|
| 360 | |
---|
| 361 | test encoding-23.1 {iso2022-jp escape encoding test} { |
---|
| 362 | string equal $iso2022uniData $iso2022uniData2 |
---|
| 363 | } 1 |
---|
| 364 | test encoding-23.2 {iso2022-jp escape encoding test} { |
---|
| 365 | # This checks that 'gets' isn't resetting the encoding inappropriately. |
---|
| 366 | # [Bug #523988] |
---|
| 367 | set fid [open iso2022.txt r] |
---|
| 368 | fconfigure $fid -encoding iso2022-jp |
---|
| 369 | set out "" |
---|
| 370 | set count 0 |
---|
| 371 | while {[set num [gets $fid line]] >= 0} { |
---|
| 372 | if {$count} { |
---|
| 373 | incr count 1 ; # account for newline |
---|
| 374 | append out \n |
---|
| 375 | } |
---|
| 376 | append out $line |
---|
| 377 | incr count $num |
---|
| 378 | } |
---|
| 379 | close $fid |
---|
| 380 | if {[string compare $iso2022uniData $out]} { |
---|
| 381 | return -code error "iso2022-jp read in doesn't match original" |
---|
| 382 | } |
---|
| 383 | list $count $out |
---|
| 384 | } [list [string length $iso2022uniData] $iso2022uniData] |
---|
| 385 | test encoding-23.3 {iso2022-jp escape encoding test} { |
---|
| 386 | # read $fis <size> reads size in chars, not raw bytes. |
---|
| 387 | set fid [open iso2022.txt r] |
---|
| 388 | fconfigure $fid -encoding iso2022-jp |
---|
| 389 | set data [read $fid 50] |
---|
| 390 | close $fid |
---|
| 391 | set data |
---|
| 392 | } [string range $iso2022uniData 0 49] ; # 0 .. 49 inclusive == 50 |
---|
| 393 | cd [workingDirectory] |
---|
| 394 | |
---|
| 395 | test encoding-24.1 {EscapeFreeProc on open channels} -constraints { |
---|
| 396 | exec |
---|
| 397 | } -setup { |
---|
| 398 | # Bug #524674 input |
---|
| 399 | set file [makeFile { |
---|
| 400 | set f [open [file join [file dirname [info script]] iso2022.txt]] |
---|
| 401 | fconfigure $f -encoding iso2022-jp |
---|
| 402 | gets $f |
---|
| 403 | } iso2022.tcl] |
---|
| 404 | } -body { |
---|
| 405 | exec [interpreter] $file |
---|
| 406 | } -cleanup { |
---|
| 407 | removeFile iso2022.tcl |
---|
| 408 | } -result {} |
---|
| 409 | |
---|
| 410 | test encoding-24.2 {EscapeFreeProc on open channels} -constraints { |
---|
| 411 | exec |
---|
| 412 | } -setup { |
---|
| 413 | # Bug #524674 output |
---|
| 414 | set file [makeFile { |
---|
| 415 | fconfigure stdout -encoding iso2022-jp |
---|
| 416 | puts ab\u4e4e\u68d9g |
---|
| 417 | exit |
---|
| 418 | } iso2022.tcl] |
---|
| 419 | } -body { |
---|
| 420 | viewable [exec [interpreter] $file] |
---|
| 421 | } -cleanup { |
---|
| 422 | removeFile iso2022.tcl |
---|
| 423 | } -result "ab\x1b\$B8C\x1b\$(DD%\x1b(Bg (ab\\u001b\$B8C\\u001b\$(DD%\\u001b(Bg)" |
---|
| 424 | |
---|
| 425 | test encoding-24.3 {EscapeFreeProc on open channels} {stdio} { |
---|
| 426 | # Bug #219314 - if we don't free escape encodings correctly on |
---|
| 427 | # channel closure, we go boom |
---|
| 428 | set file [makeFile { |
---|
| 429 | encoding system iso2022-jp |
---|
| 430 | set a "\u4e4e\u4e5e\u4e5f"; # 3 Japanese Kanji letters |
---|
| 431 | puts $a |
---|
| 432 | } iso2022.tcl] |
---|
| 433 | set f [open "|[list [interpreter] $file]"] |
---|
| 434 | fconfigure $f -encoding iso2022-jp |
---|
| 435 | set count [gets $f line] |
---|
| 436 | close $f |
---|
| 437 | removeFile iso2022.tcl |
---|
| 438 | list $count [viewable $line] |
---|
| 439 | } [list 3 "\u4e4e\u4e5e\u4e5f (\\u4e4e\\u4e5e\\u4e5f)"] |
---|
| 440 | |
---|
| 441 | file delete [file join [temporaryDirectory] iso2022.txt] |
---|
| 442 | |
---|
| 443 | # |
---|
| 444 | # Begin jajp encoding round-trip conformity tests |
---|
| 445 | # |
---|
| 446 | proc foreach-jisx0208 {varName command} { |
---|
| 447 | upvar 1 $varName code |
---|
| 448 | foreach range { |
---|
| 449 | {2121 217E} |
---|
| 450 | {2221 222E} |
---|
| 451 | {223A 2241} |
---|
| 452 | {224A 2250} |
---|
| 453 | {225C 226A} |
---|
| 454 | {2272 2279} |
---|
| 455 | {227E 227E} |
---|
| 456 | {2330 2339} |
---|
| 457 | {2421 2473} |
---|
| 458 | {2521 2576} |
---|
| 459 | {2821 2821} |
---|
| 460 | {282C 282C} |
---|
| 461 | {2837 2837} |
---|
| 462 | |
---|
| 463 | {30 21 4E 7E} |
---|
| 464 | {4F21 4F53} |
---|
| 465 | |
---|
| 466 | {50 21 73 7E} |
---|
| 467 | {7421 7426} |
---|
| 468 | } { |
---|
| 469 | if {[llength $range] == 2} { |
---|
| 470 | # for adhoc range. simple {first last}. inclusive. |
---|
| 471 | set first [scan [lindex $range 0] %x] |
---|
| 472 | set last [scan [lindex $range 1] %x] |
---|
| 473 | for {set i $first} {$i <= $last} {incr i} { |
---|
| 474 | set code $i |
---|
| 475 | uplevel 1 $command |
---|
| 476 | } |
---|
| 477 | } elseif {[llength $range] == 4} { |
---|
| 478 | # for uniform range. |
---|
| 479 | set h0 [scan [lindex $range 0] %x] |
---|
| 480 | set l0 [scan [lindex $range 1] %x] |
---|
| 481 | set hend [scan [lindex $range 2] %x] |
---|
| 482 | set lend [scan [lindex $range 3] %x] |
---|
| 483 | for {set hi $h0} {$hi <= $hend} {incr hi} { |
---|
| 484 | for {set lo $l0} {$lo <= $lend} {incr lo} { |
---|
| 485 | set code [expr {$hi << 8 | ($lo & 0xff)}] |
---|
| 486 | uplevel 1 $command |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | } else { |
---|
| 490 | error "really?" |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | proc gen-jisx0208-euc-jp {code} { |
---|
| 495 | binary format cc \ |
---|
| 496 | [expr {($code >> 8) | 0x80}] [expr {($code & 0xff) | 0x80}] |
---|
| 497 | } |
---|
| 498 | proc gen-jisx0208-iso2022-jp {code} { |
---|
| 499 | binary format a3cca3 \ |
---|
| 500 | "\x1b\$B" [expr {$code >> 8}] [expr {$code & 0xff}] "\x1b(B" |
---|
| 501 | } |
---|
| 502 | proc gen-jisx0208-cp932 {code} { |
---|
| 503 | set c1 [expr {($code >> 8) | 0x80}] |
---|
| 504 | set c2 [expr {($code & 0xff)| 0x80}] |
---|
| 505 | if {$c1 % 2} { |
---|
| 506 | set c1 [expr {($c1 >> 1) + ($c1 < 0xdf ? 0x31 : 0x71)}] |
---|
| 507 | incr c2 [expr {- (0x60 + ($c2 < 0xe0))}] |
---|
| 508 | } else { |
---|
| 509 | set c1 [expr {($c1 >> 1) + ($c1 < 0xdf ? 0x30 : 0x70)}] |
---|
| 510 | incr c2 -2 |
---|
| 511 | } |
---|
| 512 | binary format cc $c1 $c2 |
---|
| 513 | } |
---|
| 514 | proc channel-diff {fa fb} { |
---|
| 515 | set diff {} |
---|
| 516 | while {[gets $fa la] >= 0 && [gets $fb lb] >= 0} { |
---|
| 517 | if {[string compare $la $lb] == 0} continue |
---|
| 518 | # lappend diff $la $lb |
---|
| 519 | |
---|
| 520 | # For more readable (easy to analyze) output. |
---|
| 521 | set code [lindex $la 0] |
---|
| 522 | binary scan [lindex $la 1] H* expected |
---|
| 523 | binary scan [lindex $lb 1] H* got |
---|
| 524 | lappend diff [list $code $expected $got] |
---|
| 525 | } |
---|
| 526 | set diff |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | # Create char tables. |
---|
| 530 | cd [temporaryDirectory] |
---|
| 531 | foreach enc {cp932 euc-jp iso2022-jp} { |
---|
| 532 | set f [open $enc.chars w] |
---|
| 533 | fconfigure $f -encoding binary |
---|
| 534 | foreach-jisx0208 code { |
---|
| 535 | puts $f [format "%04X %s" $code [gen-jisx0208-$enc $code]] |
---|
| 536 | } |
---|
| 537 | close $f |
---|
| 538 | } |
---|
| 539 | # shiftjis == cp932 for jisx0208. |
---|
| 540 | file copy -force cp932.chars shiftjis.chars |
---|
| 541 | |
---|
| 542 | set NUM 0 |
---|
| 543 | foreach from {cp932 shiftjis euc-jp iso2022-jp} { |
---|
| 544 | foreach to {cp932 shiftjis euc-jp iso2022-jp} { |
---|
| 545 | test encoding-25.[incr NUM] "jisx0208 $from => $to" { |
---|
| 546 | cd [temporaryDirectory] |
---|
| 547 | set f [open $from.chars] |
---|
| 548 | fconfigure $f -encoding $from |
---|
| 549 | set out [open $from.$to.tcltestout w] |
---|
| 550 | fconfigure $out -encoding $to |
---|
| 551 | puts -nonewline $out [read $f] |
---|
| 552 | close $out |
---|
| 553 | close $f |
---|
| 554 | |
---|
| 555 | # then compare $to.chars <=> $from.to.tcltestout as binary. |
---|
| 556 | set fa [open $to.chars] |
---|
| 557 | fconfigure $fa -encoding binary |
---|
| 558 | set fb [open $from.$to.tcltestout] |
---|
| 559 | fconfigure $fb -encoding binary |
---|
| 560 | set diff [channel-diff $fa $fb] |
---|
| 561 | close $fa |
---|
| 562 | close $fb |
---|
| 563 | |
---|
| 564 | # Difference should be empty. |
---|
| 565 | set diff |
---|
| 566 | } {} |
---|
| 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | testConstraint testgetdefenc [llength [info commands testgetdefenc]] |
---|
| 571 | |
---|
| 572 | test encoding-26.0 {Tcl_GetDefaultEncodingDir} -constraints { |
---|
| 573 | testgetdefenc |
---|
| 574 | } -setup { |
---|
| 575 | set origDir [testgetdefenc] |
---|
| 576 | testsetdefenc slappy |
---|
| 577 | } -body { |
---|
| 578 | testgetdefenc |
---|
| 579 | } -cleanup { |
---|
| 580 | testsetdefenc $origDir |
---|
| 581 | } -result slappy |
---|
| 582 | |
---|
| 583 | file delete {*}[glob -directory [temporaryDirectory] *.chars *.tcltestout] |
---|
| 584 | # ===> Cut here <=== |
---|
| 585 | |
---|
| 586 | # EscapeFreeProc, GetTableEncoding, unilen |
---|
| 587 | # are fully tested by the rest of this file |
---|
| 588 | } |
---|
| 589 | runtests |
---|
| 590 | |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | # cleanup |
---|
| 594 | namespace delete ::tcl::test::encoding |
---|
| 595 | ::tcltest::cleanupTests |
---|
| 596 | return |
---|