aboutsummaryrefslogtreecommitdiff
path: root/tests/unit-core/test-to-length.c
blob: 657b9260ce651379e5e1c76446550ceaba21bd4b (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "jerryscript-types.h"
#include "jerryscript.h"

#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-globals.h"
#include "ecma-helpers-number.h"
#include "ecma-helpers.h"
#include "ecma-init-finalize.h"

#include "jcontext.h"
#include "lit-globals.h"
#include "test-common.h"

/**
 * Unit test's main function.
 */
int
main (void)
{
  TEST_INIT ();

  jmem_init ();
  ecma_init ();

  ecma_length_t num;

  ecma_value_t int_num = ecma_make_int32_value (123);

  ecma_value_t result = ecma_op_to_length (int_num, &num);

  ecma_free_value (int_num);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
  TEST_ASSERT (num == 123);

  /* 1, 3 */
  ecma_value_t error_throw = ecma_raise_standard_error (JERRY_ERROR_TYPE, (const lit_utf8_byte_t *) "I'm an error");

  result = ecma_op_to_length (error_throw, &num);

  jcontext_release_exception ();

  TEST_ASSERT (ECMA_IS_VALUE_ERROR (result));

  /* zero */
  ecma_value_t zero = ecma_make_int32_value (0);

  result = ecma_op_to_length (zero, &num);

  ecma_free_value (zero);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
  TEST_ASSERT (num == 0);

  /* negative */
  ecma_value_t negative = ecma_make_number_value (-26.5973f);

  result = ecma_op_to_length (negative, &num);

  ecma_free_value (negative);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
#if JERRY_ESNEXT
  TEST_ASSERT (num == 0);
#else /* !JERRY_ESNEXT */
  TEST_ASSERT (num == 4294967270);
#endif /* JERRY_ESNEXT */

  /* +infinity */
  ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false));

  result = ecma_op_to_length (positive_infinity, &num);

  ecma_free_value (positive_infinity);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
#if JERRY_ESNEXT
  TEST_ASSERT ((ecma_number_t) num == ECMA_NUMBER_MAX_SAFE_INTEGER);
#else /* !JERRY_ESNEXT */
  TEST_ASSERT (num == 0);
#endif /* JERRY_ESNEXT */

  /* -infinity */
  ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true));

  result = ecma_op_to_length (negative_infinity, &num);

  ecma_free_value (negative_infinity);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
  TEST_ASSERT (num == 0);

  /* NaN */
  ecma_value_t nan = ecma_make_nan_value ();

  result = ecma_op_to_length (nan, &num);

  ecma_free_value (nan);

  TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
  TEST_ASSERT (num == 0);

  ecma_finalize ();
  jmem_finalize ();

  return 0;
} /* main */