1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci/** 17425bb815Sopenharmony_ci * Unit test for pool manager. 18425bb815Sopenharmony_ci */ 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ci#include <stdbool.h> 21425bb815Sopenharmony_ci#include <stdint.h> 22425bb815Sopenharmony_ci#include <string.h> 23425bb815Sopenharmony_ciextern "C" 24425bb815Sopenharmony_ci{ 25425bb815Sopenharmony_ci #include "jmem.h" 26425bb815Sopenharmony_ci} 27425bb815Sopenharmony_ci#include "jerryscript-port.h" 28425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 29425bb815Sopenharmony_ci 30425bb815Sopenharmony_ci#define JMEM_ALLOCATOR_INTERNAL 31425bb815Sopenharmony_ci#include "jmem-allocator-internal.h" 32425bb815Sopenharmony_ci 33425bb815Sopenharmony_ci#include "test-common.h" 34425bb815Sopenharmony_ci#include <gtest/gtest.h> 35425bb815Sopenharmony_ci/* Iterations count. */ 36425bb815Sopenharmony_ciconst uint32_t test_iters = 1024; 37425bb815Sopenharmony_ci 38425bb815Sopenharmony_ci/* Subiterations count. */ 39425bb815Sopenharmony_ci#define TEST_MAX_SUB_ITERS 1024 40425bb815Sopenharmony_ci#define TEST_CHUNK_SIZE 8 41425bb815Sopenharmony_ci 42425bb815Sopenharmony_ciuint8_t *ptrs[TEST_MAX_SUB_ITERS]; 43425bb815Sopenharmony_ciuint8_t data[TEST_MAX_SUB_ITERS][TEST_CHUNK_SIZE]; 44425bb815Sopenharmony_ci 45425bb815Sopenharmony_ciclass PoolmanTest : public testing::Test{ 46425bb815Sopenharmony_cipublic: 47425bb815Sopenharmony_ci static void SetUpTestCase() 48425bb815Sopenharmony_ci { 49425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "PoolmanTest SetUpTestCase"; 50425bb815Sopenharmony_ci } 51425bb815Sopenharmony_ci 52425bb815Sopenharmony_ci static void TearDownTestCase() 53425bb815Sopenharmony_ci { 54425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "PoolmanTest TearDownTestCase"; 55425bb815Sopenharmony_ci } 56425bb815Sopenharmony_ci 57425bb815Sopenharmony_ci void SetUp() override {} 58425bb815Sopenharmony_ci void TearDown() override {} 59425bb815Sopenharmony_ci 60425bb815Sopenharmony_ci}; 61425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 62425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 63425bb815Sopenharmony_ci{ 64425bb815Sopenharmony_ci (void)cb_data; 65425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 66425bb815Sopenharmony_ci return malloc(newSize); 67425bb815Sopenharmony_ci} 68425bb815Sopenharmony_ciHWTEST_F(PoolmanTest, Test001, testing::ext::TestSize.Level1) 69425bb815Sopenharmony_ci{ 70425bb815Sopenharmony_ci TEST_INIT (); 71425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 72425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 73425bb815Sopenharmony_ci jmem_init (); 74425bb815Sopenharmony_ci for (uint32_t i = 0; i < test_iters; i++) 75425bb815Sopenharmony_ci { 76425bb815Sopenharmony_ci const size_t subiters = ((size_t) rand () % TEST_MAX_SUB_ITERS) + 1; 77425bb815Sopenharmony_ci /* jmem_heap_print (false); */ 78425bb815Sopenharmony_ci for (size_t j = 0; j < subiters; j++) 79425bb815Sopenharmony_ci { 80425bb815Sopenharmony_ci if (rand () % 256 == 0) 81425bb815Sopenharmony_ci { 82425bb815Sopenharmony_ci jmem_pools_collect_empty (); 83425bb815Sopenharmony_ci } 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci if (ptrs[j] != NULL) 86425bb815Sopenharmony_ci { 87425bb815Sopenharmony_ci TEST_ASSERT (!memcmp (data[j], ptrs[j], TEST_CHUNK_SIZE)); 88425bb815Sopenharmony_ci jmem_pools_free (ptrs[j], TEST_CHUNK_SIZE); 89425bb815Sopenharmony_ci } 90425bb815Sopenharmony_ci } 91425bb815Sopenharmony_ci } 92425bb815Sopenharmony_ci 93425bb815Sopenharmony_ci#ifdef JMEM_STATS 94425bb815Sopenharmony_ci jmem_heap_stats_print (); 95425bb815Sopenharmony_ci#endif /* JMEM_STATS */ 96425bb815Sopenharmony_ci jmem_finalize (); 97425bb815Sopenharmony_ci free (ctx_p); 98425bb815Sopenharmony_ci return; 99425bb815Sopenharmony_ci} 100