[PATCH] D56136: [compiler-rt] [sanitizer_common] Add tests for more stdio.h functions
Michał Górny via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 29 01:00:58 PST 2018
mgorny updated this revision to Diff 179679.
mgorny marked an inline comment as done.
mgorny added a comment.
Moved variable definitions, and added asserts.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D56136/new/
https://reviews.llvm.org/D56136
Files:
test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
Index: test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
@@ -0,0 +1,19 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+ FILE *fp = fopen(argv[0], "r");
+ assert(fp);
+
+ // the file should be at least one character long, always
+ assert(fgetc(fp) != EOF);
+ // POSIX guarantees being able to ungetc() at least one character
+ assert(ungetc('X', fp) != EOF);
+ // check whether ungetc() worked
+ assert(getc(fp) == 'X');
+
+ fclose(fp);
+ return 0;
+}
Index: test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
@@ -0,0 +1,40 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ FILE *fp = fopen(argv[0], "r");
+ assert(fp);
+
+ // file should be good upon opening
+ assert(!feof(fp) && !ferror(fp));
+
+ // read until EOF
+ char buf[BUFSIZ];
+ while (fread(buf, 1, sizeof buf, fp) != 0) {}
+ assert(feof(fp));
+
+ // clear EOF
+ clearerr(fp);
+ assert(!feof(fp) && !ferror(fp));
+
+ // get file descriptor
+ int fd = fileno(fp);
+ assert(fd != -1);
+
+ // break the file by closing underlying descriptor
+ assert(close(fd) != -1);
+
+ // verify that an error is signalled
+ assert(fread(buf, 1, sizeof buf, fp) == 0);
+ assert(ferror(fp));
+
+ // clear error
+ clearerr(fp);
+ assert(!feof(fp) && !ferror(fp));
+
+ fclose(fp);
+ return 0;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56136.179679.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181229/f74e12da/attachment.bin>
More information about the cfe-commits
mailing list