• 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 
getfileall(char * fname)22 char *getfileall(char *fname)
23 {
24     FILE *fp;
25     char *str;
26     char txt[1000];
27     int filesize;
28     if ((fp = fopen(fname, "r")) == NULL) {
29         printf("打开文件%s错误\n", fname);
30         return NULL;
31     }
32     fseek(fp, 0, SEEK_END);
33     filesize = ftell(fp);
34     str = (char *)malloc(filesize);
35     str[0] = 0;
36     rewind(fp);
37 
38     while ((fgets(txt, 1000, fp)) != NULL) {
39         strcat(str, txt);
40     }
41     fclose(fp);
42     return str;
43 }
44 
vfprintf_test(char * file_name,char * format,char * func_name,char * want_reuslt,...)45 void vfprintf_test(char *file_name, char *format, char *func_name, char *want_reuslt, ...)
46 {
47     FILE *file = fopen(file_name, "w");
48     if (!file) {
49         t_error("%s fopen failed\n", func_name);
50     }
51     va_list ap;
52     va_start(ap, want_reuslt);
53     int result = vfprintf(file, format, ap);
54     va_end(ap);
55     fclose(file);
56     if (result < 0) {
57         t_error("%s vfprintf get result is %d not less 0", func_name, result);
58     }
59     char *buffer = getfileall(file_name);
60     if (strcmp(buffer, want_reuslt) != 0) {
61         t_error("%s vfprintf get is '%s' not '%s'", func_name, buffer, want_reuslt);
62     }
63     remove(file_name);
64 }
65 
vfprintf_n_test(char * file_name,char * format,char * func_name,char * want_reuslt,...)66 void vfprintf_n_test(char *file_name, char *format, char *func_name, char *want_reuslt, ...)
67 {
68     FILE *file = fopen(file_name, "w");
69     if (!file) {
70         t_error("%s fopen failed\n", func_name);
71     }
72     va_list ap;
73     va_start(ap, want_reuslt);
74     int result = vfprintf(file, format, ap);
75     va_end(ap);
76     fclose(file);
77     if (result < 0) {
78         t_error("%s vfprintf get result is %d not less 0", func_name, result);
79     }
80     char *buffer = getfileall(file_name);
81     if (strcmp(buffer, want_reuslt) == 0) {
82         t_error("%s vfprintf get is '%s'", func_name, buffer);
83     }
84     remove(file_name);
85 }
86 
main(int argc,char * argv[])87 int main(int argc, char *argv[])
88 {
89     /**
90      * @tc.name      : vfprintf_0100
91      * @tc.desc      : Format the output string through vfprintf, the formatted part is a number
92      * @tc.level     : Level 0
93      */
94     vfprintf_test("/data/vfprintf.txt", "value is %s and %s", "vfprintf_0100", "value is qwe and 1", "qwe", "1");
95     /**
96      * @tc.name      : vfprintf_0200
97      * @tc.desc      : Format the output string through vfprintf, the formatted part is a string
98      * @tc.level     : Level 1
99      */
100     vfprintf_test("/data/vfprintf.txt", "value is %d and %d", "vfprintf_0200", "value is 1 and 1", 1, 1);
101     /**
102      * @tc.name      : vfprintf_0300
103      * @tc.desc      : When calling vfprintf, the parameters passed in exceeds the number of formats to be formatted
104      * @tc.level     : Level 2
105      */
106     vfprintf_test("/data/vfprintf.txt", "value is %d", "vfprintf_0300", "value is 1", 1, 1);
107     /**
108      * @tc.name      : vfprintf_0400
109      * @tc.desc      : The parameters passed in when calling vfprintf are inconsistent with the data type to be
110      * formatted
111      * @tc.level     : Level 2
112      */
113     vfprintf_n_test("/data/vfprintf.txt", "value is %s and %d", "vfprintf_0400", "value is qer and erq", "qer", "erq");
114     return t_status;
115 }