• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20 
21 const size_t ERRORLEN = -1;
22 
23 /**
24  * @tc.name:      fprintf_0100
25  * @tc.desc:      Verify fprintf and stdout process success.
26  * @tc.level:     level 0.
27  */
fprintf_0100(void)28 void fprintf_0100(void)
29 {
30     char str[] = "This is a fprintf_0100";
31     size_t len = fprintf(stdout, "%s", str);
32     EXPECT_EQ("fprintf_0100", len, strlen(str));
33 }
34 
35 /**
36  * @tc.name:      fprintf_0200
37  * @tc.desc:      Verify fprintf and stderr process success.
38  * @tc.level:     level 0.
39  */
fprintf_0200(void)40 void fprintf_0200(void)
41 {
42     char str[] = "This is a test fprintf_0200";
43     size_t len = fprintf(stderr, "%s", str);
44     EXPECT_EQ("fprintf_0200", len, strlen(str));
45 }
46 
47 /**
48  * @tc.name:      fprintf_0300
49  * @tc.desc:      Verify fprintf and fopen("w") process success.
50  * @tc.level:     level 0.
51  */
fprintf_0300(void)52 void fprintf_0300(void)
53 {
54     char str[] = "这是一个测试";
55     int len = 0;
56     FILE *fptr = fopen("tempory_testfprintf.txt", "w");
57     if (fptr != NULL) {
58         len = fprintf(fptr, "%s", str);
59         fclose(fptr);
60     }
61     EXPECT_EQ("fprintf_0300", len, strlen(str));
62 }
63 
64 /**
65  * @tc.name:      fprintf_0400
66  * @tc.desc:      Verify fflush and fopen("r") and fclose and return -1.
67  * @tc.level:     level 2.
68  */
fprintf_0400(void)69 void fprintf_0400(void)
70 {
71     char str[] = "这是一个测试";
72     size_t len = 0;
73     FILE *fptr = fopen("tempory_testfprintf.txt", "r");
74     if (fptr != NULL) {
75         len = fprintf(fptr, "%s", str);
76         fclose(fptr);
77     }
78     EXPECT_EQ("fprintf_0400", len, ERRORLEN);
79     remove("tempory_testfprintf.txt");
80 }
81 
82 /**
83  * @tc.name:      fprintf_0500
84  * @tc.desc:      Verify fprintf with multiple format specifiers.
85  * @tc.level:     level 0.
86  */
fprintf_0500(void)87 void fprintf_0500(void)
88 {
89     char str[] = "Test";
90     int num = 123;
91     double d = 45.67;
92     size_t len = 0;
93     FILE *fptr = fopen("tempory_testfprintf.txt", "w");
94     if (fptr != NULL) {
95         len = fprintf(fptr, "String: %s, Int: %d, Double: %.2f", str, num, d);
96         fclose(fptr);
97     }
98     int expected_len = strlen("String: Test, Int: 123, Double: 45.67");
99     EXPECT_EQ("fprintf_0500", len, expected_len);
100     (void)remove("tempory_testfprintf.txt");
101 }
102 
103 
104 /**
105  * @tc.name:      fprintf_0600
106  * @tc.desc:      Verify fprintf with append mode.
107  * @tc.level:     level 0.
108  */
fprintf_0600(void)109 void fprintf_0600(void)
110 {
111     char str1[] = "First line\n";
112     char str2[] = "Second line\n";
113 
114     // Write first line
115     FILE *fptr = fopen("tempory_testfprintf.txt", "w");
116     if (fptr != NULL) {
117         fprintf(fptr, "%s", str1);
118         fclose(fptr);
119     }
120 
121     // Append second line
122     fptr = fopen("tempory_testfprintf.txt", "a");
123     size_t len = 0;
124     if (fptr != NULL) {
125         len = fprintf(fptr, "%s", str2);
126         fclose(fptr);
127     }
128 
129     EXPECT_EQ("fprintf_0600", len, strlen(str2));
130     (void)remove("tempory_testfprintf.txt");
131 }
132 
133 /**
134  * @tc.name:      fprintf_0700
135  * @tc.desc:      Verify fprintf with large string.
136  * @tc.level:     level 0.
137  */
fprintf_0700(void)138 void fprintf_0700(void)
139 {
140     char large_str[1024];
141     memset(large_str, 'A', sizeof(large_str)-1);
142     large_str[sizeof(large_str)-1] = '\0';
143 
144     size_t len = 0;
145     FILE *fptr = fopen("tempory_testfprintf.txt", "w");
146     if (fptr != NULL) {
147         len = fprintf(fptr, "%s", large_str);
148         fclose(fptr);
149     }
150 
151     EXPECT_EQ("fprintf_0700", len, strlen(large_str));
152     (void)remove("tempory_testfprintf.txt");
153 }
154 
155 /**
156  * @tc.name:      fprintf_0800
157  * @tc.desc:      Verify fprintf with special characters.
158  * @tc.level:     level 0.
159  */
fprintf_0800(void)160 void fprintf_0800(void)
161 {
162     char str[] = "Special chars: \t\n\r\\\"\'";
163     size_t len = 0;
164     FILE *fptr = fopen("tempory_testfprintf.txt", "w");
165     if (fptr != NULL) {
166         len = fprintf(fptr, "%s", str);
167         fclose(fptr);
168     }
169 
170     EXPECT_EQ("fprintf_0800", len, strlen(str));
171     (void)remove("tempory_testfprintf.txt");
172 }
173 
main(void)174 int main(void)
175 {
176     fprintf_0100();
177     fprintf_0200();
178     fprintf_0300();
179     fprintf_0400();
180     fprintf_0500();
181     fprintf_0600();
182     fprintf_0700();
183     fprintf_0800();
184     return t_status;
185 }
186