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 "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "test-common.h"
20 #include <gtest/gtest.h>
21
22 class DataViewTest : public testing::Test{
23 public:
SetUpTestCase()24 static void SetUpTestCase()
25 {
26 GTEST_LOG_(INFO) << "DataViewTest SetUpTestCase";
27 }
28
TearDownTestCase()29 static void TearDownTestCase()
30 {
31 GTEST_LOG_(INFO) << "DataViewTest 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(DataViewTest, Test001, testing::ext::TestSize.Level1)
48 {
49 if (!jerry_is_feature_enabled (JERRY_FEATURE_DATAVIEW))
50 {
51 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "DataView support is disabled!\n");
52 }
53 else{
54 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
55 jerry_port_default_set_current_context (ctx_p);
56 /* DataView builtin requires the TypedArray builtin */
57 TEST_ASSERT (jerry_is_feature_enabled (JERRY_FEATURE_TYPEDARRAY));
58
59 jerry_init (JERRY_INIT_EMPTY);
60
61 /* Test accessors */
62 jerry_value_t arraybuffer = jerry_create_arraybuffer (16);
63 jerry_value_t view1 = jerry_create_dataview (arraybuffer, 0, 16);
64 TEST_ASSERT (!jerry_value_is_error (view1));
65 TEST_ASSERT (jerry_value_is_dataview (view1));
66
67 jerry_length_t byteOffset = 0;
68 jerry_length_t byteLength = 0;;
69 jerry_value_t internal_buffer = jerry_get_dataview_buffer (view1, &byteOffset, &byteLength);
70 TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, internal_buffer, arraybuffer));
71 TEST_ASSERT (byteOffset == 0);
72 TEST_ASSERT (byteLength == 16);
73 jerry_release_value (internal_buffer);
74
75 jerry_value_t view2 = jerry_create_dataview (arraybuffer, 12, 4);
76 TEST_ASSERT (!jerry_value_is_error (view1));
77 TEST_ASSERT (jerry_value_is_dataview (view2));
78 internal_buffer = jerry_get_dataview_buffer (view2, &byteOffset, &byteLength);
79 TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, internal_buffer, arraybuffer));
80 TEST_ASSERT (byteOffset == 12);
81 TEST_ASSERT (byteLength == 4);
82 jerry_release_value (internal_buffer);
83
84 /* Test invalid construction */
85 jerry_value_t empty_object = jerry_create_object ();
86 jerry_value_t view3 = jerry_create_dataview (empty_object, 20, 10);
87 TEST_ASSERT (jerry_value_is_error (view3));
88 jerry_value_t error_obj = jerry_get_value_from_error (view3, true);
89 TEST_ASSERT (jerry_get_error_type (error_obj) == JERRY_ERROR_TYPE);
90 jerry_release_value (error_obj);
91 jerry_release_value (empty_object);
92
93 jerry_value_t view4 = jerry_create_dataview (arraybuffer, 20, 10);
94 TEST_ASSERT (jerry_value_is_error (view3));
95 error_obj = jerry_get_value_from_error (view4, true);
96 TEST_ASSERT (jerry_get_error_type (error_obj) == JERRY_ERROR_RANGE);
97 jerry_release_value (error_obj);
98
99 /* Test getting/setting values */
100 jerry_value_t global_obj = jerry_get_global_object ();
101 jerry_value_t view1_str = jerry_create_string ((const jerry_char_t *) "view1");
102 jerry_value_t view2_str = jerry_create_string ((const jerry_char_t *) "view2");
103 TEST_ASSERT (jerry_set_property (global_obj, view1_str, view1));
104 TEST_ASSERT (jerry_set_property (global_obj, view2_str, view2));
105
106 jerry_release_value (view1_str);
107 jerry_release_value (view2_str);
108 jerry_release_value (global_obj);
109
110 const jerry_char_t set_src[] = "view1.setInt16 (12, 255)";
111 TEST_ASSERT (jerry_value_is_undefined (jerry_eval (set_src, sizeof (set_src) - 1, JERRY_PARSE_NO_OPTS)));
112
113 const jerry_char_t get_src[] = "view2.getInt16 (0)";
114 TEST_ASSERT (jerry_get_number_value (jerry_eval (get_src, sizeof (get_src) - 1, JERRY_PARSE_NO_OPTS)) == 255);
115
116 const jerry_char_t get_src_little_endian[] = "view2.getInt16 (0, true)";
117 TEST_ASSERT (jerry_get_number_value (jerry_eval (get_src_little_endian,
118 sizeof (get_src_little_endian) - 1,
119 JERRY_PARSE_NO_OPTS)) == -256);
120
121 /* Cleanup */
122 jerry_release_value (view2);
123 jerry_release_value (view1);
124 jerry_release_value (arraybuffer);
125
126 jerry_cleanup ();
127 free (ctx_p);
128 }
129 }
130