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 <stdio.h>
17 #include <wchar.h>
18 #include "test.h"
19 
20 #define BUFF_SIZE (20)
21 #define RESULT_VAL (2)
22 
23 /**
24  * @tc.name      : swscanf_0100
25  * @tc.desc      : Reads data from the wide string ws and stores them according to parameter format
26  * @tc.level     : Level 0
27  */
swscanf_0100(void)28 void swscanf_0100(void)
29 {
30     wchar_t wstr[] = L"swscanf 123";
31     wchar_t tmp[BUFF_SIZE];
32     int i;
33     int result = swscanf(wstr, L"%ls %d", tmp, &i);
34     if (result == EOF) {
35         t_error("%s swscanf failed\n", __func__);
36         return;
37     }
38 
39     if (i != 123) {
40         t_error("%s i = %d is not want 123\n", __func__, i);
41     }
42     if (wcscmp(tmp, L"swscanf")) {
43         t_error("%s wide string is %s not swscanf\n", __func__, tmp);
44     }
45 }
46 
47 /**
48  * @tc.name      : swscanf_0200
49  * @tc.desc      : Different formatted data
50  * @tc.level     : Level 1
51  */
swscanf_0200(void)52 void swscanf_0200(void)
53 {
54     wchar_t wstr[] = L"swscanf 123";
55     wchar_t tmp1[BUFF_SIZE];
56     wchar_t tmp2[BUFF_SIZE];
57     int i;
58     int result = swscanf(wstr, L"%ls %ls", tmp1, tmp2);
59     if (result == EOF) {
60         t_error("%s swscanf failed\n", __func__);
61         return;
62     }
63     if (wcscmp(tmp1, L"swscanf")) {
64         t_error("%s tmp1 = %s is not want swscanf\n", __func__, tmp1);
65     }
66     if (wcscmp(tmp2, L"123")) {
67         t_error("%s tmp2 = %s is not want 123\n", __func__, tmp2);
68     }
69 }
70 
71 /**
72  * @tc.name      : swscanf_0300
73  * @tc.desc      : Formatting exception
74  * @tc.level     : Level 2
75  */
swscanf_0300(void)76 void swscanf_0300(void)
77 {
78     wchar_t wstr[] = L"swscanf 123";
79     int i, j;
80     int result = swscanf(wstr, L"%d %d", &i, &j);
81     if (result >= RESULT_VAL) {
82         t_error("%s swscanf expect failed\n", __func__);
83     }
84 }
85 
main(int argc, char *argv[])86 int main(int argc, char *argv[])
87 {
88     swscanf_0100();
89     swscanf_0200();
90     swscanf_0300();
91     return t_status;
92 }