ctdb-tests: Add reqid wrapping test
[amitay/samba.git] / ctdb / tests / src / reqid_test.c
1 /*
2    reqid tests
3
4    Copyright (C) Amitay Isaacs  2015
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21
22 #include <assert.h>
23
24 #include "common/reqid.c"
25
26
27 int main(void)
28 {
29         struct reqid_context *reqid_ctx;
30         TALLOC_CTX *mem_ctx = talloc_new(NULL);
31         int i, ret;
32         uint32_t reqid;
33         int *data, *tmp;
34
35         ret = reqid_init(mem_ctx, INT_MAX-200, &reqid_ctx);
36         assert(ret == 0);
37
38         data = talloc_zero(mem_ctx, int);
39         assert(data != 0);
40
41         for (i=0; i<1024*1024; i++) {
42                 reqid = reqid_new(reqid_ctx, data);
43                 assert(reqid != -1);
44         }
45
46         for (i=0; i<1024; i++) {
47                 tmp = reqid_find(reqid_ctx, i, int);
48                 assert(tmp == data);
49         }
50
51         for (i=0; i<1024; i++) {
52                 ret = reqid_remove(reqid_ctx, i);
53                 assert(ret == 0);
54         }
55
56         for (i=0; i<1024; i++) {
57                 tmp = reqid_find(reqid_ctx, i, int);
58                 assert(tmp == NULL);
59         }
60
61         for (i=0; i<1024; i++) {
62                 ret = reqid_remove(reqid_ctx, i);
63                 assert(ret == ENOENT);
64         }
65
66         talloc_free(reqid_ctx);
67         assert(talloc_get_size(mem_ctx) == 0);
68
69         ret = reqid_init(mem_ctx, INT_MAX-1, &reqid_ctx);
70         assert(ret == 0);
71
72         reqid = reqid_new(reqid_ctx, data);
73         assert(reqid == INT_MAX);
74
75         reqid = reqid_new(reqid_ctx, data);
76         assert(reqid == 0);
77
78         reqid_remove(reqid_ctx, 0);
79
80         reqid = reqid_new(reqid_ctx, data);
81         assert(reqid == 1);
82
83         talloc_free(reqid_ctx);
84
85         talloc_free(mem_ctx);
86
87         return 0;
88 }