]> git.ozlabs.org Git - ccan/blob - ccan/noerr/test/run.c
merge
[ccan] / ccan / noerr / test / run.c
1 #include "noerr/noerr.h"
2 #include "tap/tap.h"
3 #include "noerr/noerr.c"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <assert.h>
9
10 int main(int argc, char *argv[])
11 {
12         /* tempnam(3) is generally a bad idea, but OK here. */
13         char *name = tempnam(NULL, "noerr");
14         int fd;
15         FILE *fp;
16
17         plan_tests(15);
18         /* Should fail to unlink. */
19         ok1(unlink(name) != 0);
20         ok1(errno == ENOENT);
21
22         /* This one should not set errno. */
23         errno = 100;
24         ok1(unlink_noerr(name) == ENOENT);
25         ok1(errno == 100);
26
27         /* Should fail to close. */
28         ok1(close(-1) != 0);
29         ok1(errno == EBADF);
30
31         /* This one should not set errno. */
32         errno = 100;
33         ok1(close_noerr(-1) == EBADF);
34         ok1(errno == 100);
35
36         /* Test successful close/unlink doesn't hit errno either. */
37         fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600);
38         assert(fd >= 0);
39
40         errno = 100;
41         ok1(close_noerr(fd) == 0);
42         ok1(errno == 100);
43
44         errno = 100;
45         ok1(unlink_noerr(name) == 0);
46         ok1(errno == 100);
47
48         /* Test failing fclose */
49         fp = fopen(name, "wb");
50         assert(fp);
51         close(fileno(fp));
52         ok1(fclose_noerr(fp) == EBADF);
53
54         /* Test successful fclose */
55         fp = fopen(name, "wb");
56         assert(fp);
57
58         errno = 100;
59         ok1(fclose_noerr(fp) == 0);
60         ok1(errno == 100);
61
62         return exit_status();
63 }