From ba8ffad16ca3187163f8568895cd4524aa158559 Mon Sep 17 00:00:00 2001 From: yasuoka <> Date: Sun, 25 May 2025 00:20:54 +0000 Subject: Add tests for the functions in . --- src/regress/lib/libc/stdio/Makefile | 22 ++++++- src/regress/lib/libc/stdio/test___fpending.c | 58 ++++++++++++++++++ src/regress/lib/libc/stdio/test___freadahead.c | 71 +++++++++++++++++++++ src/regress/lib/libc/stdio/test___freading.c | 85 ++++++++++++++++++++++++++ src/regress/lib/libc/stdio/test___freadptr.c | 78 +++++++++++++++++++++++ src/regress/lib/libc/stdio/test___fseterr.c | 60 ++++++++++++++++++ src/regress/lib/libc/stdio/test___fwriting.c | 83 +++++++++++++++++++++++++ 7 files changed, 455 insertions(+), 2 deletions(-) create mode 100644 src/regress/lib/libc/stdio/test___fpending.c create mode 100644 src/regress/lib/libc/stdio/test___freadahead.c create mode 100644 src/regress/lib/libc/stdio/test___freading.c create mode 100644 src/regress/lib/libc/stdio/test___freadptr.c create mode 100644 src/regress/lib/libc/stdio/test___fseterr.c create mode 100644 src/regress/lib/libc/stdio/test___fwriting.c (limited to 'src') diff --git a/src/regress/lib/libc/stdio/Makefile b/src/regress/lib/libc/stdio/Makefile index ee865013dd..3f2c2bab8e 100644 --- a/src/regress/lib/libc/stdio/Makefile +++ b/src/regress/lib/libc/stdio/Makefile @@ -1,8 +1,26 @@ -# $OpenBSD: Makefile,v 1.1 2025/05/24 11:07:20 yasuoka Exp $ +# $OpenBSD: Makefile,v 1.2 2025/05/25 00:20:54 yasuoka Exp $ PROGS= test_fflush - CLEANFILES= test_fflush.tmp + +PROGS+= test___freading +CLEANFILES+= test___freading.tmp + +PROGS+= test___fwriting +CLEANFILES+= test___fwriting.tmp + +PROGS+= test___fpending +CLEANFILES+= test___fpending.tmp + +PROGS+= test___freadahead +CLEANFILES+= test___freadahead.tmp + +PROGS+= test___freadptr +CLEANFILES+= test___freadptr.tmp + +PROGS+= test___fseterr +CLEANFILES+= test___fseterr.tmp + WARNINGS= yes # until fflush comply POSIX-2008 diff --git a/src/regress/lib/libc/stdio/test___fpending.c b/src/regress/lib/libc/stdio/test___fpending.c new file mode 100644 index 0000000000..96ace2e481 --- /dev/null +++ b/src/regress/lib/libc/stdio/test___fpending.c @@ -0,0 +1,58 @@ +/* $OpenBSD: test___fpending.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___fpending.tmp" + +void test___fpending0(void); + +void +test___fpending0(void) +{ + FILE *fp; + int r; + size_t s; + + fp = fopen(TMPFILENAME, "w"); + assert(fp != NULL); + r = fputs("Hello world", fp); + assert(r >= 0); + s = __fpending(fp); + assert(s > 0); /* assume buffered */ + r = fflush(fp); + assert(r == 0); + s = __fpending(fp); + assert(s == 0); /* buffer must be 0 */ + r = fclose(fp); + assert(r == 0); +} + +int +main(int argc, char *argv[]) +{ + test___fpending0(); + + exit(0); +} diff --git a/src/regress/lib/libc/stdio/test___freadahead.c b/src/regress/lib/libc/stdio/test___freadahead.c new file mode 100644 index 0000000000..c853e23975 --- /dev/null +++ b/src/regress/lib/libc/stdio/test___freadahead.c @@ -0,0 +1,71 @@ +/* $OpenBSD: test___freadahead.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___freadahead.tmp" + +void test___freadahead0(void); + +void +test___freadahead0(void) +{ + FILE *fp; + int r; + size_t s; + + fp = fopen(TMPFILENAME, "w"); + assert(fp != NULL); + r = fputs("Hello world", fp); + assert(r >= 0); + r = fclose(fp); + + fp = fopen(TMPFILENAME, "r"); + s = __freadahead(fp); + assert(s == 0); + assert(fgetc(fp) == 'H'); + s = __freadahead(fp); + assert(s == 10); + r = fflush(fp); +#if 1 + /* fflush() to reading file is not supported (yet) */ + assert(errno == EBADF); +#else + assert(r == 0); + s = __freadahead(fp); + assert(s == 0); +#endif + + r = fclose(fp); + assert(r == 0); +} + +int +main(int argc, char *argv[]) +{ + test___freadahead0(); + + exit(0); +} diff --git a/src/regress/lib/libc/stdio/test___freading.c b/src/regress/lib/libc/stdio/test___freading.c new file mode 100644 index 0000000000..05716f3a38 --- /dev/null +++ b/src/regress/lib/libc/stdio/test___freading.c @@ -0,0 +1,85 @@ +/* $OpenBSD: test___freading.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___freading.tmp" + +void test___freading0(void); +void test___freading1(void); + +void +test___freading0(void) +{ + FILE *fp; + int r; + char buf[80]; + + fp = popen("echo Hello world", "r"); + assert(fp != NULL); + assert(__freading(fp) != 0); + assert(fgets(buf, sizeof(buf), fp) != NULL); + assert(strcmp(buf, "Hello world\n") == 0); + r = pclose(fp); + assert(r == 0); +} + +void +test___freading1(void) +{ + FILE *fp; + int r; + + /* when the last operaiton is read, __freading() returns true */ + fp = fopen(TMPFILENAME, "w+"); + assert(fp != NULL); + assert(__freading(fp) == 0); + r = fputs("Hello world\n", fp); + assert(r >= 0); + assert(__freading(fp) == 0); + rewind(fp); + assert(fgetc(fp) == 'H'); + assert(__freading(fp) != 0); + /* write */ + fseek(fp, 0, SEEK_END); + r = fputs("\n", fp); + assert(__freading(fp) == 0); + /* ungetc */ + rewind(fp); + assert(ungetc('X', fp) != 0); + assert(__freading(fp) != 0); /* reading */ + + r = fclose(fp); + assert(r == 0); +} + +int +main(int argc, char *argv[]) +{ + test___freading0(); + test___freading1(); + + exit(0); +} diff --git a/src/regress/lib/libc/stdio/test___freadptr.c b/src/regress/lib/libc/stdio/test___freadptr.c new file mode 100644 index 0000000000..cce362f2ae --- /dev/null +++ b/src/regress/lib/libc/stdio/test___freadptr.c @@ -0,0 +1,78 @@ +/* $OpenBSD: test___freadptr.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___freadptr.tmp" + +void test___freadptr0(void); + +/* test __freadptr() and __freadptrinc() */ +void +test___freadptr0(void) +{ + FILE *fp; + int r; + ssize_t s; + const char *p; + + fp = fopen(TMPFILENAME, "w"); + assert(fp != NULL); + r = fputs("Hello world", fp); + assert(r >= 0); + r = fclose(fp); + + fp = fopen(TMPFILENAME, "r"); + assert(fgetc(fp) == 'H'); + p = __freadptr(fp, &s); + assert(p != NULL); + assert(s > 4); /* this test assume this (not by the spec) */ + assert(*p == 'e'); + assert(strncmp(p, "ello world", s) == 0); + + __freadptrinc(fp, 4); + assert(fgetc(fp) == ' '); + + ungetc('A', fp); + ungetc('A', fp); + ungetc('A', fp); + p = __freadptr(fp, &s); + assert(s > 0); + assert(*p == 'A'); + /* ptr will contains only the pushback buffer */ + assert(strncmp(p, "AAAworld", s) == 0); + + r = fclose(fp); + assert(r == 0); +} + +int +main(int argc, char *argv[]) +{ + test___freadptr0(); + + exit(0); +} diff --git a/src/regress/lib/libc/stdio/test___fseterr.c b/src/regress/lib/libc/stdio/test___fseterr.c new file mode 100644 index 0000000000..70fb491c6c --- /dev/null +++ b/src/regress/lib/libc/stdio/test___fseterr.c @@ -0,0 +1,60 @@ +/* $OpenBSD: test___fseterr.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___fseterr.tmp" + +void test___fseterr0(void); + +void +test___fseterr0(void) +{ + FILE *fp; + int r; + + fp = fopen(TMPFILENAME, "w+"); + assert(fp != NULL); + + assert(!ferror(fp)); + + r = fprintf(fp, "hello world\n"); + assert(r > 0); + + __fseterr(fp); + assert(ferror(fp)); + + r = fprintf(fp, "hello world\n"); + assert(r == -1); + + fclose(fp); +} + +int +main(int argc, char *argv[]) +{ + test___fseterr0(); + + exit(0); +} diff --git a/src/regress/lib/libc/stdio/test___fwriting.c b/src/regress/lib/libc/stdio/test___fwriting.c new file mode 100644 index 0000000000..eb4671d3cf --- /dev/null +++ b/src/regress/lib/libc/stdio/test___fwriting.c @@ -0,0 +1,83 @@ +/* $OpenBSD: test___fwriting.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */ + +/* + * Copyright (c) 2025 YASUOKA Masahiko + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +/* we use assert() */ +#undef NDEBUG + +#define TMPFILENAME "test___fwriting.tmp" + +void test___fwriting0(void); +void test___fwriting1(void); + +void +test___fwriting0(void) +{ + FILE *fp; + int r; + + fp = fopen(TMPFILENAME, "w"); /* write only */ + assert(fp != NULL); + assert(__fwriting(fp) != 0); /* writing is true immediately */ + r = fputs("Hello world\n", fp); + assert(r >= 0); + r = fclose(fp); + assert(r == 0); + + fp = fopen(TMPFILENAME, "a"); /* append only */ + assert(fp != NULL); + assert(__fwriting(fp) != 0); /* writing immediately */ + r = fclose(fp); + assert(r == 0); +} + +void +test___fwriting1(void) +{ + FILE *fp; + int r; + + fp = fopen(TMPFILENAME, "w+"); /* read / write */ + assert(fp != NULL); + r = fputs("Hello world\n", fp); + assert(r >= 0); + assert(__fwriting(fp) != 0); + rewind(fp); + assert(fgetc(fp) == 'H'); /* read */ + assert(__fwriting(fp) == 0); /* writing becomes false */ + fputc('e', fp); + assert(__fwriting(fp) != 0); /* writing becomes true */ + ungetc('e', fp); + assert(__fwriting(fp) == 0); /* ungetc -> writing becomes false */ + + r = fclose(fp); + assert(r == 0); +} + +int +main(int argc, char *argv[]) +{ + test___fwriting0(); + test___fwriting1(); + + exit(0); +} -- cgit v1.2.3-55-g6feb