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 "test.h"
20
21 /**
22 * @tc.name : vasprintf_0100
23 * @tc.desc : Format the output string through vasprintf, the formatted part is a number
24 * @tc.level : Level 0
25 */
vasprintf_0100(char * v,...)26 void vasprintf_0100(char *v, ...)
27 {
28 va_list ap;
29 va_start(ap, v);
30 char ta[] = " ";
31 char *temp = ta;
32 int result = vasprintf(&temp, v, ap);
33 if (result == 0) {
34 t_error("%s vasprintf result value is 0\n", __func__);
35 }
36 if (strcmp(temp, "value is 123 and 321.") != 0) {
37 t_error("%s vasprintf get tmep is %s are not 'value is 123 and 321.'\n", __func__, temp);
38 }
39 va_end(ap);
40 }
41
42 /**
43 * @tc.name : vasprintf_0200
44 * @tc.desc : Format the output string through vasprintf, the formatted part is a string
45 * @tc.level : Level 1
46 */
vasprintf_0200(char * v,...)47 void vasprintf_0200(char *v, ...)
48 {
49 va_list ap;
50 va_start(ap, v);
51 char ta[] = " ";
52 char *temp = ta;
53 int result = vasprintf(&temp, v, ap);
54 if (result == 0) {
55 t_error("%s vasprintf result value is 0\n", __func__);
56 }
57 if (strcmp(temp, "value is qer and erq.") != 0) {
58 t_error("%s vasprintf get tmep is %s are not 'value is qer and erq.'\n", __func__, temp);
59 }
60 va_end(ap);
61 }
62
63 /**
64 * @tc.name : vasprintf_0300
65 * @tc.desc : The parameters passed in when calling vasprintf are inconsistent with the data type to be formatted
66 * @tc.level : Level 2
67 */
vasprintf_0300(char * v,...)68 void vasprintf_0300(char *v, ...)
69 {
70 va_list ap;
71 va_start(ap, v);
72 char ta[] = " ";
73 char *temp = ta;
74 int result = vasprintf(&temp, v, ap);
75 if (result == 0) {
76 t_error("%s vasprintf result value is 0\n", __func__);
77 }
78 if (strcmp(temp, "value is qer and erq.") == 0) {
79 t_error("%s vasprintf error get tmep is '%s'\n", __func__, temp);
80 }
81 va_end(ap);
82 }
83
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86 vasprintf_0100("value is %d and %d.", 123, 321);
87 vasprintf_0200("value is %s and %s.", "qer", "erq");
88 vasprintf_0300("value is %s and %d.", "qer", "erq");
89 return t_status;
90 }