Split unit tests into separate directories (#1761)

The unit tests should follow the component structure, so this patch
moves all `jerry-core` unit tests under `tests/unit-core` and the
`jerry-libm` unit tests under `tests/unit-libm`.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2017-04-22 03:57:45 +02:00
committed by yichoi
parent 66ade0d81c
commit 6ca8319aea
20 changed files with 60 additions and 15 deletions
+45
View File
@@ -0,0 +1,45 @@
# 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.
cmake_minimum_required (VERSION 2.8.12)
project (unit-core C)
if (NOT IS_ABSOLUTE ${FEATURE_PROFILE})
set(FEATURE_PROFILE "${CMAKE_SOURCE_DIR}/jerry-core/profiles/${FEATURE_PROFILE}.profile")
endif()
if(NOT ${FEATURE_PROFILE} STREQUAL "${CMAKE_SOURCE_DIR}/jerry-core/profiles/es5.1.profile")
message(FATAL_ERROR "FEATURE_PROFILE='${FEATURE_PROFILE}' isn't supported with UNITTESTS=ON")
endif()
# Unit tests main modules
file(GLOB SOURCE_UNIT_TEST_MAIN_MODULES *.c)
# Unit tests declaration
add_custom_target(unittests-core)
foreach(SOURCE_UNIT_TEST_MAIN ${SOURCE_UNIT_TEST_MAIN_MODULES})
get_filename_component(TARGET_NAME ${SOURCE_UNIT_TEST_MAIN} NAME_WE)
set(TARGET_NAME unit-${TARGET_NAME})
add_executable(${TARGET_NAME} ${SOURCE_UNIT_TEST_MAIN})
set_property(TARGET ${TARGET_NAME}
PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}")
link_directories(${CMAKE_BINARY_DIR})
target_link_libraries(${TARGET_NAME} jerry-core)
add_dependencies(unittests-core ${TARGET_NAME})
endforeach()
File diff suppressed because it is too large Load Diff
+71
View File
@@ -0,0 +1,71 @@
/* 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.
*/
#ifndef TEST_COMMON_H
#define TEST_COMMON_H
#include "jrt.h"
#include <math.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define TEST_ASSERT(x) \
do \
{ \
if (unlikely (!(x))) \
{ \
jerry_port_log (JERRY_LOG_LEVEL_ERROR, \
"TEST: Assertion '%s' failed at %s(%s):%lu.\n", \
#x, \
__FILE__, \
__func__, \
(unsigned long) __LINE__); \
jerry_fatal (ERR_FAILED_INTERNAL_ASSERTION); \
} \
} while (0)
/**
* Test initialization statement that should be included
* at the beginning of main function in every unit test.
*/
#define TEST_INIT() \
do \
{ \
FILE *f_rnd = fopen ("/dev/urandom", "r"); \
\
if (f_rnd == NULL) \
{ \
return 1; \
} \
\
uint32_t seed; \
\
size_t bytes_read = fread (&seed, 1, sizeof (seed), f_rnd); \
\
fclose (f_rnd); \
\
if (bytes_read != sizeof (seed)) \
{ \
return 1; \
} \
\
srand (seed); \
} while (0)
#endif /* TEST_COMMON_H */
+139
View File
@@ -0,0 +1,139 @@
/* 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-builtin-helpers.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "test-common.h"
#define MS_PER_DAY ((ecma_number_t) 86400000)
#define MS_PER_YEAR ((ecma_number_t) 365 * MS_PER_DAY)
#define START_OF_GREGORIAN_CALENDAR ((ecma_number_t) (-1970 * MS_PER_YEAR \
- (1970 / 4) * MS_PER_DAY \
+ (1970 / 100) * MS_PER_DAY \
- (1970 / 400) * MS_PER_DAY \
- MS_PER_DAY))
/**
* Unit test's main function.
*/
int
main (void)
{
/* int ecma_date_day (time)*/
TEST_ASSERT (ecma_date_day (0) == 0);
TEST_ASSERT (ecma_date_day (MS_PER_DAY) == 1);
/* ecma_number_t ecma_date_time_within_day (time) */
TEST_ASSERT (ecma_date_time_within_day (0) == 0);
TEST_ASSERT (ecma_date_time_within_day (42) == 42);
TEST_ASSERT (ecma_date_time_within_day (42.51) == 42.51);
TEST_ASSERT (ecma_date_time_within_day (MS_PER_DAY + 42) == 42);
/* int ecma_date_year_from_time (time) */
TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY) == 1970);
TEST_ASSERT (ecma_date_year_from_time ((MS_PER_DAY) * (ecma_number_t) 365 - 1) == 1970);
TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) 365) == 1971);
TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365 * (2015 - 1970)))
== 2014);
TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365.25 * (2015 - 1970)))
== 2015);
TEST_ASSERT (ecma_date_year_from_time (-MS_PER_YEAR) == 1969);
TEST_ASSERT (ecma_date_year_from_time (-1970 * MS_PER_YEAR) == 1);
TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 1) == -1);
TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 3 * MS_PER_YEAR) == -3);
/* int ecma_date_month_from_time (time) */
TEST_ASSERT (ecma_date_month_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_month_from_time (0) == 0);
TEST_ASSERT (ecma_date_month_from_time (-MS_PER_DAY) == 11);
TEST_ASSERT (ecma_date_month_from_time (31 * MS_PER_DAY) == 1);
/* int ecma_date_date_from_time (time) */
TEST_ASSERT (ecma_date_date_from_time (START_OF_GREGORIAN_CALENDAR) == 1);
TEST_ASSERT (ecma_date_date_from_time (0) == 1);
TEST_ASSERT (ecma_date_date_from_time (-MS_PER_DAY) == 31);
TEST_ASSERT (ecma_date_date_from_time (31 * MS_PER_DAY) == 1);
/* int ecma_date_week_day (ecma_number_t time) */
/* FIXME: Implement */
/* ecma_number_t ecma_date_utc (time) */
/* FIXME: Implement */
/* ecma_number_t ecma_date_hour_from_time (time) */
TEST_ASSERT (ecma_date_hour_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_hour_from_time (0) == 0);
TEST_ASSERT (ecma_date_hour_from_time (-MS_PER_DAY) == 0);
TEST_ASSERT (ecma_date_hour_from_time (-1) == 23);
/* ecma_number_t ecma_date_min_from_time (time) */
TEST_ASSERT (ecma_date_min_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_min_from_time (0) == 0);
TEST_ASSERT (ecma_date_min_from_time (-MS_PER_DAY) == 0);
TEST_ASSERT (ecma_date_min_from_time (-1) == 59);
/* ecma_number_t ecma_date_sec_from_time (time) */
TEST_ASSERT (ecma_date_sec_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_sec_from_time (0) == 0);
TEST_ASSERT (ecma_date_sec_from_time (-MS_PER_DAY) == 0);
TEST_ASSERT (ecma_date_sec_from_time (-1) == 59);
/* ecma_number_t ecma_date_ms_from_time (time) */
TEST_ASSERT (ecma_date_ms_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
TEST_ASSERT (ecma_date_ms_from_time (0) == 0);
TEST_ASSERT (ecma_date_ms_from_time (-MS_PER_DAY) == 0);
TEST_ASSERT (ecma_date_ms_from_time (-1) == 999);
/* ecma_number_t ecma_date_make_time (hour, min, sec, ms) */
/* FIXME: Implement */
/* ecma_number_t ecma_date_make_day (year, month, date) */
TEST_ASSERT (ecma_date_make_day (1970, 0, 1) == 0);
TEST_ASSERT (ecma_date_make_day (1970, -1, 1) == -31);
TEST_ASSERT (ecma_date_make_day (1970, 0, 2.5) == 1);
TEST_ASSERT (ecma_date_make_day (1970, 1, 35) == 65);
TEST_ASSERT (ecma_date_make_day (1970, 13, 35) == 430);
TEST_ASSERT (ecma_date_make_day (2016, 2, 1) == 16861);
TEST_ASSERT (ecma_date_make_day (2016, 8, 31) == 17075);
TEST_ASSERT (ecma_date_make_day (2016, 9, 1) == 17075);
/* ecma_number_t ecma_date_make_date (day, time) */
/* FIXME: Implement */
/* ecma_number_t ecma_date_time_clip (year) */
/* FIXME: Implement */
return 0;
} /* main */
+125
View File
@@ -0,0 +1,125 @@
/* 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 <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "jmem.h"
#define JMEM_ALLOCATOR_INTERNAL
#include "jmem-allocator-internal.h"
#include "test-common.h"
/* Heap size is 32K. */
#define test_heap_size (32 * 1024)
/* Iterations count. */
#define test_iters (4 * 1024)
/* Subiterations count. */
#define test_sub_iters 32
/* Threshold size of block to allocate. */
#define test_threshold_block_size 8192
uint8_t *ptrs[test_sub_iters];
size_t sizes[test_sub_iters];
bool is_one_chunked[test_sub_iters];
static void
test_heap_give_some_memory_back (jmem_free_unused_memory_severity_t severity)
{
int p;
if (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW)
{
p = 8;
}
else
{
TEST_ASSERT (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_HIGH);
p = 1;
}
for (int i = 0; i < test_sub_iters; i++)
{
if (rand () % p == 0)
{
if (ptrs[i] != NULL)
{
for (size_t k = 0; k < sizes[i]; k++)
{
TEST_ASSERT (ptrs[i][k] == 0);
}
jmem_heap_free_block (ptrs[i], sizes[i]);
ptrs[i] = NULL;
}
}
}
} /* test_heap_give_some_memory_back */
int
main (void)
{
TEST_INIT ();
jmem_heap_init ();
jmem_register_free_unused_memory_callback (test_heap_give_some_memory_back);
#ifdef JMEM_STATS
jmem_heap_stats_print ();
#endif /* JMEM_STATS */
for (uint32_t i = 0; i < test_iters; i++)
{
for (uint32_t j = 0; j < test_sub_iters; j++)
{
size_t size = (size_t) rand () % test_threshold_block_size;
ptrs[j] = (uint8_t *) jmem_heap_alloc_block (size);
sizes[j] = size;
TEST_ASSERT (sizes[j] == 0 || ptrs[j] != NULL);
memset (ptrs[j], 0, sizes[j]);
}
/* jmem_heap_print (true); */
for (uint32_t j = 0; j < test_sub_iters; j++)
{
if (ptrs[j] != NULL)
{
for (size_t k = 0; k < sizes[j]; k++)
{
TEST_ASSERT (ptrs[j][k] == 0);
}
jmem_heap_free_block (ptrs[j], sizes[j]);
ptrs[j] = NULL;
}
}
}
#ifdef JMEM_STATS
jmem_heap_stats_print ();
#endif /* JMEM_STATS */
return 0;
} /* main */
+80
View File
@@ -0,0 +1,80 @@
/* 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 "lit-char-helpers.h"
#include "js-parser-internal.h"
#include "test-common.h"
int
main (void)
{
TEST_INIT ();
jmem_init ();
ecma_init ();
const uint8_t _1_byte_long1[] = "\\u007F";
const uint8_t _1_byte_long2[] = "\\u0000";
const uint8_t _1_byte_long3[] = "\\u0065";
const uint8_t _2_byte_long1[] = "\\u008F";
const uint8_t _2_byte_long2[] = "\\u00FF";
const uint8_t _2_byte_long3[] = "\\u07FF";
const uint8_t _3_byte_long1[] = "\\u08FF";
const uint8_t _3_byte_long2[] = "\\u0FFF";
const uint8_t _3_byte_long3[] = "\\uFFFF";
size_t length;
/* Test 1-byte-long unicode sequences. */
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _1_byte_long1 + 2, 4));
TEST_ASSERT (length == 1);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _1_byte_long2 + 2, 4));
TEST_ASSERT (length == 1);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _1_byte_long3 + 2, 4));
TEST_ASSERT (length == 1);
/* Test 2-byte-long unicode sequences. */
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _2_byte_long1 + 2, 4));
TEST_ASSERT (length == 2);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _2_byte_long2 + 2, 4));
TEST_ASSERT (length == 2);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _2_byte_long3 + 2, 4));
TEST_ASSERT (length == 2);
/* Test 3-byte-long unicode sequences. */
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _3_byte_long1 + 2, 4));
TEST_ASSERT (length != 2);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _3_byte_long2 + 2, 4));
TEST_ASSERT (length == 3);
length = lit_char_get_utf8_length (lexer_hex_to_character (0, _3_byte_long3 + 2, 4));
TEST_ASSERT (length == 3);
ecma_finalize ();
jmem_finalize ();
return 0;
} /* main */
+133
View File
@@ -0,0 +1,133 @@
/* 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++)
{
jmem_cpointer_t lit1;
jmem_cpointer_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 (lit1 == lit2);
}
else
{
lit1 = ecma_find_or_create_literal_number (numbers[j]);
lit2 = ecma_find_or_create_literal_number (numbers[j]);
TEST_ASSERT (lit1 == lit2);
}
TEST_ASSERT (lit1);
TEST_ASSERT (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 */
+81
View File
@@ -0,0 +1,81 @@
/* 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 "test-common.h"
#define TEST_MAX_DEPTH 10
#define TEST_ITERATIONS_NUM 256
jmp_buf buffers[TEST_MAX_DEPTH];
static void
test_setjmp_longjmp (volatile int depth)
{
if (depth != TEST_MAX_DEPTH)
{
int a = 1, b = 2, c = 3;
int array[256];
for (int i = 0; i < 256; i++)
{
array[i] = i;
}
(void) a;
(void) b;
(void) c;
(void) array;
int k = setjmp (buffers[depth]);
if (k == 0)
{
test_setjmp_longjmp (depth + 1);
}
else
{
TEST_ASSERT (k == depth + 1);
TEST_ASSERT (a == 1);
TEST_ASSERT (b == 2);
TEST_ASSERT (c == 3);
for (int i = 0; i < 256; i++)
{
TEST_ASSERT (array[i] == i);
}
}
}
else
{
int t = rand () % depth;
TEST_ASSERT (t >= 0 && t < depth);
longjmp (buffers[t], t + 1);
}
} /* test_setjmp_longjmp */
int
main (void)
{
TEST_INIT ();
for (int i = 0; i < TEST_ITERATIONS_NUM; i++)
{
test_setjmp_longjmp (0);
}
return 0;
} /* main */
+108
View File
@@ -0,0 +1,108 @@
/* 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-globals.h"
#include "ecma-helpers.h"
#include "test-common.h"
typedef struct
{
ecma_number_t num;
uint32_t uint32_num;
} uint32_test_case_t;
typedef struct
{
ecma_number_t num;
int32_t int32_num;
} int32_test_case_t;
/**
* Unit test's main function.
*/
int
main (void)
{
TEST_INIT ();
const uint32_test_case_t test_cases_uint32[] =
{
#define TEST_CASE(num, uint32) { num, uint32 }
TEST_CASE (1.0, 1),
TEST_CASE (0.0, 0),
TEST_CASE (ecma_number_negate (0.0), 0),
TEST_CASE (NAN, 0),
TEST_CASE (-NAN, 0),
TEST_CASE (INFINITY, 0),
TEST_CASE (-INFINITY, 0),
TEST_CASE (0.1, 0),
TEST_CASE (-0.1, 0),
TEST_CASE (1.1, 1),
TEST_CASE (-1.1, 4294967295),
TEST_CASE (4294967295, 4294967295),
TEST_CASE (-4294967295, 1),
TEST_CASE (4294967296, 0),
TEST_CASE (-4294967296, 0),
TEST_CASE (4294967297, 1),
TEST_CASE (-4294967297, 4294967295)
#undef TEST_CASE
};
for (uint32_t i = 0;
i < sizeof (test_cases_uint32) / sizeof (test_cases_uint32[0]);
i++)
{
TEST_ASSERT (ecma_number_to_uint32 (test_cases_uint32[i].num) == test_cases_uint32[i].uint32_num);
}
int32_test_case_t test_cases_int32[] =
{
#define TEST_CASE(num, int32) { num, int32 }
TEST_CASE (1.0, 1),
TEST_CASE (0.0, 0),
TEST_CASE (ecma_number_negate (0.0), 0),
TEST_CASE (NAN, 0),
TEST_CASE (-NAN, 0),
TEST_CASE (INFINITY, 0),
TEST_CASE (-INFINITY, 0),
TEST_CASE (0.1, 0),
TEST_CASE (-0.1, 0),
TEST_CASE (1.1, 1),
TEST_CASE (-1.1, -1),
TEST_CASE (4294967295, -1),
TEST_CASE (-4294967295, 1),
TEST_CASE (4294967296, 0),
TEST_CASE (-4294967296, 0),
TEST_CASE (4294967297, 1),
TEST_CASE (-4294967297, -1),
TEST_CASE (2147483648, -2147483648),
TEST_CASE (-2147483648, -2147483648),
TEST_CASE (2147483647, 2147483647),
TEST_CASE (-2147483647, -2147483647),
TEST_CASE (-2147483649, 2147483647),
TEST_CASE (2147483649, -2147483647)
#undef TEST_CASE
};
for (uint32_t i = 0;
i < sizeof (test_cases_int32) / sizeof (test_cases_int32[0]);
i++)
{
TEST_ASSERT (ecma_number_to_int32 (test_cases_int32[i].num) == test_cases_int32[i].int32_num);
}
return 0;
} /* main */
+74
View File
@@ -0,0 +1,74 @@
/* 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-globals.h"
#include "ecma-helpers.h"
#include "test-common.h"
/**
* Unit test's main function.
*/
int
main (void)
{
TEST_INIT ();
const lit_utf8_byte_t *strings[] =
{
(const lit_utf8_byte_t *) "1",
(const lit_utf8_byte_t *) "0.5",
(const lit_utf8_byte_t *) "12345",
(const lit_utf8_byte_t *) "12345.123",
(const lit_utf8_byte_t *) "1e-45",
(const lit_utf8_byte_t *) "-2.5e+38",
(const lit_utf8_byte_t *) "NaN",
(const lit_utf8_byte_t *) "Infinity",
(const lit_utf8_byte_t *) "-Infinity",
(const lit_utf8_byte_t *) "0",
(const lit_utf8_byte_t *) "0",
};
const ecma_number_t nums[] =
{
(ecma_number_t) 1.0,
(ecma_number_t) 0.5,
(ecma_number_t) 12345.0,
(ecma_number_t) 12345.123,
(ecma_number_t) 1.0e-45,
(ecma_number_t) -2.5e+38,
(ecma_number_t) NAN,
(ecma_number_t) INFINITY,
(ecma_number_t) -INFINITY,
(ecma_number_t) +0.0,
(ecma_number_t) -0.0
};
for (uint32_t i = 0;
i < sizeof (nums) / sizeof (nums[0]);
i++)
{
lit_utf8_byte_t str[64];
lit_utf8_size_t str_size = ecma_number_to_utf8_string (nums[i], str, sizeof (str));
if (strncmp ((char *) str, (char *) strings[i], str_size) != 0)
{
return 1;
}
}
return 0;
} /* main */
+92
View File
@@ -0,0 +1,92 @@
/* 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.
*/
/**
* Unit test for pool manager.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "jmem.h"
#define JMEM_ALLOCATOR_INTERNAL
#include "jmem-allocator-internal.h"
#include "test-common.h"
/* Iterations count. */
const uint32_t test_iters = 1024;
/* Subiterations count. */
#define TEST_MAX_SUB_ITERS 1024
#define TEST_CHUNK_SIZE 8
uint8_t *ptrs[TEST_MAX_SUB_ITERS];
uint8_t data[TEST_MAX_SUB_ITERS][TEST_CHUNK_SIZE];
int
main (void)
{
TEST_INIT ();
jmem_init ();
for (uint32_t i = 0; i < test_iters; i++)
{
const size_t subiters = ((size_t) rand () % TEST_MAX_SUB_ITERS) + 1;
for (size_t j = 0; j < subiters; j++)
{
ptrs[j] = (uint8_t *) jmem_pools_alloc (TEST_CHUNK_SIZE);
if (ptrs[j] != NULL)
{
for (size_t k = 0; k < TEST_CHUNK_SIZE; k++)
{
ptrs[j][k] = (uint8_t) (rand () % 256);
}
memcpy (data[j], ptrs[j], TEST_CHUNK_SIZE);
}
}
/* jmem_heap_print (false); */
for (size_t j = 0; j < subiters; j++)
{
if (rand () % 256 == 0)
{
jmem_pools_collect_empty ();
}
if (ptrs[j] != NULL)
{
TEST_ASSERT (!memcmp (data[j], ptrs[j], TEST_CHUNK_SIZE));
jmem_pools_free (ptrs[j], TEST_CHUNK_SIZE);
}
}
}
#ifdef JMEM_STATS
jmem_pools_stats_print ();
#endif /* JMEM_STATS */
jmem_finalize ();
return 0;
} /* main */
+89
View File
@@ -0,0 +1,89 @@
/* 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-globals.h"
#include "ecma-helpers.h"
#include "jerryscript.h"
#include "test-common.h"
/**
* Unit test's main function.
*/
int
main (void)
{
TEST_INIT ();
const jerry_char_t *strings[] =
{
(const jerry_char_t *) "1",
(const jerry_char_t *) "0.5",
(const jerry_char_t *) "12345",
(const jerry_char_t *) "1e-45",
(const jerry_char_t *) "-2.5e+38",
(const jerry_char_t *) "-2.5e38",
(const jerry_char_t *) "- 2.5e+38",
(const jerry_char_t *) "-2 .5e+38",
(const jerry_char_t *) "-2. 5e+38",
(const jerry_char_t *) "-2.5e+ 38",
(const jerry_char_t *) "-2.5 e+38",
(const jerry_char_t *) "-2.5e +38",
(const jerry_char_t *) "NaN",
(const jerry_char_t *) "abc",
(const jerry_char_t *) " Infinity ",
(const jerry_char_t *) "-Infinity",
(const jerry_char_t *) "0",
(const jerry_char_t *) "0",
};
const ecma_number_t nums[] =
{
(ecma_number_t) 1.0,
(ecma_number_t) 0.5,
(ecma_number_t) 12345.0,
(ecma_number_t) 1.0e-45,
(ecma_number_t) -2.5e+38,
(ecma_number_t) -2.5e+38,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) NAN,
(ecma_number_t) INFINITY,
(ecma_number_t) -INFINITY,
(ecma_number_t) +0.0,
(ecma_number_t) -0.0
};
for (uint32_t i = 0;
i < sizeof (nums) / sizeof (nums[0]);
i++)
{
ecma_number_t num = ecma_utf8_string_to_number (strings[i], lit_zt_utf8_string_size (strings[i]));
if (num != nums[i]
&& (!ecma_number_is_nan (num)
|| !ecma_number_is_nan (nums[i])))
{
return 1;
}
}
return 0;
} /* main */
+222
View File
@@ -0,0 +1,222 @@
/* 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 (16 * 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_utf8_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_utf8_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_utf8_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_utf8_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_utf8_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 */
+53
View File
@@ -0,0 +1,53 @@
/* 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 const char *identifying_string = "identifying string";
static bool user_context_new_was_called = false;
static bool user_context_free_was_called = false;
static void *
user_context_new (void)
{
user_context_new_was_called = true;
return (void *) identifying_string;
} /* user_context_new */
static void
user_context_free (void *user_context_p)
{
user_context_free_was_called = true;
TEST_ASSERT (((const char *) user_context_p) == identifying_string);
} /* user_context_free */
int
main (void)
{
TEST_INIT ();
jerry_init_with_user_context (JERRY_INIT_EMPTY, user_context_new, user_context_free);
TEST_ASSERT ((((const char *)(jerry_get_user_context ()))) == identifying_string);
jerry_cleanup ();
TEST_ASSERT (user_context_new_was_called);
TEST_ASSERT (user_context_free_was_called);
return 0;
} /* main */