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
vsnprintf_test(char * str,size_t n,char * fmt,const char * func_name,...)22 void vsnprintf_test(char *str, size_t n, char *fmt, const char *func_name, ...)
23 {
24 char s[n];
25 va_list ap;
26 va_start(ap, func_name);
27 int result = vsnprintf(s, n, fmt, ap);
28 va_end(ap);
29 if (result < 0) {
30 t_error("%s vsnprintf get result is %d are less 0\n", func_name, result);
31 }
32 if (strcmp(s, str) != 0) {
33 t_error("%s vsnprintf get is '%s' are not '%s'\n", func_name, s, str);
34 }
35 }
36
vsnprintf_zeron(char * str,char * fmt,const char * func_name,...)37 void vsnprintf_zeron(char *str, char *fmt, const char *func_name, ...)
38 {
39 char s[10];
40 va_list ap;
41 va_start(ap, func_name);
42 int result = vsnprintf(s, 0, fmt, ap);
43 va_end(ap);
44 if (result < 0) {
45 t_error("%s vsnprintf get result is %d are less 0\n", func_name, result);
46 }
47 if (strcmp(s, str) == 0) {
48 t_error("%s vsnprintf get is '%s' but donnot want that\n", func_name, s);
49 }
50 }
51
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 /**
55 * @tc.name : vsnprintf_0100
56 * @tc.desc : Call vsnprinf to get formatted output
57 * @tc.level : Level 0
58 */
59 vsnprintf_test("value is use", 13, "value is %s", "vsnprintf_0100", "use");
60 /**
61 * @tc.name : vsnprintf_0200
62 * @tc.desc : The number of digits to be truncated is less than the length of the format string
63 * @tc.level : Level 1
64 */
65 vsnprintf_test("value is ", 10, "value is %s", "vsnprintf_0200", "use");
66 /**
67 * @tc.name : vsnprintf_0300
68 * @tc.desc : The number of bits to be truncated is greater than the format string length
69 * @tc.level : Level 1
70 */
71 vsnprintf_test("value is use", 15, "value is %s", "vsnprintf_0300", "use");
72 /**
73 * @tc.name : vsnprintf_0400
74 * @tc.desc : Truncate bits to 0
75 * @tc.level : Level 2
76 */
77 vsnprintf_zeron("value is use", "value is %s", "vsnprintf_0400", "use");
78 return t_status;
79 }