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 "ecma-init-finalize.h"
18425bb815Sopenharmony_ci  #include "jmem.h"
19425bb815Sopenharmony_ci  #include "lit-char-helpers.h"
20425bb815Sopenharmony_ci}
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci#include "jerryscript-port.h"
23425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
24425bb815Sopenharmony_ci#include "ecma-helpers.h"
25425bb815Sopenharmony_ci#include "lit-strings.h"
26425bb815Sopenharmony_ci#include "js-parser-internal.h"
27425bb815Sopenharmony_ci#include "test-common.h"
28425bb815Sopenharmony_ci#include <gtest/gtest.h>
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_cistatic lit_code_point_t
31425bb815Sopenharmony_cilexer_hex_to_character (const uint8_t *source_p) /**< current source position */
32425bb815Sopenharmony_ci{
33425bb815Sopenharmony_ci  lit_code_point_t result = 0;
34425bb815Sopenharmony_ci
35425bb815Sopenharmony_ci  do
36425bb815Sopenharmony_ci  {
37425bb815Sopenharmony_ci    uint32_t byte = *source_p++;
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_ci    result <<= 4;
40425bb815Sopenharmony_ci
41425bb815Sopenharmony_ci    if (byte >= LIT_CHAR_0 && byte <= LIT_CHAR_9)
42425bb815Sopenharmony_ci    {
43425bb815Sopenharmony_ci      result += byte - LIT_CHAR_0;
44425bb815Sopenharmony_ci    }
45425bb815Sopenharmony_ci    else
46425bb815Sopenharmony_ci    {
47425bb815Sopenharmony_ci      byte = LEXER_TO_ASCII_LOWERCASE (byte);
48425bb815Sopenharmony_ci      if (byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
49425bb815Sopenharmony_ci      {
50425bb815Sopenharmony_ci        result += byte - (LIT_CHAR_LOWERCASE_A - 10);
51425bb815Sopenharmony_ci      }
52425bb815Sopenharmony_ci      else
53425bb815Sopenharmony_ci      {
54425bb815Sopenharmony_ci        return UINT32_MAX;
55425bb815Sopenharmony_ci      }
56425bb815Sopenharmony_ci    }
57425bb815Sopenharmony_ci  }
58425bb815Sopenharmony_ci  while (*source_p);
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci  return result;
61425bb815Sopenharmony_ci} /* lexer_hex_to_character */
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_ciclass LitCharHelpersTest : public testing::Test{
64425bb815Sopenharmony_cipublic:
65425bb815Sopenharmony_ci    static void SetUpTestCase()
66425bb815Sopenharmony_ci    {
67425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "LitCharHelpersTest SetUpTestCase";
68425bb815Sopenharmony_ci    }
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_ci    static void TearDownTestCase()
71425bb815Sopenharmony_ci    {
72425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "LitCharHelpersTest TearDownTestCase";
73425bb815Sopenharmony_ci    }
74425bb815Sopenharmony_ci
75425bb815Sopenharmony_ci    void SetUp() override {}
76425bb815Sopenharmony_ci    void TearDown() override {}
77425bb815Sopenharmony_ci
78425bb815Sopenharmony_ci};
79425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
80425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
81425bb815Sopenharmony_ci{
82425bb815Sopenharmony_ci    (void)cb_data;
83425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
84425bb815Sopenharmony_ci    return malloc(newSize);
85425bb815Sopenharmony_ci}
86425bb815Sopenharmony_ciHWTEST_F(LitCharHelpersTest, Test001, testing::ext::TestSize.Level1)
87425bb815Sopenharmony_ci{
88425bb815Sopenharmony_ci  TEST_INIT ();
89425bb815Sopenharmony_ci  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
90425bb815Sopenharmony_ci  jerry_port_default_set_current_context (ctx_p);
91425bb815Sopenharmony_ci  jmem_init ();
92425bb815Sopenharmony_ci  ecma_init ();
93425bb815Sopenharmony_ci
94425bb815Sopenharmony_ci  const uint8_t _1_byte_long1[] = "007F";
95425bb815Sopenharmony_ci  const uint8_t _1_byte_long2[] = "0000";
96425bb815Sopenharmony_ci  const uint8_t _1_byte_long3[] = "0065";
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_ci  const uint8_t _2_byte_long1[] = "008F";
99425bb815Sopenharmony_ci  const uint8_t _2_byte_long2[] = "00FF";
100425bb815Sopenharmony_ci  const uint8_t _2_byte_long3[] = "07FF";
101425bb815Sopenharmony_ci
102425bb815Sopenharmony_ci  const uint8_t _3_byte_long1[] = "08FF";
103425bb815Sopenharmony_ci  const uint8_t _3_byte_long2[] = "0FFF";
104425bb815Sopenharmony_ci  const uint8_t _3_byte_long3[] = "FFFF";
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci  const uint8_t _6_byte_long1[] = "10000";
107425bb815Sopenharmony_ci  const uint8_t _6_byte_long2[] = "10FFFF";
108425bb815Sopenharmony_ci
109425bb815Sopenharmony_ci  size_t length;
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_ci  /* Test 1-byte-long unicode sequences. */
112425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long1));
113425bb815Sopenharmony_ci  TEST_ASSERT (length == 1);
114425bb815Sopenharmony_ci
115425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long2));
116425bb815Sopenharmony_ci  TEST_ASSERT (length == 1);
117425bb815Sopenharmony_ci
118425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long3));
119425bb815Sopenharmony_ci  TEST_ASSERT (length == 1);
120425bb815Sopenharmony_ci
121425bb815Sopenharmony_ci  /* Test 2-byte-long unicode sequences. */
122425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long1));
123425bb815Sopenharmony_ci  TEST_ASSERT (length == 2);
124425bb815Sopenharmony_ci
125425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long2));
126425bb815Sopenharmony_ci  TEST_ASSERT (length == 2);
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long3));
129425bb815Sopenharmony_ci  TEST_ASSERT (length == 2);
130425bb815Sopenharmony_ci
131425bb815Sopenharmony_ci  /* Test 3-byte-long unicode sequences. */
132425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long1));
133425bb815Sopenharmony_ci  TEST_ASSERT (length == 3);
134425bb815Sopenharmony_ci
135425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long2));
136425bb815Sopenharmony_ci  TEST_ASSERT (length == 3);
137425bb815Sopenharmony_ci
138425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long3));
139425bb815Sopenharmony_ci  TEST_ASSERT (length == 3);
140425bb815Sopenharmony_ci
141425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long1));
142425bb815Sopenharmony_ci  TEST_ASSERT (length == 6);
143425bb815Sopenharmony_ci
144425bb815Sopenharmony_ci  length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long2));
145425bb815Sopenharmony_ci  TEST_ASSERT (length == 6);
146425bb815Sopenharmony_ci
147425bb815Sopenharmony_ci  ecma_finalize ();
148425bb815Sopenharmony_ci  jmem_finalize ();
149425bb815Sopenharmony_ci  free (ctx_p);
150425bb815Sopenharmony_ci  return;
151425bb815Sopenharmony_ci}
152