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