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"
17extern "C"
18{
19  #include "ecma-helpers.h"
20}
21
22#include "test-common.h"
23#include <gtest/gtest.h>
24
25typedef struct
26{
27  ecma_number_t num;
28  uint32_t uint32_num;
29} uint32_test_case_t;
30
31typedef struct
32{
33  ecma_number_t num;
34  int32_t int32_num;
35} int32_test_case_t;
36
37/**
38 * Unit test's main function.
39 */
40class NumberToInt32Test : public testing::Test{
41public:
42    static void SetUpTestCase()
43    {
44        GTEST_LOG_(INFO) << "NumberToInt32Test SetUpTestCase";
45    }
46
47    static void TearDownTestCase()
48    {
49        GTEST_LOG_(INFO) << "NumberToInt32Test TearDownTestCase";
50    }
51
52    void SetUp() override {}
53    void TearDown() override {}
54
55};
56
57HWTEST_F(NumberToInt32Test, Test001, testing::ext::TestSize.Level1)
58{
59  TEST_INIT ();
60
61  const uint32_test_case_t test_cases_uint32[] =
62  {
63#define TEST_CASE(num, uint32) { num, uint32 }
64    TEST_CASE (1.0, 1),
65    TEST_CASE (0.0, 0),
66    TEST_CASE (NAN, 0),
67    TEST_CASE (-NAN, 0),
68    TEST_CASE (INFINITY, 0),
69    TEST_CASE (-INFINITY, 0),
70    TEST_CASE (0.1, 0),
71    TEST_CASE (-0.1, 0),
72    TEST_CASE (1.1, 1),
73    TEST_CASE (-1.1, 4294967295),
74    TEST_CASE (4294967295, 4294967295),
75    TEST_CASE (-4294967295, 1),
76    TEST_CASE (4294967296, 0),
77    TEST_CASE (-4294967296, 0),
78    TEST_CASE (4294967297, 1),
79    TEST_CASE (-4294967297, 4294967295)
80#undef TEST_CASE
81  };
82
83  for (uint32_t i = 0;
84       i < sizeof (test_cases_uint32) / sizeof (test_cases_uint32[0]);
85       i++)
86  {
87    TEST_ASSERT (ecma_number_to_uint32 (test_cases_uint32[i].num) == test_cases_uint32[i].uint32_num);
88  }
89
90  int32_test_case_t test_cases_int32[] =
91  {
92#define TEST_CASE(num, int32) { num, int32 }
93    TEST_CASE (1.0, 1),
94    TEST_CASE (0.0, 0),
95    TEST_CASE (NAN, 0),
96    TEST_CASE (-NAN, 0),
97    TEST_CASE (INFINITY, 0),
98    TEST_CASE (-INFINITY, 0),
99    TEST_CASE (0.1, 0),
100    TEST_CASE (-0.1, 0),
101    TEST_CASE (1.1, 1),
102    TEST_CASE (-1.1, -1),
103    TEST_CASE (4294967295, -1),
104    TEST_CASE (-4294967295, 1),
105    TEST_CASE (4294967296, 0),
106    TEST_CASE (-4294967296, 0),
107    TEST_CASE (4294967297, 1),
108    TEST_CASE (-4294967297, -1),
109    TEST_CASE (2147483648, -2147483648),
110    TEST_CASE (-2147483648, -2147483648),
111    TEST_CASE (2147483647, 2147483647),
112    TEST_CASE (-2147483647, -2147483647),
113    TEST_CASE (-2147483649, 2147483647),
114    TEST_CASE (2147483649, -2147483647)
115#undef TEST_CASE
116  };
117
118  for (uint32_t i = 0;
119       i < sizeof (test_cases_int32) / sizeof (test_cases_int32[0]);
120       i++)
121  {
122    TEST_ASSERT (ecma_number_to_int32 (test_cases_int32[i].num) == test_cases_int32[i].int32_num);
123  }
124  return;
125}
126