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 extern "C"
16 {
17 #include "lit-strings.h"
18 #include "ecma-helpers.h"
19 }
20
21 #include "ecma-globals.h"
22 #include "jerryscript.h"
23 #include "test-common.h"
24 #include <gtest/gtest.h>
25
26 /**
27 * Unit test's main function.
28 */
29 class StringToNumberTest : public testing::Test{
30 public:
SetUpTestCase()31 static void SetUpTestCase()
32 {
33 GTEST_LOG_(INFO) << "StringToNumberTest SetUpTestCase";
34 }
35
TearDownTestCase()36 static void TearDownTestCase()
37 {
38 GTEST_LOG_(INFO) << "StringToNumberTest TearDownTestCase";
39 }
40
41 void SetUp() override {}
42 void TearDown() override {}
43
44 };
45
HWTEST_F(StringToNumberTest, Test001, testing::ext::TestSize.Level1)46 HWTEST_F(StringToNumberTest, Test001, testing::ext::TestSize.Level1)
47 {
48 TEST_INIT ();
49
50 const jerry_char_t *strings[] =
51 {
52 (const jerry_char_t *) "1",
53 (const jerry_char_t *) "0.5",
54 (const jerry_char_t *) "12345",
55 (const jerry_char_t *) "1e-45",
56 (const jerry_char_t *) "-2.5e+38",
57 (const jerry_char_t *) "-2.5e38",
58 (const jerry_char_t *) "- 2.5e+38",
59 (const jerry_char_t *) "-2 .5e+38",
60 (const jerry_char_t *) "-2. 5e+38",
61 (const jerry_char_t *) "-2.5e+ 38",
62 (const jerry_char_t *) "-2.5 e+38",
63 (const jerry_char_t *) "-2.5e +38",
64 (const jerry_char_t *) "NaN",
65 (const jerry_char_t *) "abc",
66 (const jerry_char_t *) " Infinity ",
67 (const jerry_char_t *) "-Infinity",
68 (const jerry_char_t *) "0",
69 (const jerry_char_t *) "0",
70 };
71
72 const ecma_number_t nums[] =
73 {
74 (ecma_number_t) 1.0,
75 (ecma_number_t) 0.5,
76 (ecma_number_t) 12345.0,
77 (ecma_number_t) 1.0e-45,
78 (ecma_number_t) -2.5e+38,
79 (ecma_number_t) -2.5e+38,
80 (ecma_number_t) NAN,
81 (ecma_number_t) NAN,
82 (ecma_number_t) NAN,
83 (ecma_number_t) NAN,
84 (ecma_number_t) NAN,
85 (ecma_number_t) NAN,
86 (ecma_number_t) NAN,
87 (ecma_number_t) NAN,
88 (ecma_number_t) INFINITY,
89 (ecma_number_t) -INFINITY,
90 (ecma_number_t) +0.0,
91 (ecma_number_t) -0.0
92 };
93
94 for (uint32_t i = 0;
95 i < sizeof (nums) / sizeof (nums[0]);
96 i++)
97 {
98 ecma_number_t num = ecma_utf8_string_to_number (strings[i], lit_zt_utf8_string_size (strings[i]));
99
100 if (num != nums[i]
101 && (!ecma_number_is_nan (num)
102 || !ecma_number_is_nan (nums[i])))
103 {
104 return;
105 }
106 }
107
108 return;
109 }
110