forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-map-string.c
More file actions
43 lines (37 loc) · 1.04 KB
/
Copy pathtest-map-string.c
File metadata and controls
43 lines (37 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*===
*** test_1 (duk_safe_call)
test 1
result: 'TEST_STRING'
test 2
result: 'FOOXBAR'
final top: 0
==> rc=0, result='undefined'
===*/
static duk_codepoint_t map_char(void *udata, duk_codepoint_t codepoint) {
if (codepoint >= (duk_codepoint_t) 'a' && codepoint <= (duk_codepoint_t) 'z') {
/* Convert ASCII to uppercase. */
return codepoint - (duk_codepoint_t) 'a' + (duk_codepoint_t) 'A';
} else if (codepoint == 0x1234) {
/* Convert U+1234 to 'X' */
return (duk_codepoint_t) 'X';
}
return codepoint;
}
static duk_ret_t test_1(duk_context *ctx) {
printf("test 1\n");
duk_push_string(ctx, "test_string");
duk_map_string(ctx, -1, map_char, NULL);
printf("result: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
printf("test 2\n");
duk_push_string(ctx, "foo" "\xe1\x88\xb4" "bar");
duk_map_string(ctx, -1, map_char, NULL);
printf("result: '%s'\n", duk_to_string(ctx, -1));
duk_pop(ctx);
/* XXX: error cases */
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
}