• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-1.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@netscape.com
40    Date:               12 november 1997
41*/
42
43    var SECTION = "15.5.4.12-1";
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
53function getTestCases() {
54    var array = new Array();
55    var item = 0;
56
57    array[item++] = new TestCase( SECTION,  "String.prototype.toUpperCase.length",        0,          String.prototype.toUpperCase.length );
58    array[item++] = new TestCase( SECTION,  "delete String.prototype.toUpperCase.length", false,      delete String.prototype.toUpperCase.length );
59    array[item++] = new TestCase( SECTION,  "delete String.prototype.toupperCase.length; String.prototype.toupperCase.length", 0,      eval("delete String.prototype.toUpperCase.length; String.prototype.toUpperCase.length") );
60
61    // Basic Latin, Latin-1 Supplement, Latin Extended A
62    for ( var i = 0; i <= 0x017f; i++ ) {
63        var U = new Unicode( i );
64
65        // XXX DF fails in java
66
67        if ( i == 0x00DF ) {
68            continue;
69        }
70
71
72        array[item++] = new TestCase(   SECTION,
73                                        "var s = new String( String.fromCharCode("+i+") ); s.toUpperCase().charCodeAt(0)",
74                                        U.upper,
75                                        eval("var s = new String( String.fromCharCode(i) ); s.toUpperCase().charCodeAt(0)") );
76    }
77
78    return array;
79}
80function test() {
81    for ( tc=0; tc < testcases.length; tc++ ) {
82        testcases[tc].passed = writeTestCaseResult(
83                            testcases[tc].expect,
84                            testcases[tc].actual,
85                            testcases[tc].description +" = "+
86                            testcases[tc].actual );
87
88        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
89    }
90    stopTest();
91    return ( testcases );
92}
93function MyObject( value ) {
94    this.value = value;
95    this.substring = String.prototype.substring;
96    this.toString = new Function ( "return this.value+''" );
97}
98function Unicode( c ) {
99    u = GetUnicodeValues( c );
100    this.upper = u[0];
101    this.lower = u[1]
102    return this;
103}
104function GetUnicodeValues( c ) {
105    u = new Array();
106
107    u[0] = c;
108    u[1] = c;
109
110    // upper case Basic Latin
111
112    if ( c >= 0x0041 && c <= 0x005A) {
113        u[0] = c;
114        u[1] = c + 32;
115        return u;
116    }
117
118    // lower case Basic Latin
119    if ( c >= 0x0061 && c <= 0x007a ) {
120        u[0] = c - 32;
121        u[1] = c;
122        return u;
123    }
124
125    // upper case Latin-1 Supplement
126    if ( c == 0x00B5 ) {
127        u[0] = 0x039C;
128        u[1] = c;
129        return u;
130    }
131    if ( (c >= 0x00C0 && c <= 0x00D6) || (c >= 0x00D8 && c<=0x00DE) ) {
132        u[0] = c;
133        u[1] = c + 32;
134        return u;
135    }
136
137    // lower case Latin-1 Supplement
138    if ( (c >= 0x00E0 && c <= 0x00F6) || (c >= 0x00F8 && c <= 0x00FE) ) {
139        u[0] = c - 32;
140        u[1] = c;
141        return u;
142    }
143    if ( c == 0x00FF ) {
144        u[0] = 0x0178;
145        u[1] = c;
146        return u;
147    }
148    // Latin Extended A
149    if ( (c >= 0x0100 && c < 0x0138) || (c > 0x0149 && c < 0x0178) ) {
150        // special case for capital I
151        if ( c == 0x0130 ) {
152            u[0] = c;
153            u[1] = 0x0069;
154            return u;
155        }
156        if ( c == 0x0131 ) {
157            u[0] = 0x0049;
158            u[1] = c;
159            return u;
160        }
161
162        if ( c % 2 == 0 ) {
163        // if it's even, it's a capital and the lower case is c +1
164            u[0] = c;
165            u[1] = c+1;
166        } else {
167        // if it's odd, it's a lower case and upper case is c-1
168            u[0] = c-1;
169            u[1] = c;
170        }
171        return u;
172    }
173    if ( c == 0x0178 ) {
174        u[0] = c;
175        u[1] = 0x00FF;
176        return u;
177    }
178
179    // LATIN SMALL LETTER N PRECEDED BY APOSTROPHE, uppercase takes two code points
180    if (c == 0x0149) {
181        u[0] = 0x02bc;
182        u[1] = c;
183        return u;
184    }
185
186    if ( (c >= 0x0139 && c < 0x0149) || (c > 0x0178 && c < 0x017F) ) {
187        if ( c % 2 == 1 ) {
188        // if it's odd, it's a capital and the lower case is c +1
189            u[0] = c;
190            u[1] = c+1;
191        } else {
192        // if it's even, it's a lower case and upper case is c-1
193            u[0] = c-1;
194            u[1] = c;
195        }
196        return u;
197    }
198    if ( c == 0x017F ) {
199        u[0] = 0x0053;
200        u[1] = c;
201    }
202
203    // Latin Extended B
204    // need to improve this set
205
206    if ( c >= 0x0200 && c <= 0x0217 ) {
207        if ( c % 2 == 0 ) {
208            u[0] = c;
209            u[1] = c+1;
210        } else {
211            u[0] = c-1;
212            u[1] = c;
213        }
214        return u;
215    }
216
217    // Latin Extended Additional
218    // Range: U+1E00 to U+1EFF
219    // http://www.unicode.org/Unicode.charts/glyphless/U1E00.html
220
221    // Spacing Modifier Leters
222    // Range: U+02B0 to U+02FF
223
224    // Combining Diacritical Marks
225    // Range: U+0300 to U+036F
226
227    // skip Greek for now
228    // Greek
229    // Range: U+0370 to U+03FF
230
231    // Cyrillic
232    // Range: U+0400 to U+04FF
233
234    if ( c >= 0x0400 && c <= 0x040F) {
235        u[0] = c;
236        u[1] = c + 80;
237        return u;
238    }
239
240
241    if ( c >= 0x0410  && c <= 0x042F ) {
242        u[0] = c;
243        u[1] = c + 32;
244        return u;
245    }
246
247    if ( c >= 0x0430 && c<= 0x044F ) {
248        u[0] = c - 32;
249        u[1] = c;
250        return u;
251
252    }
253    if ( c >= 0x0450 && c<= 0x045F ) {
254        u[0] = c -80;
255        u[1] = c;
256        return u;
257    }
258
259    if ( c >= 0x0460 && c <= 0x047F ) {
260        if ( c % 2 == 0 ) {
261            u[0] = c;
262            u[1] = c +1;
263        } else {
264            u[0] = c - 1;
265            u[1] = c;
266        }
267        return u;
268    }
269
270    // Armenian
271    // Range: U+0530 to U+058F
272    if ( c >= 0x0531 && c <= 0x0556 ) {
273        u[0] = c;
274        u[1] = c + 48;
275        return u;
276    }
277    if ( c >= 0x0561 && c < 0x0587 ) {
278        u[0] = c - 48;
279        u[1] = c;
280        return u;
281    }
282    if (c == 0x0587) {
283        u[0] = 0x0535;
284        u[1] = c;
285        return u;
286    }
287
288    // Hebrew
289    // Range: U+0590 to U+05FF
290
291
292    // Arabic
293    // Range: U+0600 to U+06FF
294
295    // Devanagari
296    // Range: U+0900 to U+097F
297
298
299    // Bengali
300    // Range: U+0980 to U+09FF
301
302
303    // Gurmukhi
304    // Range: U+0A00 to U+0A7F
305
306
307    // Gujarati
308    // Range: U+0A80 to U+0AFF
309
310
311    // Oriya
312    // Range: U+0B00 to U+0B7F
313    // no capital / lower case
314
315
316    // Tamil
317    // Range: U+0B80 to U+0BFF
318    // no capital / lower case
319
320
321    // Telugu
322    // Range: U+0C00 to U+0C7F
323    // no capital / lower case
324
325
326    // Kannada
327    // Range: U+0C80 to U+0CFF
328    // no capital / lower case
329
330
331    // Malayalam
332    // Range: U+0D00 to U+0D7F
333
334    // Thai
335    // Range: U+0E00 to U+0E7F
336
337
338    // Lao
339    // Range: U+0E80 to U+0EFF
340
341
342    // Tibetan
343    // Range: U+0F00 to U+0FBF
344
345    // Georgian
346    // Range: U+10A0 to U+10F0
347
348    // Hangul Jamo
349    // Range: U+1100 to U+11FF
350
351    // Greek Extended
352    // Range: U+1F00 to U+1FFF
353    // skip for now
354
355
356    // General Punctuation
357    // Range: U+2000 to U+206F
358
359    // Superscripts and Subscripts
360    // Range: U+2070 to U+209F
361
362    // Currency Symbols
363    // Range: U+20A0 to U+20CF
364
365
366    // Combining Diacritical Marks for Symbols
367    // Range: U+20D0 to U+20FF
368    // skip for now
369
370
371    // Number Forms
372    // Range: U+2150 to U+218F
373    // skip for now
374
375
376    // Arrows
377    // Range: U+2190 to U+21FF
378
379    // Mathematical Operators
380    // Range: U+2200 to U+22FF
381
382    // Miscellaneous Technical
383    // Range: U+2300 to U+23FF
384
385    // Control Pictures
386    // Range: U+2400 to U+243F
387
388    // Optical Character Recognition
389    // Range: U+2440 to U+245F
390
391    // Enclosed Alphanumerics
392    // Range: U+2460 to U+24FF
393
394    // Box Drawing
395    // Range: U+2500 to U+257F
396
397    // Block Elements
398    // Range: U+2580 to U+259F
399
400    // Geometric Shapes
401    // Range: U+25A0 to U+25FF
402
403    // Miscellaneous Symbols
404    // Range: U+2600 to U+26FF
405
406    // Dingbats
407    // Range: U+2700 to U+27BF
408
409    // CJK Symbols and Punctuation
410    // Range: U+3000 to U+303F
411
412    // Hiragana
413    // Range: U+3040 to U+309F
414
415    // Katakana
416    // Range: U+30A0 to U+30FF
417
418    // Bopomofo
419    // Range: U+3100 to U+312F
420
421    // Hangul Compatibility Jamo
422    // Range: U+3130 to U+318F
423
424    // Kanbun
425    // Range: U+3190 to U+319F
426
427
428    // Enclosed CJK Letters and Months
429    // Range: U+3200 to U+32FF
430
431    // CJK Compatibility
432    // Range: U+3300 to U+33FF
433
434    // Hangul Syllables
435    // Range: U+AC00 to U+D7A3
436
437    // High Surrogates
438    // Range: U+D800 to U+DB7F
439
440    // Private Use High Surrogates
441    // Range: U+DB80 to U+DBFF
442
443    // Low Surrogates
444    // Range: U+DC00 to U+DFFF
445
446    // Private Use Area
447    // Range: U+E000 to U+F8FF
448
449    // CJK Compatibility Ideographs
450    // Range: U+F900 to U+FAFF
451
452    // Alphabetic Presentation Forms
453    // Range: U+FB00 to U+FB4F
454
455    // Arabic Presentation Forms-A
456    // Range: U+FB50 to U+FDFF
457
458    // Combining Half Marks
459    // Range: U+FE20 to U+FE2F
460
461    // CJK Compatibility Forms
462    // Range: U+FE30 to U+FE4F
463
464    // Small Form Variants
465    // Range: U+FE50 to U+FE6F
466
467    // Arabic Presentation Forms-B
468    // Range: U+FE70 to U+FEFF
469
470    // Halfwidth and Fullwidth Forms
471    // Range: U+FF00 to U+FFEF
472
473    if ( c >= 0xFF21 && c <= 0xFF3A ) {
474        u[0] = c;
475        u[1] = c + 32;
476        return u;
477    }
478
479    if ( c >= 0xFF41 && c <= 0xFF5A ) {
480        u[0] = c - 32;
481        u[1] = c;
482        return u;
483    }
484
485    // Specials
486    // Range: U+FFF0 to U+FFFF
487
488    return u;
489}
490
491function DecimalToHexString( n ) {
492    n = Number( n );
493    var h = "0x";
494
495    for ( var i = 3; i >= 0; i-- ) {
496        if ( n >= Math.pow(16, i) ){
497            var t = Math.floor( n  / Math.pow(16, i));
498            n -= t * Math.pow(16, i);
499            if ( t >= 10 ) {
500                if ( t == 10 ) {
501                    h += "A";
502                }
503                if ( t == 11 ) {
504                    h += "B";
505                }
506                if ( t == 12 ) {
507                    h += "C";
508                }
509                if ( t == 13 ) {
510                    h += "D";
511                }
512                if ( t == 14 ) {
513                    h += "E";
514                }
515                if ( t == 15 ) {
516                    h += "F";
517                }
518            } else {
519                h += String( t );
520            }
521        } else {
522            h += "0";
523        }
524    }
525
526    return h;
527}