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.h"
18 #include "test-common.h"
19
20 static bool test_context_data1_new_called = false;
21 static bool test_context_data2_new_called = false;
22 static bool test_context_data3_new_called = false;
23 static bool test_context_data4_new_called = false;
24 static bool test_context_data1_free_called = false;
25 static bool test_context_data2_free_called = false;
26 static bool test_context_data4_free_called = false;
27 static bool test_context_data1_finalize_called = false;
28 static bool test_context_data4_finalize_called = false;
29
30 /* Context item 1 */
31 const char *string1 = "item1";
32
33 static void
test_context_data1_new(void * user_data_p)34 test_context_data1_new (void *user_data_p)
35 {
36 test_context_data1_new_called = true;
37 *((const char **) user_data_p) = string1;
38 } /* test_context_data1_new */
39
40 static void
test_context_data1_free(void * user_data_p)41 test_context_data1_free (void *user_data_p)
42 {
43 test_context_data1_free_called = true;
44 TEST_ASSERT ((*(const char **) user_data_p) == string1);
45 TEST_ASSERT (!test_context_data1_finalize_called);
46 } /* test_context_data1_free */
47
48 static void
test_context_data1_finalize(void * user_data_p)49 test_context_data1_finalize (void *user_data_p)
50 {
51 TEST_ASSERT (test_context_data1_free_called);
52 TEST_ASSERT (!test_context_data1_finalize_called);
53 TEST_ASSERT ((*(const char **) user_data_p) == string1);
54 test_context_data1_finalize_called = true;
55 } /* test_context_data1_finalize */
56
57 static const jerry_context_data_manager_t manager1 =
58 {
59 .init_cb = test_context_data1_new,
60 .deinit_cb = test_context_data1_free,
61 .finalize_cb = test_context_data1_finalize,
62 .bytes_needed = sizeof (const char *)
63 };
64
65 /* Context item 2 */
66 const char *string2 = "item2";
67
68 static void
test_context_data2_new(void * user_data_p)69 test_context_data2_new (void *user_data_p)
70 {
71 test_context_data2_new_called = true;
72 *((const char **) user_data_p) = string2;
73 } /* test_context_data2_new */
74
75 static void
test_context_data2_free(void * user_data_p)76 test_context_data2_free (void *user_data_p)
77 {
78 test_context_data2_free_called = true;
79 TEST_ASSERT ((*(const char **) user_data_p) == string2);
80 } /* test_context_data2_free */
81
82 static const jerry_context_data_manager_t manager2 =
83 {
84 .init_cb = test_context_data2_new,
85 .deinit_cb = test_context_data2_free,
86 .bytes_needed = sizeof (const char *)
87 };
88
89 /* Context item 3 */
90
91 static void
test_context_data3_new(void * user_data_p)92 test_context_data3_new (void *user_data_p)
93 {
94 JERRY_UNUSED (user_data_p);
95 test_context_data3_new_called = true;
96 } /* test_context_data3_new */
97
98 static const jerry_context_data_manager_t manager3 =
99 {
100 .init_cb = test_context_data3_new,
101 /* NULL is allowed: */
102 .deinit_cb = NULL,
103 .finalize_cb = NULL,
104 .bytes_needed = 0,
105 };
106
107 /* Context item 4 */
108
109 static void
test_context_data4_new(void * user_data_p)110 test_context_data4_new (void *user_data_p)
111 {
112 test_context_data4_new_called = true;
113 TEST_ASSERT (user_data_p == NULL);
114 } /* test_context_data4_new */
115
116 static void
test_context_data4_free(void * user_data_p)117 test_context_data4_free (void *user_data_p)
118 {
119 test_context_data4_free_called = true;
120 TEST_ASSERT (user_data_p == NULL);
121 TEST_ASSERT (!test_context_data4_finalize_called);
122 } /* test_context_data4_free */
123
124 static void
test_context_data4_finalize(void * user_data_p)125 test_context_data4_finalize (void *user_data_p)
126 {
127 TEST_ASSERT (!test_context_data4_finalize_called);
128 test_context_data4_finalize_called = true;
129 TEST_ASSERT (user_data_p == NULL);
130 } /* test_context_data4_finalize */
131
132 static const jerry_context_data_manager_t manager4 =
133 {
134 .init_cb = test_context_data4_new,
135 .deinit_cb = test_context_data4_free,
136 .finalize_cb = test_context_data4_finalize,
137 .bytes_needed = 0
138 };
139
140 int
main(void)141 main (void)
142 {
143 TEST_INIT ();
144
145 jerry_init (JERRY_INIT_EMPTY);
146
147 TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager1)), "item1"));
148 TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager2)), "item2"));
149 TEST_ASSERT (jerry_get_context_data (&manager3) == NULL);
150 TEST_ASSERT (jerry_get_context_data (&manager4) == NULL);
151
152 TEST_ASSERT (test_context_data1_new_called);
153 TEST_ASSERT (test_context_data2_new_called);
154 TEST_ASSERT (test_context_data3_new_called);
155 TEST_ASSERT (test_context_data4_new_called);
156
157 TEST_ASSERT (!test_context_data1_free_called);
158 TEST_ASSERT (!test_context_data2_free_called);
159 TEST_ASSERT (!test_context_data4_free_called);
160
161 jerry_cleanup ();
162
163 TEST_ASSERT (test_context_data1_free_called);
164 TEST_ASSERT (test_context_data2_free_called);
165 TEST_ASSERT (test_context_data4_free_called);
166
167 TEST_ASSERT (test_context_data1_finalize_called);
168 TEST_ASSERT (test_context_data4_finalize_called);
169
170 return 0;
171 } /* main */
172