jerryscript/tests/unit-core/test-api-strings.c
Dániel Bátyai 9860d66a56
Rework the public API (#4829)
Related to #4186.

Some notable changes:
  - The term 'Error' now strictly refers to native Error objects defined in
    the ECMA standard, which are ordinary objects. All other uses of
    'error' or 'error reference' where the term refers to a thrown value is
    now called 'exception'.

  - Simplified the naming scheme of many String API functions. These functions
    will now also take an 'encoding' argument to specify the desired
    encoding in which to operate.

  - Removed the substring-copy-to-buffer functions. These functions
    behaved awkwardly, as they use character index to specify the
    start/end positions, and were mostly used incorrectly with byte
    offsets instead. The functionality can still be replicated with
    other functions if necessary.

  - String-to-buffer functions will no longer fail if the buffer is not
    sufficiently large, the string will instead be cropped.

  - Fixed the usage of the '_sz' prefix in many API functions. The term
    'sz' means zero-terminated string in hungarian notation, this was
    used incorrectly in many cases.

  - Renamed most of the public API functions to have shorter, more on-point
    names, rather than the often too long descriptive names. Functions are now
    also grouped by the type of value they operate on, where this makes
    sense.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
2021-12-06 10:20:09 +01:00

166 lines
6.5 KiB
C

/* 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"
static bool
strict_equals (jerry_value_t a, /**< the first string to compare */
jerry_value_t b) /**< the second string to compare */
{
const jerry_char_t is_equal_src[] = "var isEqual = function(a, b) { return (a === b); }; isEqual";
jerry_value_t is_equal_fn_val = jerry_eval (is_equal_src, sizeof (is_equal_src) - 1, JERRY_PARSE_NO_OPTS);
TEST_ASSERT (!jerry_value_is_exception (is_equal_fn_val));
jerry_value_t args[2] = { a, b };
jerry_value_t res = jerry_call (is_equal_fn_val, jerry_undefined (), args, 2);
TEST_ASSERT (!jerry_value_is_exception (res));
TEST_ASSERT (jerry_value_is_boolean (res));
bool is_strict_equal = jerry_value_is_true (res);
jerry_value_free (res);
jerry_value_free (is_equal_fn_val);
return is_strict_equal;
} /* strict_equals */
int
main (void)
{
jerry_size_t sz, utf8_sz, cesu8_sz;
jerry_value_t args[2];
TEST_INIT ();
jerry_init (JERRY_INIT_EMPTY);
/* Test corner case for jerry_string_to_char_buffer */
args[0] = jerry_string_sz ("");
sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8);
TEST_ASSERT (sz == 0);
jerry_value_free (args[0]);
/* Test create_jerry_string_from_utf8 with 4-byte long unicode sequences,
* test string: 'str: {DESERET CAPITAL LETTER LONG I}'
*/
char *utf8_bytes_p = "\x73\x74\x72\x3a \xf0\x90\x90\x80";
char *cesu8_bytes_p = "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80";
args[0] = jerry_string ((jerry_char_t *) utf8_bytes_p, (jerry_size_t) strlen (utf8_bytes_p), JERRY_ENCODING_UTF8);
args[1] = jerry_string ((jerry_char_t *) cesu8_bytes_p, (jerry_size_t) strlen (cesu8_bytes_p), JERRY_ENCODING_CESU8);
/* These sizes must be equal */
utf8_sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8);
cesu8_sz = jerry_string_size (args[1], JERRY_ENCODING_CESU8);
JERRY_VLA (jerry_char_t, string_from_utf8, utf8_sz);
JERRY_VLA (jerry_char_t, string_from_cesu8, cesu8_sz);
jerry_string_to_buffer (args[0], JERRY_ENCODING_CESU8, string_from_utf8, utf8_sz);
jerry_string_to_buffer (args[1], JERRY_ENCODING_CESU8, string_from_cesu8, cesu8_sz);
TEST_ASSERT (utf8_sz == cesu8_sz);
TEST_ASSERT (!memcmp (string_from_utf8, string_from_cesu8, utf8_sz));
jerry_value_free (args[0]);
jerry_value_free (args[1]);
/* Test jerry_string_to_buffer, test string: 'str: {DESERET CAPITAL LETTER LONG I}' */
utf8_bytes_p = "\x73\x74\x72\x3a \xf0\x90\x90\x80";
cesu8_bytes_p = "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80";
args[0] = jerry_string ((jerry_char_t *) utf8_bytes_p, (jerry_size_t) strlen (utf8_bytes_p), JERRY_ENCODING_UTF8);
args[1] = jerry_string ((jerry_char_t *) cesu8_bytes_p, (jerry_size_t) strlen (cesu8_bytes_p), JERRY_ENCODING_CESU8);
/* Test that the strings are equal / ensure hashes are equal */
TEST_ASSERT (strict_equals (args[0], args[1]));
/* These sizes must be equal */
utf8_sz = jerry_string_size (args[0], JERRY_ENCODING_UTF8);
cesu8_sz = jerry_string_size (args[1], JERRY_ENCODING_UTF8);
TEST_ASSERT (utf8_sz == cesu8_sz && utf8_sz > 0);
JERRY_VLA (jerry_char_t, string_from_utf8_string, utf8_sz);
JERRY_VLA (jerry_char_t, string_from_cesu8_string, cesu8_sz);
jerry_string_to_buffer (args[0], JERRY_ENCODING_UTF8, string_from_utf8_string, utf8_sz);
jerry_string_to_buffer (args[1], JERRY_ENCODING_UTF8, string_from_cesu8_string, cesu8_sz);
TEST_ASSERT (!memcmp (string_from_utf8_string, string_from_cesu8_string, utf8_sz));
jerry_value_free (args[0]);
jerry_value_free (args[1]);
/* Test string: 'str: {MATHEMATICAL FRAKTUR SMALL F}{MATHEMATICAL FRAKTUR SMALL G}' */
utf8_bytes_p = "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4";
args[0] = jerry_string ((jerry_char_t *) utf8_bytes_p, (jerry_size_t) strlen (utf8_bytes_p), JERRY_ENCODING_UTF8);
cesu8_sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8);
utf8_sz = jerry_string_size (args[0], JERRY_ENCODING_UTF8);
TEST_ASSERT (jerry_string_length (args[0]) == 10);
TEST_ASSERT (cesu8_sz != utf8_sz);
TEST_ASSERT (utf8_sz == 14 && cesu8_sz == 18);
JERRY_VLA (char, test_string, utf8_sz);
TEST_ASSERT (jerry_string_to_buffer (args[0], JERRY_ENCODING_UTF8, (jerry_char_t *) test_string, utf8_sz) == 14);
TEST_ASSERT (!strncmp (test_string, utf8_bytes_p, utf8_sz));
jerry_value_free (args[0]);
/* Test string: 'str: {DESERET CAPITAL LETTER LONG I}' */
cesu8_bytes_p = "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80";
args[0] = jerry_string ((jerry_char_t *) cesu8_bytes_p, (jerry_size_t) strlen (cesu8_bytes_p), JERRY_ENCODING_CESU8);
cesu8_sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8);
utf8_sz = jerry_string_size (args[0], JERRY_ENCODING_UTF8);
TEST_ASSERT (jerry_string_length (args[0]) == 7);
TEST_ASSERT (cesu8_sz != utf8_sz);
TEST_ASSERT (utf8_sz == 9 && cesu8_sz == 11);
jerry_value_free (args[0]);
/* Test string: 'price: 10{EURO SIGN}' */
utf8_bytes_p = "\x70\x72\x69\x63\x65\x3a \x31\x30\xe2\x82\xac";
args[0] = jerry_string ((jerry_char_t *) utf8_bytes_p, (jerry_size_t) strlen (utf8_bytes_p), JERRY_ENCODING_UTF8);
cesu8_sz = jerry_string_size (args[0], JERRY_ENCODING_CESU8);
utf8_sz = jerry_string_size (args[0], JERRY_ENCODING_UTF8);
TEST_ASSERT (jerry_string_length (args[0]) == 10);
TEST_ASSERT (cesu8_sz == utf8_sz);
TEST_ASSERT (utf8_sz == 12);
jerry_value_free (args[0]);
/* Test string: '3' */
{
jerry_value_t test_str = jerry_string_sz ("3");
char result_string[1] = { 'E' };
jerry_size_t copied_utf8 =
jerry_string_to_buffer (test_str, JERRY_ENCODING_UTF8, (jerry_char_t *) result_string, sizeof (result_string));
TEST_ASSERT (copied_utf8 == 1);
TEST_ASSERT (result_string[0] == '3');
result_string[0] = 'E';
jerry_size_t copied =
jerry_string_to_buffer (test_str, JERRY_ENCODING_CESU8, (jerry_char_t *) result_string, sizeof (result_string));
TEST_ASSERT (copied == 1);
TEST_ASSERT (result_string[0] == '3');
jerry_value_free (test_str);
}
jerry_cleanup ();
return 0;
} /* main */