1 /*
2 * Copyright (c) 2023 Huawei Device 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
16 #include "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <cfloat>
21 #include <clocale>
22 #include <cmath>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include <ctime>
27 #include <cwchar>
28 #include <fcntl.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <securec.h>
32 #include <hilog/log.h>
33
34 #define PARAM_0 0
35 #define PARAM_1 1
36 #define MPARAM_1 (-1)
37 #define PARAM_2 2
38 #define PARAM_3 3
39 #define PARAM_314 3.14
40 #define PARAM_4 4
41 #define PARAM_25 25
42 #define PARAM_31 31
43 #define PARAM_32 32
44 #define PARAM_6 6
45 #define PARAM_7 7
46 #define PARAM_10 10
47 #define PARAM_13 13
48 #define PARAM_16 16
49 #define PARAM_17 17
50 #define PARAM_5 5
51 #define PARAM_80 80
52 #define PARAM_0777 0777
53 #define PARAM_250068492 250068492
54 #define PARAM_2064035584 2064035584
55 #define PARAM_208622688 208622688
56 #define PARAM_7340031 7340031
57 #define PARAM_1856892505 1856892505
58 #define PARAM_6358606123 6358606123
59 #define MPARAM_208340076 (-208340076)
60 #define PARAM_2001 2001
61 #define PARAM_6340800 6340800
62 #define MPARAM_36242240 (-3624224)
63 #define SIZE_100 100
64 #define ARRY_MAX 128
65 #define ZERO 0x0
66 #define PARAM_11 11
67 #define PARAM_20 20
68 #define PARAM_14 14
69 #define PARAM_18 18
70 #define ERRON_0 0
71 #define MINUSONE (-1)
72 #define SUCCESS 1
73 #define FAILD (-1)
74 #define SIZE_256 256
75 #define PARAM_12 12
76
77 #undef LOG_DOMAIN
78 #undef LOG_TAG
79 #define LOG_DOMAIN 0xFEFE
80 #define LOG_TAG "MUSL_STATNDK"
81
82 #define SIZEOF_WCHAR(x) (sizeof(x) / sizeof(wchar_t))
83 static const char *TEMP_FILE = "/data/storage/el2/base/files/fzl.txt";
84
Btowc_One(napi_env env,napi_callback_info info)85 static napi_value Btowc_One(napi_env env, napi_callback_info info)
86 {
87 wint_t ret = btowc('6');
88 NAPI_ASSERT(env, ret == L'6', "btowc Error");
89 ret = btowc('A');
90 NAPI_ASSERT(env, ret == L'A', "btowc Error");
91 ret = btowc('&');
92 NAPI_ASSERT(env, ret == L'&', "btowc Error");
93 napi_value result = nullptr;
94 napi_create_int32(env, SUCCESS, &result);
95 return result;
96 }
97
Btowc_Two(napi_env env,napi_callback_info info)98 static napi_value Btowc_Two(napi_env env, napi_callback_info info)
99 {
100 wint_t ret = btowc(EOF);
101 napi_value result = nullptr;
102 napi_create_int32(env, ret == WEOF, &result);
103 return result;
104 }
105
Mbtowc_One(napi_env env,napi_callback_info info)106 static napi_value Mbtowc_One(napi_env env, napi_callback_info info)
107 {
108 const char *test = "musl";
109 wchar_t wc[ARRY_MAX];
110 size_t ret = mbtowc(wc, test, PARAM_1);
111 napi_value result = nullptr;
112 napi_create_int32(env, ret == SUCCESS && wc[PARAM_0] == L'm', &result);
113 return result;
114 }
115
Mbtowc_Two(napi_env env,napi_callback_info info)116 static napi_value Mbtowc_Two(napi_env env, napi_callback_info info)
117 {
118 wchar_t wc[ARRY_MAX];
119 errno = 0;
120 int ret = mbtowc(wc, nullptr, MINUSONE);
121 napi_value result = nullptr;
122 napi_create_int32(env, ret == SUCCESS && errno == EBADF, &result);
123 return result;
124 }
125
Fputwc_One(napi_env env,napi_callback_info info)126 static napi_value Fputwc_One(napi_env env, napi_callback_info info)
127 {
128 wchar_t mb_one_bytes = L'h';
129 FILE *fp = fopen(TEMP_FILE, "w");
130 NAPI_ASSERT(env, fp != nullptr, "Fputwc_One fopen Error");
131 int ret = PARAM_0;
132 if (fp) {
133 ret = fputwc(mb_one_bytes, fp);
134 fclose(fp);
135 remove(TEMP_FILE);
136 }
137 napi_value result = nullptr;
138 napi_create_int32(env, static_cast<wchar_t>(ret) == mb_one_bytes, &result);
139 return result;
140 }
141
Fputws_One(napi_env env,napi_callback_info info)142 static napi_value Fputws_One(napi_env env, napi_callback_info info)
143 {
144 char wstr[SIZE_100] = {PARAM_0};
145 FILE *fp = fopen(TEMP_FILE, "w+");
146 NAPI_ASSERT(env, fp != nullptr, "Fputws_One fopen Error");
147 int ret = PARAM_0;
148 int rsize = PARAM_0;
149 if (fp) {
150 ret = fputws(L"This is a test", fp);
151 fseek(fp, PARAM_0, SEEK_SET);
152 rsize = fread(wstr, sizeof(char), SIZE_100, fp);
153 fclose(fp);
154 remove(TEMP_FILE);
155 }
156 napi_value result = nullptr;
157 napi_create_int32(env, rsize == ret, &result);
158 return result;
159 }
160
Fwide_One(napi_env env,napi_callback_info info)161 static napi_value Fwide_One(napi_env env, napi_callback_info info)
162 {
163 errno = PARAM_0;
164 FILE *fp = fopen(TEMP_FILE, "w+");
165 NAPI_ASSERT(env, fp != nullptr, "Fwide_One fopen Error");
166 int ret = PARAM_0;
167 if (fp) {
168 ret = fwide(fp, PARAM_5);
169 fclose(fp);
170 remove(TEMP_FILE);
171 }
172 napi_value result = nullptr;
173 napi_create_int32(env, ret > PARAM_0 && errno == ERRON_0, &result);
174 return result;
175 }
176
Fwide_Two(napi_env env,napi_callback_info info)177 static napi_value Fwide_Two(napi_env env, napi_callback_info info)
178 {
179 errno = PARAM_0;
180 FILE *fp = fopen(TEMP_FILE, "w+");
181 NAPI_ASSERT(env, fp != nullptr, "Fwide_Two fopen Error");
182 int ret = PARAM_0;
183 if (fp) {
184 ret = fwide(fp, PARAM_0);
185 fclose(fp);
186 remove(TEMP_FILE);
187 }
188 napi_value result = nullptr;
189 napi_create_int32(env, ret == ERRON_0 && errno == ERRON_0, &result);
190 return result;
191 }
192
Fwprintf_One(napi_env env,napi_callback_info info)193 static napi_value Fwprintf_One(napi_env env, napi_callback_info info)
194 {
195 setlocale(LC_ALL, "en_US.UTF-8");
196 int ret = fwprintf(stdout, L"This is a test\n");
197 napi_value result = nullptr;
198 napi_create_int32(env, ret == wcslen(L"This is a test\n"), &result);
199 return result;
200 }
201
Fwprintf_Two(napi_env env,napi_callback_info info)202 static napi_value Fwprintf_Two(napi_env env, napi_callback_info info)
203 {
204 setlocale(LC_ALL, "en_US.UTF-8");
205 int ret = fwprintf(stderr, L"This is a test\n");
206 napi_value result = nullptr;
207 napi_create_int32(env, ret == wcslen(L"This is a test\n"), &result);
208 return result;
209 }
210
Wprintf_One(napi_env env,napi_callback_info info)211 static napi_value Wprintf_One(napi_env env, napi_callback_info info)
212 {
213 int ret = wprintf(L"%ls", L"This is a test");
214 napi_value result = nullptr;
215 napi_create_int32(env, ret == wcslen(L"This is a test"), &result);
216 return result;
217 }
218
Wprintf_Two(napi_env env,napi_callback_info info)219 static napi_value Wprintf_Two(napi_env env, napi_callback_info info)
220 {
221 int ret = wprintf(L"%lc", L'a');
222 napi_value result = nullptr;
223 napi_create_int32(env, ret == SUCCESS, &result);
224 return result;
225 }
226
Fwscanf_One(napi_env env,napi_callback_info info)227 static napi_value Fwscanf_One(napi_env env, napi_callback_info info)
228 {
229 int fd = open(TEMP_FILE, O_CREAT | O_RDWR, PARAM_0777);
230 int ret = PARAM_0;
231 FILE *fp = fdopen(fd, "w+");
232 if (fp) {
233 wchar_t wstr[PARAM_10] = {PARAM_0};
234 setlocale(LC_ALL, "en_US.UTF-8");
235 fwprintf(fp, L"%ls", L"test");
236 rewind(fp);
237 ret = fwscanf(fp, L"%ls", wstr);
238 fclose(fp);
239 remove(TEMP_FILE);
240 }
241 close(fd);
242 napi_value result = nullptr;
243 napi_create_int32(env, ret == SUCCESS, &result);
244 return result;
245 }
246
Fwscanf_Two(napi_env env,napi_callback_info info)247 static napi_value Fwscanf_Two(napi_env env, napi_callback_info info)
248 {
249 int fd = open(TEMP_FILE, O_CREAT | O_RDWR, PARAM_0777);
250 int ret = PARAM_0;
251 FILE *fp = fdopen(fd, "w+");
252 if (fp) {
253 wchar_t wstr[50] = {PARAM_0};
254 fwprintf(fp, L"%ls", L"Summation");
255 rewind(fp);
256 ret = fwscanf(fp, L"%ls %lc", wstr);
257 fclose(fp);
258 remove(TEMP_FILE);
259 }
260 close(fd);
261 napi_value result = nullptr;
262 napi_create_int32(env, ret == SUCCESS, &result);
263 return result;
264 }
265
Swprintf_One(napi_env env,napi_callback_info info)266 static napi_value Swprintf_One(napi_env env, napi_callback_info info)
267 {
268 wchar_t wbuf[SIZE_100] = {PARAM_0};
269 int ret = swprintf(wbuf, SIZE_100, L"%ls", L"This is test");
270 napi_value result = nullptr;
271 napi_create_int32(env, ret == wcslen(L"This is test"), &result);
272 return result;
273 }
274
Swprintf_Two(napi_env env,napi_callback_info info)275 static napi_value Swprintf_Two(napi_env env, napi_callback_info info)
276 {
277 wchar_t wbuf[SIZE_100] = {PARAM_0};
278 int ret = swprintf(wbuf, SIZE_100, L"%s", "xyz");
279 napi_value result = nullptr;
280 napi_create_int32(env, ret == strlen("xyz"), &result);
281 return result;
282 }
283
Swscanf_One(napi_env env,napi_callback_info info)284 static napi_value Swscanf_One(napi_env env, napi_callback_info info)
285 {
286 wchar_t wstr[] = L"swscanf 123";
287 wchar_t wtmp[PARAM_20];
288 setlocale(LC_ALL, "en_US.UTF-8");
289 int ret = swscanf(wstr, L"%ls", wtmp);
290 napi_value result = nullptr;
291 napi_create_int32(env, ret == SUCCESS, &result);
292 return result;
293 }
294
Swscanf_Two(napi_env env,napi_callback_info info)295 static napi_value Swscanf_Two(napi_env env, napi_callback_info info)
296 {
297 wchar_t buffer[SIZE_100] = L"Theta \u03b8";
298 wchar_t symbol, desc[PARAM_10];
299 setlocale(LC_ALL, "en_US.UTF-8");
300 int ret = swscanf(buffer, L"%ls %lc", desc, &symbol);
301 napi_value result = nullptr;
302 napi_create_int32(env, ret == PARAM_2, &result);
303 return result;
304 }
305
Mbrtowc_One(napi_env env,napi_callback_info info)306 static napi_value Mbrtowc_One(napi_env env, napi_callback_info info)
307 {
308 const char pt[] = "mbrtowc example";
309 wchar_t dest;
310 mbstate_t mbs;
311 mbrlen(nullptr, PARAM_0, &mbs);
312 int length = mbrtowc(&dest, pt, sizeof(pt), &mbs);
313 napi_value result = nullptr;
314 napi_create_int32(env, length == SUCCESS, &result);
315 return result;
316 }
317
Mbsinit_One(napi_env env,napi_callback_info info)318 static napi_value Mbsinit_One(napi_env env, napi_callback_info info)
319 {
320 char buffer[PARAM_80];
321 mbstate_t mbst;
322 memset_s(&mbst, sizeof(mbst), 0, sizeof(mbst));
323 const wchar_t wcs[] = L"mbsinit";
324 const wchar_t *p = nullptr;
325 p = wcs;
326 int ret = mbsinit(&mbst);
327 memset_s(&mbst, sizeof(mbst), 0, sizeof(mbst));
328 memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer));
329 wcsrtombs(buffer, &p, PARAM_80, &mbst);
330 napi_value result = nullptr;
331 napi_create_int32(env, ret == SUCCESS, &result);
332 return result;
333 }
334
Mbstowcs_One(napi_env env,napi_callback_info info)335 static napi_value Mbstowcs_One(napi_env env, napi_callback_info info)
336 {
337 const int TEST_BUFFER_SIZE = ARRY_MAX;
338 const char *src = "mbstowcs_One";
339 wchar_t dst[TEST_BUFFER_SIZE];
340 memset(dst, ZERO, sizeof(wchar_t) * TEST_BUFFER_SIZE);
341 size_t ret = mbstowcs(dst, src, strlen(src));
342 napi_value result = nullptr;
343 napi_create_int32(env, ret == strlen(src), &result);
344 return result;
345 }
346
Mbsrtowcs_One(napi_env env,napi_callback_info info)347 static napi_value Mbsrtowcs_One(napi_env env, napi_callback_info info)
348 {
349 setlocale(LC_ALL, "en_US.utf8");
350 const char *src = "\u0763\u0757";
351 wchar_t dest[PARAM_20];
352 mbstate_t ps = mbstate_t();
353 int max = PARAM_10;
354 int ret = mbsrtowcs(dest, &src, max, &ps);
355 napi_value result = nullptr;
356 napi_create_int32(env, ret == PARAM_2, &result);
357 return result;
358 }
359
Mbsnrtowcs_One(napi_env env,napi_callback_info info)360 static napi_value Mbsnrtowcs_One(napi_env env, napi_callback_info info)
361 {
362 setlocale(LC_ALL, "en_US.utf8");
363 const char *src = "\u0763\u0757";
364 wchar_t dest[PARAM_20];
365 mbstate_t ps = mbstate_t();
366 int max = PARAM_10;
367 int ret = mbsnrtowcs(dest, &src, max, max, &ps);
368 napi_value result = nullptr;
369 napi_create_int32(env, ret == PARAM_2, &result);
370 return result;
371 }
372
OpenWmemstream_One(napi_env env,napi_callback_info info)373 static napi_value OpenWmemstream_One(napi_env env, napi_callback_info info)
374 {
375 size_t len = PARAM_0;
376 wchar_t *ptr = nullptr;
377 FILE *fp = open_wmemstream(&ptr, &len);
378 if (fp) {
379 fputws(L"hello, world!", fp);
380 fclose(fp);
381 }
382 free(ptr);
383 ptr = nullptr;
384 napi_value result = nullptr;
385 napi_create_int32(env, wcslen(L"hello, world!") == len, &result);
386 return result;
387 }
388
Putwc_One(napi_env env,napi_callback_info info)389 static napi_value Putwc_One(napi_env env, napi_callback_info info)
390 {
391 wint_t ret = MINUSONE;
392 FILE *fp = fopen(TEMP_FILE, "w");
393 NAPI_ASSERT(env, fp != nullptr, "Putwc_One fopen Error");
394 wchar_t wc = L'p';
395 ret = putwc(wc, fp);
396 fclose(fp);
397 remove(TEMP_FILE);
398 napi_value result = nullptr;
399 napi_create_int32(env, static_cast<wchar_t>(ret) == L'p', &result);
400 return result;
401 }
402
PutWChar_One(napi_env env,napi_callback_info info)403 static napi_value PutWChar_One(napi_env env, napi_callback_info info)
404 {
405 wint_t ret = MINUSONE;
406 wchar_t wch[] = L"this is putwchar_0100 test case, std output success!";
407 int length = SIZEOF_WCHAR(wch);
408 for (int nLoop = PARAM_0; nLoop < length; nLoop++) {
409 ret = putwchar(wch[nLoop]);
410 NAPI_ASSERT(env, static_cast<wchar_t>(ret) == wch[nLoop], "putwchar Error");
411 }
412 napi_value result = nullptr;
413 napi_create_int32(env, SUCCESS, &result);
414 return result;
415 }
416
Towlower_One(napi_env env,napi_callback_info info)417 static napi_value Towlower_One(napi_env env, napi_callback_info info)
418 {
419 wchar_t wc = L'A';
420 wint_t wt = PARAM_0;
421 wt = towlower(wc);
422 napi_value result = nullptr;
423 napi_create_int32(env, wt == L'a', &result);
424 return result;
425 }
426
Towupper_One(napi_env env,napi_callback_info info)427 static napi_value Towupper_One(napi_env env, napi_callback_info info)
428 {
429 wchar_t wc = L'a';
430 wint_t wt = PARAM_0;
431 wt = towupper(wc);
432 napi_value result = nullptr;
433 napi_create_int32(env, wt == L'A', &result);
434 return result;
435 }
436
Ungetwc_One(napi_env env,napi_callback_info info)437 static napi_value Ungetwc_One(napi_env env, napi_callback_info info)
438 {
439 FILE *fp = fopen(TEMP_FILE, "w+");
440 NAPI_ASSERT(env, fp != nullptr, "ungetwc fopen Error");
441 wint_t wc = L'a';
442 putwc(wc, fp);
443 fseek(fp, PARAM_0, SEEK_SET);
444 wint_t ret = ungetwc(wc, fp);
445 ret = getwc(fp);
446 fclose(fp);
447 remove(TEMP_FILE);
448 napi_value result = nullptr;
449 napi_create_int32(env, static_cast<wchar_t>(ret) == L'a', &result);
450 return result;
451 }
452
Wcpcpy_One(napi_env env,napi_callback_info info)453 static napi_value Wcpcpy_One(napi_env env, napi_callback_info info)
454 {
455 wchar_t src[] = L"Source string";
456 wchar_t dst[PARAM_14] = {PARAM_0};
457 wcpcpy(dst, src);
458 napi_value result = nullptr;
459 napi_create_int32(env, wcscmp(src, dst) == ERRON_0, &result);
460 return result;
461 }
462
Wcpncpy_One(napi_env env,napi_callback_info info)463 static napi_value Wcpncpy_One(napi_env env, napi_callback_info info)
464 {
465 wchar_t src[] = L"Source string";
466 wchar_t dst[PARAM_18] = {PARAM_0};
467 size_t src_len = PARAM_13;
468 wcpncpy(dst, src, src_len + PARAM_1);
469 napi_value result = nullptr;
470 napi_create_int32(env, wcscmp(src, dst) == ERRON_0, &result);
471 return result;
472 }
473
Wcrtomb_One(napi_env env,napi_callback_info info)474 static napi_value Wcrtomb_One(napi_env env, napi_callback_info info)
475 {
476 wchar_t wcs = L'A';
477 char s[PARAM_20] = {PARAM_0};
478 mbstate_t pr;
479 memset(&pr, PARAM_0, sizeof(pr));
480 int ret = MINUSONE;
481 ret = wcrtomb(s, wcs, &pr);
482 NAPI_ASSERT(env, ret == SUCCESS, "wcrtomb Error");
483 napi_value result = nullptr;
484 napi_create_int32(env, strcmp(s, "A") == ERRON_0, &result);
485
486 return result;
487 }
488
Wcscasecmp_One(napi_env env,napi_callback_info info)489 static napi_value Wcscasecmp_One(napi_env env, napi_callback_info info)
490 {
491 int ret = MINUSONE;
492 ret = wcscasecmp(L"hello1", L"HELLO2");
493 NAPI_ASSERT(env, ret < PARAM_0, "wcscasecmp Error");
494 ret = wcscasecmp(L"hello2", L"HELLO1");
495 NAPI_ASSERT(env, ret > PARAM_0, "wcscasecmp Error");
496 ret = wcscasecmp(L"hello", L"HELLO");
497 NAPI_ASSERT(env, ret == ERRON_0, "wcscasecmp Error");
498 napi_value result = nullptr;
499 napi_create_int32(env, ret == ERRON_0, &result);
500 return result;
501 }
502
Wcscasecmpl_One(napi_env env,napi_callback_info info)503 static napi_value Wcscasecmpl_One(napi_env env, napi_callback_info info)
504 {
505 int ret = MINUSONE;
506 locale_t localInfo = nullptr;
507 ret = wcscasecmp_l(L"hello1", L"HELLO2", localInfo);
508 NAPI_ASSERT(env, ret < PARAM_0, "wcscasecmp_l Error");
509 ret = wcscasecmp_l(L"hello2", L"HELLO1", localInfo);
510 NAPI_ASSERT(env, ret > PARAM_0, "wcscasecmp_l Error");
511 ret = wcscasecmp_l(L"hello", L"HELLO", localInfo);
512 NAPI_ASSERT(env, ret == ERRON_0, "wcscasecmp_l Error");
513 napi_value result = nullptr;
514 napi_create_int32(env, ret == ERRON_0, &result);
515 return result;
516 }
517
Wcscat_One(napi_env env,napi_callback_info info)518 static napi_value Wcscat_One(napi_env env, napi_callback_info info)
519 {
520 wchar_t wcs[PARAM_80];
521 wcscpy(wcs, L"these ");
522 wcscat(wcs, L"wide strings ");
523 wcscat(wcs, L"are ");
524 wcscat(wcs, L"concatenated.");
525 napi_value result = nullptr;
526 napi_create_int32(env, wcscmp(wcs, L"these wide strings are concatenated.") == ERRON_0, &result);
527 return result;
528 }
529
Wcschr_One(napi_env env,napi_callback_info info)530 static napi_value Wcschr_One(napi_env env, napi_callback_info info)
531 {
532 wchar_t wcs[] = L"This is a sample wide string";
533 wchar_t *pwc = nullptr;
534 pwc = wcschr(wcs, L's');
535 napi_value result = nullptr;
536 napi_create_int32(env, pwc - wcs + PARAM_1 == PARAM_4, &result);
537 return result;
538 }
539
Wcscmp_One(napi_env env,napi_callback_info info)540 static napi_value Wcscmp_One(napi_env env, napi_callback_info info)
541 {
542 int ret = MINUSONE;
543 ret = wcscmp(L"ABCD", L"ABCE");
544 NAPI_ASSERT(env, ret < PARAM_0, "wcscmp Error");
545 ret = wcscmp(L"ABCE", L"ABCD");
546 NAPI_ASSERT(env, ret > PARAM_0, "wcscmp Error");
547 ret = wcscmp(L"test wcscmp", L"test wcscmp");
548 NAPI_ASSERT(env, ret == ERRON_0, "wcscmp Error");
549 napi_value result = nullptr;
550 napi_create_int32(env, ret == ERRON_0, &result);
551 return result;
552 }
553
Wcscoll_One(napi_env env,napi_callback_info info)554 static napi_value Wcscoll_One(napi_env env, napi_callback_info info)
555 {
556 setlocale(LC_COLLATE, "en_US.utf8");
557 int ret = wcscoll(L"år", L"ängel");
558 napi_value result = nullptr;
559 napi_create_int32(env, ret == SUCCESS, &result);
560 return result;
561 }
562
Wcscolll_One(napi_env env,napi_callback_info info)563 static napi_value Wcscolll_One(napi_env env, napi_callback_info info)
564 {
565 int ret = MINUSONE;
566 locale_t localInfo = nullptr;
567 ret = wcscoll_l(L"abcdefg", L"abcdef", localInfo);
568 NAPI_ASSERT(env, ret > PARAM_0, "wcscoll_l Error");
569 ret = wcscoll_l(L"abcde", L"abcdef", localInfo);
570 NAPI_ASSERT(env, ret < PARAM_0, "wcscoll_l Error");
571 ret = wcscoll_l(L"abcdef", L"abcdef", localInfo);
572 napi_value result = nullptr;
573 napi_create_int32(env, ret == ERRON_0, &result);
574 return result;
575 }
576
Wcscpy_One(napi_env env,napi_callback_info info)577 static napi_value Wcscpy_One(napi_env env, napi_callback_info info)
578 {
579 wchar_t wcs[] = L"wcscpy test";
580 wchar_t des[40];
581 wcscpy(des, wcs);
582 napi_value result = nullptr;
583 napi_create_int32(env, wcscmp(wcs, des) == ERRON_0, &result);
584 return result;
585 }
586
Wcscspn_One(napi_env env,napi_callback_info info)587 static napi_value Wcscspn_One(napi_env env, napi_callback_info info)
588 {
589 wchar_t wcs[] = L"fcba73";
590 wchar_t keys[] = L"1234567890";
591 int ret = MINUSONE;
592 ret = wcscspn(wcs, keys);
593 napi_value result = nullptr;
594 napi_create_int32(env, ret == PARAM_4, &result);
595 return result;
596 }
597
Wcsdup_One(napi_env env,napi_callback_info info)598 static napi_value Wcsdup_One(napi_env env, napi_callback_info info)
599 {
600 wchar_t src[] = L"wcsdup";
601 wchar_t *ret = wcsdup(src);
602 NAPI_ASSERT(env, ret != nullptr, "wcsdup Error");
603 napi_value result = nullptr;
604 napi_create_int32(env, wcscmp(src, ret) == ERRON_0, &result);
605 return result;
606 }
607
Wcsftime_One(napi_env env,napi_callback_info info)608 static napi_value Wcsftime_One(napi_env env, napi_callback_info info)
609 {
610 int ret = MINUSONE;
611 time_t rawtime;
612 struct tm *timeinfo;
613 wchar_t buffer[PARAM_80];
614 time(&rawtime);
615 timeinfo = localtime(&rawtime);
616 ret = wcsftime(buffer, PARAM_80, L"Now it's %I:%M%p.", timeinfo);
617 napi_value result = nullptr;
618 napi_create_int32(env, ret == PARAM_17, &result);
619 return result;
620 }
621
Wcsftimel_One(napi_env env,napi_callback_info info)622 static napi_value Wcsftimel_One(napi_env env, napi_callback_info info)
623 {
624 int ret = MINUSONE;
625 time_t rtime;
626 struct tm *timeinfo;
627 wchar_t buffer[PARAM_80];
628 time(&rtime);
629 timeinfo = localtime(&rtime);
630 locale_t newlocale_ = newlocale(LC_ALL_MASK, "en_US", nullptr);
631 ret = wcsftime_l(buffer, PARAM_80, L"%I:%M%p", timeinfo, newlocale_);
632 if (newlocale_) {
633 freelocale(newlocale_);
634 newlocale_ = nullptr;
635 }
636 napi_value result = nullptr;
637 napi_create_int32(env, ret == PARAM_7, &result);
638 return result;
639 }
640
Wcslen_One(napi_env env,napi_callback_info info)641 static napi_value Wcslen_One(napi_env env, napi_callback_info info)
642 {
643 wchar_t str[] = L"wcslen";
644 size_t ret = PARAM_6;
645 napi_value result = nullptr;
646 napi_create_int32(env, ret == wcslen(str), &result);
647 return result;
648 }
649
Wcsncasecmp_One(napi_env env,napi_callback_info info)650 static napi_value Wcsncasecmp_One(napi_env env, napi_callback_info info)
651 {
652 int ret = MINUSONE;
653 ret = wcsncasecmp(L"hello1", L"HELLO2", PARAM_6);
654 NAPI_ASSERT(env, ret < PARAM_0, "wcsncasecmp Error");
655 ret = wcsncasecmp(L"hello2", L"HELLO1", PARAM_6);
656 NAPI_ASSERT(env, ret > PARAM_0, "wcsncasecmp Error");
657 ret = wcsncasecmp(L"hello1", L"HELLO2", PARAM_5);
658 napi_value result = nullptr;
659 napi_create_int32(env, ret == ERRON_0, &result);
660 return result;
661 }
662
Wcsncasecmpl_One(napi_env env,napi_callback_info info)663 static napi_value Wcsncasecmpl_One(napi_env env, napi_callback_info info)
664 {
665 int ret = MINUSONE;
666 ret = wcsncasecmp_l(L"hello1", L"HELLO2", PARAM_6, nullptr);
667 NAPI_ASSERT(env, ret < PARAM_0, "wcsncasecmp_l Error");
668 ret = wcsncasecmp_l(L"hello2", L"HELLO1", PARAM_6, nullptr);
669 NAPI_ASSERT(env, ret > PARAM_0, "wcsncasecmp_l Error");
670 ret = wcsncasecmp_l(L"hello1", L"HELLO2", PARAM_5, nullptr);
671 napi_value result = nullptr;
672 napi_create_int32(env, ret == ERRON_0, &result);
673 return result;
674 }
675
Wcsncat_One(napi_env env,napi_callback_info info)676 static napi_value Wcsncat_One(napi_env env, napi_callback_info info)
677 {
678 wchar_t dst[PARAM_32] = {PARAM_0};
679 wchar_t *ret = wcsncat(dst, L"hello, world!", PARAM_5);
680 napi_value result = nullptr;
681 napi_create_int32(env, wcscmp(dst, ret) == ERRON_0, &result);
682 return result;
683 }
684
Wcsncmp_One(napi_env env,napi_callback_info info)685 static napi_value Wcsncmp_One(napi_env env, napi_callback_info info)
686 {
687 int ret = MINUSONE;
688 ret = wcsncmp(L"aaaa", L"aaab", PARAM_4);
689 NAPI_ASSERT(env, ret < PARAM_0, "wcsncmp Error");
690 ret = wcsncmp(L"aaab", L"aaaa", PARAM_4);
691 NAPI_ASSERT(env, ret > PARAM_0, "wcsncmp Error");
692 ret = wcsncmp(L"aaaa", L"aaab", PARAM_3);
693 NAPI_ASSERT(env, ret == ERRON_0, "wcsncmp Error");
694 napi_value result = nullptr;
695 napi_create_int32(env, ret == ERRON_0, &result);
696 return result;
697 }
698
Wcsncpy_One(napi_env env,napi_callback_info info)699 static napi_value Wcsncpy_One(napi_env env, napi_callback_info info)
700 {
701 const wchar_t src[] = L"Hello";
702 wchar_t buf[SIZEOF_WCHAR(src) + PARAM_1];
703 wmemset(buf, L'A', SIZEOF_WCHAR(buf));
704 wchar_t *ret = static_cast<wchar_t *>(buf);
705
706 napi_value result = nullptr;
707 napi_create_int32(env, ret == wcsncpy(ret, src, SIZEOF_WCHAR(src)), &result);
708 return result;
709 }
710
Wcsnlen_One(napi_env env,napi_callback_info info)711 static napi_value Wcsnlen_One(napi_env env, napi_callback_info info)
712 {
713 const wchar_t *wc = L"ABCDEF";
714 size_t want = PARAM_6;
715 size_t ret = wcsnlen(wc, PARAM_6);
716 napi_value result = nullptr;
717 napi_create_int32(env, ret == want, &result);
718 return result;
719 }
720
Wcsnrtombs_One(napi_env env,napi_callback_info info)721 static napi_value Wcsnrtombs_One(napi_env env, napi_callback_info info)
722 {
723 const wchar_t src[] = L"test wcsnrtombs";
724 const wchar_t *p;
725 mbstate_t mbs;
726 char buffer[PARAM_32];
727 int wn = PARAM_4;
728 int n = PARAM_4;
729 mbrlen(nullptr, PARAM_0, &mbs);
730 memset(buffer, PARAM_0, sizeof(buffer));
731 p = src;
732 int ret = wcsnrtombs(buffer, &p, wn, n, &mbs);
733 NAPI_ASSERT(env, ret > PARAM_0, "wcsnrtombs Error");
734 napi_value result = nullptr;
735 napi_create_int32(env, strcmp(buffer, "test") == ERRON_0, &result);
736 return result;
737 }
738
Wcspbrk_One(napi_env env,napi_callback_info info)739 static napi_value Wcspbrk_One(napi_env env, napi_callback_info info)
740 {
741 wchar_t s[] = L"hello, world!";
742 wchar_t *ret = wcspbrk(s, L"abch");
743 NAPI_ASSERT(env, ret != nullptr, "wcspbrk Error");
744 napi_value result = nullptr;
745 napi_create_int32(env, wcscmp(ret, s) == ERRON_0, &result);
746 return result;
747 }
748
Wcsrchr_One(napi_env env,napi_callback_info info)749 static napi_value Wcsrchr_One(napi_env env, napi_callback_info info)
750 {
751 wchar_t ch[] = L"hello, world";
752 wchar_t *ret = wcsrchr(ch, L'h');
753 NAPI_ASSERT(env, ret != nullptr, "wcsrchr Error");
754 napi_value result = nullptr;
755 napi_create_int32(env, wcscmp(ret, ch) == ERRON_0, &result);
756 return result;
757 }
758
Wcsrtombs_One(napi_env env,napi_callback_info info)759 static napi_value Wcsrtombs_One(napi_env env, napi_callback_info info)
760 {
761 char str[SIZE_256] = {PARAM_0};
762 size_t want = PARAM_6;
763 const wchar_t *src = L"ABCDEF";
764 size_t ret = wcsrtombs(str, &src, PARAM_10, nullptr);
765 napi_value result = nullptr;
766 napi_create_int32(env, ret == want, &result);
767 return result;
768 }
769
Wcsspn_One(napi_env env,napi_callback_info info)770 static napi_value Wcsspn_One(napi_env env, napi_callback_info info)
771 {
772 size_t ret = wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz");
773 size_t want = 5U;
774 napi_value result = nullptr;
775 napi_create_int32(env, ret == want, &result);
776 return result;
777 }
778
Wcsstr_One(napi_env env,napi_callback_info info)779 static napi_value Wcsstr_One(napi_env env, napi_callback_info info)
780 {
781 wchar_t wcs[] = L"This is a simple string";
782 wchar_t *ret = nullptr;
783 ret = wcsstr(wcs, L"simple");
784 NAPI_ASSERT(env, ret != nullptr, "wcsstr Error");
785 wcsncpy(ret, L"sample", PARAM_6);
786 napi_value result = nullptr;
787 napi_create_int32(env, wcscmp(ret, L"sample string") == ERRON_0, &result);
788 return result;
789 }
790
Wcstod_One(napi_env env,napi_callback_info info)791 static napi_value Wcstod_One(napi_env env, napi_callback_info info)
792 {
793 wchar_t str0[] = L"3.14wcstod";
794 wchar_t *end = nullptr;
795 wchar_t str1[] = L"wcstod";
796 double ret = wcstod(str0, &end);
797 NAPI_ASSERT(env, (fabs(ret - PARAM_314) < DBL_EPSILON), "wcstod Error");
798 napi_value result = nullptr;
799 napi_create_int32(env, wcscmp(end, str1) == ERRON_0, &result);
800 return result;
801 }
802
Wcstof_One(napi_env env,napi_callback_info info)803 static napi_value Wcstof_One(napi_env env, napi_callback_info info)
804 {
805 wchar_t str0[] = L"3.14wcstof";
806 wchar_t *end = nullptr;
807 wchar_t str1[] = L"wcstof";
808 float ret = wcstof(str0, &end);
809 NAPI_ASSERT(env, (fabsf(ret - 3.14f) < FLT_EPSILON), "wcstof Error");
810 napi_value result = nullptr;
811 napi_create_int32(env, wcscmp(end, str1) == ERRON_0, &result);
812 return result;
813 }
814
Wcstok_One(napi_env env,napi_callback_info info)815 static napi_value Wcstok_One(napi_env env, napi_callback_info info)
816 {
817 wchar_t str[] = L"parrot,owl.sparrow,pigeon,crow";
818 wchar_t delim[] = L",.";
819 wchar_t *ptr;
820 wchar_t *ret = wcstok(str, delim, &ptr);
821 NAPI_ASSERT(env, ret != nullptr, "wcstok Error");
822 napi_value result = nullptr;
823 napi_create_int32(env, wcscmp(ret, L"parrot") == ERRON_0, &result);
824 return result;
825 }
826
Wcstol_One(napi_env env,napi_callback_info info)827 static napi_value Wcstol_One(napi_env env, napi_callback_info info)
828 {
829 wchar_t wsNumbers[] = L"2001 60c0c0 -1101110100110100100000 0x6fffff";
830 wchar_t *pEnd = nullptr;
831 long int li1 = wcstol(wsNumbers, &pEnd, PARAM_10);
832 long int li2 = wcstol(pEnd, &pEnd, PARAM_16);
833 long int li3 = wcstol(pEnd, &pEnd, PARAM_2);
834 long int li4 = wcstol(pEnd, nullptr, PARAM_0);
835 napi_value result = nullptr;
836 napi_create_int32(env, li1 == PARAM_2001 && li2 == PARAM_6340800 && li3 == MPARAM_36242240 && li4 == PARAM_7340031,
837 &result);
838 return result;
839 }
840
Wcstold_One(napi_env env,napi_callback_info info)841 static napi_value Wcstold_One(napi_env env, napi_callback_info info)
842 {
843 wchar_t str0[] = L"3.14wcstold";
844 wchar_t *end = nullptr;
845 wchar_t str1[] = L"wcstold";
846 long double ret = wcstold(str0, &end);
847 NAPI_ASSERT(env, (fabsl(ret - 3.14L) < LDBL_EPSILON), "wcstold Error");
848 napi_value result = nullptr;
849 napi_create_int32(env, wcscmp(end, str1) == ERRON_0, &result);
850 return result;
851 }
852
Wcstoll_One(napi_env env,napi_callback_info info)853 static napi_value Wcstoll_One(napi_env env, napi_callback_info info)
854 {
855 wchar_t wsNumbers[] = L"1856892505 17b00a12b -01100011010110000010001101100 0x6fffff";
856 wchar_t *pEnd = nullptr;
857 long long int lli1 = wcstoll(wsNumbers, &pEnd, PARAM_10);
858 long long int lli2 = wcstoll(pEnd, &pEnd, PARAM_16);
859 long long int lli3 = wcstoll(pEnd, &pEnd, PARAM_2);
860 long long int lli4 = wcstoll(pEnd, nullptr, PARAM_0);
861 napi_value result = nullptr;
862 napi_create_int32(
863 env, lli1 == PARAM_1856892505 && lli2 == PARAM_6358606123 && lli3 == MPARAM_208340076 && lli4 == PARAM_7340031,
864 &result);
865 return result;
866 }
867
Wcstombs_One(napi_env env,napi_callback_info info)868 static napi_value Wcstombs_One(napi_env env, napi_callback_info info)
869 {
870 const wchar_t str[] = L"wcstombs example";
871 char buffer[PARAM_32];
872 int ret = wcstombs(buffer, str, sizeof(buffer));
873 napi_value result = nullptr;
874 napi_create_int32(env, ret == wcslen(L"wcstombs example"), &result);
875 return result;
876 }
877
Wcstoul_One(napi_env env,napi_callback_info info)878 static napi_value Wcstoul_One(napi_env env, napi_callback_info info)
879 {
880 unsigned long want = PARAM_25;
881 unsigned long ret = wcstoul(L"25", nullptr, PARAM_0);
882 napi_value result = nullptr;
883 napi_create_int32(env, ret == want, &result);
884 return result;
885 }
886
Wcstoull_One(napi_env env,napi_callback_info info)887 static napi_value Wcstoull_One(napi_env env, napi_callback_info info)
888 {
889 wchar_t wsNumbers[] = L"250068492 7b06af00 1100011011110101010001100000 0x6fffff";
890 wchar_t *pEnd;
891 unsigned long long int ulli1 = wcstoull(wsNumbers, &pEnd, PARAM_10);
892 unsigned long long int ulli2 = wcstoull(pEnd, &pEnd, PARAM_16);
893 unsigned long long int ulli3 = wcstoull(pEnd, &pEnd, PARAM_2);
894 unsigned long long int ulli4 = wcstoull(pEnd, nullptr, PARAM_0);
895 napi_value result = nullptr;
896 napi_create_int32(env,
897 ulli1 == PARAM_250068492 && ulli2 == PARAM_2064035584 && ulli3 == PARAM_208622688 &&
898 ulli4 == PARAM_7340031,
899 &result);
900 return result;
901 }
902
Wcswcs_One(napi_env env,napi_callback_info info)903 static napi_value Wcswcs_One(napi_env env, napi_callback_info info)
904 {
905 wchar_t wstr[] = L"abcd";
906 wchar_t wch[] = L"a";
907 wchar_t *q = wcswcs(wstr, wch);
908 napi_value result = nullptr;
909 napi_create_int32(env, q - wstr == ERRON_0, &result);
910 return result;
911 }
912
Wcswidth_One(napi_env env,napi_callback_info info)913 static napi_value Wcswidth_One(napi_env env, napi_callback_info info)
914 {
915 int want = PARAM_2;
916 int ret = wcswidth(L"abc", want);
917 napi_value result = nullptr;
918 napi_create_int32(env, ret == want, &result);
919 return result;
920 }
921
Wcsxfrm_One(napi_env env,napi_callback_info info)922 static napi_value Wcsxfrm_One(napi_env env, napi_callback_info info)
923 {
924 wchar_t wc[] = L"ABCD";
925 wchar_t src[PARAM_20] = {PARAM_0};
926 size_t ret = wcsxfrm(src, wc, PARAM_5);
927 napi_value result = nullptr;
928 napi_create_int32(env, ret == wcslen(wc), &result);
929 return result;
930 }
931
Wcsxfrml_One(napi_env env,napi_callback_info info)932 static napi_value Wcsxfrml_One(napi_env env, napi_callback_info info)
933 {
934 locale_t localInfo = nullptr;
935 wchar_t wc[] = L"ABCD";
936 wchar_t src[PARAM_20] = {PARAM_0};
937 size_t ret = wcsxfrm_l(src, wc, PARAM_5, localInfo);
938 napi_value result = nullptr;
939 napi_create_int32(env, ret == wcslen(wc), &result);
940 return result;
941 }
942
Wctob_One(napi_env env,napi_callback_info info)943 static napi_value Wctob_One(napi_env env, napi_callback_info info)
944 {
945 wchar_t c = L'A';
946 int ret = wctob(c);
947 napi_value result = nullptr;
948 napi_create_int32(env, ret == 'A', &result);
949 return result;
950 }
951
Wctomb_One(napi_env env,napi_callback_info info)952 static napi_value Wctomb_One(napi_env env, napi_callback_info info)
953 {
954 char byte[SIZE_256] = {PARAM_0};
955 int ret = wctomb(byte, L'h');
956 napi_value result = nullptr;
957 napi_create_int32(env, ret == SUCCESS, &result);
958 return result;
959 }
960
Wcwidth_One(napi_env env,napi_callback_info info)961 static napi_value Wcwidth_One(napi_env env, napi_callback_info info)
962 {
963 int ret = MINUSONE;
964 const int INPUT_VALUE[PARAM_12] = {0x0300, 0x20dd, 0x00ad, 0x200b, 0x4e00, 0x9fff,
965 0x3400, 0x4dbf, 0x20000, 0x2a6df, 0xac00, 0xd7a3};
966 ret = wcwidth(INPUT_VALUE[PARAM_0]);
967 NAPI_ASSERT(env, ret == ERRON_0, "wcwidth Error");
968 ret = wcwidth(INPUT_VALUE[PARAM_1]);
969 NAPI_ASSERT(env, ret == ERRON_0, "wcwidth Error");
970 ret = wcwidth(INPUT_VALUE[PARAM_2]);
971 NAPI_ASSERT(env, ret == SUCCESS, "wcwidth Error");
972 ret = wcwidth(INPUT_VALUE[PARAM_3]);
973 NAPI_ASSERT(env, ret == ERRON_0, "wcwidth Error");
974 ret = wcwidth(INPUT_VALUE[PARAM_4]);
975 NAPI_ASSERT(env, ret == PARAM_2, "wcwidth Error");
976 ret = wcwidth(INPUT_VALUE[PARAM_11]);
977 NAPI_ASSERT(env, ret == PARAM_2, "wcwidth Error");
978 napi_value result = nullptr;
979 napi_create_int32(env, ret == PARAM_2, &result);
980 return result;
981 }
982
Wmemchr_One(napi_env env,napi_callback_info info)983 static napi_value Wmemchr_One(napi_env env, napi_callback_info info)
984 {
985 wchar_t wcs[] = L"Example wide string";
986 wchar_t *ret = wmemchr(wcs, L'p', wcslen(wcs));
987 NAPI_ASSERT(env, ret != nullptr, "wcwidth Error");
988 napi_value result = nullptr;
989 napi_create_int32(env, wcscmp(ret, L"ple wide string") == ERRON_0, &result);
990 return result;
991 }
992
Wmemcmp_One(napi_env env,napi_callback_info info)993 static napi_value Wmemcmp_One(napi_env env, napi_callback_info info)
994 {
995 int ret = MINUSONE;
996 wchar_t str[] = L"ABCDEFG";
997 wchar_t tar[] = L"ABCDEFF";
998 ret = wmemcmp(str, tar, wcslen(str));
999 NAPI_ASSERT(env, ret == SUCCESS, "wmemcmp Error");
1000 ret = wmemcmp(tar, str, wcslen(str));
1001 NAPI_ASSERT(env, ret == MPARAM_1, "wmemcmp Error");
1002 ret = wmemcmp(str, str, wcslen(str));
1003 NAPI_ASSERT(env, ret == ERRON_0, "wmemcmp Error");
1004 napi_value result = nullptr;
1005 napi_create_int32(env, ret == ERRON_0, &result);
1006 return result;
1007 }
1008
Wmemcpy_One(napi_env env,napi_callback_info info)1009 static napi_value Wmemcpy_One(napi_env env, napi_callback_info info)
1010 {
1011 wchar_t dest[PARAM_3];
1012 wchar_t src[] = L"aaa";
1013 wchar_t *ret = wmemcpy(dest, src, PARAM_3);
1014 NAPI_ASSERT(env, ret != nullptr, "wmemcpy Error");
1015 napi_value result = nullptr;
1016 napi_create_int32(env, wcsncmp(dest, src, PARAM_3) == ERRON_0, &result);
1017 return result;
1018 }
1019
Wmemmove_One(napi_env env,napi_callback_info info)1020 static napi_value Wmemmove_One(napi_env env, napi_callback_info info)
1021 {
1022 wchar_t src[] = L"This is a c test for wmemmove function";
1023 wchar_t *dest = &src[PARAM_0];
1024 wchar_t *ret = wmemmove(dest, src, PARAM_5);
1025 NAPI_ASSERT(env, ret != nullptr, "wmemmove Error");
1026 napi_value result = nullptr;
1027 napi_create_int32(env, ret == dest, &result);
1028 return result;
1029 }
1030
Wmemset_One(napi_env env,napi_callback_info info)1031 static napi_value Wmemset_One(napi_env env, napi_callback_info info)
1032 {
1033 wchar_t wcs[] = L"almost every programmer should know wmemset!";
1034 wchar_t *ret = wmemset(wcs, L'-', PARAM_6);
1035 NAPI_ASSERT(env, ret != nullptr, "wmemset Error");
1036 napi_value result = nullptr;
1037 napi_create_int32(env, wcscmp(ret, L"------ every programmer should know wmemset!") == ERRON_0, &result);
1038 return result;
1039 }
1040
1041 extern "C" int Vfwprintf_Test(const wchar_t *format, ...);
Vfwprintf_Test(FILE * stream,const wchar_t * format,...)1042 int Vfwprintf_Test(FILE *stream, const wchar_t *format, ...)
1043 {
1044 va_list args;
1045 va_start(args, format);
1046 int ret = vfwprintf(stream, format, args);
1047 va_end(args);
1048 return ret;
1049 }
Vfwprintf_One(napi_env env,napi_callback_info info)1050 static napi_value Vfwprintf_One(napi_env env, napi_callback_info info)
1051 {
1052 FILE *fp = fopen(TEMP_FILE, "w");
1053 NAPI_ASSERT(env, fp != nullptr, "Vfwprintf_One fopen Error");
1054 int ret = MINUSONE;
1055 ret = Vfwprintf_Test(fp, L"Call with %d variable argument.\n", PARAM_1);
1056 NAPI_ASSERT(env, ret = PARAM_31, "vfwprintf Error");
1057 ret = Vfwprintf_Test(fp, L"Call with %d variable %ls.\n", PARAM_2, L"arguments");
1058 fclose(fp);
1059 remove(TEMP_FILE);
1060 napi_value result = nullptr;
1061 napi_create_int32(env, ret == PARAM_32, &result);
1062 return result;
1063 }
1064
1065 extern "C" int Vswprintf_Test(const wchar_t *format, ...);
Vswprintf_Test(const wchar_t * format,...)1066 int Vswprintf_Test(const wchar_t *format, ...)
1067 {
1068 wchar_t buffer[SIZE_256];
1069 va_list args;
1070 va_start(args, format);
1071 int ret = vswprintf(buffer, SIZE_256, format, args);
1072 fputws(buffer, stdout);
1073 va_end(args);
1074 return ret;
1075 }
1076
Vswprintf_One(napi_env env,napi_callback_info info)1077 static napi_value Vswprintf_One(napi_env env, napi_callback_info info)
1078 {
1079 wchar_t str[] = L"test string has %d wide characters.\n";
1080 size_t ret = MINUSONE;
1081 ret = Vswprintf_Test(str, wcslen(str));
1082 napi_value result = nullptr;
1083 napi_create_int32(env, ret == wcslen(str), &result);
1084 return result;
1085 }
1086
1087 extern "C" int Vwscanf_Test(const wchar_t *format, ...);
Vwscanf_Test(const wchar_t * format,...)1088 int Vwscanf_Test(const wchar_t *format, ...)
1089 {
1090 va_list args;
1091 va_start(args, format);
1092 int ret = vwscanf(format, args);
1093 va_end(args);
1094 return ret;
1095 }
1096
Vwscanf_One(napi_env env,napi_callback_info info)1097 static napi_value Vwscanf_One(napi_env env, napi_callback_info info)
1098 {
1099 FILE *fp = fopen(TEMP_FILE, "w");
1100 NAPI_ASSERT(env, fp != nullptr, "Vwscanf_One fopen Error");
1101 wchar_t wstr[] = L"vwscanf";
1102 fputws(wstr, fp);
1103 fseek(fp, PARAM_0, SEEK_SET);
1104 FILE *fp1 = freopen(TEMP_FILE, "r", stdin);
1105 NAPI_ASSERT(env, fp1 != nullptr, "Vwscanf_One freopen Error");
1106 wchar_t wch[PARAM_20];
1107 int ret = Vwscanf_Test(L"%ls", wch);
1108 NAPI_ASSERT(env, ret == SUCCESS, "vwscanf Error");
1109 fclose(fp);
1110 fclose(fp1);
1111 remove(TEMP_FILE);
1112 napi_value result = nullptr;
1113 napi_create_int32(env, wcscmp(wch, wstr) == ERRON_0, &result);
1114 return result;
1115 }
1116
1117 extern "C" int Vwprintf_Test(const wchar_t *format, ...);
Vwprintf_Test(const wchar_t * format,...)1118 int Vwprintf_Test(const wchar_t *format, ...)
1119 {
1120 va_list args;
1121 va_start(args, format);
1122 int ret = vwprintf(format, args);
1123 va_end(args);
1124 return ret;
1125 }
Vwprintf_One(napi_env env,napi_callback_info info)1126 static napi_value Vwprintf_One(napi_env env, napi_callback_info info)
1127 {
1128 int ret = MINUSONE;
1129 ret = Vwprintf_Test(L"Call with %d variable %ls.\n", PARAM_2, L"arguments");
1130 napi_value result = nullptr;
1131 napi_create_int32(env, ret == PARAM_32, &result);
1132 return result;
1133 }
1134
1135 extern "C" int Vfwscanf_Test(FILE *fp, const wchar_t *format, ...);
Vfwscanf_Test(FILE * fp,const wchar_t * format,...)1136 int Vfwscanf_Test(FILE *fp, const wchar_t *format, ...)
1137 {
1138 va_list args;
1139 va_start(args, format);
1140 int result = vfwscanf(fp, format, args);
1141 va_end(args);
1142 return result;
1143 }
1144
Vfwscanf_One(napi_env env,napi_callback_info info)1145 static napi_value Vfwscanf_One(napi_env env, napi_callback_info info)
1146 {
1147 setlocale(LC_ALL, "en_US.UTF-8");
1148 wchar_t symbol[] = L"\u0915\u0916\u0917\u0918\u0919";
1149 wchar_t names[PARAM_5][PARAM_5] = {L"Ka", L"Kha", L"Ga", L"Gha", L"Nga"};
1150 FILE *fp = fopen(TEMP_FILE, "w+");
1151 NAPI_ASSERT(env, fp != nullptr, "vfwscanf fopen Error");
1152 for (int i = PARAM_0; i < PARAM_5; i++) {
1153 fwprintf(fp, L"%lc %ls ", symbol[i], names[i]);
1154 }
1155 fseek(fp, PARAM_0, SEEK_SET);
1156 wchar_t ch, str[PARAM_5];
1157 int ret = PARAM_0;
1158 for (ret = PARAM_0; ret < PARAM_5; ret++) {
1159 Vfwscanf_Test(fp, L"%lc %ls ", &ch, str);
1160 NAPI_ASSERT(env, wcscmp(str, names[ret]) == ERRON_0, "vfwscanf Error");
1161 }
1162 fclose(fp);
1163 remove(TEMP_FILE);
1164 napi_value result = nullptr;
1165 napi_create_int32(env, ret == PARAM_5, &result);
1166 return result;
1167 }
1168
Getwchar_One(napi_env env,napi_callback_info info)1169 static napi_value Getwchar_One(napi_env env, napi_callback_info info)
1170 {
1171 FILE *fp = fopen(TEMP_FILE, "w");
1172 NAPI_ASSERT(env, fp != nullptr, "Getwchar_One fopen Error");
1173 wchar_t wstr[] = L"getwchar";
1174 fputws(wstr, fp);
1175 fseek(fp, PARAM_0, SEEK_SET);
1176 FILE *fp1 = freopen(TEMP_FILE, "r", stdin);
1177 NAPI_ASSERT(env, fp1 != nullptr, "Getwchar_One freopen Error");
1178 wint_t ret = getwchar();
1179 fclose(fp);
1180 fclose(fp1);
1181 remove(TEMP_FILE);
1182 napi_value result = nullptr;
1183 napi_create_int32(env, static_cast<wchar_t>(ret) == L'g', &result);
1184 return result;
1185 }
1186
Wscanf_One(napi_env env,napi_callback_info info)1187 static napi_value Wscanf_One(napi_env env, napi_callback_info info)
1188 {
1189 FILE *fp = fopen(TEMP_FILE, "w+");
1190 NAPI_ASSERT(env, fp != nullptr, "Wscanf_One fopen Error");
1191 wchar_t wstr[] = L"testwscanf";
1192 fputws(wstr, fp);
1193 fseek(fp, PARAM_0, SEEK_SET);
1194 wchar_t tmp[PARAM_80] = {PARAM_0};
1195 fgetws(tmp, sizeof(tmp) / sizeof(wchar_t), fp);
1196 fseek(fp, PARAM_0, SEEK_SET);
1197 OH_LOG_INFO(LOG_APP, "Wscanf_One tmp : %{public}ls ", tmp);
1198 FILE *fp1 = freopen(TEMP_FILE, "r", stdin);
1199 NAPI_ASSERT(env, fp1 != nullptr, "Wscanf_One freopen Error");
1200 wchar_t wch[50] = {PARAM_0};
1201 int ret = wscanf(L"%ls", wch);
1202 NAPI_ASSERT(env, ret == SUCCESS, "wscanf Error");
1203 fclose(fp);
1204 fclose(fp1);
1205 remove(TEMP_FILE);
1206 napi_value result = nullptr;
1207 OH_LOG_INFO(LOG_APP, "Wscanf_One wch : %{public}ls ", wch);
1208 napi_create_int32(env, wcscmp(wch, L"testwscanf") == ERRON_0, &result);
1209 return result;
1210 }
1211
1212 EXTERN_C_START
Init(napi_env env,napi_value exports)1213 static napi_value Init(napi_env env, napi_value exports)
1214 {
1215 napi_property_descriptor desc[] = {
1216 {"Btowc_One", nullptr, Btowc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1217 {"Btowc_Two", nullptr, Btowc_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1218 {"Fputwc_One", nullptr, Fputwc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1219 {"Fputws_One", nullptr, Fputws_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1220 {"Fwide_One", nullptr, Fwide_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1221 {"Fwide_Two", nullptr, Fwide_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1222 {"Fwprintf_One", nullptr, Fwprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1223 {"Fwprintf_Two", nullptr, Fwprintf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1224 {"Fwscanf_One", nullptr, Fwscanf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1225 {"Fwscanf_Two", nullptr, Fwscanf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1226 {"Getwchar_One", nullptr, Getwchar_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1227 {"Mbrtowc_One", nullptr, Mbrtowc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1228 {"Mbsinit_One", nullptr, Mbsinit_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1229 {"Mbsnrtowcs_One", nullptr, Mbsnrtowcs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1230 {"Mbsrtowcs_One", nullptr, Mbsrtowcs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1231 {"Mbstowcs_One", nullptr, Mbstowcs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1232 {"Mbtowc_One", nullptr, Mbtowc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1233 {"Mbtowc_Two", nullptr, Mbtowc_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1234 {"OpenWmemstream_One", nullptr, OpenWmemstream_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1235 {"Putwc_One", nullptr, Putwc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1236 {"PutWChar_One", nullptr, PutWChar_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1237 {"Swprintf_One", nullptr, Swprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1238 {"Swprintf_Two", nullptr, Swprintf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1239 {"Swscanf_One", nullptr, Swscanf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1240 {"Swscanf_Two", nullptr, Swscanf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1241 {"Towlower_One", nullptr, Towlower_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1242 {"Towupper_One", nullptr, Towupper_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1243 {"Ungetwc_One", nullptr, Ungetwc_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1244 {"Vfwprintf_One", nullptr, Vfwprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1245 {"Vfwscanf_One", nullptr, Vfwscanf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1246 {"Vswprintf_One", nullptr, Vswprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1247 {"Vwprintf_One", nullptr, Vwprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1248 {"Vwscanf_One", nullptr, Vwscanf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1249 {"Wcpcpy_One", nullptr, Wcpcpy_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1250 {"Wcpncpy_One", nullptr, Wcpncpy_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1251 {"Wcrtomb_One", nullptr, Wcrtomb_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1252 {"Wcscasecmp_One", nullptr, Wcscasecmp_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1253 {"Wcscasecmpl_One", nullptr, Wcscasecmpl_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1254 {"Wcscat_One", nullptr, Wcscat_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1255 {"Wcschr_One", nullptr, Wcschr_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1256 {"Wcscmp_One", nullptr, Wcscmp_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1257 {"Wcscoll_One", nullptr, Wcscoll_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1258 {"Wcscolll_One", nullptr, Wcscolll_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1259 {"Wcscpy_One", nullptr, Wcscpy_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1260 {"Wcscspn_One", nullptr, Wcscspn_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1261 {"Wcsdup_One", nullptr, Wcsdup_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1262 {"Wcsftime_One", nullptr, Wcsftime_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1263 {"Wcsftimel_One", nullptr, Wcsftimel_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1264 {"Wcslen_One", nullptr, Wcslen_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1265 {"Wcsncasecmp_One", nullptr, Wcsncasecmp_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1266 {"Wcsncasecmpl_One", nullptr, Wcsncasecmpl_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1267 {"Wcsncat_One", nullptr, Wcsncat_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1268 {"Wcsncmp_One", nullptr, Wcsncmp_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1269 {"Wcsncpy_One", nullptr, Wcsncpy_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1270 {"Wcsnlen_One", nullptr, Wcsnlen_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1271 {"Wcsnrtombs_One", nullptr, Wcsnrtombs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1272 {"Wcspbrk_One", nullptr, Wcspbrk_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1273 {"Wcsrchr_One", nullptr, Wcsrchr_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1274 {"Wcsrtombs_One", nullptr, Wcsrtombs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1275 {"Wcsspn_One", nullptr, Wcsspn_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1276 {"Wcsstr_One", nullptr, Wcsstr_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1277 {"Wcstod_One", nullptr, Wcstod_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1278 {"Wcstof_One", nullptr, Wcstof_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1279 {"Wcstok_One", nullptr, Wcstok_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1280 {"Wcstol_One", nullptr, Wcstol_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1281 {"Wcstold_One", nullptr, Wcstold_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1282 {"Wcstoll_One", nullptr, Wcstoll_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1283 {"Wcstombs_One", nullptr, Wcstombs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1284 {"Wcstoul_One", nullptr, Wcstoul_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1285 {"Wcstoull_One", nullptr, Wcstoull_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1286 {"Wcswcs_One", nullptr, Wcswcs_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1287 {"Wcswidth_One", nullptr, Wcswidth_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1288 {"Wcsxfrm_One", nullptr, Wcsxfrm_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1289 {"Wcsxfrml_One", nullptr, Wcsxfrml_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1290 {"Wctob_One", nullptr, Wctob_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1291 {"Wctomb_One", nullptr, Wctomb_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1292 {"Wcwidth_One", nullptr, Wcwidth_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1293 {"Wmemchr_One", nullptr, Wmemchr_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1294 {"Wmemcmp_One", nullptr, Wmemcmp_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1295 {"Wmemcpy_One", nullptr, Wmemcpy_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1296 {"Wmemmove_One", nullptr, Wmemmove_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1297 {"Wmemset_One", nullptr, Wmemset_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1298 {"Wprintf_One", nullptr, Wprintf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1299 {"Wprintf_Two", nullptr, Wprintf_Two, nullptr, nullptr, nullptr, napi_default, nullptr},
1300 {"Wscanf_One", nullptr, Wscanf_One, nullptr, nullptr, nullptr, napi_default, nullptr},
1301 };
1302 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1303 return exports;
1304 }
1305
1306 EXTERN_C_END
1307
1308 static napi_module demoModule = {
1309 .nm_version = 1,
1310 .nm_flags = 0,
1311 .nm_filename = nullptr,
1312 .nm_register_func = Init,
1313 .nm_modname = "wcharndk1",
1314 .nm_priv = ((void *)0),
1315 .reserved = {0},
1316 };
1317
RegisterEntryModule(void)1318 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
1319