From c6040bf4da22b8baa6071f2d510d0388ca9e8218 Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Sat, 23 Jul 2022 11:29:26 +0800 Subject: [PATCH] Fix JerryScript Unittests Issue: I5IJKU Signed-off-by: gavin1012_hw --- jerry-port/default/default-external-context.c | 26 +- tests/unit-core/test-backtrace.c | 259 ---------- tests/unit-core/test-literal-storage.c | 134 ----- tests/unit-core/test-mem-stats.c | 57 --- tests/unit-core/test-regexp.c | 71 --- tests/unit-core/test-snapshot.c | 473 ------------------ tests/unit-core/test-stringbuilder.c | 244 --------- tests/unit-core/test-strings.c | 222 -------- 8 files changed, 25 insertions(+), 1461 deletions(-) delete mode 100644 tests/unit-core/test-backtrace.c delete mode 100644 tests/unit-core/test-literal-storage.c delete mode 100644 tests/unit-core/test-mem-stats.c delete mode 100644 tests/unit-core/test-regexp.c delete mode 100644 tests/unit-core/test-snapshot.c delete mode 100644 tests/unit-core/test-stringbuilder.c delete mode 100644 tests/unit-core/test-strings.c diff --git a/jerry-port/default/default-external-context.c b/jerry-port/default/default-external-context.c index 46ac88dc..a5c0511e 100644 --- a/jerry-port/default/default-external-context.c +++ b/jerry-port/default/default-external-context.c @@ -92,4 +92,28 @@ jerry_port_get_current_context (void) #endif // defined (JERRY_FOR_IAR_CONFIG) -#endif // (JERRY_EXTERNAL_CONTEXT == 1) +#else // (JERRY_EXTERNAL_CONTEXT == 0) + +static jerry_context_t *current_context_p = NULL; + +/** + * Set the current_context_p as the passed pointer. + */ +void +jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */ +{ + current_context_p = context_p; +} /* jerry_port_default_set_current_context */ + +/** + * Get the current context. + * + * @return the pointer to the current context + */ +jerry_context_t * +jerry_port_get_current_context (void) +{ + return current_context_p; +} /* jerry_port_get_current_context */ + +#endif // (JERRY_EXTERNAL_CONTEXT == 0) diff --git a/tests/unit-core/test-backtrace.c b/tests/unit-core/test-backtrace.c deleted file mode 100644 index 663d1be7..00000000 --- a/tests/unit-core/test-backtrace.c +++ /dev/null @@ -1,259 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "config.h" -#include "jerryscript.h" -#include "test-common.h" - -static jerry_value_t -backtrace_handler (const jerry_value_t function_obj, /**< function object */ - const jerry_value_t this_val, /**< this value */ - const jerry_value_t args_p[], /**< argument list */ - const jerry_length_t args_count) /**< argument count */ -{ - JERRY_UNUSED (function_obj); - JERRY_UNUSED (this_val); - - uint32_t max_depth = 0; - - if (args_count > 0 && jerry_value_is_number (args_p[0])) - { - max_depth = (uint32_t) jerry_get_number_value (args_p[0]); - } - - return jerry_get_backtrace (max_depth); -} /* backtrace_handler */ - -static jerry_value_t -run (const char *resource_name_p, /**< resource name */ - const char *source_p) /**< source code */ -{ - jerry_value_t code = jerry_parse ((const jerry_char_t *) resource_name_p, - strlen (resource_name_p), - (const jerry_char_t *) source_p, - strlen (source_p), - JERRY_PARSE_NO_OPTS); - TEST_ASSERT (!jerry_value_is_error (code)); - - jerry_value_t result = jerry_run (code); - jerry_release_value (code); - - return result; -} /* run */ - -static void -compare (jerry_value_t array, /**< array */ - uint32_t index, /**< item index */ - const char *str) /**< string to compare */ -{ - jerry_char_t buf[64]; - - size_t len = strlen (str); - - TEST_ASSERT (len < sizeof (buf)); - - jerry_value_t value = jerry_get_property_by_index (array, index); - - TEST_ASSERT (!jerry_value_is_error (value) - && jerry_value_is_string (value)); - - TEST_ASSERT (jerry_get_string_size (value) == len); - - jerry_size_t str_len = jerry_string_to_char_buffer (value, buf, (jerry_size_t) len); - TEST_ASSERT (str_len == len); - - jerry_release_value (value); - - TEST_ASSERT (memcmp (buf, str, len) == 0); -} /* compare */ - -static void -test_get_backtrace_api_call (void) -{ - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t global = jerry_get_global_object (); - - jerry_value_t func = jerry_create_external_function (backtrace_handler); - jerry_value_t name = jerry_create_string ((const jerry_char_t *) "backtrace"); - jerry_value_t result = jerry_set_property (global, name, func); - TEST_ASSERT (!jerry_value_is_error (result)); - - jerry_release_value (result); - jerry_release_value (name); - jerry_release_value (func); - - jerry_release_value (global); - - const char *source = ("function f() {\n" - " return backtrace(0);\n" - "}\n" - "\n" - "function g() {\n" - " return f();\n" - "}\n" - "\n" - "function h() {\n" - " return g();\n" - "}\n" - "\n" - "h();\n"); - - jerry_value_t backtrace = run ("something.js", source); - - TEST_ASSERT (!jerry_value_is_error (backtrace) - && jerry_value_is_array (backtrace)); - - TEST_ASSERT (jerry_get_array_length (backtrace) == 4); - - compare (backtrace, 0, "something.js:2"); - compare (backtrace, 1, "something.js:6"); - compare (backtrace, 2, "something.js:10"); - compare (backtrace, 3, "something.js:13"); - - jerry_release_value (backtrace); - - /* Depth set to 2 this time. */ - - source = ("function f() {\n" - " return backtrace(2);\n" - "}\n" - "\n" - "function g() {\n" - " return f();\n" - "}\n" - "\n" - "function h() {\n" - " return g();\n" - "}\n" - "\n" - "h();\n"); - - backtrace = run ("something_else.js", source); - - TEST_ASSERT (!jerry_value_is_error (backtrace) - && jerry_value_is_array (backtrace)); - - TEST_ASSERT (jerry_get_array_length (backtrace) == 2); - - compare (backtrace, 0, "something_else.js:2"); - compare (backtrace, 1, "something_else.js:6"); - - jerry_release_value (backtrace); - - jerry_cleanup (); -} /* test_get_backtrace_api_call */ - -static void -test_exception_backtrace (void) -{ - jerry_init (JERRY_INIT_EMPTY); - - const char *source = ("function f() {\n" - " undef_reference;\n" - "}\n" - "\n" - "function g() {\n" - " return f();\n" - "}\n" - "\n" - "g();\n"); - - jerry_value_t error = run ("bad.js", source); - - TEST_ASSERT (jerry_value_is_error (error)); - - error = jerry_get_value_from_error (error, true); - - TEST_ASSERT (jerry_value_is_object (error)); - - jerry_value_t name = jerry_create_string ((const jerry_char_t *) "stack"); - jerry_value_t backtrace = jerry_get_property (error, name); - - jerry_release_value (name); - jerry_release_value (error); - - TEST_ASSERT (!jerry_value_is_error (backtrace) - && jerry_value_is_array (backtrace)); - - TEST_ASSERT (jerry_get_array_length (backtrace) == 3); - - compare (backtrace, 0, "bad.js:2"); - compare (backtrace, 1, "bad.js:6"); - compare (backtrace, 2, "bad.js:9"); - - jerry_release_value (backtrace); - - jerry_cleanup (); -} /* test_exception_backtrace */ - -static void -test_large_line_count (void) -{ - jerry_init (JERRY_INIT_EMPTY); - - const char *source = ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" - "g();\n"); - - jerry_value_t error = run ("bad.js", source); - - TEST_ASSERT (jerry_value_is_error (error)); - - error = jerry_get_value_from_error (error, true); - - TEST_ASSERT (jerry_value_is_object (error)); - - jerry_value_t name = jerry_create_string ((const jerry_char_t *) "stack"); - jerry_value_t backtrace = jerry_get_property (error, name); - - jerry_release_value (name); - jerry_release_value (error); - - TEST_ASSERT (!jerry_value_is_error (backtrace) - && jerry_value_is_array (backtrace)); - - TEST_ASSERT (jerry_get_array_length (backtrace) == 1); - - compare (backtrace, 0, "bad.js:385"); - - jerry_release_value (backtrace); - - jerry_cleanup (); -} /* test_large_line_count */ - -int -main (void) -{ - TEST_INIT (); - - TEST_ASSERT (jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO)); - - test_get_backtrace_api_call (); - test_exception_backtrace (); - test_large_line_count (); - - return 0; -} /* main */ diff --git a/tests/unit-core/test-literal-storage.c b/tests/unit-core/test-literal-storage.c deleted file mode 100644 index c471c03f..00000000 --- a/tests/unit-core/test-literal-storage.c +++ /dev/null @@ -1,134 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ecma-helpers.h" -#include "ecma-literal-storage.h" -#include "test-common.h" - -/* Iterations count. */ -#define test_iters 64 - -/* Subiterations count. */ -#define test_sub_iters 64 - -/* Max characters in a string. */ -#define max_characters_in_string 256 - -static void -generate_string (lit_utf8_byte_t *str, lit_utf8_size_t len) -{ - static const lit_utf8_byte_t bytes[] = "!@#$%^&*()_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; - static const lit_utf8_size_t length = (lit_utf8_size_t) (sizeof (bytes) - 1); - for (lit_utf8_size_t i = 0; i < len; ++i) - { - str[i] = bytes[(unsigned long) rand () % length]; - } -} /* generate_string */ - -static ecma_number_t -generate_number (void) -{ - ecma_number_t num = ((ecma_number_t) rand () / 32767.0); - if (rand () % 2) - { - num = -num; - } - int power = rand () % 30; - while (power-- > 0) - { - num *= 10; - } - return num; -} /* generate_number */ - -int -main (void) -{ - TEST_INIT (); - - const lit_utf8_byte_t *ptrs[test_sub_iters]; - ecma_number_t numbers[test_sub_iters]; - lit_utf8_byte_t strings[test_sub_iters][max_characters_in_string + 1]; - lit_utf8_size_t lengths[test_sub_iters]; - - jmem_init (); - - for (uint32_t i = 0; i < test_iters; i++) - { - memset (numbers, 0, sizeof (ecma_number_t) * test_sub_iters); - memset (lengths, 0, sizeof (lit_utf8_size_t) * test_sub_iters); - memset (ptrs, 0, sizeof (lit_utf8_byte_t *) * test_sub_iters); - - for (uint32_t j = 0; j < test_sub_iters; j++) - { - int type = rand () % 3; - if (type == 0) - { - lengths[j] = (lit_utf8_size_t) (rand () % max_characters_in_string + 1); - generate_string (strings[j], lengths[j]); - ecma_find_or_create_literal_string (strings[j], lengths[j]); - strings[j][lengths[j]] = '\0'; - ptrs[j] = strings[j]; - TEST_ASSERT (ptrs[j]); - } - else if (type == 1) - { - lit_magic_string_id_t msi = (lit_magic_string_id_t) (rand () % LIT_NON_INTERNAL_MAGIC_STRING__COUNT); - ptrs[j] = lit_get_magic_string_utf8 (msi); - TEST_ASSERT (ptrs[j]); - lengths[j] = (lit_utf8_size_t) lit_zt_utf8_string_size (ptrs[j]); - ecma_find_or_create_literal_string (ptrs[j], lengths[j]); - } - else - { - ecma_number_t num = generate_number (); - lengths[j] = ecma_number_to_utf8_string (num, strings[j], max_characters_in_string); - ecma_find_or_create_literal_number (num); - } - } - - /* Add empty string. */ - ecma_find_or_create_literal_string (NULL, 0); - - for (uint32_t j = 0; j < test_sub_iters; j++) - { - ecma_value_t lit1; - ecma_value_t lit2; - if (ptrs[j]) - { - lit1 = ecma_find_or_create_literal_string (ptrs[j], lengths[j]); - lit2 = ecma_find_or_create_literal_string (ptrs[j], lengths[j]); - TEST_ASSERT (ecma_is_value_string (lit1)); - TEST_ASSERT (ecma_is_value_string (lit2)); - TEST_ASSERT (lit1 == lit2); - } - else - { - lit1 = ecma_find_or_create_literal_number (numbers[j]); - lit2 = ecma_find_or_create_literal_number (numbers[j]); - TEST_ASSERT (ecma_is_value_number (lit1)); - TEST_ASSERT (ecma_is_value_number (lit2)); - TEST_ASSERT (lit1 == lit2); - } - } - - /* Check empty string exists. */ - TEST_ASSERT (ecma_find_or_create_literal_string (NULL, 0) != JMEM_CP_NULL); - } - - ecma_finalize_lit_storage (); - jmem_finalize (); - return 0; -} /* main */ diff --git a/tests/unit-core/test-mem-stats.c b/tests/unit-core/test-mem-stats.c deleted file mode 100644 index 2a29ae6e..00000000 --- a/tests/unit-core/test-mem-stats.c +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "jerryscript.h" -#include "test-common.h" - -int main (void) -{ - if (!jerry_is_feature_enabled (JERRY_FEATURE_MEM_STATS)) - { - return 0; - } - const jerry_char_t test_source[] = TEST_STRING_LITERAL ( - "var a = 'hello';" - "var b = 'world';" - "var c = a + ' ' + b;" - ); - - jerry_init (JERRY_INIT_EMPTY); - jerry_value_t parsed_code_val = jerry_parse (NULL, - 0, - test_source, - sizeof (test_source) - 1, - JERRY_PARSE_NO_OPTS); - TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); - - jerry_value_t res = jerry_run (parsed_code_val); - TEST_ASSERT (!jerry_value_is_error (res)); - - jerry_heap_stats_t stats; - memset (&stats, 0, sizeof (stats)); - bool get_stats_ret = jerry_get_memory_stats (&stats); - TEST_ASSERT (get_stats_ret); - TEST_ASSERT (stats.version == 1); - TEST_ASSERT (stats.size == 524280); - - TEST_ASSERT (!jerry_get_memory_stats (NULL)); - - jerry_release_value (res); - jerry_release_value (parsed_code_val); - - jerry_cleanup (); - - return 0; -} /* main */ diff --git a/tests/unit-core/test-regexp.c b/tests/unit-core/test-regexp.c deleted file mode 100644 index 2c7ac042..00000000 --- a/tests/unit-core/test-regexp.c +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "jerryscript.h" - -#include "test-common.h" - -int -main (void) -{ - TEST_INIT (); - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t global_obj_val = jerry_get_global_object (); - - jerry_char_t pattern[] = "[^.]+"; - uint16_t flags = JERRY_REGEXP_FLAG_GLOBAL | JERRY_REGEXP_FLAG_MULTILINE; - jerry_value_t regex_obj = jerry_create_regexp (pattern, flags); - TEST_ASSERT (jerry_value_is_object (regex_obj)); - - const jerry_char_t func_resource[] = "unknown"; - const jerry_char_t func_arg_list[] = "regex"; - const jerry_char_t func_src[] = "return [regex.exec('something.domain.com'), regex.multiline, regex.global];"; - jerry_value_t func_val = jerry_parse_function (func_resource, - sizeof (func_resource) - 1, - func_arg_list, - sizeof (func_arg_list) - 1, - func_src, - sizeof (func_src) - 1, - JERRY_PARSE_NO_OPTS); - - jerry_value_t res = jerry_call_function (func_val, global_obj_val, ®ex_obj, 1); - jerry_value_t regex_res = jerry_get_property_by_index (res, 0); - jerry_value_t regex_res_str = jerry_get_property_by_index (regex_res, 0); - jerry_value_t is_multiline = jerry_get_property_by_index (res, 1); - jerry_value_t is_global = jerry_get_property_by_index (res, 2); - - jerry_size_t str_size = jerry_get_string_size (regex_res_str); - JERRY_VLA (jerry_char_t, res_buff, str_size); - jerry_size_t res_size = jerry_string_to_char_buffer (regex_res_str, res_buff, str_size); - - const char expected_result[] = "something"; - TEST_ASSERT (res_size == (sizeof (expected_result) - 1)); - TEST_ASSERT (strncmp (expected_result, (const char *) res_buff, res_size) == 0); - TEST_ASSERT (jerry_get_boolean_value (is_multiline)); - TEST_ASSERT (jerry_get_boolean_value (is_global)); - - jerry_release_value (regex_obj); - jerry_release_value (res); - jerry_release_value (func_val); - jerry_release_value (regex_res); - jerry_release_value (regex_res_str); - jerry_release_value (is_multiline); - jerry_release_value (is_global); - jerry_release_value (global_obj_val); - - jerry_cleanup (); - return 0; -} /* main */ diff --git a/tests/unit-core/test-snapshot.c b/tests/unit-core/test-snapshot.c deleted file mode 100644 index 8c1a39ed..00000000 --- a/tests/unit-core/test-snapshot.c +++ /dev/null @@ -1,473 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "config.h" -#include "jerryscript.h" - -#include "test-common.h" - -/** - * Maximum size of snapshots buffer - */ -#define SNAPSHOT_BUFFER_SIZE (256) - -/** - * Maximum size of literal buffer - */ -#define LITERAL_BUFFER_SIZE (256) - -/** - * Magic strings - */ -static const jerry_char_t *magic_strings[] = -{ - (const jerry_char_t *) " ", - (const jerry_char_t *) "a", - (const jerry_char_t *) "b", - (const jerry_char_t *) "c", - (const jerry_char_t *) "from", - (const jerry_char_t *) "func", - (const jerry_char_t *) "string", - (const jerry_char_t *) "snapshot" -}; - -/** - * Magic string lengths - */ -static const jerry_length_t magic_string_lengths[] = -{ - 1, 1, 1, 1, 4, 4, 6, 8 -}; - -static void test_function_snapshot (void) -{ - /* function to snapshot */ - if (!jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) - || !jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) - { - return; - } - - const jerry_init_flag_t flags = JERRY_INIT_EMPTY; - static uint32_t function_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; - - const jerry_char_t func_args[] = "a, b"; - const jerry_char_t code_to_snapshot[] = "return a + b"; - - jerry_init (flags); - jerry_value_t generate_result; - generate_result = jerry_generate_function_snapshot (NULL, - 0, - code_to_snapshot, - sizeof (code_to_snapshot) - 1, - func_args, - sizeof (func_args) - 1, - 0, - function_snapshot_buffer, - SNAPSHOT_BUFFER_SIZE); - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - size_t function_snapshot_size = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - jerry_cleanup (); - - jerry_init (flags); - - jerry_value_t function_obj = jerry_load_function_snapshot (function_snapshot_buffer, - function_snapshot_size, - 0, - 0); - - TEST_ASSERT (!jerry_value_is_error (function_obj)); - TEST_ASSERT (jerry_value_is_function (function_obj)); - - jerry_value_t this_val = jerry_create_undefined (); - jerry_value_t args[2]; - args[0] = jerry_create_number (1.0); - args[1] = jerry_create_number (2.0); - - jerry_value_t res = jerry_call_function (function_obj, this_val, args, 2); - - TEST_ASSERT (!jerry_value_is_error (res)); - TEST_ASSERT (jerry_value_is_number (res)); - double num = jerry_get_number_value (res); - TEST_ASSERT (num == 3); - - jerry_release_value (args[0]); - jerry_release_value (args[1]); - jerry_release_value (res); - jerry_release_value (function_obj); - - jerry_cleanup (); -} /* test_function_snapshot */ - -static void arguments_test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint32_t exec_snapshot_flags) -{ - jerry_init (JERRY_INIT_EMPTY); - jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags); - TEST_ASSERT (!jerry_value_is_error (res)); - TEST_ASSERT (jerry_value_is_number (res)); - double raw_value = jerry_get_number_value (res); - TEST_ASSERT (raw_value == 15); - jerry_release_value (res); - - jerry_cleanup (); -} /* arguments_test_exec_snapshot */ - -static void test_function_arguments_snapshot (void) -{ - if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) - && jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) - { - static uint32_t arguments_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; - - const jerry_char_t code_to_snapshot[] = TEST_STRING_LITERAL ( - "function f(a,b,c) {" - " arguments[0]++;" - " arguments[1]++;" - " arguments[2]++;" - " return a + b + c;" - "}" - "f(3,4,5);" - ); - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t generate_result; - generate_result = jerry_generate_snapshot (NULL, - 0, - code_to_snapshot, - sizeof (code_to_snapshot) - 1, - 0, - arguments_snapshot_buffer, - SNAPSHOT_BUFFER_SIZE); - - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - size_t snapshot_size = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - jerry_cleanup (); - - arguments_test_exec_snapshot (arguments_snapshot_buffer, snapshot_size, 0); - arguments_test_exec_snapshot (arguments_snapshot_buffer, snapshot_size, JERRY_SNAPSHOT_EXEC_COPY_DATA); - } -} /* test_function_arguments_snapshot */ - -static void test_exec_snapshot (uint32_t *snapshot_p, size_t snapshot_size, uint32_t exec_snapshot_flags) -{ - char string_data[32]; - - jerry_init (JERRY_INIT_EMPTY); - - jerry_register_magic_strings (magic_strings, - sizeof (magic_string_lengths) / sizeof (jerry_length_t), - magic_string_lengths); - - jerry_value_t res = jerry_exec_snapshot (snapshot_p, snapshot_size, 0, exec_snapshot_flags); - - TEST_ASSERT (!jerry_value_is_error (res)); - TEST_ASSERT (jerry_value_is_string (res)); - jerry_size_t sz = jerry_get_string_size (res); - TEST_ASSERT (sz == 20); - sz = jerry_string_to_char_buffer (res, (jerry_char_t *) string_data, sz); - TEST_ASSERT (sz == 20); - jerry_release_value (res); - TEST_ASSERT (!strncmp (string_data, "string from snapshot", (size_t) sz)); - - jerry_cleanup (); -} /* test_exec_snapshot */ - -int -main (void) -{ - static uint32_t snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; - - TEST_INIT (); - - /* Dump / execute snapshot */ - if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) - && jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) - { - const jerry_char_t code_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();"; - - jerry_init (JERRY_INIT_EMPTY); - jerry_value_t generate_result; - generate_result = jerry_generate_snapshot (NULL, - 0, - code_to_snapshot, - sizeof (code_to_snapshot) - 1, - 0, - snapshot_buffer, - SNAPSHOT_BUFFER_SIZE); - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - size_t snapshot_size = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - /* Check the snapshot data. Unused bytes should be filled with zeroes */ - const uint8_t expected_data[] = - { - 0x4A, 0x52, 0x52, 0x59, 0x2C, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, - 0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x01, 0x00, - 0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, - 0x2C, 0x00, 0xC9, 0x53, 0x00, 0x00, 0x00, 0x00, - 0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x00, - 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x14, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, - 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x73, 0x6E, - 0x61, 0x70, 0x73, 0x68, 0x6F, 0x74, - }; - - if (sizeof (expected_data) != snapshot_size || memcmp (expected_data, snapshot_buffer, sizeof (expected_data))) - { - printf ("Snapshot data has been changed, please update tests/unit-core/test-snapshot.c.\n"); - printf ("-------------------------------------------------------------------------------\n"); - printf (" const uint8_t expected_data[] =\n"); - printf (" {"); - for (unsigned int i = 0; i < snapshot_size; i++) - { - if ((i % 8) == 0) - { - printf ("\n "); - } - printf (" 0x%02X,", ((uint8_t *) snapshot_buffer)[i]); - } - printf ("\n };\n"); - printf ("-------------------------------------------------------------------------------\n"); - } - - TEST_ASSERT (sizeof (expected_data) == snapshot_size); - TEST_ASSERT (0 == memcmp (expected_data, snapshot_buffer, sizeof (expected_data))); - - jerry_cleanup (); - - test_exec_snapshot (snapshot_buffer, snapshot_size, 0); - test_exec_snapshot (snapshot_buffer, snapshot_size, JERRY_SNAPSHOT_EXEC_COPY_DATA); - } - - /* Static snapshot */ - if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) - && jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) - { - const jerry_char_t code_to_snapshot[] = TEST_STRING_LITERAL ( - "function func(a, b, c) {" - " c = 'snapshot';" - " return arguments[0] + ' ' + b + ' ' + arguments[2];" - "};" - "func('string', 'from');" - ); - - jerry_init (JERRY_INIT_EMPTY); - jerry_register_magic_strings (magic_strings, - sizeof (magic_string_lengths) / sizeof (jerry_length_t), - magic_string_lengths); - - jerry_value_t generate_result; - generate_result = jerry_generate_snapshot (NULL, - 0, - code_to_snapshot, - sizeof (code_to_snapshot) - 1, - JERRY_SNAPSHOT_SAVE_STATIC, - snapshot_buffer, - SNAPSHOT_BUFFER_SIZE); - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - size_t snapshot_size = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - /* Static snapshots are not supported by default. */ - jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0); - TEST_ASSERT (jerry_value_is_error (exec_result)); - jerry_release_value (exec_result); - - jerry_cleanup (); - - test_exec_snapshot (snapshot_buffer, snapshot_size, JERRY_SNAPSHOT_EXEC_ALLOW_STATIC); - } - - /* Merge snapshot */ - if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) - && jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) - { - static uint32_t snapshot_buffer_0[SNAPSHOT_BUFFER_SIZE]; - static uint32_t snapshot_buffer_1[SNAPSHOT_BUFFER_SIZE]; - size_t snapshot_sizes[2]; - static uint32_t merged_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; - - const jerry_char_t code_to_snapshot1[] = "var a = 'hello'; 123"; - - jerry_init (JERRY_INIT_EMPTY); - jerry_value_t generate_result; - generate_result = jerry_generate_snapshot (NULL, - 0, - code_to_snapshot1, - sizeof (code_to_snapshot1) - 1, - 0, - snapshot_buffer_0, - SNAPSHOT_BUFFER_SIZE); - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - snapshot_sizes[0] = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - jerry_cleanup (); - - const jerry_char_t code_to_snapshot2[] = "var b = 'hello'; 456"; - - jerry_init (JERRY_INIT_EMPTY); - generate_result = jerry_generate_snapshot (NULL, - 0, - code_to_snapshot2, - sizeof (code_to_snapshot2) - 1, - 0, - snapshot_buffer_1, - SNAPSHOT_BUFFER_SIZE); - TEST_ASSERT (!jerry_value_is_error (generate_result) - && jerry_value_is_number (generate_result)); - - snapshot_sizes[1] = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - jerry_cleanup (); - - jerry_init (JERRY_INIT_EMPTY); - - const char *error_p; - const uint32_t *snapshot_buffers[2]; - - snapshot_buffers[0] = snapshot_buffer_0; - snapshot_buffers[1] = snapshot_buffer_1; - - static uint32_t snapshot_buffer_0_bck[SNAPSHOT_BUFFER_SIZE]; - static uint32_t snapshot_buffer_1_bck[SNAPSHOT_BUFFER_SIZE]; - - memcpy (snapshot_buffer_0_bck, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE); - memcpy (snapshot_buffer_1_bck, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE); - - size_t merged_size = jerry_merge_snapshots (snapshot_buffers, - snapshot_sizes, - 2, - merged_snapshot_buffer, - SNAPSHOT_BUFFER_SIZE, - &error_p); - - jerry_cleanup (); - - TEST_ASSERT (0 == memcmp (snapshot_buffer_0_bck, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE)); - TEST_ASSERT (0 == memcmp (snapshot_buffer_1_bck, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE)); - - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 0, 0); - TEST_ASSERT (!jerry_value_is_error (res)); - TEST_ASSERT (jerry_get_number_value (res) == 123); - jerry_release_value (res); - - res = jerry_exec_snapshot (merged_snapshot_buffer, merged_size, 1, 0); - TEST_ASSERT (!jerry_value_is_error (res)); - TEST_ASSERT (jerry_get_number_value (res) == 456); - jerry_release_value (res); - - jerry_cleanup (); - } - - /* Save literals */ - if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE)) - { - /* C format generation */ - jerry_init (JERRY_INIT_EMPTY); - - static jerry_char_t literal_buffer_c[LITERAL_BUFFER_SIZE]; - static uint32_t literal_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; - static const jerry_char_t code_for_c_format[] = "var object = { aa:'fo\" o\\n \\\\', Bb:'max', aaa:'xzy0' };"; - - jerry_value_t generate_result; - generate_result = jerry_generate_snapshot (NULL, - 0, - code_for_c_format, - sizeof (code_for_c_format) - 1, - 0, - literal_snapshot_buffer, - SNAPSHOT_BUFFER_SIZE); - - TEST_ASSERT (!jerry_value_is_error (generate_result)); - TEST_ASSERT (jerry_value_is_number (generate_result)); - - size_t snapshot_size = (size_t) jerry_get_number_value (generate_result); - jerry_release_value (generate_result); - - /* In ES2015 we emit extra bytecode instructions to check global variable redeclaration. */ - const size_t expected_size = (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL)) ? 132 : 124; - TEST_ASSERT (snapshot_size == expected_size); - - const size_t lit_c_buf_sz = jerry_get_literals_from_snapshot (literal_snapshot_buffer, - snapshot_size, - literal_buffer_c, - LITERAL_BUFFER_SIZE, - true); - TEST_ASSERT (lit_c_buf_sz == 239); - - static const char *expected_c_format = ( - "jerry_length_t literal_count = 5;\n\n" - "jerry_char_t *literals[5] =\n" - "{\n" - " \"Bb\",\n" - " \"aa\",\n" - " \"aaa\",\n" - " \"xzy0\",\n" - " \"fo\\\" o\\x0A \\\\\"\n" - "};\n\n" - "jerry_length_t literal_sizes[5] =\n" - "{\n" - " 2 /* Bb */,\n" - " 2 /* aa */,\n" - " 3 /* aaa */,\n" - " 4 /* xzy0 */,\n" - " 8 /* fo\" o\n \\ */\n" - "};\n" - ); - - TEST_ASSERT (!strncmp ((char *) literal_buffer_c, expected_c_format, lit_c_buf_sz)); - - /* List format generation */ - static jerry_char_t literal_buffer_list[LITERAL_BUFFER_SIZE]; - const size_t lit_list_buf_sz = jerry_get_literals_from_snapshot (literal_snapshot_buffer, - snapshot_size, - literal_buffer_list, - LITERAL_BUFFER_SIZE, - false); - TEST_ASSERT (lit_list_buf_sz == 34); - TEST_ASSERT (!strncmp ((char *) literal_buffer_list, - "2 Bb\n2 aa\n3 aaa\n4 xzy0\n8 fo\" o\n \\\n", - lit_list_buf_sz)); - - jerry_cleanup (); - } - - test_function_snapshot (); - - test_function_arguments_snapshot (); - - return 0; -} /* main */ diff --git a/tests/unit-core/test-stringbuilder.c b/tests/unit-core/test-stringbuilder.c deleted file mode 100644 index 7aa4eab3..00000000 --- a/tests/unit-core/test-stringbuilder.c +++ /dev/null @@ -1,244 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ecma-helpers.h" -#include "ecma-init-finalize.h" -#include "lit-char-helpers.h" -#include "lit-strings.h" - -#include "test-common.h" - -int -main (void) -{ - TEST_INIT (); - - jmem_init (); - ecma_init (); - - { - static const lit_utf8_byte_t string_data[] = "A simple string"; - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - } - - { - static const lit_utf8_byte_t string_data[] = "a"; - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_A); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - static const lit_utf8_byte_t string_data[] = "A simple string"; - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append (&builder, str_p); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - } - - { - static const lit_utf8_byte_t string_data[] = "abc"; - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_A); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_B); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_LOWERCASE_C); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_1); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_2); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_3); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_new_ecma_string_from_uint32 (123); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - static const lit_utf8_byte_t string_data[] = "abc"; - ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_1); - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - ecma_stringbuilder_append (&builder, uint_str_p); - ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - static const lit_utf8_byte_t expected_data[] = "1abc234string"; - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - static const lit_utf8_byte_t string_data[] = "abc"; - ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_1); - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - ecma_stringbuilder_append (&builder, uint_str_p); - ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING); - /* Test that we do not leak. */ - ecma_stringbuilder_destroy (&builder); - } - - { - static const lit_utf8_byte_t string_data[] = "abcdefghijklmnop"; - const size_t count = UINT16_MAX / (sizeof (string_data) - 1) + 1; - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - for (size_t i = 0; i < count; i++) - { - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - } - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *expected_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); - for (size_t i = 0; i < count; i++) - { - expected_p = ecma_append_chars_to_string (expected_p, - string_data, - sizeof (string_data) - 1, - sizeof (string_data) - 1); - } - - TEST_ASSERT (ecma_compare_ecma_strings (result_p, expected_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (expected_p); - } - - { - static const lit_utf8_byte_t string_data[] = "abc"; - ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_stringbuilder_append_char (&builder, LIT_CHAR_1); - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - - ecma_string_t *another_string = ecma_new_ecma_string_from_utf8 (string_data, sizeof (string_data) - 1); - - ecma_stringbuilder_append (&builder, uint_str_p); - ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - static const lit_utf8_byte_t expected_data[] = "1abc234string"; - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - ecma_deref_ecma_string (another_string); - } - - { - static const lit_utf8_byte_t string_data[] = "abc"; - ecma_string_t *uint_str_p = ecma_new_ecma_string_from_uint32 (234); - - ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (uint_str_p); - ecma_stringbuilder_append_raw (&builder, string_data, sizeof (string_data) - 1); - ecma_stringbuilder_append_magic (&builder, LIT_MAGIC_STRING_STRING); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - static const lit_utf8_byte_t expected_data[] = "234abcstring"; - ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (expected_data, sizeof (expected_data) - 1); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_stringbuilder_t builder = ecma_stringbuilder_create (); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); - ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (str_p); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - { - ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING); - ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (str_p); - ecma_string_t *result_p = ecma_stringbuilder_finalize (&builder); - - TEST_ASSERT (ecma_compare_ecma_strings (result_p, str_p)); - ecma_deref_ecma_string (result_p); - ecma_deref_ecma_string (str_p); - } - - ecma_finalize (); - jmem_finalize (); - - return 0; -} /* main */ diff --git a/tests/unit-core/test-strings.c b/tests/unit-core/test-strings.c deleted file mode 100644 index 7896d7a0..00000000 --- a/tests/unit-core/test-strings.c +++ /dev/null @@ -1,222 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ecma-helpers.h" -#include "lit-strings.h" -#include "ecma-init-finalize.h" - -#include "test-common.h" - -/* Iterations count. */ -#define test_iters (1024) - -/* Sub iterations count. */ -#define test_subiters (128) - -/* Max bytes in string. */ -#define max_bytes_in_string (65 * 1024) -#define max_code_units_in_string (max_bytes_in_string) - -typedef enum -{ - CESU8_ANY_SIZE, - CESU8_ONE_BYTE, - CESU8_TWO_BYTES, - CESU8_THREE_BYTES, -} utf8_char_size; - -static lit_utf8_size_t -generate_cesu8_char (utf8_char_size char_size, - lit_utf8_byte_t *buf) -{ - TEST_ASSERT (char_size >= 0 && char_size <= LIT_CESU8_MAX_BYTES_IN_CODE_UNIT); - lit_code_point_t code_point = (lit_code_point_t) rand (); - - if (char_size == 1) - { - code_point %= LIT_UTF8_1_BYTE_CODE_POINT_MAX; - } - else if (char_size == 2) - { - code_point = LIT_UTF8_2_BYTE_CODE_POINT_MIN + code_point % (LIT_UTF8_2_BYTE_CODE_POINT_MAX - - LIT_UTF8_2_BYTE_CODE_POINT_MIN); - } - else if (char_size == 3) - { - code_point = LIT_UTF8_3_BYTE_CODE_POINT_MIN + code_point % (LIT_UTF8_3_BYTE_CODE_POINT_MAX - - LIT_UTF8_3_BYTE_CODE_POINT_MIN); - } - else - { - code_point %= LIT_UTF8_3_BYTE_CODE_POINT_MAX; - } - - if (code_point >= LIT_UTF16_HIGH_SURROGATE_MIN - && code_point <= LIT_UTF16_LOW_SURROGATE_MAX) - { - code_point = LIT_UTF16_HIGH_SURROGATE_MIN - 1; - } - - return lit_code_unit_to_utf8 ((ecma_char_t) code_point, buf); -} /* generate_cesu8_char */ - -static ecma_length_t -generate_cesu8_string (lit_utf8_byte_t *buf_p, - lit_utf8_size_t buf_size) -{ - ecma_length_t length = 0; - - lit_utf8_size_t size = 0; - while (size < buf_size) - { - const utf8_char_size char_size = (((buf_size - size) > LIT_CESU8_MAX_BYTES_IN_CODE_UNIT) - ? CESU8_ANY_SIZE - : (utf8_char_size) (buf_size - size)); - - lit_utf8_size_t bytes_generated = generate_cesu8_char (char_size, buf_p); - - TEST_ASSERT (lit_is_valid_cesu8_string (buf_p, bytes_generated)); - - size += bytes_generated; - buf_p += bytes_generated; - length++; - } - - TEST_ASSERT (size == buf_size); - - return length; -} /* generate_cesu8_string */ - -int -main (void) -{ - TEST_INIT (); - - jmem_init (); - ecma_init (); - - lit_utf8_byte_t cesu8_string[max_bytes_in_string]; - ecma_char_t code_units[max_code_units_in_string]; - const lit_utf8_byte_t *saved_positions[max_code_units_in_string]; - - for (int i = 0; i < test_iters; i++) - { - lit_utf8_size_t cesu8_string_size = (i == 0) ? 0 : (lit_utf8_size_t) (rand () % max_bytes_in_string); - ecma_length_t length = generate_cesu8_string (cesu8_string, cesu8_string_size); - - ecma_string_t *char_collection_string_p = ecma_new_ecma_string_from_utf8 (cesu8_string, cesu8_string_size); - ecma_length_t char_collection_len = ecma_string_get_length (char_collection_string_p); - TEST_ASSERT (char_collection_len == length); - ecma_deref_ecma_string (char_collection_string_p); - - TEST_ASSERT (lit_utf8_string_length (cesu8_string, cesu8_string_size) == length); - - const lit_utf8_byte_t *curr_p = cesu8_string; - const lit_utf8_byte_t *end_p = cesu8_string + cesu8_string_size; - - ecma_length_t calculated_length = 0; - ecma_length_t code_units_count = 0; - - while (curr_p < end_p) - { - code_units[code_units_count] = lit_cesu8_peek_next (curr_p); - saved_positions[code_units_count] = curr_p; - code_units_count++; - calculated_length++; - - lit_utf8_incr (&curr_p); - } - - TEST_ASSERT (length == calculated_length); - - if (code_units_count > 0) - { - for (int j = 0; j < test_subiters; j++) - { - ecma_length_t index = (ecma_length_t) rand () % code_units_count; - curr_p = saved_positions[index]; - TEST_ASSERT (lit_cesu8_peek_next (curr_p) == code_units[index]); - } - } - - curr_p = (lit_utf8_byte_t *) end_p; - while (curr_p > cesu8_string) - { - TEST_ASSERT (code_units_count > 0); - calculated_length--; - TEST_ASSERT (code_units[calculated_length] == lit_cesu8_peek_prev (curr_p)); - lit_utf8_decr (&curr_p); - } - - TEST_ASSERT (calculated_length == 0); - - while (curr_p < end_p) - { - ecma_char_t code_unit = lit_cesu8_read_next (&curr_p); - TEST_ASSERT (code_unit == code_units[calculated_length]); - calculated_length++; - } - - TEST_ASSERT (length == calculated_length); - - while (curr_p > cesu8_string) - { - TEST_ASSERT (code_units_count > 0); - calculated_length--; - TEST_ASSERT (code_units[calculated_length] == lit_cesu8_read_prev (&curr_p)); - } - - TEST_ASSERT (calculated_length == 0); - } - - /* Overlong-encoded code point */ - lit_utf8_byte_t invalid_cesu8_string_1[] = {0xC0, 0x82}; - TEST_ASSERT (!lit_is_valid_cesu8_string (invalid_cesu8_string_1, sizeof (invalid_cesu8_string_1))); - - /* Overlong-encoded code point */ - lit_utf8_byte_t invalid_cesu8_string_2[] = {0xE0, 0x80, 0x81}; - TEST_ASSERT (!lit_is_valid_cesu8_string (invalid_cesu8_string_2, sizeof (invalid_cesu8_string_2))); - - /* Pair of surrogates: 0xD901 0xDFF0 which encode Unicode character 0x507F0 */ - lit_utf8_byte_t invalid_cesu8_string_3[] = {0xED, 0xA4, 0x81, 0xED, 0xBF, 0xB0}; - TEST_ASSERT (lit_is_valid_cesu8_string (invalid_cesu8_string_3, sizeof (invalid_cesu8_string_3))); - - /* Isolated high surrogate 0xD901 */ - lit_utf8_byte_t valid_utf8_string_1[] = {0xED, 0xA4, 0x81}; - TEST_ASSERT (lit_is_valid_cesu8_string (valid_utf8_string_1, sizeof (valid_utf8_string_1))); - - lit_utf8_byte_t res_buf[3]; - lit_utf8_size_t res_size; - - res_size = lit_code_unit_to_utf8 (0x73, res_buf); - TEST_ASSERT (res_size == 1); - TEST_ASSERT (res_buf[0] == 0x73); - - res_size = lit_code_unit_to_utf8 (0x41A, res_buf); - TEST_ASSERT (res_size == 2); - TEST_ASSERT (res_buf[0] == 0xD0); - TEST_ASSERT (res_buf[1] == 0x9A); - - res_size = lit_code_unit_to_utf8 (0xD7FF, res_buf); - TEST_ASSERT (res_size == 3); - TEST_ASSERT (res_buf[0] == 0xED); - TEST_ASSERT (res_buf[1] == 0x9F); - TEST_ASSERT (res_buf[2] == 0xBF); - - ecma_finalize (); - jmem_finalize (); - - return 0; -} /* main */ -- Gitee