aboutsummaryrefslogtreecommitdiff
path: root/tests/unit-core/test-container-operation.c
blob: a0aca68a67afa18b57f2e504d433f51384a1771a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "jerryscript.h"

#include "test-common.h"

int
main (void)
{
  jerry_init (JERRY_INIT_EMPTY);

  if (!jerry_is_feature_enabled (JERRY_FEATURE_MAP) || !jerry_is_feature_enabled (JERRY_FEATURE_SET)
      || !jerry_is_feature_enabled (JERRY_FEATURE_WEAKMAP) || !jerry_is_feature_enabled (JERRY_FEATURE_WEAKSET))
  {
    jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Containers are disabled!\n");
    jerry_cleanup ();
    return 0;
  }

  // Map container tests
  jerry_value_t map = jerry_create_container (JERRY_CONTAINER_TYPE_MAP, NULL, 0);
  TEST_ASSERT (jerry_get_container_type (map) == JERRY_CONTAINER_TYPE_MAP);

  jerry_value_t key_str = jerry_create_string ((jerry_char_t *) "number");
  jerry_value_t number = jerry_create_number (10);
  jerry_value_t args[2] = { key_str, number };
  jerry_value_t result = jerry_container_operation (JERRY_CONTAINER_OP_SET, map, args, 2);
  TEST_ASSERT (!jerry_value_is_error (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_GET, map, &key_str, 1);
  TEST_ASSERT (jerry_get_number_value (result) == 10);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_HAS, map, &key_str, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, map, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 1);
  jerry_release_value (result);

  key_str = jerry_create_string ((jerry_char_t *) "number2");
  number = jerry_create_number (11);
  jerry_value_t args2[2] = { key_str, number };
  result = jerry_container_operation (JERRY_CONTAINER_OP_SET, map, args2, 2);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, map, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 2);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_DELETE, map, &key_str, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, map, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 1);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_CLEAR, map, NULL, 0);
  TEST_ASSERT (jerry_value_is_undefined (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, map, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 0);
  jerry_release_value (result);

  // Set container tests
  number = jerry_create_number (10);
  jerry_value_t set = jerry_create_container (JERRY_CONTAINER_TYPE_SET, NULL, 0);
  TEST_ASSERT (jerry_get_container_type (set) == JERRY_CONTAINER_TYPE_SET);
  result = jerry_container_operation (JERRY_CONTAINER_OP_ADD, set, &number, 1);
  TEST_ASSERT (!jerry_value_is_error (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_HAS, set, &number, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, set, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 1);
  jerry_release_value (result);

  number = jerry_create_number (11);
  result = jerry_container_operation (JERRY_CONTAINER_OP_ADD, set, &number, 1);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, set, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 2);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_DELETE, set, &number, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, set, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 1);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_CLEAR, set, NULL, 0);
  TEST_ASSERT (jerry_value_is_undefined (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_SIZE, set, NULL, 0);
  TEST_ASSERT (jerry_get_number_value (result) == 0);
  jerry_release_value (result);
  jerry_release_value (set);

  // WeakMap contanier tests
  number = jerry_create_number (10);
  jerry_value_t weak_map = jerry_create_container (JERRY_CONTAINER_TYPE_WEAKMAP, NULL, 0);
  TEST_ASSERT (jerry_get_container_type (weak_map) == JERRY_CONTAINER_TYPE_WEAKMAP);

  jerry_value_t obj = jerry_create_object ();
  number = jerry_create_number (10);
  jerry_value_t args4[2] = { obj, number };
  result = jerry_container_operation (JERRY_CONTAINER_OP_SET, weak_map, args4, 2);
  TEST_ASSERT (!jerry_value_is_error (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_HAS, weak_map, &obj, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_DELETE, weak_map, &obj, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);
  jerry_release_value (weak_map);

  // WeakSet contanier tests,
  jerry_value_t weak_set = jerry_create_container (JERRY_CONTAINER_TYPE_WEAKSET, NULL, 0);
  TEST_ASSERT (jerry_get_container_type (weak_set) == JERRY_CONTAINER_TYPE_WEAKSET);

  result = jerry_container_operation (JERRY_CONTAINER_OP_ADD, weak_set, &obj, 1);
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_HAS, weak_set, &obj, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);

  result = jerry_container_operation (JERRY_CONTAINER_OP_DELETE, weak_set, &obj, 1);
  TEST_ASSERT (jerry_value_is_true (result));
  jerry_release_value (result);
  jerry_release_value (weak_set);

  // container is not a object
  jerry_value_t empty_val = jerry_create_undefined ();
  result = jerry_container_operation (JERRY_CONTAINER_OP_SET, empty_val, args, 2);
  TEST_ASSERT (jerry_value_is_error (result));
  jerry_release_value (result);

  // arguments is a error
  const char *const error_message_p = "Random error.";
  jerry_value_t error_val = jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) error_message_p);
  jerry_value_t args3[2] = { error_val, error_val };
  result = jerry_container_operation (JERRY_CONTAINER_OP_SET, map, args3, 2);
  TEST_ASSERT (jerry_value_is_error (result));
  jerry_release_value (result);
  jerry_release_value (error_val);
  jerry_release_value (map);

  jerry_release_value (key_str);
  jerry_release_value (number);
  jerry_release_value (obj);
  jerry_cleanup ();
  return 0;

} /* main */