• 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 <stdio.h>
17 #include <string.h>
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include "test.h"
21 
get_return_vaule(char * s,char * fmt,...)22 int get_return_vaule(char *s, char *fmt, ...)
23 {
24     va_list ap;
25     va_start(ap, fmt);
26     int result = vsscanf(s, fmt, ap);
27     va_end(ap);
28     return result;
29 }
30 
31 /**
32  * @tc.name      : vsscanf_0100
33  * @tc.desc      : Call vsscanf to convert and format the data from the data according to format
34  * @tc.level     : Level 0
35  */
vsscanf_0100(void)36 void vsscanf_0100(void)
37 {
38     int val = 0;
39     char buffer[100];
40     int result = get_return_vaule("value 123 info", "%s %d", &buffer, &val);
41     if (result < 0) {
42         t_error("%s vsscanf get result is %d are less 0\n", __func__, result);
43     }
44     if (strcmp(buffer, "value") != 0) {
45         t_error("%s vsscanf get is '%s' are not 'value'\n", __func__, buffer);
46     }
47     if (val != 123) {
48         t_error("%s vsscanf get is %d are not 123\n", __func__, val);
49     }
50 }
51 
52 /**
53  * @tc.name      : vsscanf_0200
54  * @tc.desc      : Test conversion of different formats
55  * @tc.level     : Level 1
56  */
vsscanf_0200(void)57 void vsscanf_0200(void)
58 {
59     char buffer[100];
60     char get_val[100];
61     int result = get_return_vaule("value 123 info", "%s %s", &buffer, &get_val);
62     if (result < 0) {
63         t_error("%s vsscanf get result is %d are less 0\n", __func__, result);
64     }
65     if (strcmp(buffer, "value") != 0) {
66         t_error("%s vsscanf get is '%s' are not 'value'\n", __func__, buffer);
67     }
68     if (strcmp(get_val, "123") != 0) {
69         t_error("%s vsscanf get is %s are not 123\n", __func__, get_val);
70     }
71 }
72 
73 /**
74  * @tc.name      : vsscanf_0300
75  * @tc.desc      : The test corresponding position does not match the format
76  * @tc.level     : Level 2
77  */
vsscanf_0300(void)78 void vsscanf_0300(void)
79 {
80     int val = 0;
81     int val_temp = 0;
82     int result = get_return_vaule("value 123 info", "%d %d", &val, &val_temp);
83     if (result > 0) {
84         t_error("%s vsscanf get result is %d are more than the 0\n", __func__, result);
85     }
86     if (val != 0) {
87         t_error("%s vsscanf get is %d are not 0\n", __func__, val);
88     }
89     if (val_temp != 0) {
90         t_error("%s vsscanf get is %d are not 0\n", __func__, val_temp);
91     }
92 }
93 
main(int argc,char * argv[])94 int main(int argc, char *argv[])
95 {
96     vsscanf_0100();
97     vsscanf_0200();
98     vsscanf_0300();
99     return t_status;
100 }