mirror of https://github.com/svaarala/duktape.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
11 years ago
|
/*===
|
||
|
positive numbers truncate towards zero
|
||
|
number: 3
|
||
|
negative numbers truncate towards zero
|
||
|
number: -3
|
||
11 years ago
|
below DUK_INT_MIN
|
||
|
number is DUK_INT_MIN: 1
|
||
|
above DUK_INT_MAX
|
||
|
number is DUK_INT_MAX: 1
|
||
11 years ago
|
NaN coerces to zero
|
||
|
number: 0
|
||
|
non-number coerces to zero
|
||
|
number: 0
|
||
|
===*/
|
||
|
|
||
|
void test(duk_context *ctx) {
|
||
|
double d;
|
||
|
|
||
11 years ago
|
/* DUK_INT_MIN/DUK_INT_MAX cases don't print concrete numbers because
|
||
11 years ago
|
* the limits are platform dependent. In particular, on platforms
|
||
|
* with 64-bit ints we want to allow the full range.
|
||
|
*/
|
||
|
|
||
|
printf("positive numbers truncate towards zero\n");
|
||
|
d = 3.9;
|
||
|
duk_push_number(ctx, d);
|
||
11 years ago
|
printf("number: %ld\n", (long) duk_get_int(ctx, -1));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
|
||
|
printf("negative numbers truncate towards zero\n");
|
||
|
d = -3.9;
|
||
|
duk_push_number(ctx, d);
|
||
11 years ago
|
printf("number: %ld\n", (long) duk_get_int(ctx, -1));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
|
||
11 years ago
|
/* On 64-bit platforms DUK_INT_MIN, DUK_INT_MAX, DUK_UINT_MAX
|
||
|
* cannot be represented accurately with IEEE doubles, so we
|
||
|
* can't test for exact limit here. For simplicity, just double
|
||
|
* the limit.
|
||
|
*/
|
||
11 years ago
|
|
||
11 years ago
|
printf("below DUK_INT_MIN\n");
|
||
|
d = ((double) DUK_INT_MIN) * 2.0;
|
||
11 years ago
|
duk_push_number(ctx, d);
|
||
|
#if 0
|
||
|
printf("number: %d\n", duk_get_int(ctx, -1));
|
||
|
#endif
|
||
11 years ago
|
printf("number is DUK_INT_MIN: %d\n", (duk_get_int(ctx, -1) == DUK_INT_MIN));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
|
||
11 years ago
|
printf("above DUK_INT_MAX\n");
|
||
|
d = ((double) DUK_INT_MAX) * 2.0;
|
||
11 years ago
|
duk_push_number(ctx, d);
|
||
|
#if 0
|
||
|
printf("number: %d\n", duk_get_int(ctx, -1));
|
||
|
#endif
|
||
11 years ago
|
printf("number is DUK_INT_MAX: %d\n", (duk_get_int(ctx, -1) == DUK_INT_MAX));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
|
||
|
printf("NaN coerces to zero\n");
|
||
|
duk_push_nan(ctx);
|
||
11 years ago
|
printf("number: %ld\n", (long) duk_get_int(ctx, -1));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
|
||
|
printf("non-number coerces to zero\n");
|
||
|
duk_push_string(ctx, "123");
|
||
11 years ago
|
printf("number: %ld\n", (long) duk_get_int(ctx, -1));
|
||
11 years ago
|
duk_pop(ctx);
|
||
|
}
|