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 <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20 
21 const int32_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     int32_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     int32_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     int32_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 
main(void)82 int main(void)
83 {
84     fprintf_0100();
85     fprintf_0200();
86     fprintf_0300();
87     fprintf_0400();
88     return t_status;
89 }
90