summaryrefslogtreecommitdiff
path: root/tests/stdio_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2017-10-30 17:47:12 -0700
committerElliott Hughes <enh@google.com>2017-10-31 13:23:41 -0700
commit37ad9597839c70a7ec79578e5072df9c189fc830 (patch)
treef1f70a143c99b072b040f5592f1b42da464b35f8 /tests/stdio_test.cpp
parente379a202504911d228ae07d9ba32c5a02274fe1d (diff)
More missing _unlocked <stdio.h> functions.
Also simplify trivial one-liners like perror/puts/fputs, and clean up fread/fwrite slightly. Fix perror to match POSIX. Add basic perror and *_unlocked tests. Bug: N/A Test: ran tests Change-Id: I63f83c8e0c15c3c4096509d17421ac331b6fc23d
Diffstat (limited to 'tests/stdio_test.cpp')
-rw-r--r--tests/stdio_test.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 7b7737d6d..97b222c48 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -1962,3 +1962,61 @@ TEST(STDIO_TEST, constants) {
ASSERT_LE(FILENAME_MAX, PATH_MAX);
ASSERT_EQ(L_tmpnam, PATH_MAX);
}
+
+TEST(STDIO_TEST, perror) {
+ ExecTestHelper eth;
+ eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
+ eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
+ eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
+}
+
+TEST(STDIO_TEST, puts) {
+ ExecTestHelper eth;
+ eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
+}
+
+TEST(STDIO_TEST, unlocked) {
+ TemporaryFile tf;
+
+ FILE* fp = fopen(tf.filename, "w+");
+ ASSERT_TRUE(fp != nullptr);
+
+ clearerr_unlocked(fp);
+ ASSERT_FALSE(feof_unlocked(fp));
+ ASSERT_FALSE(ferror_unlocked(fp));
+
+ ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
+
+ ASSERT_NE(EOF, putc_unlocked('a', fp));
+ ASSERT_NE(EOF, putc('b', fp));
+ ASSERT_NE(EOF, fputc_unlocked('c', fp));
+ ASSERT_NE(EOF, fputc('d', fp));
+
+ rewind(fp);
+ ASSERT_EQ('a', getc_unlocked(fp));
+ ASSERT_EQ('b', getc(fp));
+ ASSERT_EQ('c', fgetc_unlocked(fp));
+ ASSERT_EQ('d', fgetc(fp));
+
+ rewind(fp);
+ ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
+ ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
+ ASSERT_EQ(0, fflush_unlocked(fp));
+
+ rewind(fp);
+ char buf[BUFSIZ] = {};
+ ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
+ ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
+ ASSERT_STREQ("ABCD", buf);
+
+ rewind(fp);
+ ASSERT_NE(EOF, fputs("hello ", fp));
+ ASSERT_NE(EOF, fputs_unlocked("world", fp));
+ ASSERT_NE(EOF, fputc('\n', fp));
+
+ rewind(fp);
+ ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
+ ASSERT_STREQ("hello world\n", buf);
+
+ ASSERT_EQ(0, fclose(fp));
+}