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_ciextern "C"
17425bb815Sopenharmony_ci{
18425bb815Sopenharmony_ci  #include "ecma-init-finalize.h"
19425bb815Sopenharmony_ci  #include "jmem.h"
20425bb815Sopenharmony_ci}
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci#include "jerryscript-port.h"
23425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
24425bb815Sopenharmony_ci#include "jerryscript.h"
25425bb815Sopenharmony_ci#include "test-common.h"
26425bb815Sopenharmony_ci#include <gtest/gtest.h>
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci#define BASIC_SIZE (64)
29425bb815Sopenharmony_ci
30425bb815Sopenharmony_ciclass JmemTest : public testing::Test{
31425bb815Sopenharmony_cipublic:
32425bb815Sopenharmony_ci    static void SetUpTestCase()
33425bb815Sopenharmony_ci    {
34425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "JmemTest SetUpTestCase";
35425bb815Sopenharmony_ci    }
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci    static void TearDownTestCase()
38425bb815Sopenharmony_ci    {
39425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "JmemTest TearDownTestCase";
40425bb815Sopenharmony_ci    }
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_ci    void SetUp() override {}
43425bb815Sopenharmony_ci    void TearDown() override {}
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ci};
46425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
47425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
48425bb815Sopenharmony_ci{
49425bb815Sopenharmony_ci    (void)cb_data;
50425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
51425bb815Sopenharmony_ci    return malloc(newSize);
52425bb815Sopenharmony_ci}
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ciHWTEST_F(JmemTest, Test001, testing::ext::TestSize.Level1)
55425bb815Sopenharmony_ci{
56425bb815Sopenharmony_ci  TEST_INIT ();
57425bb815Sopenharmony_ci  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
58425bb815Sopenharmony_ci  jerry_port_default_set_current_context (ctx_p);
59425bb815Sopenharmony_ci  jmem_init ();
60425bb815Sopenharmony_ci  ecma_init ();
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci  {
63425bb815Sopenharmony_ci    uint8_t *block1_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
64425bb815Sopenharmony_ci    uint8_t *block2_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
65425bb815Sopenharmony_ci    uint8_t *block3_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE);
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci    /* [block1 64] [block2 64] [block3 64] [...] */
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE; i++)
70425bb815Sopenharmony_ci    {
71425bb815Sopenharmony_ci      block2_p[i] = i;
72425bb815Sopenharmony_ci    }
73425bb815Sopenharmony_ci
74425bb815Sopenharmony_ci    /* Realloc by moving */
75425bb815Sopenharmony_ci    block2_p = (uint8_t *)jmem_heap_realloc_block (block2_p, BASIC_SIZE, BASIC_SIZE * 2);
76425bb815Sopenharmony_ci
77425bb815Sopenharmony_ci    /* [block1 64] [free 64] [block3 64] [block2 128] [...] */
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE; i++)
80425bb815Sopenharmony_ci    {
81425bb815Sopenharmony_ci      TEST_ASSERT (block2_p[i] == i);
82425bb815Sopenharmony_ci    }
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci    for (uint8_t i = BASIC_SIZE; i < BASIC_SIZE * 2; i++)
85425bb815Sopenharmony_ci    {
86425bb815Sopenharmony_ci      block2_p[i] = i;
87425bb815Sopenharmony_ci    }
88425bb815Sopenharmony_ci
89425bb815Sopenharmony_ci    uint8_t *block4_p = (uint8_t *) jmem_heap_alloc_block (BASIC_SIZE * 2);
90425bb815Sopenharmony_ci
91425bb815Sopenharmony_ci    /* [block1 64] [free 64] [block3 64] [block2 128] [block4 128] [...] */
92425bb815Sopenharmony_ci
93425bb815Sopenharmony_ci    jmem_heap_free_block (block3_p, BASIC_SIZE);
94425bb815Sopenharmony_ci
95425bb815Sopenharmony_ci    /* [block1 64] [free 128] [block2 128] [block4 128] [...] */
96425bb815Sopenharmony_ci
97425bb815Sopenharmony_ci    /* Realloc by extending front */
98425bb815Sopenharmony_ci    block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 2, BASIC_SIZE * 3);
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci    /* [block1 64] [free 64] [block2 192] [block4 128] [...] */
101425bb815Sopenharmony_ci
102425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE * 2; i++)
103425bb815Sopenharmony_ci    {
104425bb815Sopenharmony_ci      TEST_ASSERT (block2_p[i] == i);
105425bb815Sopenharmony_ci    }
106425bb815Sopenharmony_ci
107425bb815Sopenharmony_ci    /* Shrink */
108425bb815Sopenharmony_ci    block2_p = (uint8_t *) jmem_heap_realloc_block (block2_p, BASIC_SIZE * 3, BASIC_SIZE);
109425bb815Sopenharmony_ci
110425bb815Sopenharmony_ci    /* [block1 64] [free 64] [block2 64] [free 128] [block4 128] [...] */
111425bb815Sopenharmony_ci
112425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE; i++)
113425bb815Sopenharmony_ci    {
114425bb815Sopenharmony_ci      TEST_ASSERT (block2_p[i] == i);
115425bb815Sopenharmony_ci    }
116425bb815Sopenharmony_ci
117425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE; i++)
118425bb815Sopenharmony_ci    {
119425bb815Sopenharmony_ci      block1_p[i] = i;
120425bb815Sopenharmony_ci    }
121425bb815Sopenharmony_ci
122425bb815Sopenharmony_ci    /* Grow in place */
123425bb815Sopenharmony_ci    block1_p = (uint8_t *) jmem_heap_realloc_block (block1_p, BASIC_SIZE, BASIC_SIZE * 2);
124425bb815Sopenharmony_ci
125425bb815Sopenharmony_ci    /* [block1 128] [block2 64] [free 128] [block4 128] [...] */
126425bb815Sopenharmony_ci
127425bb815Sopenharmony_ci    for (uint8_t i = 0; i < BASIC_SIZE; i++)
128425bb815Sopenharmony_ci    {
129425bb815Sopenharmony_ci      TEST_ASSERT (block1_p[i] == i);
130425bb815Sopenharmony_ci    }
131425bb815Sopenharmony_ci
132425bb815Sopenharmony_ci    jmem_heap_free_block (block1_p, BASIC_SIZE * 2);
133425bb815Sopenharmony_ci    jmem_heap_free_block (block2_p, BASIC_SIZE);
134425bb815Sopenharmony_ci    jmem_heap_free_block (block4_p, BASIC_SIZE * 2);
135425bb815Sopenharmony_ci  }
136425bb815Sopenharmony_ci
137425bb815Sopenharmony_ci  ecma_finalize ();
138425bb815Sopenharmony_ci  jmem_finalize ();
139425bb815Sopenharmony_ci  free (ctx_p);
140425bb815Sopenharmony_ci  return;
141425bb815Sopenharmony_ci}
142