• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <stdarg.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <locale.h>
23 #include <wchar.h>
24 #include "test.h"
25 
deal_aberrant(int code)26 void deal_aberrant(int code)
27 {
28     if (code != SIGSEGV) {
29         t_error("vfwscanf_0200 code is %d are not SIGSEGV", __func__, code);
30     }
31     exit(t_status);
32 }
33 
read_from_file(FILE * fp,const wchar_t * format,...)34 int read_from_file(FILE *fp, const wchar_t *format, ...)
35 {
36     va_list args;
37     va_start(args, format);
38     int result = vfwscanf(fp, format, args);
39     va_end(args);
40     return result;
41 }
42 
43 /**
44  * @tc.name      : vfwscanf_0100
45  * @tc.desc      : Test vfwscanf method to read wide string from file
46  * @tc.level     : Level 0
47  */
vfwscanf_0100(void)48 void vfwscanf_0100(void)
49 {
50     setlocale(LC_ALL, "en_US.UTF-8");
51 
52     wchar_t symbol[] = L"\u0915\u0916\u0917\u0918\u0919";
53     wchar_t names[5][5] = {L"Ka", L"Kha", L"Ga", L"Gha", L"Nga"};
54     FILE *fp = fopen("example.txt", "w+");
55 
56     for (int i = 0; i < 5; i++)
57         fwprintf(fp, L"%lc %ls ", symbol[i], names[i]);
58     rewind(fp);
59 
60     wchar_t ch, str[5];
61     for (int i = 0; i < 5; i++) {
62         read_from_file(fp, L"%lc %ls ", &ch, str);
63         if (wcscmp(str, names[i]) != 0) {
64             t_error("%s vfwscanf in %d get result is %ls are not want %ls\n", __func__, i, str, names[i]);
65         }
66     }
67 
68     fclose(fp);
69     unlink("example.txt");
70 }
71 
72 /**
73  * @tc.name      : vfwscanf_0200
74  * @tc.desc      : Test the result of the vfwscanf function when the incoming file is empty
75  * @tc.level     : Level 2
76  */
vfwscanf_0200(void)77 void vfwscanf_0200(void)
78 {
79     wchar_t symbol[] = L"\u0915\u0916\u0917\u0918\u0919";
80     wchar_t names[5][5] = {L"Ka", L"Kha", L"Ga", L"Gha", L"Nga"};
81     signal(SIGSEGV, deal_aberrant);
82     read_from_file(NULL, symbol, names);
83 }
84 
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87     vfwscanf_0100();
88     vfwscanf_0200();
89     return t_status;
90 }