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 "ecma-init-finalize.h"
18 #include "jmem.h"
19 #include "lit-char-helpers.h"
20 }
21
22 #include "jerryscript-port.h"
23 #include "jerryscript-port-default.h"
24 #include "ecma-helpers.h"
25 #include "lit-strings.h"
26 #include "js-parser-internal.h"
27 #include "test-common.h"
28 #include <gtest/gtest.h>
29
30 static lit_code_point_t
lexer_hex_to_character(const uint8_t * source_p)31 lexer_hex_to_character (const uint8_t *source_p) /**< current source position */
32 {
33 lit_code_point_t result = 0;
34
35 do
36 {
37 uint32_t byte = *source_p++;
38
39 result <<= 4;
40
41 if (byte >= LIT_CHAR_0 && byte <= LIT_CHAR_9)
42 {
43 result += byte - LIT_CHAR_0;
44 }
45 else
46 {
47 byte = LEXER_TO_ASCII_LOWERCASE (byte);
48 if (byte >= LIT_CHAR_LOWERCASE_A && byte <= LIT_CHAR_LOWERCASE_F)
49 {
50 result += byte - (LIT_CHAR_LOWERCASE_A - 10);
51 }
52 else
53 {
54 return UINT32_MAX;
55 }
56 }
57 }
58 while (*source_p);
59
60 return result;
61 } /* lexer_hex_to_character */
62
63 class LitCharHelpersTest : public testing::Test{
64 public:
SetUpTestCase()65 static void SetUpTestCase()
66 {
67 GTEST_LOG_(INFO) << "LitCharHelpersTest SetUpTestCase";
68 }
69
TearDownTestCase()70 static void TearDownTestCase()
71 {
72 GTEST_LOG_(INFO) << "LitCharHelpersTest TearDownTestCase";
73 }
74
SetUp()75 void SetUp() override {}
TearDown()76 void TearDown() override {}
77
78 };
79 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)80 static void* context_alloc_fn(size_t size, void* cb_data)
81 {
82 (void)cb_data;
83 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
84 return malloc(newSize);
85 }
86 HWTEST_F(LitCharHelpersTest, Test001, testing::ext::TestSize.Level1)
87 {
88 TEST_INIT ();
89 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
90 jerry_port_default_set_current_context (ctx_p);
91 jmem_init ();
92 ecma_init ();
93
94 const uint8_t _1_byte_long1[] = "007F";
95 const uint8_t _1_byte_long2[] = "0000";
96 const uint8_t _1_byte_long3[] = "0065";
97
98 const uint8_t _2_byte_long1[] = "008F";
99 const uint8_t _2_byte_long2[] = "00FF";
100 const uint8_t _2_byte_long3[] = "07FF";
101
102 const uint8_t _3_byte_long1[] = "08FF";
103 const uint8_t _3_byte_long2[] = "0FFF";
104 const uint8_t _3_byte_long3[] = "FFFF";
105
106 const uint8_t _6_byte_long1[] = "10000";
107 const uint8_t _6_byte_long2[] = "10FFFF";
108
109 size_t length;
110
111 /* Test 1-byte-long unicode sequences. */
112 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long1));
113 TEST_ASSERT (length == 1);
114
115 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long2));
116 TEST_ASSERT (length == 1);
117
118 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_1_byte_long3));
119 TEST_ASSERT (length == 1);
120
121 /* Test 2-byte-long unicode sequences. */
122 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long1));
123 TEST_ASSERT (length == 2);
124
125 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long2));
126 TEST_ASSERT (length == 2);
127
128 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_2_byte_long3));
129 TEST_ASSERT (length == 2);
130
131 /* Test 3-byte-long unicode sequences. */
132 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long1));
133 TEST_ASSERT (length == 3);
134
135 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long2));
136 TEST_ASSERT (length == 3);
137
138 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_3_byte_long3));
139 TEST_ASSERT (length == 3);
140
141 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long1));
142 TEST_ASSERT (length == 6);
143
144 length = lit_code_point_get_cesu8_length (lexer_hex_to_character (_6_byte_long2));
145 TEST_ASSERT (length == 6);
146
147 ecma_finalize ();
148 jmem_finalize ();
149 free (ctx_p);
150 return;
151 }
152