Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Posix / feof_fileno_ferror.cpp
blobb8c1a5f04575fda70ecb1df5966ef982c95b5298
1 // RUN: %clangxx -g %s -o %t && %run %t
3 // Newer versions of Android have FDSan, and will fail this test as FDSan will
4 // catch the error instead.
5 // UNSUPPORTED: android
7 #include <assert.h>
8 #include <stdio.h>
9 #include <unistd.h>
11 int main(int argc, char **argv) {
12 FILE *fp = fopen(argv[0], "r");
13 assert(fp);
15 // file should be good upon opening
16 assert(!feof(fp) && !ferror(fp));
18 // read until EOF
19 char buf[BUFSIZ];
20 while (fread(buf, 1, sizeof buf, fp) != 0) {}
21 assert(feof(fp));
23 // clear EOF
24 clearerr(fp);
25 assert(!feof(fp) && !ferror(fp));
27 // get file descriptor
28 int fd = fileno(fp);
29 assert(fd != -1);
31 // break the file by closing underlying descriptor
32 assert(close(fd) != -1);
34 // verify that an error is signalled
35 assert(fread(buf, 1, sizeof buf, fp) == 0);
36 assert(ferror(fp));
38 // clear error
39 clearerr(fp);
40 assert(!feof(fp) && !ferror(fp));
42 // fclose() will return EBADF because of closed fd
43 assert(fclose(fp) == -1);
44 return 0;