summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-09-04 14:12:54 -0400
committerRich Felker <dalias@aerifal.cx>2011-09-04 14:12:54 -0400
commit23818fb196a33770d48a6b0bd1acf5aaae50bb63 (patch)
tree9e51141539f78a2a9b0e131345a08f4233f93cd8
parentfeb41271b88b03aecee2b38c91060f91ca526113 (diff)
downloadlibc-testsuite-23818fb196a33770d48a6b0bd1acf5aaae50bb63.tar.gz
test memory streams (incomplete)
-rw-r--r--memstream.c57
-rw-r--r--testsuite.c1
2 files changed, 58 insertions, 0 deletions
diff --git a/memstream.c b/memstream.c
new file mode 100644
index 0000000..99be094
--- /dev/null
+++ b/memstream.c
@@ -0,0 +1,57 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#define TEST(r, f, x, m) ( \
+((r) = (f)) == (x) || \
+(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )
+
+#define TEST_E(f) ( (errno = 0), (f) || \
+(printf(__FILE__ ":%d: %s failed (errno = %d)\n", __LINE__, #f, errno), err++, 0) )
+
+#define TEST_S(s, x, m) ( \
+!strcmp((s),(x)) || \
+(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
+
+#define TEST_M(s, x, n, m) ( \
+!memcmp((s),(x),(n)) || \
+(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )
+
+int test_memstream(void)
+{
+ int err = 0;
+ FILE *f;
+ char *s;
+ size_t l;
+ int i;
+
+ s = 0;
+ TEST_E(f = open_memstream(&s, &l));
+ TEST_E(putc('a', f) == 'a');
+ TEST_E(putc('b', f) == 'b');
+ TEST_E(putc('c', f) == 'c');
+ TEST_E(!fflush(f));
+ fclose(f);
+ if (s) TEST_S(s, "abc", "wrong output");
+ free(s);
+
+ s = 0;
+ TEST_E(f = open_memstream(&s, &l));
+ TEST_E(fseek(f,1,SEEK_CUR)>=0);
+ TEST_E(putc('q', f) == 'q');
+ TEST_E(!fflush(f));
+ if (s) TEST_M(s, "\0q", 3, "wrong output");
+ TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
+ TEST(i, errno, EINVAL, "%d != %d");
+ TEST(i, ftell(f), 2, "%d != %d");
+ TEST_E(fseek(f,-2,SEEK_CUR)>=0);
+ TEST_E(putc('e', f) == 'e');
+ TEST_E(!fflush(f));
+ if (s) TEST_S(s, "eq", "wrong output");
+ fclose(f);
+ free(s);
+
+ return err;
+}
diff --git a/testsuite.c b/testsuite.c
index 0ba976d..a4761cc 100644
--- a/testsuite.c
+++ b/testsuite.c
@@ -29,6 +29,7 @@ int main()
RUN_TEST(wcstol);
RUN_TEST(basename);
RUN_TEST(dirname);
+ RUN_TEST(memstream);
RUN_TEST(mbc);
RUN_TEST(sem);
RUN_TEST(pthread);