1 /*
2 * Copyright (C) 2023 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 <stdlib.h>
18 #include "functionalext.h"
19
20 #define BUFFER_SIZE 1024
21
sprintf_specifier_tests(void)22 void sprintf_specifier_tests(void)
23 {
24 char str[BUFFER_SIZE];
25 int ret = 0;
26
27 ret = sprintf(str, "test");
28 EXPECT_EQ("sprintf_specifier_null", ret, 4);
29 EXPECT_STREQ("sprintf_specifier_null", str, "test");
30
31 ret = sprintf(str, "%d,%i", 1, 2);
32 EXPECT_EQ("sprintf_specifier_integer", ret, 3);
33 EXPECT_STREQ("sprintf_specifier_integer", str, "1,2");
34
35 ret = sprintf(str, "%u,%u", 1, -1);
36 EXPECT_EQ("sprintf_specifier_Unsigned_integer", ret, 12);
37 EXPECT_STREQ("sprintf_specifier_Unsigned_integer", str, "1,4294967295");
38
39 ret = sprintf(str, "%o", 9);
40 EXPECT_EQ("sprintf_specifier_octal_integer", ret, 2);
41 EXPECT_STREQ("sprintf_specifier_octal_integer", str, "11");
42
43 ret = sprintf(str, "%x,%X", 15, 15);
44 EXPECT_EQ("sprintf_specifier_hexadecimal_integer", ret, 3);
45 EXPECT_STREQ("sprintf_specifier_hexadecimal_integer", str, "f,F");
46
47 ret = sprintf(str, "%f,%F", 1.23, 1.23);
48 EXPECT_EQ("sprintf_specifier_float", ret, 17);
49 EXPECT_STREQ("sprintf_specifier_float", str, "1.230000,1.230000");
50
51 ret = sprintf(str, "%e,%E", 1.23, 1.23);
52 EXPECT_EQ("sprintf_specifier_scientific_notation", ret, 25);
53 EXPECT_STREQ("sprintf_specifier_scientific_notation", str, "1.230000e+00,1.230000E+00");
54
55 ret = sprintf(str, "%c", '1');
56 EXPECT_EQ("sprintf_specifier_char", ret, 1);
57 EXPECT_STREQ("sprintf_specifier_char", str, "1");
58
59 ret = sprintf(str, "%s,%s", "123", "456");
60 EXPECT_EQ("sprintf_specifier_str", ret, 7);
61 EXPECT_STREQ("sprintf_specifier_str", str, "123,456");
62
63 ret = sprintf(str, "%p", 6618688);
64 EXPECT_EQ("sprintf_specifier_pointer_address", ret, 8);
65 EXPECT_STREQ("sprintf_specifier_pointer_address", str, "0x64fe40");
66
67 ret = sprintf(str, "%%test");
68 EXPECT_EQ("sprintf_specifier_double_percent_sign", ret, 5);
69 EXPECT_STREQ("sprintf_specifier_double_percent_sign", str, "%test");
70
71 ret = sprintf(str, "%3$d,%2$s,%1$c", 'a', "bc", 123);
72 EXPECT_EQ("sprintf_$_symbol", ret, 8);
73 EXPECT_STREQ("sprintf_$_symbol", str, "123,bc,a");
74 }
75
sprintf_flag_tests(void)76 void sprintf_flag_tests(void)
77 {
78 char str[BUFFER_SIZE];
79 int ret = 0;
80
81 ret = sprintf(str, "%+d %+d %d %d", -1, 1, -1, 1);
82 EXPECT_EQ("sprintf_flag_+_and_space", ret, 10);
83 EXPECT_STREQ("sprintf_flag_+_and_space", str, "-1 +1 -1 1");
84
85 ret = sprintf(str, "%#o,%#x,%#X", 9, 9, 9);
86 EXPECT_EQ("sprintf_flag_#", ret, 11);
87 EXPECT_STREQ("sprintf_flag_#", str, "011,0x9,0X9");
88
89 ret = sprintf(str, "%5d,%05d,%0-5d", 123, 123, 123);
90 EXPECT_EQ("sprintf_flag_-_and_0", ret, 17);
91 EXPECT_STREQ("sprintf_flag_-_and_0", str, " 123,00123,123 ");
92
93 }
94
sprintf_width_tests(void)95 void sprintf_width_tests(void)
96 {
97 char str[BUFFER_SIZE];
98 int ret = 0;
99 ret = sprintf(str, "%d,%3d,%10d", 12345, 12345, 12345);
100 EXPECT_EQ("sprintf_width_num", ret, 22);
101 EXPECT_STREQ("sprintf_width_num", str, "12345,12345, 12345");
102
103 ret = sprintf(str, "%*d", 10, 12345);
104 EXPECT_EQ("sprintf_width_*", ret, 10);
105 EXPECT_STREQ("sprintf_width_*", str, " 12345");
106 }
107
sprintf_precision_tests(void)108 void sprintf_precision_tests(void)
109 {
110 char str[BUFFER_SIZE];
111 int ret = 0;
112 ret = sprintf(str, "%f,%.3f", 1.2345, 1.2345);
113 EXPECT_EQ("sprintf_precision_num", ret, 14);
114 EXPECT_STREQ("sprintf_precision_num", str, "1.234500,1.234");
115
116 ret = sprintf(str, "%.*f", 3, 1.2345);
117 EXPECT_EQ("sprintf_precision_*", ret, 5);
118 EXPECT_STREQ("sprintf_precision_*", str, "1.234");
119 }
120
121
main(void)122 int main(void)
123 {
124 // %[flags][width][.precision][length]specifier
125 sprintf_specifier_tests();
126 sprintf_flag_tests();
127 sprintf_width_tests();
128 sprintf_precision_tests();
129 return t_status;
130 }
131