1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdio.h>
17570af302Sopenharmony_ci#include <string.h>
18570af302Sopenharmony_ci#include <stdarg.h>
19570af302Sopenharmony_ci#include <stdlib.h>
20570af302Sopenharmony_ci#include "test.h"
21570af302Sopenharmony_ci
22570af302Sopenharmony_cichar *getfileall(char *fname)
23570af302Sopenharmony_ci{
24570af302Sopenharmony_ci    FILE *fp;
25570af302Sopenharmony_ci    char *str;
26570af302Sopenharmony_ci    char txt[1000];
27570af302Sopenharmony_ci    int filesize;
28570af302Sopenharmony_ci    if ((fp = fopen(fname, "r")) == NULL) {
29570af302Sopenharmony_ci        printf("打开文件%s错误\n", fname);
30570af302Sopenharmony_ci        return NULL;
31570af302Sopenharmony_ci    }
32570af302Sopenharmony_ci    fseek(fp, 0, SEEK_END);
33570af302Sopenharmony_ci    filesize = ftell(fp);
34570af302Sopenharmony_ci    str = (char *)malloc(filesize);
35570af302Sopenharmony_ci    str[0] = 0;
36570af302Sopenharmony_ci    rewind(fp);
37570af302Sopenharmony_ci
38570af302Sopenharmony_ci    while ((fgets(txt, 1000, fp)) != NULL) {
39570af302Sopenharmony_ci        strcat(str, txt);
40570af302Sopenharmony_ci    }
41570af302Sopenharmony_ci    fclose(fp);
42570af302Sopenharmony_ci    return str;
43570af302Sopenharmony_ci}
44570af302Sopenharmony_ci
45570af302Sopenharmony_civoid vfprintf_test(char *file_name, char *format, char *func_name, char *want_reuslt, ...)
46570af302Sopenharmony_ci{
47570af302Sopenharmony_ci    FILE *file = fopen(file_name, "w");
48570af302Sopenharmony_ci    if (!file) {
49570af302Sopenharmony_ci        t_error("%s fopen failed\n", func_name);
50570af302Sopenharmony_ci    }
51570af302Sopenharmony_ci    va_list ap;
52570af302Sopenharmony_ci    va_start(ap, want_reuslt);
53570af302Sopenharmony_ci    int result = vfprintf(file, format, ap);
54570af302Sopenharmony_ci    va_end(ap);
55570af302Sopenharmony_ci    fclose(file);
56570af302Sopenharmony_ci    if (result < 0) {
57570af302Sopenharmony_ci        t_error("%s vfprintf get result is %d not less 0", func_name, result);
58570af302Sopenharmony_ci    }
59570af302Sopenharmony_ci    char *buffer = getfileall(file_name);
60570af302Sopenharmony_ci    if (strcmp(buffer, want_reuslt) != 0) {
61570af302Sopenharmony_ci        t_error("%s vfprintf get is '%s' not '%s'", func_name, buffer, want_reuslt);
62570af302Sopenharmony_ci    }
63570af302Sopenharmony_ci    remove(file_name);
64570af302Sopenharmony_ci}
65570af302Sopenharmony_ci
66570af302Sopenharmony_civoid vfprintf_n_test(char *file_name, char *format, char *func_name, char *want_reuslt, ...)
67570af302Sopenharmony_ci{
68570af302Sopenharmony_ci    FILE *file = fopen(file_name, "w");
69570af302Sopenharmony_ci    if (!file) {
70570af302Sopenharmony_ci        t_error("%s fopen failed\n", func_name);
71570af302Sopenharmony_ci    }
72570af302Sopenharmony_ci    va_list ap;
73570af302Sopenharmony_ci    va_start(ap, want_reuslt);
74570af302Sopenharmony_ci    int result = vfprintf(file, format, ap);
75570af302Sopenharmony_ci    va_end(ap);
76570af302Sopenharmony_ci    fclose(file);
77570af302Sopenharmony_ci    if (result < 0) {
78570af302Sopenharmony_ci        t_error("%s vfprintf get result is %d not less 0", func_name, result);
79570af302Sopenharmony_ci    }
80570af302Sopenharmony_ci    char *buffer = getfileall(file_name);
81570af302Sopenharmony_ci    if (strcmp(buffer, want_reuslt) == 0) {
82570af302Sopenharmony_ci        t_error("%s vfprintf get is '%s'", func_name, buffer);
83570af302Sopenharmony_ci    }
84570af302Sopenharmony_ci    remove(file_name);
85570af302Sopenharmony_ci}
86570af302Sopenharmony_ci
87570af302Sopenharmony_ciint main(int argc, char *argv[])
88570af302Sopenharmony_ci{
89570af302Sopenharmony_ci    /**
90570af302Sopenharmony_ci     * @tc.name      : vfprintf_0100
91570af302Sopenharmony_ci     * @tc.desc      : Format the output string through vfprintf, the formatted part is a number
92570af302Sopenharmony_ci     * @tc.level     : Level 0
93570af302Sopenharmony_ci     */
94570af302Sopenharmony_ci    vfprintf_test("/data/vfprintf.txt", "value is %s and %s", "vfprintf_0100", "value is qwe and 1", "qwe", "1");
95570af302Sopenharmony_ci    /**
96570af302Sopenharmony_ci     * @tc.name      : vfprintf_0200
97570af302Sopenharmony_ci     * @tc.desc      : Format the output string through vfprintf, the formatted part is a string
98570af302Sopenharmony_ci     * @tc.level     : Level 1
99570af302Sopenharmony_ci     */
100570af302Sopenharmony_ci    vfprintf_test("/data/vfprintf.txt", "value is %d and %d", "vfprintf_0200", "value is 1 and 1", 1, 1);
101570af302Sopenharmony_ci    /**
102570af302Sopenharmony_ci     * @tc.name      : vfprintf_0300
103570af302Sopenharmony_ci     * @tc.desc      : When calling vfprintf, the parameters passed in exceeds the number of formats to be formatted
104570af302Sopenharmony_ci     * @tc.level     : Level 2
105570af302Sopenharmony_ci     */
106570af302Sopenharmony_ci    vfprintf_test("/data/vfprintf.txt", "value is %d", "vfprintf_0300", "value is 1", 1, 1);
107570af302Sopenharmony_ci    /**
108570af302Sopenharmony_ci     * @tc.name      : vfprintf_0400
109570af302Sopenharmony_ci     * @tc.desc      : The parameters passed in when calling vfprintf are inconsistent with the data type to be
110570af302Sopenharmony_ci     * formatted
111570af302Sopenharmony_ci     * @tc.level     : Level 2
112570af302Sopenharmony_ci     */
113570af302Sopenharmony_ci    vfprintf_n_test("/data/vfprintf.txt", "value is %s and %d", "vfprintf_0400", "value is qer and erq", "qer", "erq");
114570af302Sopenharmony_ci    return t_status;
115570af302Sopenharmony_ci}