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 #include "config.h"
17 #include "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "jerryscript.h"
20 #include "test-common.h"
21 #include <gtest/gtest.h>
22
23 static bool test_context_data1_new_called = false;
24 static bool test_context_data2_new_called = false;
25 static bool test_context_data3_new_called = false;
26 static bool test_context_data4_new_called = false;
27 static bool test_context_data1_free_called = false;
28 static bool test_context_data2_free_called = false;
29 static bool test_context_data4_free_called = false;
30 static bool test_context_data1_finalize_called = false;
31 static bool test_context_data4_finalize_called = false;
32
33 /* Context item 1 */
34 const char *string1 = "item1";
35
36 static void
test_context_data1_new(void * user_data_p)37 test_context_data1_new (void *user_data_p)
38 {
39 test_context_data1_new_called = true;
40 *((const char **) user_data_p) = string1;
41 } /* test_context_data1_new */
42
43 static void
test_context_data1_free(void * user_data_p)44 test_context_data1_free (void *user_data_p)
45 {
46 test_context_data1_free_called = true;
47 TEST_ASSERT ((*(const char **) user_data_p) == string1);
48 TEST_ASSERT (!test_context_data1_finalize_called);
49 } /* test_context_data1_free */
50
51 static void
test_context_data1_finalize(void * user_data_p)52 test_context_data1_finalize (void *user_data_p)
53 {
54 TEST_ASSERT (test_context_data1_free_called);
55 TEST_ASSERT (!test_context_data1_finalize_called);
56 TEST_ASSERT ((*(const char **) user_data_p) == string1);
57 test_context_data1_finalize_called = true;
58 } /* test_context_data1_finalize */
59
60 static const jerry_context_data_manager_t manager1 =
61 {
62 .init_cb = test_context_data1_new,
63 .deinit_cb = test_context_data1_free,
64 .finalize_cb = test_context_data1_finalize,
65 .bytes_needed = sizeof (const char *)
66 };
67
68 /* Context item 2 */
69 const char *string2 = "item2";
70
71 static void
test_context_data2_new(void * user_data_p)72 test_context_data2_new (void *user_data_p)
73 {
74 test_context_data2_new_called = true;
75 *((const char **) user_data_p) = string2;
76 } /* test_context_data2_new */
77
78 static void
test_context_data2_free(void * user_data_p)79 test_context_data2_free (void *user_data_p)
80 {
81 test_context_data2_free_called = true;
82 TEST_ASSERT ((*(const char **) user_data_p) == string2);
83 } /* test_context_data2_free */
84
85 static const jerry_context_data_manager_t manager2 =
86 {
87 .init_cb = test_context_data2_new,
88 .deinit_cb = test_context_data2_free,
89 .bytes_needed = sizeof (const char *)
90 };
91
92 /* Context item 3 */
93
94 static void
test_context_data3_new(void * user_data_p)95 test_context_data3_new (void *user_data_p)
96 {
97 JERRY_UNUSED (user_data_p);
98 test_context_data3_new_called = true;
99 } /* test_context_data3_new */
100
101 static const jerry_context_data_manager_t manager3 =
102 {
103 .init_cb = test_context_data3_new,
104 /* NULL is allowed: */
105 .deinit_cb = NULL,
106 .finalize_cb = NULL,
107 .bytes_needed = 0,
108 };
109
110 /* Context item 4 */
111
112 static void
test_context_data4_new(void * user_data_p)113 test_context_data4_new (void *user_data_p)
114 {
115 test_context_data4_new_called = true;
116 TEST_ASSERT (user_data_p == NULL);
117 } /* test_context_data4_new */
118
119 static void
test_context_data4_free(void * user_data_p)120 test_context_data4_free (void *user_data_p)
121 {
122 test_context_data4_free_called = true;
123 TEST_ASSERT (user_data_p == NULL);
124 TEST_ASSERT (!test_context_data4_finalize_called);
125 } /* test_context_data4_free */
126
127 static void
test_context_data4_finalize(void * user_data_p)128 test_context_data4_finalize (void *user_data_p)
129 {
130 TEST_ASSERT (!test_context_data4_finalize_called);
131 test_context_data4_finalize_called = true;
132 TEST_ASSERT (user_data_p == NULL);
133 } /* test_context_data4_finalize */
134
135 static const jerry_context_data_manager_t manager4 =
136 {
137 .init_cb = test_context_data4_new,
138 .deinit_cb = test_context_data4_free,
139 .finalize_cb = test_context_data4_finalize,
140 .bytes_needed = 0
141 };
142
143 class ContextDataTest : public testing::Test{
144 public:
SetUpTestCase()145 static void SetUpTestCase()
146 {
147 GTEST_LOG_(INFO) << "ContextDataTest SetUpTestCase";
148 }
149
TearDownTestCase()150 static void TearDownTestCase()
151 {
152 GTEST_LOG_(INFO) << "ContextDataTest TearDownTestCase";
153 }
154
SetUp()155 void SetUp() override {}
TearDown()156 void TearDown() override {}
157
158 };
159
160 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)161 static void* context_alloc_fn(size_t size, void* cb_data)
162 {
163 (void)cb_data;
164 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
165 return malloc(newSize);
166 }
167 HWTEST_F(ContextDataTest, Test001, testing::ext::TestSize.Level1)
168 {
169 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
170 jerry_port_default_set_current_context (ctx_p);
171 TEST_INIT ();
172
173 jerry_init (JERRY_INIT_EMPTY);
174
175 TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager1)), "item1"));
176 TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager2)), "item2"));
177 TEST_ASSERT (jerry_get_context_data (&manager3) == NULL);
178 TEST_ASSERT (jerry_get_context_data (&manager4) == NULL);
179
180 TEST_ASSERT (test_context_data1_new_called);
181 TEST_ASSERT (test_context_data2_new_called);
182 TEST_ASSERT (test_context_data3_new_called);
183 TEST_ASSERT (test_context_data4_new_called);
184
185 TEST_ASSERT (!test_context_data1_free_called);
186 TEST_ASSERT (!test_context_data2_free_called);
187 TEST_ASSERT (!test_context_data4_free_called);
188
189 jerry_cleanup ();
190
191
192 TEST_ASSERT (test_context_data1_free_called);
193 TEST_ASSERT (test_context_data2_free_called);
194 TEST_ASSERT (test_context_data4_free_called);
195
196 TEST_ASSERT (test_context_data1_finalize_called);
197 TEST_ASSERT (test_context_data4_finalize_called);
198
199 free (ctx_p);
200 }
201