• 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 #include "jerryscript.h"
17 #include "test-common.h"
18 #include "jerryscript-port.h"
19 #include "jerryscript-port-default.h"
20 #include <gtest/gtest.h>
21 
22 class ContainerTest : public testing::Test{
23 public:
SetUpTestCase()24     static void SetUpTestCase()
25     {
26         GTEST_LOG_(INFO) << "ContainerTest SetUpTestCase";
27     }
28 
TearDownTestCase()29     static void TearDownTestCase()
30     {
31         GTEST_LOG_(INFO) << "ContainerTest TearDownTestCase";
32     }
33 
SetUp()34     void SetUp() override {}
TearDown()35     void TearDown() override {}
36 
37 };
38 
39 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)40 static void* context_alloc_fn(size_t size, void* cb_data)
41 {
42     (void)cb_data;
43     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
44     return malloc(newSize);
45 }
46 
47 HWTEST_F(ContainerTest, Test001, testing::ext::TestSize.Level1)
48 {
49   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
50   jerry_port_default_set_current_context (ctx_p);
51   jerry_init (JERRY_INIT_EMPTY);
52 
53   if (!jerry_is_feature_enabled (JERRY_FEATURE_MAP)
54       || !jerry_is_feature_enabled (JERRY_FEATURE_SET)
55       || !jerry_is_feature_enabled (JERRY_FEATURE_WEAKMAP)
56       || !jerry_is_feature_enabled (JERRY_FEATURE_WEAKSET))
57   {
58     jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Containers are disabled!\n");
59     jerry_cleanup ();
60     return;
61   }
62 
63   jerry_value_t instance_check;
64   jerry_value_t global = jerry_get_global_object ();
65   jerry_value_t map_str = jerry_create_string ((jerry_char_t *) "Map");
66   jerry_value_t set_str = jerry_create_string ((jerry_char_t *) "Set");
67   jerry_value_t weakmap_str = jerry_create_string ((jerry_char_t *) "WeakMap");
68   jerry_value_t weakset_str = jerry_create_string ((jerry_char_t *) "WeakSet");
69   jerry_value_t global_map = jerry_get_property (global, map_str);
70   jerry_value_t global_set = jerry_get_property (global, set_str);
71   jerry_value_t global_weakmap = jerry_get_property (global, weakmap_str);
72   jerry_value_t global_weakset = jerry_get_property (global, weakset_str);
73 
74   jerry_release_value (global);
75 
76   jerry_release_value (map_str);
77   jerry_release_value (set_str);
78   jerry_release_value (weakmap_str);
79   jerry_release_value (weakset_str);
80 
81   jerry_value_t empty_map = jerry_create_container (JERRY_CONTAINER_TYPE_MAP, NULL, 0);
82   TEST_ASSERT (jerry_get_container_type (empty_map) == JERRY_CONTAINER_TYPE_MAP);
83   instance_check = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF, empty_map, global_map);
84   TEST_ASSERT (jerry_get_boolean_value (instance_check));
85   jerry_release_value (instance_check);
86   jerry_release_value (global_map);
87   jerry_release_value (empty_map);
88 
89   jerry_value_t empty_set = jerry_create_container (JERRY_CONTAINER_TYPE_SET, NULL, 0);
90   TEST_ASSERT (jerry_get_container_type (empty_set) == JERRY_CONTAINER_TYPE_SET);
91   instance_check = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF, empty_set, global_set);
92   TEST_ASSERT (jerry_get_boolean_value (instance_check));
93   jerry_release_value (instance_check);
94   jerry_release_value (global_set);
95   jerry_release_value (empty_set);
96 
97   jerry_value_t empty_weakmap = jerry_create_container (JERRY_CONTAINER_TYPE_WEAKMAP, NULL, 0);
98   TEST_ASSERT (jerry_get_container_type (empty_weakmap) == JERRY_CONTAINER_TYPE_WEAKMAP);
99   instance_check = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF, empty_weakmap, global_weakmap);
100   TEST_ASSERT (jerry_get_boolean_value (instance_check));
101   jerry_release_value (instance_check);
102   jerry_release_value (global_weakmap);
103   jerry_release_value (empty_weakmap);
104 
105   jerry_value_t empty_weakset = jerry_create_container (JERRY_CONTAINER_TYPE_WEAKSET, NULL, 0);
106   TEST_ASSERT (jerry_get_container_type (empty_weakset) == JERRY_CONTAINER_TYPE_WEAKSET);
107   instance_check = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF, empty_weakset, global_weakset);
108   TEST_ASSERT (jerry_get_boolean_value (instance_check));
109   jerry_release_value (instance_check);
110   jerry_release_value (global_weakset);
111   jerry_release_value (empty_weakset);
112 
113   jerry_cleanup ();
114   free (ctx_p);
115 }
116