• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * Unit test for pool manager.
18  */
19 
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <string.h>
23 extern "C"
24 {
25   #include "jmem.h"
26 }
27 #include "jerryscript-port.h"
28 #include "jerryscript-port-default.h"
29 
30 #define JMEM_ALLOCATOR_INTERNAL
31 #include "jmem-allocator-internal.h"
32 
33 #include "test-common.h"
34 #include <gtest/gtest.h>
35 /* Iterations count. */
36 const uint32_t test_iters = 1024;
37 
38 /* Subiterations count. */
39 #define TEST_MAX_SUB_ITERS  1024
40 #define TEST_CHUNK_SIZE 8
41 
42 uint8_t *ptrs[TEST_MAX_SUB_ITERS];
43 uint8_t data[TEST_MAX_SUB_ITERS][TEST_CHUNK_SIZE];
44 
45 class PoolmanTest : public testing::Test{
46 public:
SetUpTestCase()47     static void SetUpTestCase()
48     {
49         GTEST_LOG_(INFO) << "PoolmanTest SetUpTestCase";
50     }
51 
TearDownTestCase()52     static void TearDownTestCase()
53     {
54         GTEST_LOG_(INFO) << "PoolmanTest TearDownTestCase";
55     }
56 
SetUp()57     void SetUp() override {}
TearDown()58     void TearDown() override {}
59 
60 };
61 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)62 static void* context_alloc_fn(size_t size, void* cb_data)
63 {
64     (void)cb_data;
65     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
66     return malloc(newSize);
67 }
68 HWTEST_F(PoolmanTest, Test001, testing::ext::TestSize.Level1)
69 {
70   TEST_INIT ();
71   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
72   jerry_port_default_set_current_context (ctx_p);
73   jmem_init ();
74   for (uint32_t i = 0; i < test_iters; i++)
75   {
76     const size_t subiters = ((size_t) rand () % TEST_MAX_SUB_ITERS) + 1;
77     /* jmem_heap_print (false); */
78     for (size_t j = 0; j < subiters; j++)
79     {
80       if (rand () % 256 == 0)
81       {
82         jmem_pools_collect_empty ();
83       }
84 
85       if (ptrs[j] != NULL)
86       {
87         TEST_ASSERT (!memcmp (data[j], ptrs[j], TEST_CHUNK_SIZE));
88         jmem_pools_free (ptrs[j], TEST_CHUNK_SIZE);
89       }
90     }
91   }
92 
93 #ifdef JMEM_STATS
94   jmem_heap_stats_print ();
95 #endif /* JMEM_STATS */
96   jmem_finalize ();
97   free (ctx_p);
98   return;
99 }
100