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