• 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.5-3.js
24    ECMA Section:       15.5.4.5 String.prototype.charCodeAt(pos)
25    Description:        Returns a number (a nonnegative integer less than 2^16)
26                        representing the Unicode encoding of the character at
27                        position pos in this string.  If there is no character
28                        at that position, the number is NaN.
29
30                        When the charCodeAt method is called with one argument
31                        pos, the following steps are taken:
32                        1. Call ToString, giving it the theis value as its
33                           argument
34                        2. Call ToInteger(pos)
35                        3. Compute the number of characters in result(1).
36                        4. If Result(2) is less than 0 or is not less than
37                           Result(3), return NaN.
38                        5. Return a value of Number type, of positive sign, whose
39                           magnitude is the Unicode encoding of one character
40                           from result 1, namely the characer at position Result
41                           (2), where the first character in Result(1) is
42                           considered to be at position 0.
43
44                        Note that the charCodeAt funciton is intentionally
45                        generic; it does not require that its this value be a
46                        String object.  Therefore it can be transferred to other
47                        kinds of objects for use as a method.
48
49    Author:             christine@netscape.com
50    Date:               2 october 1997
51*/
52    var SECTION = "15.5.4.5-3";
53    var VERSION = "ECMA_1";
54    startTest();
55    var TITLE   = "String.prototype.charCodeAt";
56
57    writeHeaderToLog( SECTION + " "+ TITLE);
58
59    var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
60
61    var testcases = getTestCases();
62    test();
63
64function MyObject (v) {
65    this.value      = v;
66    this.toString   = new Function ( "return this.value +\"\"" );
67    this.charCodeAt     = String.prototype.charCodeAt;
68}
69
70function getTestCases() {
71    var array = new Array();
72    var item = 0;
73
74    var foo = new MyObject('hello');
75
76    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(0)", 0x0068, foo.charCodeAt(0)  );
77    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(1)", 0x0065, foo.charCodeAt(1)  );
78    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(2)", 0x006c, foo.charCodeAt(2)  );
79    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(3)", 0x006c, foo.charCodeAt(3)  );
80    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(4)", 0x006f, foo.charCodeAt(4)  );
81    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(-1)", Number.NaN,  foo.charCodeAt(-1)  );
82    array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(5)", Number.NaN,  foo.charCodeAt(5)  );
83
84    var boo = new MyObject(true);
85
86    array[item++] = new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(0)", 0x0074, boo.charCodeAt(0)  );
87    array[item++] = new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(1)", 0x0072, boo.charCodeAt(1)  );
88    array[item++] = new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(2)", 0x0075, boo.charCodeAt(2)  );
89    array[item++] = new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(3)", 0x0065, boo.charCodeAt(3)  );
90
91    var noo = new MyObject( Math.PI );
92
93    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(0)", 0x0033, noo.charCodeAt(0)  );
94    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(1)", 0x002E, noo.charCodeAt(1)  );
95    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(2)", 0x0031, noo.charCodeAt(2)  );
96    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(3)", 0x0034, noo.charCodeAt(3)  );
97    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(4)", 0x0031, noo.charCodeAt(4)  );
98    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(5)", 0x0035, noo.charCodeAt(5)  );
99    array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(6)", 0x0039, noo.charCodeAt(6)  );
100
101    var noo = new MyObject( null );
102
103    array[item++] = new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(0)", 0x006E, noo.charCodeAt(0)  );
104    array[item++] = new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(1)", 0x0075, noo.charCodeAt(1)  );
105    array[item++] = new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(2)", 0x006C, noo.charCodeAt(2)  );
106    array[item++] = new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(3)", 0x006C, noo.charCodeAt(3)  );
107    array[item++] = new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(4)", NaN, noo.charCodeAt(4)  );
108
109    var noo = new MyObject( void 0 );
110
111    array[item++] = new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(0)", 0x0075, noo.charCodeAt(0)  );
112    array[item++] = new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(1)", 0x006E, noo.charCodeAt(1)  );
113    array[item++] = new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(2)", 0x0064, noo.charCodeAt(2)  );
114    array[item++] = new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(3)", 0x0065, noo.charCodeAt(3)  );
115    array[item++] = new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(4)", 0x0066, noo.charCodeAt(4)  );
116
117    return array;
118}
119
120function test() {
121    for ( tc = 0; tc < testcases.length; tc++ ) {
122
123            testcases[tc].passed = writeTestCaseResult(
124                    testcases[tc].expect,
125                    testcases[tc].actual,
126                    testcases[tc].description +" = "+
127                    testcases[tc].actual );
128
129            testcases[tc].reason += ( testcases[tc].passed )
130                                    ? ""
131                                    : "wrong value ";
132
133    }
134    stopTest();
135    return ( testcases );
136}
137