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