r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[metze/samba/wip.git] / source4 / torture / local / idtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of idtree routines.
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25
26 static bool torture_local_idtree_simple(struct torture_context *tctx)
27 {
28         struct idr_context *idr;
29         int i;
30         int *ids;
31         int *present;
32         extern int torture_numops;
33         int n = torture_numops;
34         TALLOC_CTX *mem_ctx = tctx;
35
36         idr = idr_init(mem_ctx);
37
38         ids = talloc_zero_array(mem_ctx, int, n);
39         present = talloc_zero_array(mem_ctx, int, n);
40
41         for (i=0;i<n;i++) {
42                 ids[i] = -1;
43         }
44
45         for (i=0;i<n;i++) {
46                 int ii = random() % n;
47                 void *p = idr_find(idr, ids[ii]);
48                 if (present[ii]) {
49                         if (p != &ids[ii]) {
50                                 torture_fail(tctx, talloc_asprintf(tctx, 
51                                                 "wrong ptr at %d - %p should be %p", 
52                                        ii, p, &ids[ii]));
53                         }
54                         if (random() % 7 == 0) {
55                                 if (idr_remove(idr, ids[ii]) != 0) {
56                                         torture_fail(tctx, talloc_asprintf(tctx,
57                                                 "remove failed at %d (id=%d)", 
58                                                i, ids[ii]));
59                                 }
60                                 present[ii] = 0;
61                                 ids[ii] = -1;
62                         }
63                 } else {
64                         if (p != NULL) {
65                                 torture_fail(tctx, talloc_asprintf(tctx,
66                                         "non-present at %d gave %p (would be %d)", 
67                                        ii, p, 
68                                        (int)(((char *)p) - (char *)(&ids[0])) / sizeof(int)));
69                         }
70                         if (random() % 5) {
71                                 ids[ii] = idr_get_new(idr, &ids[ii], n);
72                                 if (ids[ii] < 0) {
73                                         torture_fail(tctx, talloc_asprintf(tctx,
74                                                 "alloc failure at %d (ret=%d)", 
75                                                ii, ids[ii]));
76                                 } else {
77                                         present[ii] = 1;
78                                 }
79                         }
80                 }
81         }
82
83         torture_comment(tctx, "done %d random ops\n", i);
84
85         for (i=0;i<n;i++) {
86                 if (present[i]) {
87                         if (idr_remove(idr, ids[i]) != 0) {
88                                 torture_fail(tctx, talloc_asprintf(tctx,
89                                                 "delete failed on cleanup at %d (id=%d)", 
90                                        i, ids[i]));
91                         }
92                 }
93         }
94
95         torture_comment(tctx, "cleaned up\n");
96         return true;
97 }
98
99 struct torture_suite *torture_local_idtree(TALLOC_CTX *mem_ctx)
100 {
101         struct torture_suite *suite = torture_suite_create(mem_ctx, "IDTREE");
102         torture_suite_add_simple_test(suite, "idtree", torture_local_idtree_simple);
103         return suite;
104 }