1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "ecma-globals.h"
17425bb815Sopenharmony_ciextern "C"
18425bb815Sopenharmony_ci{
19425bb815Sopenharmony_ci  #include "ecma-helpers.h"
20425bb815Sopenharmony_ci}
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci#include "test-common.h"
23425bb815Sopenharmony_ci#include <gtest/gtest.h>
24425bb815Sopenharmony_ci
25425bb815Sopenharmony_citypedef struct
26425bb815Sopenharmony_ci{
27425bb815Sopenharmony_ci  ecma_number_t num;
28425bb815Sopenharmony_ci  uint32_t uint32_num;
29425bb815Sopenharmony_ci} uint32_test_case_t;
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_citypedef struct
32425bb815Sopenharmony_ci{
33425bb815Sopenharmony_ci  ecma_number_t num;
34425bb815Sopenharmony_ci  int32_t int32_num;
35425bb815Sopenharmony_ci} int32_test_case_t;
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci/**
38425bb815Sopenharmony_ci * Unit test's main function.
39425bb815Sopenharmony_ci */
40425bb815Sopenharmony_ciclass NumberToInt32Test : public testing::Test{
41425bb815Sopenharmony_cipublic:
42425bb815Sopenharmony_ci    static void SetUpTestCase()
43425bb815Sopenharmony_ci    {
44425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "NumberToInt32Test SetUpTestCase";
45425bb815Sopenharmony_ci    }
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_ci    static void TearDownTestCase()
48425bb815Sopenharmony_ci    {
49425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "NumberToInt32Test TearDownTestCase";
50425bb815Sopenharmony_ci    }
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci    void SetUp() override {}
53425bb815Sopenharmony_ci    void TearDown() override {}
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_ci};
56425bb815Sopenharmony_ci
57425bb815Sopenharmony_ciHWTEST_F(NumberToInt32Test, Test001, testing::ext::TestSize.Level1)
58425bb815Sopenharmony_ci{
59425bb815Sopenharmony_ci  TEST_INIT ();
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_ci  const uint32_test_case_t test_cases_uint32[] =
62425bb815Sopenharmony_ci  {
63425bb815Sopenharmony_ci#define TEST_CASE(num, uint32) { num, uint32 }
64425bb815Sopenharmony_ci    TEST_CASE (1.0, 1),
65425bb815Sopenharmony_ci    TEST_CASE (0.0, 0),
66425bb815Sopenharmony_ci    TEST_CASE (NAN, 0),
67425bb815Sopenharmony_ci    TEST_CASE (-NAN, 0),
68425bb815Sopenharmony_ci    TEST_CASE (INFINITY, 0),
69425bb815Sopenharmony_ci    TEST_CASE (-INFINITY, 0),
70425bb815Sopenharmony_ci    TEST_CASE (0.1, 0),
71425bb815Sopenharmony_ci    TEST_CASE (-0.1, 0),
72425bb815Sopenharmony_ci    TEST_CASE (1.1, 1),
73425bb815Sopenharmony_ci    TEST_CASE (-1.1, 4294967295),
74425bb815Sopenharmony_ci    TEST_CASE (4294967295, 4294967295),
75425bb815Sopenharmony_ci    TEST_CASE (-4294967295, 1),
76425bb815Sopenharmony_ci    TEST_CASE (4294967296, 0),
77425bb815Sopenharmony_ci    TEST_CASE (-4294967296, 0),
78425bb815Sopenharmony_ci    TEST_CASE (4294967297, 1),
79425bb815Sopenharmony_ci    TEST_CASE (-4294967297, 4294967295)
80425bb815Sopenharmony_ci#undef TEST_CASE
81425bb815Sopenharmony_ci  };
82425bb815Sopenharmony_ci
83425bb815Sopenharmony_ci  for (uint32_t i = 0;
84425bb815Sopenharmony_ci       i < sizeof (test_cases_uint32) / sizeof (test_cases_uint32[0]);
85425bb815Sopenharmony_ci       i++)
86425bb815Sopenharmony_ci  {
87425bb815Sopenharmony_ci    TEST_ASSERT (ecma_number_to_uint32 (test_cases_uint32[i].num) == test_cases_uint32[i].uint32_num);
88425bb815Sopenharmony_ci  }
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci  int32_test_case_t test_cases_int32[] =
91425bb815Sopenharmony_ci  {
92425bb815Sopenharmony_ci#define TEST_CASE(num, int32) { num, int32 }
93425bb815Sopenharmony_ci    TEST_CASE (1.0, 1),
94425bb815Sopenharmony_ci    TEST_CASE (0.0, 0),
95425bb815Sopenharmony_ci    TEST_CASE (NAN, 0),
96425bb815Sopenharmony_ci    TEST_CASE (-NAN, 0),
97425bb815Sopenharmony_ci    TEST_CASE (INFINITY, 0),
98425bb815Sopenharmony_ci    TEST_CASE (-INFINITY, 0),
99425bb815Sopenharmony_ci    TEST_CASE (0.1, 0),
100425bb815Sopenharmony_ci    TEST_CASE (-0.1, 0),
101425bb815Sopenharmony_ci    TEST_CASE (1.1, 1),
102425bb815Sopenharmony_ci    TEST_CASE (-1.1, -1),
103425bb815Sopenharmony_ci    TEST_CASE (4294967295, -1),
104425bb815Sopenharmony_ci    TEST_CASE (-4294967295, 1),
105425bb815Sopenharmony_ci    TEST_CASE (4294967296, 0),
106425bb815Sopenharmony_ci    TEST_CASE (-4294967296, 0),
107425bb815Sopenharmony_ci    TEST_CASE (4294967297, 1),
108425bb815Sopenharmony_ci    TEST_CASE (-4294967297, -1),
109425bb815Sopenharmony_ci    TEST_CASE (2147483648, -2147483648),
110425bb815Sopenharmony_ci    TEST_CASE (-2147483648, -2147483648),
111425bb815Sopenharmony_ci    TEST_CASE (2147483647, 2147483647),
112425bb815Sopenharmony_ci    TEST_CASE (-2147483647, -2147483647),
113425bb815Sopenharmony_ci    TEST_CASE (-2147483649, 2147483647),
114425bb815Sopenharmony_ci    TEST_CASE (2147483649, -2147483647)
115425bb815Sopenharmony_ci#undef TEST_CASE
116425bb815Sopenharmony_ci  };
117425bb815Sopenharmony_ci
118425bb815Sopenharmony_ci  for (uint32_t i = 0;
119425bb815Sopenharmony_ci       i < sizeof (test_cases_int32) / sizeof (test_cases_int32[0]);
120425bb815Sopenharmony_ci       i++)
121425bb815Sopenharmony_ci  {
122425bb815Sopenharmony_ci    TEST_ASSERT (ecma_number_to_int32 (test_cases_int32[i].num) == test_cases_int32[i].int32_num);
123425bb815Sopenharmony_ci  }
124425bb815Sopenharmony_ci  return;
125425bb815Sopenharmony_ci}
126