Browse Source

Add testcase for String.prototype.codePointAt()

pull/1043/head
Sami Vaarala 8 years ago
parent
commit
9b2afe0a46
  1. 33
      tests/api/test-charcodeat.c
  2. 8
      tests/ecmascript/test-bi-string-frombuffer.js
  3. 90
      tests/ecmascript/test-bi-string-proto-codepointat.js

33
tests/api/test-charcodeat.c

@ -1,5 +1,5 @@
/*===
*** test_1 (duk_safe_call)
*** test_basic (duk_safe_call)
i=0, n=19, charcode=102
i=1, n=19, charcode=111
i=2, n=19, charcode=111
@ -20,11 +20,17 @@ i=16, n=19, charcode=0
i=17, n=19, charcode=0
i=18, n=19, charcode=0
==> rc=0, result='undefined'
*** test_2 (duk_safe_call)
*** test_invalid_arg (duk_safe_call)
==> rc=1, result='TypeError: string required, found 123 (stack index -1)'
*** test_invalid_utf8 (duk_safe_call)
index 0: 65
index 1: 65533
index 2: 66
index 3: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t test_1(duk_context *ctx, void *udata) {
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
duk_size_t i, n;
(void) udata;
@ -42,7 +48,7 @@ static duk_ret_t test_1(duk_context *ctx, void *udata) {
return 0;
}
static duk_ret_t test_2(duk_context *ctx, void *udata) {
static duk_ret_t test_invalid_arg(duk_context *ctx, void *udata) {
(void) udata;
/* TypeError for invalid arg type */
@ -53,7 +59,22 @@ static duk_ret_t test_2(duk_context *ctx, void *udata) {
return 0;
}
static duk_ret_t test_invalid_utf8(duk_context *ctx, void *udata) {
char buf[3] = { (char) 0x41, (char) 0xff, (char) 0x42 };
size_t i;
(void) udata;
duk_push_lstring(ctx, (const char *) buf, sizeof(buf));
for (i = 0; i < sizeof(buf) + 1; i++) { /* overshoot by one on purpose */
printf("index %d: %d\n", (int) i, (int) duk_char_code_at(ctx, -1, i));
}
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
TEST_SAFE_CALL(test_2);
TEST_SAFE_CALL(test_basic);
TEST_SAFE_CALL(test_invalid_arg);
TEST_SAFE_CALL(test_invalid_utf8);
}

8
tests/ecmascript/test-bi-string-frombuffer.js

@ -34,19 +34,19 @@ TypeError
TypeError
TypeError
"\u07ad\xbe\xef"
string "<U+07AD><Error>"
string "<U+07AD><U+FFFD>"
|deadbeef|
"\xff\ue74c"
string "<Error><U+E74C><U+074C><Error>"
string "<U+FFFD><U+E74C><U+074C><U+FFFD>"
|ffeeddcc|
"abcd"
string "abcd"
|61626364|
"\x00\x01\x02\xfe\xff"
string "<U+0000><U+0001><U+0002><Error><Error>"
string "<U+0000><U+0001><U+0002><U+FFFD><U+FFFD>"
|000102feff|
"\xff\xff\xff\xffxV4\x12\u03ba\xfe\xca"
string "<Error><Error><Error><Error>xV4<U+0012><U+03BA><Error><Error><Error>"
string "<U+FFFD><U+FFFD><U+FFFD><U+FFFD>xV4<U+0012><U+03BA><U+FFFD><U+FFFD><U+FFFD>"
|ffffffff78563412cefafeca|
"\x00\x00\x00\x00\x00\x00\x00\x00\x9a\x99\x99\x99\x99\x99\xb9?\x9a\x99\x99\x99\x99\x99\u067f\x00\x00\x00\x00\x00\x00\u0ff333333\u3fc0\x00\x00\x00\x00\x00\U0003f9a6ffff\U001bf000\x00\x00\x00\x00\U00fda659\x99\x99\x99\U01fc0000\x00\x00@\x8f@"
string "<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>?<U+067F>?<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0FF3>?333333<U+3FC0>?<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+F9A6>?ffffff<U+F000>?<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+A659>?<U+0000>?<U+0000><U+0000><U+0000><U+0000><U+0000>@@"

90
tests/ecmascript/test-bi-string-proto-codepointat.js

@ -0,0 +1,90 @@
/*
* String.prototype.codePointAt()
*/
/*===
-3 NaN undefined
-2 NaN undefined
-1 NaN undefined
0 102 102
1 111 111
2 111 111
3 51966 51966
4 55357 128169
5 56489 56489
6 48879 48879
7 98 98
8 97 97
9 114 114
10 55357 55357
11 55357 55357
12 55357 55357
13 120 120
14 56489 56489
15 55357 55357
16 98 98
17 97 97
18 122 122
19 55357 55357
20 NaN undefined
21 NaN undefined
22 NaN undefined
[object Object]
-3 NaN undefined
-2 NaN undefined
-1 NaN undefined
0 91 91
1 111 111
2 98 98
3 106 106
4 101 101
5 99 99
6 116 116
7 32 32
8 79 79
9 98 98
10 106 106
11 101 101
12 99 99
13 116 116
14 93 93
15 NaN undefined
16 NaN undefined
17 NaN undefined
18 NaN undefined
19 NaN undefined
===*/
function test() {
var x = 'foo' +
'\ucafe' + // simple non-surrogate
'\uD83D\uDCA9' + // valid U+1F4A9 in surrogate pair encoding
'\ubeef' + // simple non-surrogate
'bar' +
'\ud83d\ud83d' + // invalid surrogate pair
'\ud83dx' + // -""-
'\udca9\ud83d' + // -""-
'baz' +
'\ud83d'; // invalid surrogate pair, string ends before pair
for (i = -3; i < x.length + 3; i++) {
print(i, x.charCodeAt(i), x.codePointAt(i));
}
// .codePointAt() is generic like .charCodeAt(), but they simply String() coerce
// their argument first so this is coerced to [object Object] and the char lookup
// is then applied. Codepoints won't be looked up from the object properties.
var obj = { 0: 'f', 1: 'o', 2: 'o',
3: '\ud83d', 4: '\udca9',
5: 'b', 6: 'a', 7: 'r', length: 8 };
print(String(obj));
for (i = -3; i < 20; i++) {
print(i, String.prototype.charCodeAt.call(obj, i), String.prototype.codePointAt.call(obj, i));
}
}
try {
test();
} catch (e) {
print(e.stack || e);
}
Loading…
Cancel
Save