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_ciextern "C"
16425bb815Sopenharmony_ci{
17425bb815Sopenharmony_ci  #include "lit-strings.h"
18425bb815Sopenharmony_ci  #include "ecma-helpers.h"
19425bb815Sopenharmony_ci}
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ci#include "ecma-globals.h"
22425bb815Sopenharmony_ci#include "jerryscript.h"
23425bb815Sopenharmony_ci#include "test-common.h"
24425bb815Sopenharmony_ci#include <gtest/gtest.h>
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ci/**
27425bb815Sopenharmony_ci * Unit test's main function.
28425bb815Sopenharmony_ci */
29425bb815Sopenharmony_ciclass StringToNumberTest : public testing::Test{
30425bb815Sopenharmony_cipublic:
31425bb815Sopenharmony_ci    static void SetUpTestCase()
32425bb815Sopenharmony_ci    {
33425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "StringToNumberTest SetUpTestCase";
34425bb815Sopenharmony_ci    }
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ci    static void TearDownTestCase()
37425bb815Sopenharmony_ci    {
38425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "StringToNumberTest TearDownTestCase";
39425bb815Sopenharmony_ci    }
40425bb815Sopenharmony_ci
41425bb815Sopenharmony_ci    void SetUp() override {}
42425bb815Sopenharmony_ci    void TearDown() override {}
43425bb815Sopenharmony_ci
44425bb815Sopenharmony_ci};
45425bb815Sopenharmony_ci
46425bb815Sopenharmony_ciHWTEST_F(StringToNumberTest, Test001, testing::ext::TestSize.Level1)
47425bb815Sopenharmony_ci{
48425bb815Sopenharmony_ci  TEST_INIT ();
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci  const jerry_char_t *strings[] =
51425bb815Sopenharmony_ci  {
52425bb815Sopenharmony_ci    (const jerry_char_t *) "1",
53425bb815Sopenharmony_ci    (const jerry_char_t *) "0.5",
54425bb815Sopenharmony_ci    (const jerry_char_t *) "12345",
55425bb815Sopenharmony_ci    (const jerry_char_t *) "1e-45",
56425bb815Sopenharmony_ci    (const jerry_char_t *) "-2.5e+38",
57425bb815Sopenharmony_ci    (const jerry_char_t *) "-2.5e38",
58425bb815Sopenharmony_ci    (const jerry_char_t *) "- 2.5e+38",
59425bb815Sopenharmony_ci    (const jerry_char_t *) "-2 .5e+38",
60425bb815Sopenharmony_ci    (const jerry_char_t *) "-2. 5e+38",
61425bb815Sopenharmony_ci    (const jerry_char_t *) "-2.5e+ 38",
62425bb815Sopenharmony_ci    (const jerry_char_t *) "-2.5 e+38",
63425bb815Sopenharmony_ci    (const jerry_char_t *) "-2.5e +38",
64425bb815Sopenharmony_ci    (const jerry_char_t *) "NaN",
65425bb815Sopenharmony_ci    (const jerry_char_t *) "abc",
66425bb815Sopenharmony_ci    (const jerry_char_t *) "   Infinity  ",
67425bb815Sopenharmony_ci    (const jerry_char_t *) "-Infinity",
68425bb815Sopenharmony_ci    (const jerry_char_t *) "0",
69425bb815Sopenharmony_ci    (const jerry_char_t *) "0",
70425bb815Sopenharmony_ci  };
71425bb815Sopenharmony_ci
72425bb815Sopenharmony_ci  const ecma_number_t nums[] =
73425bb815Sopenharmony_ci  {
74425bb815Sopenharmony_ci    (ecma_number_t) 1.0,
75425bb815Sopenharmony_ci    (ecma_number_t) 0.5,
76425bb815Sopenharmony_ci    (ecma_number_t) 12345.0,
77425bb815Sopenharmony_ci    (ecma_number_t) 1.0e-45,
78425bb815Sopenharmony_ci    (ecma_number_t) -2.5e+38,
79425bb815Sopenharmony_ci    (ecma_number_t) -2.5e+38,
80425bb815Sopenharmony_ci    (ecma_number_t) NAN,
81425bb815Sopenharmony_ci    (ecma_number_t) NAN,
82425bb815Sopenharmony_ci    (ecma_number_t) NAN,
83425bb815Sopenharmony_ci    (ecma_number_t) NAN,
84425bb815Sopenharmony_ci    (ecma_number_t) NAN,
85425bb815Sopenharmony_ci    (ecma_number_t) NAN,
86425bb815Sopenharmony_ci    (ecma_number_t) NAN,
87425bb815Sopenharmony_ci    (ecma_number_t) NAN,
88425bb815Sopenharmony_ci    (ecma_number_t) INFINITY,
89425bb815Sopenharmony_ci    (ecma_number_t) -INFINITY,
90425bb815Sopenharmony_ci    (ecma_number_t) +0.0,
91425bb815Sopenharmony_ci    (ecma_number_t) -0.0
92425bb815Sopenharmony_ci  };
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci  for (uint32_t i = 0;
95425bb815Sopenharmony_ci       i < sizeof (nums) / sizeof (nums[0]);
96425bb815Sopenharmony_ci       i++)
97425bb815Sopenharmony_ci  {
98425bb815Sopenharmony_ci    ecma_number_t num = ecma_utf8_string_to_number (strings[i], lit_zt_utf8_string_size (strings[i]));
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci    if (num != nums[i]
101425bb815Sopenharmony_ci        && (!ecma_number_is_nan (num)
102425bb815Sopenharmony_ci            || !ecma_number_is_nan (nums[i])))
103425bb815Sopenharmony_ci    {
104425bb815Sopenharmony_ci      return;
105425bb815Sopenharmony_ci    }
106425bb815Sopenharmony_ci  }
107425bb815Sopenharmony_ci
108425bb815Sopenharmony_ci  return;
109425bb815Sopenharmony_ci}
110