• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright JS Foundation and other contributors, http://js.foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ecma-globals.h"
18 #include "ecma-helpers.h"
19 #include "ecma-conversion.h"
20 #include "ecma-init-finalize.h"
21 #include "ecma-exceptions.h"
22 #include "jerryscript.h"
23 #include "jcontext.h"
24 
25 #include "test-common.h"
26 
27 /**
28  * Unit test's main function.
29  */
30 int
main(void)31 main (void)
32 {
33   TEST_INIT ();
34 
35   jmem_init ();
36   ecma_init ();
37 
38   ecma_number_t num;
39 
40   ecma_value_t int_num = ecma_make_int32_value (123);
41 
42   ecma_number_t result = ecma_op_to_integer (int_num, &num);
43 
44   ecma_free_value (int_num);
45 
46   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
47   TEST_ASSERT (num == 123);
48 
49   /* 2 */
50   ecma_value_t error = ecma_raise_type_error (ECMA_ERR_MSG ("I am a neat little error message"));
51 
52   result = ecma_op_to_integer (error, &num);
53 
54   jcontext_release_exception ();
55 
56   TEST_ASSERT (ECMA_IS_VALUE_ERROR (result));
57 
58   /* 3 */
59   ecma_value_t nan = ecma_make_nan_value ();
60 
61   result = ecma_op_to_integer (nan, &num);
62 
63   ecma_free_value (nan);
64 
65   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
66   TEST_ASSERT (num == 0);
67 
68   /* 4 */
69     /* -0 */
70   ecma_value_t negative_zero = ecma_make_number_value (-0.0f);
71 
72   result = ecma_op_to_integer (negative_zero, &num);
73 
74   ecma_free_value (negative_zero);
75 
76   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
77   TEST_ASSERT (1.0f / num == ecma_number_make_infinity (true));
78 
79     /* +0 */
80   ecma_value_t positive_zero = ecma_make_number_value (+0.0f);
81 
82   result = ecma_op_to_integer (positive_zero, &num);
83 
84   ecma_free_value (positive_zero);
85 
86   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
87   TEST_ASSERT (1.0f / num == ecma_number_make_infinity (false));
88 
89     /* -infinity */
90   ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true));
91 
92   result = ecma_op_to_integer (negative_infinity, &num);
93 
94   ecma_free_value (negative_infinity);
95 
96   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
97   TEST_ASSERT (num == ecma_number_make_infinity (true));
98 
99     /* +infinity */
100   ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false));
101 
102   result = ecma_op_to_integer (positive_infinity, &num);
103 
104   ecma_free_value (positive_infinity);
105 
106   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
107   TEST_ASSERT (num == ecma_number_make_infinity (false));
108 
109   /* 5 */
110   ecma_value_t floor = ecma_make_number_value (3.001f);
111 
112   result = ecma_op_to_integer (floor, &num);
113 
114   ecma_free_value (floor);
115 
116   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
117   TEST_ASSERT (num == 3);
118 
119   ecma_value_t floor2 = ecma_make_number_value (-26.5973);
120 
121   result = ecma_op_to_integer (floor2, &num);
122 
123   ecma_free_value (floor2);
124 
125   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
126   TEST_ASSERT (num == -26);
127 
128   ecma_finalize ();
129   jmem_finalize ();
130 
131   return 0;
132 } /* main */
133