1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 15.5.4.12-2.js 24 ECMA Section: 15.5.4.12 String.prototype.toUpperCase() 25 Description: 26 27 Returns a string equal in length to the length of the result of converting 28 this object to a string. The result is a string value, not a String object. 29 30 Every character of the result is equal to the corresponding character of the 31 string, unless that character has a Unicode 2.0 uppercase equivalent, in which 32 case the uppercase equivalent is used instead. (The canonical Unicode 2.0 case 33 mapping shall be used, which does not depend on implementation or locale.) 34 35 Note that the toUpperCase function is intentionally generic; it does not require 36 that its this value be a String object. Therefore it can be transferred to other 37 kinds of objects for use as a method. 38 39 Author: christine (at) netscape.com 40 Date: 12 november 1997 41 */ 42 43 var SECTION = "15.5.4.12-2"; 44 var VERSION = "ECMA_1"; 45 startTest(); 46 var TITLE = "String.prototype.toUpperCase()"; 47 48 writeHeaderToLog( SECTION + " "+ TITLE); 49 50 var testcases = getTestCases(); 51 test(); 52 53 function getTestCases() { 54 var array = new Array(); 55 var item = 0; 56 57 var TEST_STRING = ""; 58 var EXPECT_STRING = ""; 59 60 // basic latin test 61 62 for ( var i = 0; i < 0x007A; i++ ) { 63 var u = new Unicode(i); 64 TEST_STRING += String.fromCharCode(i); 65 EXPECT_STRING += String.fromCharCode( u.upper ); 66 } 67 68 // don't print out the value of the strings since they contain control 69 // characters that break the driver 70 var isEqual = EXPECT_STRING == (new String( TEST_STRING )).toUpperCase(); 71 72 array[item++] = new TestCase( SECTION, 73 "isEqual", 74 true, 75 isEqual); 76 return array; 77 } 78 function test() { 79 for ( tc=0; tc < testcases.length; tc++ ) { 80 testcases[tc].passed = writeTestCaseResult( 81 testcases[tc].expect, 82 testcases[tc].actual, 83 testcases[tc].description +" = "+ 84 testcases[tc].actual ); 85 86 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 87 } 88 stopTest(); 89 return ( testcases ); 90 } 91 function MyObject( value ) { 92 this.value = value; 93 this.substring = String.prototype.substring; 94 this.toString = new Function ( "return this.value+''" ); 95 } 96 function Unicode( c ) { 97 u = GetUnicodeValues( c ); 98 this.upper = u[0]; 99 this.lower = u[1] 100 return this; 101 } 102 function GetUnicodeValues( c ) { 103 u = new Array(); 104 105 u[0] = c; 106 u[1] = c; 107 108 // upper case Basic Latin 109 110 if ( c >= 0x0041 && c <= 0x005A) { 111 u[0] = c; 112 u[1] = c + 32; 113 return u; 114 } 115 116 // lower case Basic Latin 117 if ( c >= 0x0061 && c <= 0x007a ) { 118 u[0] = c - 32; 119 u[1] = c; 120 return u; 121 } 122 123 // upper case Latin-1 Supplement 124 if ( c == 0x00B5 ) { 125 u[0] = 0x039C; 126 u[1] = c; 127 return u; 128 } 129 if ( (c >= 0x00C0 && c <= 0x00D6) || (c >= 0x00D8 && c<=0x00DE) ) { 130 u[0] = c; 131 u[1] = c + 32; 132 return u; 133 } 134 135 // lower case Latin-1 Supplement 136 if ( (c >= 0x00E0 && c <= 0x00F6) || (c >= 0x00F8 && c <= 0x00FE) ) { 137 u[0] = c - 32; 138 u[1] = c; 139 return u; 140 } 141 if ( c == 0x00FF ) { 142 u[0] = 0x0178; 143 u[1] = c; 144 return u; 145 } 146 // Latin Extended A 147 if ( (c >= 0x0100 && c < 0x0138) || (c > 0x0149 && c < 0x0178) ) { 148 // special case for capital I 149 if ( c == 0x0130 ) { 150 u[0] = c; 151 u[1] = 0x0069; 152 return u; 153 } 154 if ( c == 0x0131 ) { 155 u[0] = 0x0049; 156 u[1] = c; 157 return u; 158 } 159 160 if ( c % 2 == 0 ) { 161 // if it's even, it's a capital and the lower case is c +1 162 u[0] = c; 163 u[1] = c+1; 164 } else { 165 // if it's odd, it's a lower case and upper case is c-1 166 u[0] = c-1; 167 u[1] = c; 168 } 169 return u; 170 } 171 if ( c == 0x0178 ) { 172 u[0] = c; 173 u[1] = 0x00FF; 174 return u; 175 } 176 177 if ( (c >= 0x0139 && c < 0x0149) || (c > 0x0178 && c < 0x017F) ) { 178 if ( c % 2 == 1 ) { 179 // if it's odd, it's a capital and the lower case is c +1 180 u[0] = c; 181 u[1] = c+1; 182 } else { 183 // if it's even, it's a lower case and upper case is c-1 184 u[0] = c-1; 185 u[1] = c; 186 } 187 return u; 188 } 189 if ( c == 0x017F ) { 190 u[0] = 0x0053; 191 u[1] = c; 192 } 193 194 // Latin Extended B 195 // need to improve this set 196 197 if ( c >= 0x0200 && c <= 0x0217 ) { 198 if ( c % 2 == 0 ) { 199 u[0] = c; 200 u[1] = c+1; 201 } else { 202 u[0] = c-1; 203 u[1] = c; 204 } 205 return u; 206 } 207 208 // Latin Extended Additional 209 // Range: U+1E00 to U+1EFF 210 // http://www.unicode.org/Unicode.charts/glyphless/U1E00.html 211 212 // Spacing Modifier Leters 213 // Range: U+02B0 to U+02FF 214 215 // Combining Diacritical Marks 216 // Range: U+0300 to U+036F 217 218 // skip Greek for now 219 // Greek 220 // Range: U+0370 to U+03FF 221 222 // Cyrillic 223 // Range: U+0400 to U+04FF 224 225 if ( c >= 0x0400 && c <= 0x040F) { 226 u[0] = c; 227 u[1] = c + 80; 228 return u; 229 } 230 231 232 if ( c >= 0x0410 && c <= 0x042F ) { 233 u[0] = c; 234 u[1] = c + 32; 235 return u; 236 } 237 238 if ( c >= 0x0430 && c<= 0x044F ) { 239 u[0] = c - 32; 240 u[1] = c; 241 return u; 242 243 } 244 if ( c >= 0x0450 && c<= 0x045F ) { 245 u[0] = c -80; 246 u[1] = c; 247 return u; 248 } 249 250 if ( c >= 0x0460 && c <= 0x047F ) { 251 if ( c % 2 == 0 ) { 252 u[0] = c; 253 u[1] = c +1; 254 } else { 255 u[0] = c - 1; 256 u[1] = c; 257 } 258 return u; 259 } 260 261 // Armenian 262 // Range: U+0530 to U+058F 263 if ( c >= 0x0531 && c <= 0x0556 ) { 264 u[0] = c; 265 u[1] = c + 48; 266 return u; 267 } 268 if ( c >= 0x0561 && c < 0x0587 ) { 269 u[0] = c - 48; 270 u[1] = c; 271 return u; 272 } 273 274 // Hebrew 275 // Range: U+0590 to U+05FF 276 277 278 // Arabic 279 // Range: U+0600 to U+06FF 280 281 // Devanagari 282 // Range: U+0900 to U+097F 283 284 285 // Bengali 286 // Range: U+0980 to U+09FF 287 288 289 // Gurmukhi 290 // Range: U+0A00 to U+0A7F 291 292 293 // Gujarati 294 // Range: U+0A80 to U+0AFF 295 296 297 // Oriya 298 // Range: U+0B00 to U+0B7F 299 // no capital / lower case 300 301 302 // Tamil 303 // Range: U+0B80 to U+0BFF 304 // no capital / lower case 305 306 307 // Telugu 308 // Range: U+0C00 to U+0C7F 309 // no capital / lower case 310 311 312 // Kannada 313 // Range: U+0C80 to U+0CFF 314 // no capital / lower case 315 316 317 // Malayalam 318 // Range: U+0D00 to U+0D7F 319 320 // Thai 321 // Range: U+0E00 to U+0E7F 322 323 324 // Lao 325 // Range: U+0E80 to U+0EFF 326 327 328 // Tibetan 329 // Range: U+0F00 to U+0FBF 330 331 // Georgian 332 // Range: U+10A0 to U+10F0 333 334 // Hangul Jamo 335 // Range: U+1100 to U+11FF 336 337 // Greek Extended 338 // Range: U+1F00 to U+1FFF 339 // skip for now 340 341 342 // General Punctuation 343 // Range: U+2000 to U+206F 344 345 // Superscripts and Subscripts 346 // Range: U+2070 to U+209F 347 348 // Currency Symbols 349 // Range: U+20A0 to U+20CF 350 351 352 // Combining Diacritical Marks for Symbols 353 // Range: U+20D0 to U+20FF 354 // skip for now 355 356 357 // Number Forms 358 // Range: U+2150 to U+218F 359 // skip for now 360 361 362 // Arrows 363 // Range: U+2190 to U+21FF 364 365 // Mathematical Operators 366 // Range: U+2200 to U+22FF 367 368 // Miscellaneous Technical 369 // Range: U+2300 to U+23FF 370 371 // Control Pictures 372 // Range: U+2400 to U+243F 373 374 // Optical Character Recognition 375 // Range: U+2440 to U+245F 376 377 // Enclosed Alphanumerics 378 // Range: U+2460 to U+24FF 379 380 // Box Drawing 381 // Range: U+2500 to U+257F 382 383 // Block Elements 384 // Range: U+2580 to U+259F 385 386 // Geometric Shapes 387 // Range: U+25A0 to U+25FF 388 389 // Miscellaneous Symbols 390 // Range: U+2600 to U+26FF 391 392 // Dingbats 393 // Range: U+2700 to U+27BF 394 395 // CJK Symbols and Punctuation 396 // Range: U+3000 to U+303F 397 398 // Hiragana 399 // Range: U+3040 to U+309F 400 401 // Katakana 402 // Range: U+30A0 to U+30FF 403 404 // Bopomofo 405 // Range: U+3100 to U+312F 406 407 // Hangul Compatibility Jamo 408 // Range: U+3130 to U+318F 409 410 // Kanbun 411 // Range: U+3190 to U+319F 412 413 414 // Enclosed CJK Letters and Months 415 // Range: U+3200 to U+32FF 416 417 // CJK Compatibility 418 // Range: U+3300 to U+33FF 419 420 // Hangul Syllables 421 // Range: U+AC00 to U+D7A3 422 423 // High Surrogates 424 // Range: U+D800 to U+DB7F 425 426 // Private Use High Surrogates 427 // Range: U+DB80 to U+DBFF 428 429 // Low Surrogates 430 // Range: U+DC00 to U+DFFF 431 432 // Private Use Area 433 // Range: U+E000 to U+F8FF 434 435 // CJK Compatibility Ideographs 436 // Range: U+F900 to U+FAFF 437 438 // Alphabetic Presentation Forms 439 // Range: U+FB00 to U+FB4F 440 441 // Arabic Presentation Forms-A 442 // Range: U+FB50 to U+FDFF 443 444 // Combining Half Marks 445 // Range: U+FE20 to U+FE2F 446 447 // CJK Compatibility Forms 448 // Range: U+FE30 to U+FE4F 449 450 // Small Form Variants 451 // Range: U+FE50 to U+FE6F 452 453 // Arabic Presentation Forms-B 454 // Range: U+FE70 to U+FEFF 455 456 // Halfwidth and Fullwidth Forms 457 // Range: U+FF00 to U+FFEF 458 459 if ( c >= 0xFF21 && c <= 0xFF3A ) { 460 u[0] = c; 461 u[1] = c + 32; 462 return u; 463 } 464 465 if ( c >= 0xFF41 && c <= 0xFF5A ) { 466 u[0] = c - 32; 467 u[1] = c; 468 return u; 469 } 470 471 // Specials 472 // Range: U+FFF0 to U+FFFF 473 474 return u; 475 } 476 477 function DecimalToHexString( n ) { 478 n = Number( n ); 479 var h = "0x"; 480 481 for ( var i = 3; i >= 0; i-- ) { 482 if ( n >= Math.pow(16, i) ){ 483 var t = Math.floor( n / Math.pow(16, i)); 484 n -= t * Math.pow(16, i); 485 if ( t >= 10 ) { 486 if ( t == 10 ) { 487 h += "A"; 488 } 489 if ( t == 11 ) { 490 h += "B"; 491 } 492 if ( t == 12 ) { 493 h += "C"; 494 } 495 if ( t == 13 ) { 496 h += "D"; 497 } 498 if ( t == 14 ) { 499 h += "E"; 500 } 501 if ( t == 15 ) { 502 h += "F"; 503 } 504 } else { 505 h += String( t ); 506 } 507 } else { 508 h += "0"; 509 } 510 } 511 512 return h; 513 } 514 515