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 <stdarg.h>
17#include <stdio.h>
18#include <string.h>
19#include "test.h"
20
21/**
22 * @tc.name      : vsprintf_0100
23 * @tc.desc      : Write formatted data %d to string from variable argument list
24 * @tc.level     : Level 0
25 */
26void vsprintf_0100(char *format, ...)
27{
28    char buffer[20] = {0};
29    va_list aptr;
30    va_start(aptr, format);
31    int result = vsprintf(buffer, format, aptr);
32    va_end(aptr);
33    if (result < 0) {
34        t_error("%s vsprintf get result is less than 0", __func__);
35    }
36    if (strcmp(buffer, "1")) {
37        t_error("%s wrong string written to buf %s\n", __func__, buffer);
38    }
39}
40
41/**
42 * @tc.name      : vsprintf_0200
43 * @tc.desc      : Write formatted data %f to string from variable argument list
44 * @tc.level     : Level 1
45 */
46void vsprintf_0200(char *format, ...)
47{
48    char buffer[20] = {0};
49    va_list aptr;
50    va_start(aptr, format);
51    int result = vsprintf(buffer, format, aptr);
52    va_end(aptr);
53    if (result < 0) {
54        t_error("%s vsprintf get result is less than 0\n", __func__);
55    }
56    if (strncmp(buffer, "3.0", 3)) {
57        t_error("%s wrong string written to buf %s\n", __func__, buffer);
58    }
59}
60
61/**
62 * @tc.name      : vsprintf_0300
63 * @tc.desc      : Write formatted data %s to string from variable argument list
64 * @tc.level     : Level 1
65 */
66void vsprintf_0300(char *format, ...)
67{
68    char buffer[20];
69    va_list aptr;
70    va_start(aptr, format);
71    int result = vsprintf(buffer, format, aptr);
72    va_end(aptr);
73    if (result < 0) {
74        t_error("%s vsprintf get result is less than 0\n", __func__);
75    }
76    if (strcmp(buffer, "vsprintf test")) {
77        t_error("%s wrong string written to buf %s\n", __func__, buffer);
78    }
79}
80
81int main(int argc, char *argv[])
82{
83    int i = 1;
84    float f = 3.0f;
85    char str[] = "vsprintf test";
86    vsprintf_0100("%d", i);
87    vsprintf_0200("%f", f);
88    vsprintf_0300("%s", str);
89    return t_status;
90}
91