1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
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 #include "napi/native_api.h"
16 #include "napi_string_test.h"
17
18 const size_t BUF_SIZE_MAX = 128;
19 const size_t BUF_SIZE_50 = 50;
20 const size_t BUF_SIZE_20 = 20;
21 const size_t BUF_SIZE_10 = 10;
22 const size_t BUF_SIZE_5 = 5;
23 const size_t BUF_SIZE_3 = 3;
24
TestGetValueStringUtf8Test1(JSVM_Env env,JSVM_CallbackInfo info)25 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test1(JSVM_Env env, JSVM_CallbackInfo info)
26 {
27 size_t argc = 1;
28 JSVM_Value args[1] = {nullptr};
29 JSVM_Value thisVar = nullptr;
30 OH_JSVM_GetCbInfo(env, info, &argc, args, &thisVar, nullptr);
31
32 char resultStr[BUF_SIZE_MAX] = {0};
33 size_t size = 0;
34 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], resultStr, 128, &size);
35 if (status != JSVM_OK) {
36 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test1: GetValueStringUtf8 Failed");
37 }
38
39 bool result = true;
40 JSVM_Value value = nullptr;
41 OH_JSVM_GetBoolean(env, result, &value);
42 return value;
43 }
44
TestGetValueStringUtf8Test2(JSVM_Env env,JSVM_CallbackInfo info)45 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test2(JSVM_Env env, JSVM_CallbackInfo info)
46 {
47 size_t argc = 1;
48 JSVM_Value args[1] = {nullptr};
49 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
50 const size_t strBufsize = BUF_SIZE_MAX;
51 char resultStr[strBufsize];
52 size_t size = 0;
53 const char *testStr = "Hello world";
54 JSVM_Value setValueName = nullptr;
55 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
56 if (creatStatus != JSVM_OK) {
57 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test2: CreateStringUtf8 Failed");
58 return nullptr;
59 }
60 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, setValueName, resultStr, strBufsize, &size);
61 if (size != strlen(testStr)) {
62 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test2: GetValueStringUtf8 Failed");
63 }
64 if (status != JSVM_OK) {
65 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test2: GetValueStringUtf8 Failed");
66 }
67
68 bool result = true;
69 JSVM_Value value = nullptr;
70 OH_JSVM_GetBoolean(env, result, &value);
71 return value;
72 }
73
TestGetValueStringUtf8Test3(JSVM_Env env,JSVM_CallbackInfo info)74 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test3(JSVM_Env env, JSVM_CallbackInfo info)
75 {
76 size_t argc = 1;
77 JSVM_Value args[1] = {nullptr};
78 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
79
80 const size_t strBufsize = 5;
81 char resultStr[strBufsize];
82 size_t size = 0;
83 char *testStr = "Hello world";
84 JSVM_Value setValueName = nullptr;
85 JSVM_Status createStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
86 if (createStatus != JSVM_OK) {
87 printf("OH_JSVM_CreateStringUtf8 Failed");
88 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test3: CreateStringUtf8 Failed");
89 return nullptr;
90 }
91 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, setValueName, resultStr, sizeof(resultStr), &size);
92 if (strlen(resultStr) != size) {
93 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test3: GetValueStringUtf8 size Failed");
94 }
95 if (status != JSVM_OK) {
96 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test3: GetValueStringUtf8 Failed");
97 }
98
99 bool result = true;
100 JSVM_Value value = nullptr;
101 OH_JSVM_GetBoolean(env, result, &value);
102 return value;
103 }
104
TestGetValueStringUtf8Test4(JSVM_Env env,JSVM_CallbackInfo info)105 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test4(JSVM_Env env, JSVM_CallbackInfo info)
106 {
107 size_t argc = 1;
108 JSVM_Value args[1] = {nullptr};
109 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
110
111 const size_t strBufsize = BUF_SIZE_20;
112 char resultStr[strBufsize];
113 size_t size = 0;
114 char *testStr = "Hello world";
115 JSVM_Value setValueName = nullptr;
116 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
117 if (creatStatus != JSVM_OK) {
118 printf("OH_JSVM_CreateStringUtf8 Failed");
119 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test4: CreateStringUtf8 Failed");
120 return nullptr;
121 }
122 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, setValueName, resultStr, strBufsize, &size);
123 if (status != JSVM_OK) {
124 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test4: GetValueStringUtf8 status Failed");
125 }
126 if (size != strlen(testStr)) {
127 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test4: GetValueStringUtf8 size Failed");
128 }
129
130 bool result = true;
131 JSVM_Value value = nullptr;
132 OH_JSVM_GetBoolean(env, result, &value);
133 return value;
134 }
135
TestGetValueStringUtf8Test5(JSVM_Env env,JSVM_CallbackInfo info)136 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test5(JSVM_Env env, JSVM_CallbackInfo info)
137 {
138 size_t argc = 1;
139 JSVM_Value args[1] = {nullptr};
140 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
141
142 const size_t strBufsize = BUF_SIZE_50;
143 char resultStr[strBufsize];
144 size_t size = 0;
145 char *testStr = "Helloworld";
146 JSVM_Value setValueName = nullptr;
147 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
148 if (creatStatus != JSVM_OK) {
149 printf("OH_JSVM_CreateStringUtf8 Failed");
150 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test5: CreateStringUtf8 Failed");
151 return nullptr;
152 }
153 size_t strLen = strlen(testStr) - BUF_SIZE_3;
154 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, setValueName, resultStr, strLen, &size);
155 if (size != strlen(resultStr)) {
156 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test5: GetValueStringUtf8 size Failed");
157 }
158 if (status != JSVM_OK) {
159 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test5: GetValueStringUtf8 status Failed");
160 }
161
162 bool result = true;
163 JSVM_Value value = nullptr;
164 OH_JSVM_GetBoolean(env, result, &value);
165 return value;
166 }
167
TestGetValueStringUtf8Test6(JSVM_Env env,JSVM_CallbackInfo info)168 [[maybe_unused]] JSVM_Value TestGetValueStringUtf8Test6(JSVM_Env env, JSVM_CallbackInfo info)
169 {
170 size_t argc = 1;
171 JSVM_Value args[1] = {nullptr};
172 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
173
174 const size_t strBufsize = BUF_SIZE_50;
175 char resultStr[strBufsize] = {0};
176 size_t size = 0;
177 char *testStr = "Helloworld";
178 JSVM_Value setValueName = nullptr;
179 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
180 if (creatStatus != JSVM_OK) {
181 printf("OH_JSVM_CreateStringUtf8 Failed");
182 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test6: CreateStringUtf8 Failed");
183 return nullptr;
184 }
185 size_t strLength = strlen(testStr);
186 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, setValueName, resultStr, strLength, &size);
187 if (status != JSVM_OK) {
188 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test6: GetValueStringUtf8 Failed");
189 return nullptr;
190 }
191 if (size != strlen(resultStr)) {
192 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test6: GetValueStringUtf8 Failed");
193 }
194
195 bool result = true;
196 JSVM_Value value = nullptr;
197 OH_JSVM_GetBoolean(env, result, &value);
198 return value;
199 }
200 // OH_JSVM_CreateStringUtf8 interface
TestCreateStringUtf8Test1(JSVM_Env env,JSVM_CallbackInfo info)201 [[maybe_unused]] JSVM_Value TestCreateStringUtf8Test1(JSVM_Env env, JSVM_CallbackInfo info)
202 {
203 size_t argc = 1;
204 JSVM_Value args[1] = {nullptr};
205 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
206
207 size_t size = BUF_SIZE_20;
208 char strInput[BUF_SIZE_20] = {0};
209 JSVM_Value strRstName = nullptr;
210 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf8(env, nullptr, size, &strRstName);
211 if (rstStatus == JSVM_OK) {
212 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8Test1: CreateStringUtf8 Failed");
213 }
214
215 bool result = true;
216 JSVM_Value value = nullptr;
217 OH_JSVM_GetBoolean(env, result, &value);
218 return value;
219 }
220
TestCreateStringUtf8Test2(JSVM_Env env,JSVM_CallbackInfo info)221 [[maybe_unused]] JSVM_Value TestCreateStringUtf8Test2(JSVM_Env env, JSVM_CallbackInfo info)
222 {
223 size_t argc = 1;
224 JSVM_Value args[1] = {nullptr};
225 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
226
227 size_t size = BUF_SIZE_5;
228 const char *strInput = "Create String";
229 JSVM_Value strRstValue = nullptr;
230 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf8(env, strInput, size, &strRstValue);
231 if (rstStatus != JSVM_OK) {
232 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test2: CreateStringUtf8 Failed");
233 return nullptr;
234 }
235
236 size_t getSize = BUF_SIZE_20;
237 char resultStr[BUF_SIZE_20] = {0};
238 size_t rstSize = 0;
239 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, strRstValue, resultStr, getSize, &rstSize);
240 if (status != JSVM_OK) {
241 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test2: CreateStringUtf8 Failed");
242 return nullptr;
243 }
244 if (rstSize != strlen(resultStr)) {
245 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test2: CreateStringUtf8 Failed");
246 }
247
248 bool result = true;
249 JSVM_Value value = nullptr;
250 OH_JSVM_GetBoolean(env, result, &value);
251 return value;
252 }
253
TestCreateStringUtf8Test3(JSVM_Env env,JSVM_CallbackInfo info)254 [[maybe_unused]] JSVM_Value TestCreateStringUtf8Test3(JSVM_Env env, JSVM_CallbackInfo info)
255 {
256 size_t argc = 1;
257 JSVM_Value args[1] = {nullptr};
258 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
259
260 const char *strInput = "Create String";
261 size_t size = strlen(strInput);
262 JSVM_Value strRstValue = nullptr;
263 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf8(env, strInput, size, &strRstValue);
264 if (rstStatus != JSVM_OK) {
265 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test3: CreateStringUtf8 Failed");
266 return nullptr;
267 }
268
269 size_t getSize = BUF_SIZE_MAX;
270 char resultStr[BUF_SIZE_MAX] = {0};
271 size_t rstSize = 0;
272 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, strRstValue, resultStr, getSize, &rstSize);
273 if (status != JSVM_OK) {
274 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test3: CreateStringUtf8 Failed");
275 return nullptr;
276 }
277 if (rstSize != size) {
278 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test2: CreateStringUtf8 Failed");
279 }
280
281 bool result = true;
282 JSVM_Value value = nullptr;
283 OH_JSVM_GetBoolean(env, result, &value);
284 return value;
285 }
286
TestCreateStringUtf8Test4(JSVM_Env env,JSVM_CallbackInfo info)287 [[maybe_unused]] JSVM_Value TestCreateStringUtf8Test4(JSVM_Env env, JSVM_CallbackInfo info)
288 {
289 size_t argc = 1;
290 JSVM_Value args[1] = {nullptr};
291 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
292
293 const char *strInput = "Create String";
294 size_t size = strlen(strInput) + 3;
295 JSVM_Value strRstValue = nullptr;
296 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf8(env, strInput, size, &strRstValue);
297 if (rstStatus != JSVM_OK) {
298 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test4: CreateStringUtf8 Failed");
299 return nullptr;
300 }
301
302 size_t getSize = BUF_SIZE_MAX;
303 char resultStr[BUF_SIZE_MAX] = {0};
304 size_t rstSize = 0;
305 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, strRstValue, resultStr, getSize, &rstSize);
306 if (status != JSVM_OK) {
307 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test4: CreateStringUtf8 status Failed");
308 return nullptr;
309 }
310 if (rstSize != size) {
311 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test4: CreateStringUtf8 size Failed");
312 }
313 if (strcmp(resultStr, strInput) != 0) {
314 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf8test4: CreateStringUtf8 size Failed");
315 }
316
317 bool result = true;
318 JSVM_Value value = nullptr;
319 OH_JSVM_GetBoolean(env, result, &value);
320 return value;
321 }
322 // OH_JSVM_CreateStringUtf16
TestCreateStringUtf16Test1(JSVM_Env env,JSVM_CallbackInfo info)323 [[maybe_unused]] JSVM_Value TestCreateStringUtf16Test1(JSVM_Env env, JSVM_CallbackInfo info)
324 {
325 size_t argc = 1;
326 JSVM_Value args[1] = {nullptr};
327 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
328
329 size_t size = BUF_SIZE_20;
330 const char16_t strInput[BUF_SIZE_20] = {0};
331 JSVM_Value strRstName = nullptr;
332 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, nullptr, size, &strRstName);
333 if (rstStatus == JSVM_OK) {
334 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16test1: CreateStringUtf16 status Failed");
335 }
336
337 bool result = true;
338 JSVM_Value value = nullptr;
339 OH_JSVM_GetBoolean(env, result, &value);
340 return value;
341 }
342
TestCreateStringUtf16Test2(JSVM_Env env,JSVM_CallbackInfo info)343 [[maybe_unused]] JSVM_Value TestCreateStringUtf16Test2(JSVM_Env env, JSVM_CallbackInfo info)
344 {
345 size_t argc = 1;
346 JSVM_Value args[1] = {nullptr};
347 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
348
349 size_t size = BUF_SIZE_10;
350 const char16_t *strInput = u"Create UTF16 String";
351 JSVM_Value strRstValue = nullptr;
352 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
353 if (rstStatus != JSVM_OK) {
354 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16test2: CreateStringUtf16 Failed");
355 return nullptr;
356 }
357
358 size_t getSize = BUF_SIZE_MAX;
359 char16_t resultStr[BUF_SIZE_MAX] = {0};
360 size_t rstSize = 0;
361 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
362 if (status != JSVM_OK) {
363 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16test2: CreateStringUtf16 status Failed");
364 return nullptr;
365 }
366 size_t strRstLen = 0;
367 while (resultStr[strRstLen] != u'\0') {
368 strRstLen++;
369 }
370 if (rstSize != strRstLen) {
371 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16test2: CreateStringUtf16 size Failed");
372 }
373
374 bool result = true;
375 JSVM_Value value = nullptr;
376 OH_JSVM_GetBoolean(env, result, &value);
377 return value;
378 }
379
TestCreateStringUtf16Test3(JSVM_Env env,JSVM_CallbackInfo info)380 [[maybe_unused]] JSVM_Value TestCreateStringUtf16Test3(JSVM_Env env, JSVM_CallbackInfo info)
381 {
382 size_t argc = 1;
383 JSVM_Value args[1] = {nullptr};
384 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
385
386 const char16_t *strInput = u"Create UT16 String";
387 size_t size = 0;
388 while (strInput[size] != u'\0') {
389 ++size;
390 }
391 JSVM_Value strRstValue = nullptr;
392 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
393 if (rstStatus != JSVM_OK) {
394 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test3: CreateStringUtf16 Failed");
395 return nullptr;
396 }
397
398 size_t getSize = BUF_SIZE_MAX;
399 char16_t resultStr[getSize];
400 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
401 if (memsetRst != EOK) {
402 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test3: memset_s Failed");
403 return nullptr;
404 }
405
406 size_t rstSize = 0;
407 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
408 if (status != JSVM_OK) {
409 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test3: CreateStringUtf16 Failed");
410 return nullptr;
411 }
412 if (rstSize != size) {
413 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test3: CreateStringUtf16 Failed");
414 }
415
416 bool result = true;
417 JSVM_Value value = nullptr;
418 OH_JSVM_GetBoolean(env, result, &value);
419 return value;
420 }
421
TestCreateStringUtf16Test4(JSVM_Env env,JSVM_CallbackInfo info)422 [[maybe_unused]] JSVM_Value TestCreateStringUtf16Test4(JSVM_Env env, JSVM_CallbackInfo info)
423 {
424 size_t argc = 1;
425 JSVM_Value args[1] = {nullptr};
426 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
427
428 const char16_t *strInput = u"Create UTF16 String";
429 size_t size = 0;
430 while (strInput[size] != u'\0') {
431 ++size;
432 }
433 size = size + BUF_SIZE_3;
434 JSVM_Value strRstValue = nullptr;
435 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
436 if (rstStatus != JSVM_OK) {
437 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test4: CreateStringUtf16 Failed");
438 return nullptr;
439 }
440
441 size_t getSize = BUF_SIZE_MAX;
442 char16_t resultStr[BUF_SIZE_MAX];
443 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
444 if (memsetRst != EOK) {
445 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test4: memset_s Failed");
446 return nullptr;
447 }
448 size_t rstSize = 0;
449 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
450 if (status != JSVM_OK) {
451 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test4: CreateStringUtf16 Failed");
452 return nullptr;
453 }
454 size_t i = 0;
455 bool bCmpString = true;
456 while (resultStr[i] != u'\0') {
457 if (resultStr[i] == strInput[i]) {
458 i++;
459 } else {
460 bCmpString = false;
461 }
462 }
463 if (!bCmpString) {
464 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test4: CreateStringUtf16 string Failed");
465 }
466
467 if (rstSize != size) {
468 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringUtf16Test4: CreateStringUtf16 size Failed");
469 }
470
471 bool result = true;
472 JSVM_Value value = nullptr;
473 OH_JSVM_GetBoolean(env, result, &value);
474 return value;
475 }
476 // OH_JSVM_GetValueStringUtf16
TestGetValueStringUtf16Test1(JSVM_Env env,JSVM_CallbackInfo info)477 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test1(JSVM_Env env, JSVM_CallbackInfo info)
478 {
479 size_t argc = 1;
480 JSVM_Value args[1] = {nullptr};
481 JSVM_Value thisVar = nullptr;
482 OH_JSVM_GetCbInfo(env, info, &argc, args, &thisVar, nullptr);
483
484 char16_t resultStr[BUF_SIZE_MAX];
485 size_t size = 0;
486 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], resultStr, sizeof(resultStr), &size);
487 if (status != JSVM_OK) {
488 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16test1: GetValueStringUtf16 Failed");
489 }
490
491 bool result = true;
492 JSVM_Value value = nullptr;
493 OH_JSVM_GetBoolean(env, result, &value);
494 return value;
495 }
496
TestGetValueStringUtf16Test2(JSVM_Env env,JSVM_CallbackInfo info)497 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test2(JSVM_Env env, JSVM_CallbackInfo info)
498 {
499 size_t argc = 1;
500 JSVM_Value args[1] = {nullptr};
501 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
502
503 const char16_t *strInput = u"Create UTF16 String";
504 size_t size = 0;
505 while (strInput[size] != u'\0') {
506 ++size;
507 }
508 JSVM_Value strRstValue = nullptr;
509 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
510 if (rstStatus != JSVM_OK) {
511 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test2: CreateStringUtf16 Failed");
512 return nullptr;
513 }
514
515 size_t getSize = BUF_SIZE_MAX;
516 char16_t resultStr[getSize];
517 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
518 if (memsetRst != EOK) {
519 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test2: memset_s Failed");
520 return nullptr;
521 }
522
523 size_t rstSize = 0;
524 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
525 if (status != JSVM_OK) {
526 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test2: GetValueStringUtf16 status Failed");
527 return nullptr;
528 }
529 if (rstSize != size) {
530 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test2: GetValueStringUtf16 size Failed");
531 }
532
533 bool result = true;
534 JSVM_Value value = nullptr;
535 OH_JSVM_GetBoolean(env, result, &value);
536 return value;
537 }
538
TestGetValueStringUtf16Test3(JSVM_Env env,JSVM_CallbackInfo info)539 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test3(JSVM_Env env, JSVM_CallbackInfo info)
540 {
541 size_t argc = 1;
542 JSVM_Value args[1] = {nullptr};
543 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
544
545 const char16_t *strInput = u"Create UTF16 String";
546 size_t size = 0;
547 while (strInput[size] != u'\0') {
548 ++size;
549 }
550 JSVM_Value strRstValue = nullptr;
551 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
552 if (rstStatus != JSVM_OK) {
553 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test3: CreateStringUtf16 Failed");
554 return nullptr;
555 }
556
557 size_t getSize = size - BUF_SIZE_3;
558 char16_t resultStr[getSize];
559 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
560 if (memsetRst != EOK) {
561 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test3: memset_s Failed");
562 return nullptr;
563 }
564 size_t rstSize = 0;
565 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
566 if (status != JSVM_OK) {
567 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test3: GetValueStringUtf16 status Failed");
568 return nullptr;
569 }
570 if (rstSize > getSize) {
571 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test3: GetValueStringUtf16 size Failed");
572 }
573
574 bool result = true;
575 JSVM_Value value = nullptr;
576 OH_JSVM_GetBoolean(env, result, &value);
577 return value;
578 }
579
TestGetValueStringUtf16Test4(JSVM_Env env,JSVM_CallbackInfo info)580 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test4(JSVM_Env env, JSVM_CallbackInfo info)
581 {
582 size_t argc = 1;
583 JSVM_Value args[1] = {nullptr};
584 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
585
586 const char16_t *strInput = u"Create UTF16 String";
587 size_t size = 0;
588 while (strInput[size] != u'\0') {
589 ++size;
590 }
591 JSVM_Value strRstValue = nullptr;
592 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
593 if (rstStatus != JSVM_OK) {
594 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test4: CreateStringUtf16 Failed");
595 return nullptr;
596 }
597
598 size_t getSize = size + BUF_SIZE_3;
599 char16_t resultStr[getSize];
600 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
601 if (memsetRst != EOK) {
602 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test4: memset_s Failed");
603 return nullptr;
604 }
605 size_t rstSize = 0;
606 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
607 if (status != JSVM_OK) {
608 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test4: GetValueStringUtf16 status Failed");
609 return nullptr;
610 }
611
612 if (rstSize != size) {
613 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test4: GetValueStringUtf16 size Failed");
614 }
615
616 bool result = true;
617 JSVM_Value value = nullptr;
618 OH_JSVM_GetBoolean(env, result, &value);
619 return value;
620 }
621
TestGetValueStringUtf16Test5(JSVM_Env env,JSVM_CallbackInfo info)622 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test5(JSVM_Env env, JSVM_CallbackInfo info)
623 {
624 size_t argc = 1;
625 JSVM_Value args[1] = {nullptr};
626 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
627
628 const char16_t *strInput = u"Create UTF16 String";
629 size_t size = 0;
630 while (strInput[size] != u'\0') {
631 ++size;
632 }
633 JSVM_Value strRstValue = nullptr;
634 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
635 if (rstStatus != JSVM_OK) {
636 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test5: CreateStringUtf16 Failed");
637 return nullptr;
638 }
639
640 size_t getSize = size;
641 char16_t resultStr[getSize];
642 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
643 if (memsetRst != EOK) {
644 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test5: memset_s Failed");
645 return nullptr;
646 }
647 size_t rstSize = 0;
648 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize - BUF_SIZE_3, &rstSize);
649 if (status != JSVM_OK) {
650 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test5: GetValueStringUtf16 status Failed");
651 return nullptr;
652 }
653 if (rstSize >= getSize) {
654 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test5: GetValueStringUtf16 size Failed");
655 }
656
657 bool result = true;
658 JSVM_Value value = nullptr;
659 OH_JSVM_GetBoolean(env, result, &value);
660 return value;
661 }
662
TestGetValueStringUtf16Test6(JSVM_Env env,JSVM_CallbackInfo info)663 [[maybe_unused]] JSVM_Value TestGetValueStringUtf16Test6(JSVM_Env env, JSVM_CallbackInfo info)
664 {
665 size_t argc = 1;
666 JSVM_Value args[1] = {nullptr};
667 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
668
669 const char16_t *strInput = u"Create UTF16 String";
670 size_t size = 0;
671 while (strInput[size] != u'\0') {
672 ++size;
673 }
674 JSVM_Value strRstValue = nullptr;
675 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
676 if (rstStatus != JSVM_OK) {
677 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test6: CreateStringUtf16 Failed");
678 return nullptr;
679 }
680
681 size_t getSize = size;
682 char16_t resultStr[getSize];
683 errno_t memsetRst = memset_s(resultStr, getSize * sizeof(char16_t), 0x00, getSize * sizeof(char16_t));
684 if (memsetRst != EOK) {
685 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test6: memset_s Failed");
686 return nullptr;
687 }
688 size_t rstSize = 0;
689 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, strRstValue, resultStr, getSize, &rstSize);
690 if (status != JSVM_OK) {
691 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test6: GetValueStringUtf16 status Failed");
692 return nullptr;
693 }
694 size_t rstLength = 0;
695 while (resultStr[rstLength] != u'\0') {
696 ++rstLength;
697 }
698 if (rstSize != rstLength) {
699 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf16Test6: GetValueStringUtf16 size Failed");
700 }
701
702 bool result = true;
703 JSVM_Value value = nullptr;
704 OH_JSVM_GetBoolean(env, result, &value);
705 return value;
706 }
707 // OH_JSVM_CreateStringLatin1 interface
TestCreateStringLatin1Test1(JSVM_Env env,JSVM_CallbackInfo info)708 [[maybe_unused]] JSVM_Value TestCreateStringLatin1Test1(JSVM_Env env, JSVM_CallbackInfo info)
709 {
710 size_t argc = 1;
711 JSVM_Value args[1] = {nullptr};
712 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
713
714 size_t size = 20;
715 JSVM_Value strRstString = nullptr;
716 JSVM_Status rstStatus = OH_JSVM_CreateStringLatin1(env, nullptr, size, &strRstString);
717 if (rstStatus == JSVM_OK) {
718 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test1: CreateStringUtf8 Failed");
719 }
720
721 bool result = true;
722 JSVM_Value value = nullptr;
723 OH_JSVM_GetBoolean(env, result, &value);
724 return value;
725 }
726
TestCreateStringLatin1Test2(JSVM_Env env,JSVM_CallbackInfo info)727 [[maybe_unused]] JSVM_Value TestCreateStringLatin1Test2(JSVM_Env env, JSVM_CallbackInfo info)
728 {
729 size_t argc = 1;
730 JSVM_Value args[1] = {nullptr};
731 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
732
733 size_t stLength = BUF_SIZE_5;
734 const char *strInput = "Create Latin-1 String";
735 JSVM_Value strRstValue = nullptr;
736 JSVM_Status rstStatus = OH_JSVM_CreateStringLatin1(env, strInput, stLength, &strRstValue);
737 if (rstStatus != JSVM_OK) {
738 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test2: CreateStringLatin1 Failed");
739 return nullptr;
740 }
741
742 size_t getSize = BUF_SIZE_MAX;
743 char resultStr[BUF_SIZE_MAX] = {0};
744 size_t rstSize = 0;
745 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, strRstValue, resultStr, getSize, &rstSize);
746 if (status != JSVM_OK) {
747 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test2: GetValueStringLatin1 Failed");
748 return nullptr;
749 }
750 if (rstSize != stLength) {
751 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test2: GetValueStringLatin1 Failed");
752 }
753
754 bool result = true;
755 JSVM_Value value = nullptr;
756 OH_JSVM_GetBoolean(env, result, &value);
757 return value;
758 }
759
TestCreateStringLatin1Test3(JSVM_Env env,JSVM_CallbackInfo info)760 [[maybe_unused]] JSVM_Value TestCreateStringLatin1Test3(JSVM_Env env, JSVM_CallbackInfo info)
761 {
762 size_t argc = 1;
763 JSVM_Value args[1] = {nullptr};
764 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
765
766 const char *strInput = "Create Latin-1 String";
767 size_t stLength = strlen(strInput);
768 JSVM_Value strRstValue = nullptr;
769 JSVM_Status rstStatus = OH_JSVM_CreateStringLatin1(env, strInput, stLength, &strRstValue);
770 if (rstStatus != JSVM_OK) {
771 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test3: OH_JSVM_CreateStringLatin1 Failed");
772 return nullptr;
773 }
774
775 size_t getSize = BUF_SIZE_MAX;
776 char resultStr[BUF_SIZE_MAX] = {0};
777 size_t rstSize = 0;
778 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, strRstValue, resultStr, getSize, &rstSize);
779 if (status != JSVM_OK) {
780 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test3: OH_JSVM_GetValueStringLatin1 status Failed");
781 return nullptr;
782 }
783 if (rstSize != stLength) {
784 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test3: OH_JSVM_GetValueStringLatin1 status Failed");
785 }
786
787 bool result = true;
788 JSVM_Value value = nullptr;
789 OH_JSVM_GetBoolean(env, result, &value);
790 return value;
791 }
792
TestCreateStringLatin1Test4(JSVM_Env env,JSVM_CallbackInfo info)793 [[maybe_unused]] JSVM_Value TestCreateStringLatin1Test4(JSVM_Env env, JSVM_CallbackInfo info)
794 {
795 size_t argc = 1;
796 JSVM_Value args[1] = {nullptr};
797 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
798
799 const char *strInput = "Create Latin-1 String";
800 size_t stLength = strlen(strInput) + 3;
801 JSVM_Value strRstValue = nullptr;
802 JSVM_Status rstStatus = OH_JSVM_CreateStringLatin1(env, strInput, stLength, &strRstValue);
803 if (rstStatus != JSVM_OK) {
804 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test4: OH_JSVM_CreateStringLatin1 Failed");
805 return nullptr;
806 }
807
808 size_t getSize = BUF_SIZE_MAX;
809 char resultStr[BUF_SIZE_MAX] = {0};
810 size_t rstSize = 0;
811 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, strRstValue, resultStr, getSize, &rstSize);
812 if (status != JSVM_OK) {
813 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test4: GetValueStringLatin1 status Failed");
814 return nullptr;
815 }
816 if (strcmp(resultStr, strInput) != 0) {
817 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test4: GetValueStringLatin1 string Failed");
818 }
819 if (rstSize != stLength) {
820 OH_JSVM_ThrowError(env, nullptr, "TestCreateStringLatin1test4: GetValueStringLatin1 size Failed");
821 }
822
823 bool result = true;
824 JSVM_Value value = nullptr;
825 OH_JSVM_GetBoolean(env, result, &value);
826 return value;
827 }
828 // OH_JSVM_GetValueStringLatin1 interface
TestGetValueStringLatin1Test1(JSVM_Env env,JSVM_CallbackInfo info)829 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test1(JSVM_Env env, JSVM_CallbackInfo info)
830 {
831 size_t argc = 1;
832 JSVM_Value args[1] = {nullptr};
833 JSVM_Value thisVar = nullptr;
834 OH_JSVM_GetCbInfo(env, info, &argc, args, &thisVar, nullptr);
835
836 char resultStr[BUF_SIZE_MAX] = {0};
837 size_t size = 0;
838 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], resultStr, BUF_SIZE_MAX, &size);
839 if (status != JSVM_OK) {
840 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test1: OH_JSVM_GetValueStringLatin1 Failed");
841 }
842
843 bool result = true;
844 JSVM_Value value = nullptr;
845 OH_JSVM_GetBoolean(env, result, &value);
846 return value;
847 }
848
TestGetValueStringLatin1Test2(JSVM_Env env,JSVM_CallbackInfo info)849 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test2(JSVM_Env env, JSVM_CallbackInfo info)
850 {
851 size_t argc = 1;
852 JSVM_Value args[1] = {nullptr};
853 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
854
855 const size_t strBufsize = BUF_SIZE_MAX;
856 char resultStr[strBufsize] = {0};
857 size_t size = 0;
858 const char *testStr = "create Latin1 string";
859 JSVM_Value setValueName = nullptr;
860 JSVM_Status creatStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
861 if (creatStatus != JSVM_OK) {
862 printf("OH_JSVM_CreateStringLatin1 Failed");
863 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test2: OH_JSVM_CreateStringLatin1 Failed");
864 return nullptr;
865 }
866 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, setValueName, resultStr, strBufsize, &size);
867 if (size != strlen(testStr)) {
868 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test2: OH_JSVM_GetValueStringLatin1 Failed");
869 }
870 if (status != JSVM_OK) {
871 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test2: OH_JSVM_GetValueStringLatin1 Failed");
872 }
873
874 bool result = true;
875 JSVM_Value value = nullptr;
876 OH_JSVM_GetBoolean(env, result, &value);
877 return value;
878 }
879
TestGetValueStringLatin1Test3(JSVM_Env env,JSVM_CallbackInfo info)880 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test3(JSVM_Env env, JSVM_CallbackInfo info)
881 {
882 size_t argc = 1;
883 JSVM_Value args[1] = {nullptr};
884 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
885
886 const char *testStr = "create Latin1 string";
887 JSVM_Value setValueName = nullptr;
888 JSVM_Status createStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
889 if (createStatus != JSVM_OK) {
890 printf("OH_JSVM_CreateStringLatin1 Failed");
891 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test3: OH_JSVM_CreateStringLatin1 Failed");
892 return nullptr;
893 }
894
895 const size_t strBufsize = BUF_SIZE_5;
896 char resultStr[strBufsize] = {0};
897 size_t size = 0;
898 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, setValueName, resultStr, sizeof(resultStr), &size);
899 if (strlen(resultStr) != size) {
900 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test3: OH_JSVM_GetValueStringLatin1 size Failed");
901 }
902 if (status != JSVM_OK) {
903 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test3: OH_JSVM_GetValueStringLatin1 status Failed");
904 }
905
906 bool result = true;
907 JSVM_Value value = nullptr;
908 OH_JSVM_GetBoolean(env, result, &value);
909 return value;
910 }
911
TestGetValueStringLatin1Test4(JSVM_Env env,JSVM_CallbackInfo info)912 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test4(JSVM_Env env, JSVM_CallbackInfo info)
913 {
914 size_t argc = 1;
915 JSVM_Value args[1] = {nullptr};
916 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
917
918 const size_t strBufsize = BUF_SIZE_MAX;
919 char resultStr[strBufsize];
920 errno_t memsetRst = memset_s(resultStr, strBufsize * sizeof(char), 0x00, strBufsize * sizeof(char));
921 if (memsetRst != EOK) {
922 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test4: memset_s Failed");
923 return nullptr;
924 }
925 size_t size = 0;
926 const char *testStr = "create Latin1 string";
927 JSVM_Value setValueName = nullptr;
928 JSVM_Status creatStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
929 if (creatStatus != JSVM_OK) {
930 printf("OH_JSVM_CreateStringLatin1 Failed");
931 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test4: OH_JSVM_CreateStringLatin1 Failed");
932 return nullptr;
933 }
934 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, setValueName, resultStr, strBufsize, &size);
935 if (status != JSVM_OK) {
936 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test4: OH_JSVM_GetValueStringLatin1 status Failed");
937 }
938 if (size < strlen(testStr)) {
939 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test4: OH_JSVM_GetValueStringLatin1 size Failed");
940 }
941
942 bool result = true;
943 JSVM_Value value = nullptr;
944 OH_JSVM_GetBoolean(env, result, &value);
945 return value;
946 }
947
TestGetValueStringLatin1Test5(JSVM_Env env,JSVM_CallbackInfo info)948 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test5(JSVM_Env env, JSVM_CallbackInfo info)
949 {
950 size_t argc = 1;
951 JSVM_Value args[1] = {nullptr};
952 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
953
954 char *testStr = "create Latin1 string";
955 JSVM_Value setValueName = nullptr;
956 JSVM_Status creatStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
957 if (creatStatus != JSVM_OK) {
958 printf("OH_JSVM_CreateStringLatin1 Failed");
959 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test5: OH_JSVM_CreateStringLatin1 Failed");
960 return nullptr;
961 }
962
963 size_t strLen = strlen(testStr) - 3;
964 size_t size = 0;
965 size_t strBufsize = BUF_SIZE_MAX;
966 char resultStr[BUF_SIZE_MAX] = {0};
967 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, setValueName, resultStr, strLen, &size);
968 if (size != strlen(resultStr)) {
969 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test5: OH_JSVM_GetValueStringLatin1 size Failed");
970 }
971 if (status != JSVM_OK) {
972 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1Test5: OH_JSVM_GetValueStringLatin1 status Failed");
973 }
974
975 bool result = true;
976 JSVM_Value value = nullptr;
977 OH_JSVM_GetBoolean(env, result, &value);
978 return value;
979 }
980
TestGetValueStringLatin1Test6(JSVM_Env env,JSVM_CallbackInfo info)981 [[maybe_unused]] JSVM_Value TestGetValueStringLatin1Test6(JSVM_Env env, JSVM_CallbackInfo info)
982 {
983 size_t argc = 1;
984 JSVM_Value args[1] = {nullptr};
985 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
986
987 const char *testStr = "create Latin1 string";
988 JSVM_Value setValueName = nullptr;
989 JSVM_Status creatStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
990 if (creatStatus != JSVM_OK) {
991 printf("OH_JSVM_CreateStringUtf8 Failed");
992 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test6: CreateStringUtf8 Failed");
993 return nullptr;
994 }
995
996 size_t strLength = strlen(testStr) + 1;
997 char resultStr[BUF_SIZE_MAX] = {0};
998 size_t size = 0;
999 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, setValueName, resultStr, strLength, &size);
1000 if (status != JSVM_OK) {
1001 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test6: OH_JSVM_GetValueStringLatin1 statu Failed");
1002 return nullptr;
1003 }
1004 if (strcmp(resultStr, testStr) != 0) {
1005 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test6: OH_JSVM_GetValueStringLatin1 string Failed");
1006 }
1007 if (size != strlen(resultStr)) {
1008 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringLatin1test6: OH_JSVM_GetValueStringLatin1 size Failed");
1009 }
1010
1011 bool result = true;
1012 JSVM_Value value = nullptr;
1013 OH_JSVM_GetBoolean(env, result, &value);
1014 return value;
1015 }
1016
TestIsStringTest1(JSVM_Env env,JSVM_CallbackInfo info)1017 [[maybe_unused]] JSVM_Value TestIsStringTest1(JSVM_Env env, JSVM_CallbackInfo info)
1018 {
1019 const size_t strBufsize = BUF_SIZE_MAX;
1020 char resultStr[strBufsize];
1021 size_t size = 0;
1022 const char *testStr = "create UTF8";
1023 JSVM_Value setValueName = nullptr;
1024 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
1025 if (creatStatus != JSVM_OK) {
1026 printf("OH_JSVM_CreateStringUtf8 Failed");
1027 OH_JSVM_ThrowError(env, nullptr, "TestGetValueStringUtf8test2: CreateStringUtf8 Failed");
1028 return nullptr;
1029 }
1030 bool bIsStr = false;
1031 JSVM_Status status = OH_JSVM_IsString(env, setValueName, &bIsStr);
1032 if (status != JSVM_OK) {
1033 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest1: OH_JSVM_IsString status Failed");
1034 }
1035 if (!bIsStr) {
1036 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest1: OH_JSVM_IsString bool Failed");
1037 }
1038
1039 bool result = true;
1040 JSVM_Value value = nullptr;
1041 OH_JSVM_GetBoolean(env, result, &value);
1042 return value;
1043 }
1044
TestIsStringTest2(JSVM_Env env,JSVM_CallbackInfo info)1045 [[maybe_unused]] JSVM_Value TestIsStringTest2(JSVM_Env env, JSVM_CallbackInfo info)
1046 {
1047 const char16_t *strInput = u"Create UT16 String";
1048 size_t size = 0;
1049 while (strInput[size] != u'\0') {
1050 ++size;
1051 }
1052 JSVM_Value strRstValue = nullptr;
1053 JSVM_Status rstStatus = OH_JSVM_CreateStringUtf16(env, strInput, size, &strRstValue);
1054 if (rstStatus != JSVM_OK) {
1055 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest2: CreateStringUtf16 Failed");
1056 return nullptr;
1057 }
1058
1059 bool bIsStr = false;
1060 JSVM_Status status = OH_JSVM_IsString(env, strRstValue, &bIsStr);
1061 if (status != JSVM_OK) {
1062 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest2: OH_JSVM_IsString status Failed");
1063 return nullptr;
1064 }
1065 if (!bIsStr) {
1066 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest2: OH_JSVM_IsString bool Failed");
1067 }
1068
1069 bool result = true;
1070 JSVM_Value value = nullptr;
1071 OH_JSVM_GetBoolean(env, result, &value);
1072 return value;
1073 }
1074
TestIsStringTest3(JSVM_Env env,JSVM_CallbackInfo info)1075 [[maybe_unused]] JSVM_Value TestIsStringTest3(JSVM_Env env, JSVM_CallbackInfo info)
1076 {
1077 const char *testStr = "create Latin1 string";
1078 JSVM_Value setValueName = nullptr;
1079 JSVM_Status creatStatus = OH_JSVM_CreateStringLatin1(env, testStr, strlen(testStr), &setValueName);
1080 if (creatStatus != JSVM_OK) {
1081 printf("OH_JSVM_CreateStringLatin1 Failed");
1082 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest3: OH_JSVM_CreateStringLatin1 Failed");
1083 return nullptr;
1084 }
1085 bool bIsStr = false;
1086 JSVM_Status status = OH_JSVM_IsString(env, setValueName, &bIsStr);
1087 if (status != JSVM_OK) {
1088 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest3: OH_JSVM_IsString status Failed");
1089 }
1090 if (!bIsStr) {
1091 OH_JSVM_ThrowError(env, nullptr, "TestIsStringtest3: OH_JSVM_IsString bool Failed");
1092 }
1093
1094 bool result = true;
1095 JSVM_Value value = nullptr;
1096 OH_JSVM_GetBoolean(env, result, &value);
1097 return value;
1098 }
1099
TestOtherTest1(JSVM_Env env,JSVM_CallbackInfo info)1100 [[maybe_unused]] JSVM_Value TestOtherTest1(JSVM_Env env, JSVM_CallbackInfo info)
1101 {
1102 const size_t strBufsize = BUF_SIZE_MAX;
1103 char16_t resultStr[strBufsize] = {0};
1104 size_t size = 0;
1105 const char *testStr = "create UTF8";
1106 JSVM_Value setValueName = nullptr;
1107 JSVM_Status creatStatus = OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setValueName);
1108 if (creatStatus != JSVM_OK) {
1109 printf("OH_JSVM_CreateStringUtf8 Failed");
1110 OH_JSVM_ThrowError(env, nullptr, "TestOthertest1: CreateStringUtf8 Failed");
1111 return nullptr;
1112 }
1113 bool bIsStr = false;
1114 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, setValueName, resultStr, strBufsize, &size);
1115 if (status != JSVM_OK) {
1116 OH_JSVM_ThrowError(env, nullptr, "TestOthertest1: OH_JSVM_GetValueStringUtf16 status Failed");
1117 }
1118
1119 bool result = true;
1120 JSVM_Value value = nullptr;
1121 OH_JSVM_GetBoolean(env, result, &value);
1122 return value;
1123 }