diff options
author | Elliott Hughes <enh@google.com> | 2014-08-20 16:10:49 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2014-08-20 17:03:46 -0700 |
commit | 6b841db2baa24ffcf2a4e5f975d1d07f1699b918 (patch) | |
tree | af58fa1f4b5fa777f47e9ef797c50cff964b7046 /tests/stdio_test.cpp | |
parent | b6ed54076abdd337150d7a92a661247b69d26cb4 (diff) |
Add POSIX-2008 fmemopen, open_memstream, and open_wmemstream.
Bug: 17164505
Change-Id: I59e28a08ff8b6ab632230b11a5807cfd5278aeb5
Diffstat (limited to 'tests/stdio_test.cpp')
-rw-r--r-- | tests/stdio_test.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index bb86509c2..2cd0df25a 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -676,3 +676,82 @@ TEST(stdio, fpos_t_and_seek) { fclose(fp); } + +TEST(stdio, fmemopen) { + char buf[16]; + memset(buf, 0, sizeof(buf)); + FILE* fp = fmemopen(buf, sizeof(buf), "r+"); + ASSERT_EQ('<', fputc('<', fp)); + ASSERT_NE(EOF, fputs("abc>\n", fp)); + fflush(fp); + + ASSERT_STREQ("<abc>\n", buf); + + rewind(fp); + + char line[16]; + char* s = fgets(line, sizeof(line), fp); + ASSERT_TRUE(s != NULL); + ASSERT_STREQ("<abc>\n", s); + + fclose(fp); +} + +TEST(stdio, fmemopen_NULL) { + FILE* fp = fmemopen(nullptr, 128, "r+"); + ASSERT_NE(EOF, fputs("xyz\n", fp)); + + rewind(fp); + + char line[16]; + char* s = fgets(line, sizeof(line), fp); + ASSERT_TRUE(s != NULL); + ASSERT_STREQ("xyz\n", s); + + fclose(fp); +} + +TEST(stdio, fmemopen_EINVAL) { + char buf[16]; + + // Invalid size. + errno = 0; + ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+")); + ASSERT_EQ(EINVAL, errno); + + // No '+' with NULL buffer. + errno = 0; + ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r")); + ASSERT_EQ(EINVAL, errno); +} + +TEST(stdio, open_memstream) { + char* p = nullptr; + size_t size = 0; + FILE* fp = open_memstream(&p, &size); + ASSERT_NE(EOF, fputs("hello, world!", fp)); + fclose(fp); + + ASSERT_STREQ("hello, world!", p); + ASSERT_EQ(strlen("hello, world!"), size); + free(p); +} + +TEST(stdio, open_memstream_EINVAL) { +#if defined(__BIONIC__) + char* p; + size_t size; + + // Invalid buffer. + errno = 0; + ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); + ASSERT_EQ(EINVAL, errno); + + // Invalid size. + errno = 0; + ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); + ASSERT_EQ(EINVAL, errno); +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +} |