|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.5.4.5-3.js |
|
9 ECMA Section: 15.5.4.5 String.prototype.charCodeAt(pos) |
|
10 Description: Returns a number (a nonnegative integer less than 2^16) |
|
11 representing the Unicode encoding of the character at |
|
12 position pos in this string. If there is no character |
|
13 at that position, the number is NaN. |
|
14 |
|
15 When the charCodeAt method is called with one argument |
|
16 pos, the following steps are taken: |
|
17 1. Call ToString, giving it the theis value as its |
|
18 argument |
|
19 2. Call ToInteger(pos) |
|
20 3. Compute the number of characters in result(1). |
|
21 4. If Result(2) is less than 0 or is not less than |
|
22 Result(3), return NaN. |
|
23 5. Return a value of Number type, of positive sign, whose |
|
24 magnitude is the Unicode encoding of one character |
|
25 from result 1, namely the characer at position Result |
|
26 (2), where the first character in Result(1) is |
|
27 considered to be at position 0. |
|
28 |
|
29 Note that the charCodeAt function is intentionally |
|
30 generic; it does not require that its this value be a |
|
31 String object. Therefore it can be transferred to other |
|
32 kinds of objects for use as a method. |
|
33 |
|
34 Author: christine@netscape.com |
|
35 Date: 2 october 1997 |
|
36 */ |
|
37 var SECTION = "15.5.4.5-3"; |
|
38 var VERSION = "ECMA_1"; |
|
39 startTest(); |
|
40 var TITLE = "String.prototype.charCodeAt"; |
|
41 |
|
42 writeHeaderToLog( SECTION + " "+ TITLE); |
|
43 |
|
44 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); |
|
45 |
|
46 |
|
47 var foo = new MyObject('hello'); |
|
48 |
|
49 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(0)", 0x0068, foo.charCodeAt(0) ); |
|
50 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(1)", 0x0065, foo.charCodeAt(1) ); |
|
51 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(2)", 0x006c, foo.charCodeAt(2) ); |
|
52 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(3)", 0x006c, foo.charCodeAt(3) ); |
|
53 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(4)", 0x006f, foo.charCodeAt(4) ); |
|
54 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(-1)", Number.NaN, foo.charCodeAt(-1) ); |
|
55 new TestCase( SECTION, "var foo = new MyObject('hello');foo.charCodeAt(5)", Number.NaN, foo.charCodeAt(5) ); |
|
56 |
|
57 var boo = new MyObject(true); |
|
58 |
|
59 new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(0)", 0x0074, boo.charCodeAt(0) ); |
|
60 new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(1)", 0x0072, boo.charCodeAt(1) ); |
|
61 new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(2)", 0x0075, boo.charCodeAt(2) ); |
|
62 new TestCase( SECTION, "var boo = new MyObject(true);boo.charCodeAt(3)", 0x0065, boo.charCodeAt(3) ); |
|
63 |
|
64 var noo = new MyObject( Math.PI ); |
|
65 |
|
66 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(0)", 0x0033, noo.charCodeAt(0) ); |
|
67 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(1)", 0x002E, noo.charCodeAt(1) ); |
|
68 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(2)", 0x0031, noo.charCodeAt(2) ); |
|
69 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(3)", 0x0034, noo.charCodeAt(3) ); |
|
70 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(4)", 0x0031, noo.charCodeAt(4) ); |
|
71 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(5)", 0x0035, noo.charCodeAt(5) ); |
|
72 new TestCase( SECTION, "var noo = new MyObject(Math.PI);noo.charCodeAt(6)", 0x0039, noo.charCodeAt(6) ); |
|
73 |
|
74 var noo = new MyObject( null ); |
|
75 |
|
76 new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(0)", 0x006E, noo.charCodeAt(0) ); |
|
77 new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(1)", 0x0075, noo.charCodeAt(1) ); |
|
78 new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(2)", 0x006C, noo.charCodeAt(2) ); |
|
79 new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(3)", 0x006C, noo.charCodeAt(3) ); |
|
80 new TestCase( SECTION, "var noo = new MyObject(null);noo.charCodeAt(4)", NaN, noo.charCodeAt(4) ); |
|
81 |
|
82 var noo = new MyObject( void 0 ); |
|
83 |
|
84 new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(0)", 0x0075, noo.charCodeAt(0) ); |
|
85 new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(1)", 0x006E, noo.charCodeAt(1) ); |
|
86 new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(2)", 0x0064, noo.charCodeAt(2) ); |
|
87 new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(3)", 0x0065, noo.charCodeAt(3) ); |
|
88 new TestCase( SECTION, "var noo = new MyObject(void 0);noo.charCodeAt(4)", 0x0066, noo.charCodeAt(4) ); |
|
89 |
|
90 test(); |
|
91 |
|
92 |
|
93 function MyObject (v) { |
|
94 this.value = v; |
|
95 this.toString = new Function ( "return this.value +\"\"" ); |
|
96 this.charCodeAt = String.prototype.charCodeAt; |
|
97 } |