|
nsz |
0c7d46 |
#include <stdlib.h>
|
|
nsz |
0c7d46 |
#include <unistd.h>
|
|
nsz |
0c7d46 |
#include <stdio.h>
|
|
nsz |
0c7d46 |
#include <errno.h>
|
|
nsz |
0c7d46 |
#include <string.h>
|
|
nsz |
0c7d46 |
#include "test.h"
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
#define TEST(r, f, x, m) ( \
|
|
nsz |
0c7d46 |
((r) = (f)) == (x) || \
|
|
nsz |
0c7d46 |
(error("%s failed (" m ")\n", #f, r, x), 0) )
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
#define TEST_E(f) ( (errno = 0), (f) || \
|
|
nsz |
0c7d46 |
(error("%s failed (errno = %d)\n", #f, errno), 0) )
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
#define TEST_S(s, x, m) ( \
|
|
nsz |
0c7d46 |
!strcmp((s),(x)) || \
|
|
nsz |
0c7d46 |
(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
#define TEST_M(s, x, n, m) ( \
|
|
nsz |
0c7d46 |
!memcmp((s),(x),(n)) || \
|
|
nsz |
0c7d46 |
(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
void test_memstream(void)
|
|
nsz |
0c7d46 |
{
|
|
nsz |
0c7d46 |
FILE *f;
|
|
nsz |
0c7d46 |
char *s;
|
|
nsz |
0c7d46 |
size_t l;
|
|
nsz |
0c7d46 |
char buf[100];
|
|
nsz |
0c7d46 |
int i;
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
s = 0;
|
|
nsz |
0c7d46 |
TEST_E(f = open_memstream(&s, &l);;
|
|
nsz |
0c7d46 |
TEST_E(putc('a', f) == 'a');
|
|
nsz |
0c7d46 |
TEST_E(putc('b', f) == 'b');
|
|
nsz |
0c7d46 |
TEST_E(putc('c', f) == 'c');
|
|
nsz |
0c7d46 |
TEST_E(!fflush(f));
|
|
nsz |
0c7d46 |
fclose(f);
|
|
nsz |
0c7d46 |
if (s) TEST_S(s, "abc", "wrong output");
|
|
nsz |
0c7d46 |
free(s);
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
s = 0;
|
|
nsz |
0c7d46 |
TEST_E(f = open_memstream(&s, &l);;
|
|
nsz |
0c7d46 |
TEST_E(fseek(f,1,SEEK_CUR)>=0);
|
|
nsz |
0c7d46 |
TEST_E(putc('q', f) == 'q');
|
|
nsz |
0c7d46 |
TEST_E(!fflush(f));
|
|
nsz |
0c7d46 |
if (s) TEST_M(s, "\0q", 3, "wrong output");
|
|
nsz |
0c7d46 |
TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
|
|
nsz |
0c7d46 |
TEST(i, errno, EINVAL, "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 2, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_E(fseek(f,-2,SEEK_CUR)>=0);
|
|
nsz |
0c7d46 |
TEST_E(putc('e', f) == 'e');
|
|
nsz |
0c7d46 |
TEST_E(!fflush(f));
|
|
nsz |
0c7d46 |
if (s) TEST_S(s, "eq", "wrong output");
|
|
nsz |
0c7d46 |
fclose(f);
|
|
nsz |
0c7d46 |
free(s);
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
TEST_E(f = fmemopen(buf, 10, "r+"));
|
|
nsz |
0c7d46 |
TEST_E(fputs("hello", f) >= 0);
|
|
nsz |
0c7d46 |
TEST_E(fputc(0, f)==0);
|
|
nsz |
0c7d46 |
TEST_E(fseek(f, 0, SEEK_SET)>=0);
|
|
nsz |
0c7d46 |
i=0;
|
|
nsz |
0c7d46 |
TEST_E(fscanf(f, "hello%n", &i)==0);
|
|
nsz |
0c7d46 |
TEST(i, i, 5, "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 5, "%d != %d");
|
|
nsz |
0c7d46 |
errno = 0;
|
|
nsz |
0c7d46 |
TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, "");
|
|
nsz |
0c7d46 |
TEST(i, errno!=0, 1, "");
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 5, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_S(buf, "hello", "");
|
|
nsz |
0c7d46 |
fclose(f);
|
|
nsz |
0c7d46 |
|
|
nsz |
0c7d46 |
TEST_E(f = fmemopen(buf, 10, "a+"));
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 5, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_E(fseek(f, 0, SEEK_SET)>=0);
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'h', "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'e', "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'l', "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'l', "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'o', "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), EOF, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_E(fseek(f, 6, SEEK_SET)>=0);
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 6, "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, getc(f), EOF, "%d != %d");
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 6, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_E(fseek(f, 0, SEEK_SET)>=0);
|
|
nsz |
0c7d46 |
TEST(i, getc(f), 'h', "%d != %d");
|
|
nsz |
0c7d46 |
TEST_E(fseek(f, 0, SEEK_CUR)>=0);
|
|
nsz |
0c7d46 |
buf[7] = 'x';
|
|
nsz |
0c7d46 |
TEST_E(fprintf(f, "%d", i)==3);
|
|
nsz |
0c7d46 |
TEST_E(fflush(f)==0);
|
|
nsz |
0c7d46 |
TEST(i, ftell(f), 8, "%d != %d");
|
|
nsz |
0c7d46 |
TEST_S(buf, "hello104", "");
|
|
nsz |
0c7d46 |
fclose(f);
|
|
nsz |
0c7d46 |
}
|