Browse Source

some string testcases

pull/1/head
Sami Vaarala 12 years ago
parent
commit
5ee9aa67ba
  1. 109
      testcases/test-builtin-string-constructor.js
  2. 21
      testcases/test-builtin-string-fromcharcode-undef.js
  3. 97
      testcases/test-builtin-string-fromcharcode.js
  4. 239
      testcases/test-builtin-string-proto-charat.js
  5. 221
      testcases/test-builtin-string-proto-charcodeat.js
  6. 137
      testcases/test-builtin-string-proto-concat.js
  7. 18
      testcases/test-builtin-string.js

109
testcases/test-builtin-string-constructor.js

@ -0,0 +1,109 @@
/*===
constructor as a function
string
string undefined
string null
string true
string false
string -Infinity
string -123
string 0
string 0
string 123
string Infinity
string NaN
string 1,2
string [object Object]
string noval
===*/
print('constructor as a function');
function constructorAsFunctionTest() {
function pv(x, noval) {
print(typeof x, (noval ? 'noval' : x));
}
// No argument is handled different from undefined
pv(String());
pv(String(undefined));
pv(String(null));
pv(String(true));
pv(String(false));
pv(String(Number.NEGATIVE_INFINITY));
pv(String(-123.0));
pv(String(-0.0));
pv(String(0.0));
pv(String(123.0));
pv(String(Number.POSITIVE_INFINITY));
pv(String(Number.NaN));
pv(String([1,2]));
pv(String({ foo: 1, bar: 2 }));
// the ToString() conversion of a function is implementation
// specific so just check the type
pv(String(function(){}), true);
}
try {
constructorAsFunctionTest();
} catch (e) {
print(e);
}
/*===
constructor
object [object String] true
object undefined [object String] true
object null [object String] true
object true [object String] true
object false [object String] true
object -Infinity [object String] true
object -123 [object String] true
object 0 [object String] true
object 0 [object String] true
object 123 [object String] true
object Infinity [object String] true
object NaN [object String] true
object 1,2 [object String] true
object [object Object] [object String] true
object noval [object String] true
===*/
print('constructor');
function constructorTest() {
function pv(x, noval) {
print(typeof x, (noval ? 'noval' : x), Object.prototype.toString.call(x), Object.isExtensible(x));
}
// No argument is handled different from undefined
pv(new String());
pv(new String(undefined));
pv(new String(null));
pv(new String(true));
pv(new String(false));
pv(new String(Number.NEGATIVE_INFINITY));
pv(new String(-123.0));
pv(new String(-0.0));
pv(new String(0.0));
pv(new String(123.0));
pv(new String(Number.POSITIVE_INFINITY));
pv(new String(Number.NaN));
pv(new String([1,2]));
pv(new String({ foo: 1, bar: 2 }));
// the ToString() conversion of a function is implementation
// specific so just check the type
pv(new String(function(){}), true);
}
try {
constructorTest();
} catch (e) {
print(e);
}

21
testcases/test-builtin-string-fromcharcode-undef.js

@ -1,21 +0,0 @@
/*===
3
65
0
0
===*/
/* Undefined arguments are coerced with ToUint16(), yielding zero
* codepoints.
*/
try {
var t = String.fromCharCode(65,undefined,undefined);
print(t.length);
print(t.charCodeAt(0));
print(t.charCodeAt(1));
print(t.charCodeAt(2));
} catch (e) {
print(e.name);
}

97
testcases/test-builtin-string-fromcharcode.js

@ -0,0 +1,97 @@
/*===
string 0
string 1 A
string 2 AB
string 3 ABC
string 3 ABC
string 3 ABC
string 4 AAAA
string 4 AAAA
string 2 AB
valueOf() for 1st
valueOf() for 2nd
valueOf() for 3rd
string 3 BDF
string 7 0 127 255 837 17767 26505 65535
string 3 65 0 0
string 100 ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ
===*/
function fromCharCodeTest() {
function pv(x) {
print(typeof x, x.length, x);
}
function pv2(x) {
var tmp = [];
tmp.push(typeof x);
tmp.push(x.length);
for (i = 0; i < x.length; i++) {
tmp.push(x.charCodeAt(i));
}
print(tmp.join(' '));
}
function mkObj(name, strval, numval) {
return {
toString: function() { print('toString() for ' + name); return strval; },
valueOf: function() { print('valueOf() for ' + name); return numval }
};
}
// basic tests
pv(String.fromCharCode());
pv(String.fromCharCode(65));
pv(String.fromCharCode(65, 66));
pv(String.fromCharCode(65, 66, 67));
// ToUint16 coercion, whole numbers
pv(String.fromCharCode(65601, 6553666, 6553600067));
pv(String.fromCharCode(-65471, -6553534, -6553599933));
// ToUint16 coercion, positive values, round to zero (floor)
pv(String.fromCharCode(65.0, 65.1, 65.5, 65.9));
// ToUint16 coercion, negative values, round to zero (ceil)
// -65536 + 65 = -65471
pv(String.fromCharCode(-65471.0, -65471.1, -65471.5, -65471.9));
// Note that coercion round to zero means that even if even if
// ToUint16(65.9) = 65, ToUint16(65.9 - 65536) = ToUint16(-65470.1) = 66!
pv(String.fromCharCode(65.9, 65.9 - 65536));
// ToUint16 coercion, side effect order
pv(String.fromCharCode(mkObj('1st', '65', 66),
mkObj('2nd', '67', 68),
mkObj('3rd', '69', 70)));
// non-BMP (and modulo 2**16)
pv2(String.fromCharCode(0, 127, 255, 0x345, 0x4567, 0x56789, 0x10ffff));
// undefined arguments are coerced with ToUint16(), yielding zero
// codepoints; they are different from arguments not given at all
pv2(String.fromCharCode(65, undefined, undefined));
// a lot of arguments; this is now limited by Duktape stack so we can't
// put a REALLY long string here (separate bug test case for longer
// strings now)
pv(String.fromCharCode(
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74));
}
try {
fromCharCodeTest();
} catch (e) {
print(e);
}

239
testcases/test-builtin-string-proto-charat.js

@ -0,0 +1,239 @@
/*===
basic test
string 1 a
string 1 b
string 1 c
string 1 A
string 1 B
string 1 C
string 1 U+0000
string 1 f
string 1 o
string 1 o
string 1 U+0081
string 1 b
string 1 a
string 1 r
string 1 U+07FF
string 1 q
string 1 u
string 1 u
string 1 x
string 1 U+1234
string 1 b
string 1 a
string 1 z
string 1 U+FEDC
string 1 x
string 1 y
string 1 z
string 1 U+FFFF
===*/
print('basic test');
function charAtTest() {
var str = new String('abcABC\u0000foo\u0081bar\u07ffquux\u1234baz\ufedcxyz\uffff');
var i;
// we want to print out ascii but don't want to rely on charCodeAt().
// use a hashmap to "stringify" the non-ascii chars
var map = {
'\u0000': 'U+0000',
'\u0081': 'U+0081',
'\u07ff': 'U+07FF',
'\u1234': 'U+1234',
'\ufedc': 'U+FEDC',
'\uffff': 'U+FFFF',
}
function pc(c) {
print(typeof c, c.length, (c >= ' ' && c <= '~' ? c : map[c]));
}
// print in order
for (i = 0; i < str.length; i++) {
pc(str.charAt(i));
}
}
try {
charAtTest();
} catch (e) {
print(e);
}
/*===
oob test
-3 0
-2 0
-1 0
0 1 b
1 1 a
2 1 r
3 0
4 0
5 0
-Infinity 0
Infinity 0
NaN 1 b
===*/
print('oob test');
function outOfBoundsTest() {
var str = new String('bar');
var i;
function pc(idx) {
var c = str.charAt(idx);
print(idx, c.length, c);
}
for (i = -3; i < 6; i++) {
pc(i);
}
pc(Number.NEGATIVE_INFINITY),
pc(Number.POSITIVE_INFINITY),
// ToInteger(NaN) coerces to 0 -> 'b'
pc(Number.NaN);
}
try {
outOfBoundsTest();
} catch (e) {
print(e);
}
/*===
random access test
random access test done
===*/
/* Random access test for a large string. The purpose of this test is to
* stress the stringcache. Note that the test is not deterministic.
*/
print('random access test');
function randomAccessTest() {
var tmp = [];
var str;
var i;
var idx, c1, c2;
for (i = 0; i < 65536; i++) {
tmp.push(String.fromCharCode(i));
}
str = new String(tmp.join(''));
for (i = 0; i < 100000; i++) {
idx = Math.floor(Math.random() * 65536);
c1 = str.charAt(idx);
c2 = String.fromCharCode(idx); // XXX: unfortunately we depend on this too
if (c1 !== c2) {
print('random access test failed for index', idx);
}
}
print('random access test done');
}
try {
randomAccessTest();
} catch (e) {
print(e);
}
/*===
coercion
testing undefined
TypeError
testing null
TypeError
testing true
0 string t
1 string r
2 string u
3 string e
testing false
0 string f
1 string a
2 string l
3 string s
4 string e
testing 123
0 string 1
1 string 2
2 string 3
testing foo
0 string f
1 string o
2 string o
testing 1,2
0 string 1
1 string ,
2 string 2
testing [object Object]
0 string [
1 string o
2 string b
3 string j
4 string e
5 string c
6 string t
7 string
8 string O
9 string b
10 string j
11 string e
12 string c
13 string t
14 string ]
===*/
/* charAt() is generic, check 'this' coercion */
print('coercion');
function thisCoercionTest() {
function test(x) {
var c;
var i;
print('testing', x);
try {
for (i = 0; ; i++) {
c = String.prototype.charAt.call(x, i);
if (c === '') {
break;
}
print(i, typeof c, c);
}
} catch (e) {
print(e.name);
}
}
// undefined and null cause TypeError from CheckObjectCoercible
test(undefined);
test(null);
test(true);
test(false);
test(123);
test('foo');
test([1,2]);
test({ foo: 1, bar: 2 });
}
try {
thisCoercionTest();
} catch (e) {
print(e);
}

221
testcases/test-builtin-string-proto-charcodeat.js

@ -0,0 +1,221 @@
/*===
basic test
97
98
99
65
66
67
0
102
111
111
129
98
97
114
2047
113
117
117
120
4660
98
97
122
65244
120
121
122
65535
===*/
print('basic test');
function charCodeAtTest() {
var str = new String('abcABC\u0000foo\u0081bar\u07ffquux\u1234baz\ufedcxyz\uffff');
var i;
for (i = 0; i < str.length; i++) {
print(str.charCodeAt(i));
}
}
try {
charCodeAtTest();
} catch (e) {
print(e);
}
/*===
oob test
-3 NaN
-2 NaN
-1 NaN
0 98
1 97
2 114
3 NaN
4 NaN
5 NaN
-Infinity NaN
Infinity NaN
NaN 98
===*/
print('oob test');
function outOfBoundsTest() {
var str = new String('bar');
var i;
function pc(idx) {
var c = str.charCodeAt(idx);
print(idx, c);
}
for (i = -3; i < 6; i++) {
pc(i);
}
pc(Number.NEGATIVE_INFINITY),
pc(Number.POSITIVE_INFINITY),
// ToInteger(NaN) coerces to 0 -> 'b'
pc(Number.NaN);
}
try {
outOfBoundsTest();
} catch (e) {
print(e);
}
/*===
random access test
random access test done
===*/
/* Random access test for a large string. The purpose of this test is to
* stress the stringcache. Note that the test is not deterministic.
*/
print('random access test');
function randomAccessTest() {
var tmp = [];
var str;
var i;
var idx, c;
for (i = 0; i < 65536; i++) {
tmp.push(String.fromCharCode(i));
}
str = new String(tmp.join(''));
for (i = 0; i < 100000; i++) {
idx = Math.floor(Math.random() * 65536);
c = str.charCodeAt(idx);
if (c !== idx) {
print('random access test failed for index', idx);
}
}
print('random access test done');
}
try {
randomAccessTest();
} catch (e) {
print(e);
}
/*===
coercion
testing undefined
TypeError
testing null
TypeError
testing true
0 number 116
1 number 114
2 number 117
3 number 101
testing false
0 number 102
1 number 97
2 number 108
3 number 115
4 number 101
testing 123
0 number 49
1 number 50
2 number 51
testing foo
0 number 102
1 number 111
2 number 111
testing 1,2
0 number 49
1 number 44
2 number 50
testing [object Object]
0 number 91
1 number 111
2 number 98
3 number 106
4 number 101
5 number 99
6 number 116
7 number 32
8 number 79
9 number 98
10 number 106
11 number 101
12 number 99
13 number 116
14 number 93
===*/
/* charCodeAt() is generic, check 'this' coercion */
print('coercion');
function thisCoercionTest() {
function test(x) {
var c;
var i;
print('testing', x);
try {
for (i = 0; ; i++) {
c = String.prototype.charCodeAt.call(x, i);
if (isNaN(c)) {
break;
}
print(i, typeof c, c);
}
} catch (e) {
print(e.name);
}
}
// undefined and null cause TypeError from CheckObjectCoercible
test(undefined);
test(null);
test(true);
test(false);
test(123);
test('foo');
test([1,2]);
test({ foo: 1, bar: 2 });
}
try {
thisCoercionTest();
} catch (e) {
print(e);
}

137
testcases/test-builtin-string-proto-concat.js

@ -0,0 +1,137 @@
/*===
basic
string 3 foo
string 3 foo
string 12 fooundefined
string 16 fooundefinednull
string 20 fooundefinednulltrue
string 25 fooundefinednulltruefalse
string 28 fooundefinednulltruefalse123
string 31 fooundefinednulltruefalse123bar
string 34 fooundefinednulltruefalse123bar1,2
string 49 fooundefinednulltruefalse123bar1,2[object Object]
string 3 foo
===*/
print('basic');
function concatTest() {
var str = String('foo');
function pv(x) {
print(typeof x, x.length, x);
}
pv(str);
pv(str.concat());
pv(str.concat(undefined));
pv(str.concat(undefined, null));
pv(str.concat(undefined, null, true));
pv(str.concat(undefined, null, true, false));
pv(str.concat(undefined, null, true, false, 123));
pv(str.concat(undefined, null, true, false, 123, 'bar'));
pv(str.concat(undefined, null, true, false, 123, 'bar', [1,2]));
pv(str.concat(undefined, null, true, false, 123, 'bar', [1,2], { foo: 1, bar: 2 }));
pv(str);
}
try {
concatTest();
} catch (e) {
print(e);
}
/*===
unicode
102
111
111
4660
2046
128
102
111
111
65535
===*/
print('unicode');
function unicodeTest() {
var str = new String('foo\u1234');
var t;
var i;
t = str.concat('\u07fe', '\u0080foo', '\uffff');
for (i = 0; i < t.length; i++) {
print(t.charCodeAt(i));
}
}
try {
unicodeTest();
} catch (e) {
print(e);
}
/*===
coercion
TypeError
TypeError
string 7 truebar
string 8 falsebar
string 6 123bar
string 6 foobar
string 6 1,2bar
string 18 [object Object]bar
toString() obj1
toString() obj2
toString() obj3
string 15 firstfoobarquux
===*/
print('coercion');
function coercionTest() {
var t;
function test(x) {
var t;
try {
t = String.prototype.concat.call(x, 'bar');
print(typeof t, t.length, t);
} catch (e) {
print(e.name);
}
}
function mkObj(name, strval) {
return {
toString: function() { print('toString()', name); return strval; },
valueOf: function() { print('valueOf()', name); return 'unexpected'; }
};
}
// expect TypeError for these
test(undefined);
test(null);
// and a ToString() coercion for these
test(true);
test(false);
test(123);
test('foo');
test([1,2]);
test({ foo: 1, bar: 2 });
// coercion side effect test
t = String.prototype.concat.call('first', mkObj('obj1', 'foo'), mkObj('obj2', 'bar'), mkObj('obj3', 'quux'));
print(typeof t, t.length, t);
}
try {
coercionTest();
} catch (e) {
print(e);
}

18
testcases/test-builtin-string.js

@ -1,18 +0,0 @@
/*
* String objects (E5 Sections 15.5, B.2.3).
*/
/*---
{
"skip": true
}
---*/
/* FIXME */
/*
* toUpperCase() behavior.
*
* This behavior is also related to RegExp Canonicalization behavior.
*/
Loading…
Cancel
Save