tdb2: make error numbers negative.
[ccan] / ccan / tdb2 / test / run-tdb_errorstr.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/transaction.c>
8 #include <ccan/tap/tap.h>
9
10 int main(int argc, char *argv[])
11 {
12         struct tdb_context *tdb;
13
14         plan_tests(1 + TDB_ERR_RDONLY*-1 + 2);
15         tdb = tdb_open("run-tdb_errorstr.tdb", TDB_DEFAULT,
16                        O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
17         ok1(tdb);
18         if (tdb) {
19                 enum TDB_ERROR err;
20                 for (err = TDB_SUCCESS; err >= TDB_ERR_RDONLY; err--) {
21                         tdb->ecode = err;
22                         switch (err) {
23                         case TDB_SUCCESS:
24                                 ok1(!strcmp(tdb_errorstr(tdb),
25                                             "Success"));
26                                 break;
27                         case TDB_ERR_IO:
28                                 ok1(!strcmp(tdb_errorstr(tdb),
29                                             "IO Error"));
30                                 break;
31                         case TDB_ERR_LOCK:
32                                 ok1(!strcmp(tdb_errorstr(tdb),
33                                             "Locking error"));
34                                 break;
35                         case TDB_ERR_OOM:
36                                 ok1(!strcmp(tdb_errorstr(tdb),
37                                             "Out of memory"));
38                                 break;
39                         case TDB_ERR_EXISTS:
40                                 ok1(!strcmp(tdb_errorstr(tdb),
41                                             "Record exists"));
42                                 break;
43                         case TDB_ERR_EINVAL:
44                                 ok1(!strcmp(tdb_errorstr(tdb),
45                                             "Invalid parameter"));
46                                 break;
47                         case TDB_ERR_NOEXIST:
48                                 ok1(!strcmp(tdb_errorstr(tdb),
49                                             "Record does not exist"));
50                                 break;
51                         case TDB_ERR_RDONLY:
52                                 ok1(!strcmp(tdb_errorstr(tdb),
53                                             "write not permitted"));
54                                 break;
55                         case TDB_ERR_CORRUPT:
56                                 ok1(!strcmp(tdb_errorstr(tdb),
57                                             "Corrupt database"));
58                         }
59                 }
60                 tdb->ecode = err;
61                 ok1(!strcmp(tdb_errorstr(tdb), "Invalid error code"));
62         }
63         return exit_status();
64 }