]> git.ozlabs.org Git - ccan/blob - ccan/noerr/test/run.c
tdb2: implement tdb_chainlock_read/tdb_chainunlock_read.
[ccan] / ccan / noerr / test / run.c
1 #include <ccan/noerr/noerr.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/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         char *name = "noerr.file";
13         int fd;
14         FILE *fp;
15
16         plan_tests(15);
17         /* Should fail to unlink. */
18         ok1(unlink(name) != 0);
19         ok1(errno == ENOENT);
20
21         /* This one should not set errno. */
22         errno = 100;
23         ok1(unlink_noerr(name) == ENOENT);
24         ok1(errno == 100);
25
26         /* Should fail to close. */
27         ok1(close(-1) != 0);
28         ok1(errno == EBADF);
29
30         /* This one should not set errno. */
31         errno = 100;
32         ok1(close_noerr(-1) == EBADF);
33         ok1(errno == 100);
34
35         /* Test successful close/unlink doesn't hit errno either. */
36         fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600);
37         assert(fd >= 0);
38
39         errno = 100;
40         ok1(close_noerr(fd) == 0);
41         ok1(errno == 100);
42
43         errno = 100;
44         ok1(unlink_noerr(name) == 0);
45         ok1(errno == 100);
46
47         /* Test failing fclose */
48         fp = fopen(name, "wb");
49         assert(fp);
50         close(fileno(fp));
51         ok1(fclose_noerr(fp) == EBADF);
52
53         /* Test successful fclose */
54         fp = fopen(name, "wb");
55         assert(fp);
56
57         errno = 100;
58         ok1(fclose_noerr(fp) == 0);
59         ok1(errno == 100);
60         unlink(name);
61
62         return exit_status();
63 }