• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecma-globals.h"
17 #include "ecma-helpers.h"
18 
19 #include "test-common.h"
20 
21 typedef struct
22 {
23   ecma_number_t num;
24   uint32_t uint32_num;
25 } uint32_test_case_t;
26 
27 typedef struct
28 {
29   ecma_number_t num;
30   int32_t int32_num;
31 } int32_test_case_t;
32 
33 /**
34  * Unit test's main function.
35  */
36 int
main(void)37 main (void)
38 {
39   TEST_INIT ();
40 
41   const uint32_test_case_t test_cases_uint32[] =
42   {
43 #define TEST_CASE(num, uint32) { num, uint32 }
44     TEST_CASE (1.0, 1),
45     TEST_CASE (0.0, 0),
46     TEST_CASE (NAN, 0),
47     TEST_CASE (-NAN, 0),
48     TEST_CASE (INFINITY, 0),
49     TEST_CASE (-INFINITY, 0),
50     TEST_CASE (0.1, 0),
51     TEST_CASE (-0.1, 0),
52     TEST_CASE (1.1, 1),
53     TEST_CASE (-1.1, 4294967295),
54     TEST_CASE (4294967295, 4294967295),
55     TEST_CASE (-4294967295, 1),
56     TEST_CASE (4294967296, 0),
57     TEST_CASE (-4294967296, 0),
58     TEST_CASE (4294967297, 1),
59     TEST_CASE (-4294967297, 4294967295)
60 #undef TEST_CASE
61   };
62 
63   for (uint32_t i = 0;
64        i < sizeof (test_cases_uint32) / sizeof (test_cases_uint32[0]);
65        i++)
66   {
67     TEST_ASSERT (ecma_number_to_uint32 (test_cases_uint32[i].num) == test_cases_uint32[i].uint32_num);
68   }
69 
70   int32_test_case_t test_cases_int32[] =
71   {
72 #define TEST_CASE(num, int32) { num, int32 }
73     TEST_CASE (1.0, 1),
74     TEST_CASE (0.0, 0),
75     TEST_CASE (NAN, 0),
76     TEST_CASE (-NAN, 0),
77     TEST_CASE (INFINITY, 0),
78     TEST_CASE (-INFINITY, 0),
79     TEST_CASE (0.1, 0),
80     TEST_CASE (-0.1, 0),
81     TEST_CASE (1.1, 1),
82     TEST_CASE (-1.1, -1),
83     TEST_CASE (4294967295, -1),
84     TEST_CASE (-4294967295, 1),
85     TEST_CASE (4294967296, 0),
86     TEST_CASE (-4294967296, 0),
87     TEST_CASE (4294967297, 1),
88     TEST_CASE (-4294967297, -1),
89     TEST_CASE (2147483648, -2147483648),
90     TEST_CASE (-2147483648, -2147483648),
91     TEST_CASE (2147483647, 2147483647),
92     TEST_CASE (-2147483647, -2147483647),
93     TEST_CASE (-2147483649, 2147483647),
94     TEST_CASE (2147483649, -2147483647)
95 #undef TEST_CASE
96   };
97 
98   for (uint32_t i = 0;
99        i < sizeof (test_cases_int32) / sizeof (test_cases_int32[0]);
100        i++)
101   {
102     TEST_ASSERT (ecma_number_to_int32 (test_cases_int32[i].num) == test_cases_int32[i].int32_num);
103   }
104 
105   return 0;
106 } /* main */
107