summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/stdio/test___freading.c
diff options
context:
space:
mode:
authoryasuoka <>2025-05-25 00:20:54 +0000
committeryasuoka <>2025-05-25 00:20:54 +0000
commit05d2a9c66d6d77326bcdda5607d6a1cc119a16da (patch)
treec57eee305f3da335d6fa3adb48553f06b3da3f7c /src/regress/lib/libc/stdio/test___freading.c
parent9e8330b94fd53de40ec3bf3d580cedb3dcbe0131 (diff)
downloadopenbsd-05d2a9c66d6d77326bcdda5607d6a1cc119a16da.tar.gz
openbsd-05d2a9c66d6d77326bcdda5607d6a1cc119a16da.tar.bz2
openbsd-05d2a9c66d6d77326bcdda5607d6a1cc119a16da.zip
Add tests for the functions in <stdio_ext.h>.
Diffstat (limited to 'src/regress/lib/libc/stdio/test___freading.c')
-rw-r--r--src/regress/lib/libc/stdio/test___freading.c85
1 files changed, 85 insertions, 0 deletions
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 @@
1/* $OpenBSD: test___freading.c,v 1.1 2025/05/25 00:20:54 yasuoka Exp $ */
2
3/*
4 * Copyright (c) 2025 YASUOKA Masahiko <yasuoka@yasuoka.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <assert.h>
20#include <stdio.h>
21#include <stdio_ext.h>
22#include <stdlib.h>
23#include <string.h>
24
25/* we use assert() */
26#undef NDEBUG
27
28#define TMPFILENAME "test___freading.tmp"
29
30void test___freading0(void);
31void test___freading1(void);
32
33void
34test___freading0(void)
35{
36 FILE *fp;
37 int r;
38 char buf[80];
39
40 fp = popen("echo Hello world", "r");
41 assert(fp != NULL);
42 assert(__freading(fp) != 0);
43 assert(fgets(buf, sizeof(buf), fp) != NULL);
44 assert(strcmp(buf, "Hello world\n") == 0);
45 r = pclose(fp);
46 assert(r == 0);
47}
48
49void
50test___freading1(void)
51{
52 FILE *fp;
53 int r;
54
55 /* when the last operaiton is read, __freading() returns true */
56 fp = fopen(TMPFILENAME, "w+");
57 assert(fp != NULL);
58 assert(__freading(fp) == 0);
59 r = fputs("Hello world\n", fp);
60 assert(r >= 0);
61 assert(__freading(fp) == 0);
62 rewind(fp);
63 assert(fgetc(fp) == 'H');
64 assert(__freading(fp) != 0);
65 /* write */
66 fseek(fp, 0, SEEK_END);
67 r = fputs("\n", fp);
68 assert(__freading(fp) == 0);
69 /* ungetc */
70 rewind(fp);
71 assert(ungetc('X', fp) != 0);
72 assert(__freading(fp) != 0); /* reading */
73
74 r = fclose(fp);
75 assert(r == 0);
76}
77
78int
79main(int argc, char *argv[])
80{
81 test___freading0();
82 test___freading1();
83
84 exit(0);
85}