Windows: remove trailing whitespace
[openafs.git] / src / WINNT / afsd / test / btreetest.c
1 #include<windows.h>
2 #include<stdio.h>
3 #include<wchar.h>
4 #include<strsafe.h>
5
6 #include "..\afsd.h"
7
8 #define paste(a,b) a ## b
9 #define _L(a) paste(L,a)
10
11 #define TRACE1 wprintf
12
13 /* Setup and Fakery ... */
14 osi_log_t * afsd_logp;
15
16 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
17 {
18     fidp->cell = cell;
19     fidp->volume = volume;
20     fidp->vnode = vnode;
21     fidp->unique = unique;
22     fidp->hash = ((cell & 0xF) << 28) | ((volume & 0x3F) << 22) | ((vnode & 0x7FF) << 11) | (unique & 0x7FF);
23 }
24
25 cm_scache_t *cm_FindSCache(cm_fid_t *fidp)
26 {
27     return NULL;
28 }
29
30 void cm_ReleaseSCache(cm_scache_t *scp)
31 {
32 }
33
34
35 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
36                  osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
37                  cm_scache_t **retscp)
38 {
39     return 0;
40 }
41
42 void
43 afsi_log(char *pattern, ...)
44 {
45     char s[256], t[100], d[100], u[512];
46     va_list ap;
47     va_start(ap, pattern);
48
49     StringCbVPrintfA(s, sizeof(s), pattern, ap);
50     GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
51     GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
52     StringCbPrintfA(u, sizeof(u), "%s %s: %s\r\n", d, t, s);
53     printf("%s", u);
54 }
55
56
57 static int initialize_tests(void)
58 {
59     osi_Init();
60     cm_InitNormalization();
61
62     if (!cm_InitBPlusDir()) {
63         TRACE1(L"Can't initialize BPlusDir\n");
64         return 1;
65     }
66
67     afsd_logp = osi_LogCreate("fakelog", 100);
68
69     return 0;
70 }
71
72 int n_succeeded = 0;
73 int n_failed = 0;
74
75 int n_subSucceeded = 0;
76 int n_subFailed = 0;
77
78 static int runtest(wchar_t * testname, int (*f)(void))
79 {
80     int rv = 0;
81
82     n_subSucceeded = 0;
83     n_subFailed = 0;
84
85     TRACE1(L"Begin test %s\n", testname);
86     f();
87     TRACE1(L"End test %s\n", testname);
88     TRACE1(L"Subtests Succeeded %d, Failed %d\n", n_subSucceeded, n_subFailed);
89
90     if (n_subFailed)
91         n_failed++;
92     else
93         n_succeeded++;
94
95     return rv;
96 }
97
98 #define RUNTEST(f) runtest(_L(#f), f)
99
100 #define IS_NOT_NULL(v) (((v) != NULL)? n_subSucceeded++: (TRACE1(L"Failed %s\n", _L(#v)), n_subFailed++))
101 #define IS(e) ((e)? n_subSucceeded++ : (TRACE1(L"Failed %s\n", _L(#e)), n_subFailed++))
102 #define CHECK_IF(e) do { if (e) { n_subSucceeded++; } else { TRACE1(L"Failed %s\n", _L(#e)); n_subFailed++; return 1; }} while (0)
103
104 /**************************************************************/
105 /* Actual tests */
106
107 struct strings {
108     const fschar_t * str;
109     const clientchar_t * lookup;
110     int rc;
111 };
112
113 struct strings simple[] = {
114     {"abc", L"ABC", CM_ERROR_INEXACT_MATCH},
115     {"A", L"A", 0},
116     {".", L".", 0},
117     {"567", L"567", 0},
118     {"b", L"B", CM_ERROR_INEXACT_MATCH},
119     {"d", L"D", CM_ERROR_INEXACT_MATCH},
120     {"àáâ", L"\x00c0\x00c1\x00c2", CM_ERROR_INEXACT_MATCH},
121     {"Ŷ", L"\x0177", CM_ERROR_INEXACT_MATCH},
122     {"a\xef\xac\xb4",L"a\xfb34",0},
123     {"b\xd7\x94\xd6\xbc",L"b\xfb34",0},
124     {"c\xef\xac\xb4",L"c\x05d4\x05bc",0},
125     {"d\xd7\x94\xd6\xbc",L"d\x05d4\x05bc",0},
126 };
127
128 void init_scache(cm_scache_t * scp, cm_fid_t * fidp)
129 {
130     memset(scp, 0, sizeof(cm_scache_t));
131     scp->magic = CM_SCACHE_MAGIC;
132     lock_InitializeRWLock(&scp->rw, "cm_scache_t rw");
133     lock_InitializeRWLock(&scp->bufCreateLock, "cm_scache_t bufCreateLock");
134     lock_InitializeRWLock(&scp->dirlock, "cm_scache_t dirlock");
135     scp->serverLock = -1;
136     scp->fid = *fidp;
137     scp->refCount = 1;
138 }
139
140 int simple_test(void)
141 {
142     Tree * t;
143     int i;
144
145     t = initBtree(64, MAX_FANOUT, cm_BPlusCompareNormalizedKeys);
146     CHECK_IF(t != NULL);
147
148     for (i=0; i < lengthof(simple); i++) {
149         normchar_t * norm;
150         keyT key;
151         dataT data;
152
153         norm = cm_FsStringToNormStringAlloc(simple[i].str, -1, NULL);
154         CHECK_IF(norm != NULL);
155
156         key.name = norm;
157
158         data.cname = cm_FsStringToClientStringAlloc(simple[i].str, -1, NULL);
159         data.fsname = cm_FsStrDup(simple[i].str);
160         data.shortform = FALSE;
161         cm_SetFid(&data.fid, 1, 2, i, 4);
162
163         insert(t, key, data);
164
165         if (!cm_Is8Dot3(data.cname)) {
166             wchar_t wshortName[13];
167             cm_dirFid_t dfid;
168
169             dfid.vnode = i;
170             dfid.unique = 0;
171
172             cm_Gen8Dot3NameIntW(data.cname, &dfid, wshortName, NULL);
173
174             key.name = wshortName;
175             data.cname = cm_FsStringToClientStringAlloc(simple[i].str, -1, NULL);
176             data.fsname = cm_FsStrDup(simple[i].str);
177             data.shortform = TRUE;
178
179             insert(t, key, data);
180         }
181
182         free(norm);
183     }
184
185     for (i=0; i < lengthof(simple); i++) {
186         int rc = EINVAL;
187         normchar_t * entry = NULL;
188         keyT key = {NULL};
189         Nptr leafNode = NONODE;
190         cm_fid_t fid;
191         cm_fid_t * cfid = &fid;
192
193         TRACE1(L"Test row %d\n", i);
194
195         entry = cm_ClientStringToNormStringAlloc(simple[i].lookup, -1, NULL);
196         key.name = entry;
197
198         leafNode = bplus_Lookup(t, key);
199         if (leafNode != NONODE) {
200             int         slot;
201             Nptr        firstDataNode, dataNode, nextDataNode;
202             int         exact = 0;
203             int         count = 0;
204
205             slot = getSlot(t, leafNode);
206             if (slot <= BTERROR) {
207                 rc = (slot == BTERROR ? EINVAL : ENOENT);
208                 goto done;
209             }
210             firstDataNode = getnode(leafNode, slot);
211
212             for ( dataNode = firstDataNode; dataNode; dataNode = nextDataNode) {
213                 count++;
214                 if (!comparekeys(t)(key, getdatakey(dataNode), EXACT_MATCH) ) {
215                     exact = 1;
216                     break;
217                 }
218                 nextDataNode = getdatanext(dataNode);
219             }
220
221             if (exact) {
222                 *cfid = getdatavalue(dataNode).fid;
223                 rc = 0;
224             } else if (count == 1) {
225                 *cfid = getdatavalue(firstDataNode).fid;
226                 rc = CM_ERROR_INEXACT_MATCH;
227             } else {
228                 rc = CM_ERROR_AMBIGUOUS_FILENAME;
229             }
230         } else {
231             rc = ENOENT;
232         }
233
234     done:
235         if (entry)
236             free(entry);
237
238         IS(rc == simple[i].rc);
239         if (rc == simple[i].rc)
240             IS(fid.vnode == i);
241     }
242 }
243
244
245 int wmain(int argc, wchar_t ** argv)
246 {
247     TRACE1(L"\n\nRunning BPLUS tests...\n\n");
248     if (initialize_tests())
249         return 1;
250
251     RUNTEST(simple_test);
252
253     TRACE1(L"\nDone\n");
254     TRACE1(L"# of test Succeeded: %d\n", n_succeeded);
255     TRACE1(L"# of test Failed: %d\n", n_failed);
256
257     return 0;
258 }