OSDN Git Service

fix previous cache data.
[bbk/bchanl.git] / src / test_subjectcache.c
1 /*
2  * test_subjectcache.c
3  *
4  * Copyright (c) 2009-2012 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include    "test.h"
28
29 #include    "subjectcache.h"
30
31 #include    <btron/btron.h>
32 #include        <tcode.h>
33 #include    <bstdio.h>
34 #include    <bstring.h>
35 #include    <bstdlib.h>
36 #include        <errcode.h>
37
38 #include    <unittest_driver.h>
39
40 LOCAL UB test_sbjtcache_testdata_01[] = {"aaaaabbbbbcccccddddd"};
41 LOCAL UB test_sbjtcache_testdata_01_1[] = {"aaaaa"};
42 LOCAL UB test_sbjtcache_testdata_01_2[] = {"bbbbb"};
43 LOCAL UB test_sbjtcache_testdata_01_3[] = {"ccccc"};
44 LOCAL UB test_sbjtcache_testdata_01_4[] = {"ddddd"};
45 LOCAL UB test_sbjtcache_testdata_02[] = {"XXX abcdef\r\nAAAA: valueA\r\nBBBB: valueB\r\n\r\n"};
46 LOCAL UB test_sbjtcache_testdata_04[] = {"aaa.bbb.ccc.jp"};
47 LOCAL UB test_sbjtcache_testdata_05[] = {"thread"};
48 LOCAL UB test_sbjtcache_testdata_07[] = {"XXX abcdef\r\nAAAA: valueC\r\nBBBB: valueC\r\n\r\n"};
49
50 LOCAL Bool test_sbjtcache_util_cmp_ctx_str(sbjtcache_datareadcontext_t *context, UB *data, W len)
51 {
52         Bool cont;
53         UB *bin_cache;
54         W i = 0, len_cache, cmp;
55
56         for (;;) {
57                 cont = sbjtcache_datareadcontext_nextdata(context, &bin_cache, &len_cache);
58                 if (cont == False) {
59                         break;
60                 }
61                 cmp = memcmp(data + i, bin_cache, len_cache);
62                 if (cmp != 0) {
63                         return False;
64                 }
65                 i += len_cache;
66                 if (i > len) {
67                         return False;
68                 }
69         }
70
71         if (i != len) {
72                 return False;
73         }
74
75         return True;
76 }
77
78 /* test_sbjtcache_1 */
79
80 LOCAL UNITTEST_RESULT test_sbjtcache_1()
81 {
82         sbjtcache_t *cache;
83         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
84         UB *host;
85         W host_len;
86
87         cache = sbjtcache_new();
88
89         sbjtcache_gethost(cache, &host, &host_len);
90         if ((host != NULL)||(host_len != 0)) {
91                 result = UNITTEST_RESULT_FAIL;
92         }
93
94         sbjtcache_delete(cache);
95
96         return result;
97 }
98
99 /* test_sbjtcache_2 */
100
101 LOCAL UNITTEST_RESULT test_sbjtcache_2()
102 {
103         sbjtcache_t *cache;
104         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
105         UB *host;
106         W err, host_len, cmp;
107
108         cache = sbjtcache_new();
109
110         err = sbjtcache_updatehost(cache, test_sbjtcache_testdata_04, strlen(test_sbjtcache_testdata_04));
111         if (err < 0) {
112                 result = UNITTEST_RESULT_FAIL;
113         }
114
115         sbjtcache_gethost(cache, &host, &host_len);
116         cmp = memcmp(host, test_sbjtcache_testdata_04, host_len);
117         if (cmp != 0) {
118                 printf("sbjtcache_gethost: data error\n");
119                 result = UNITTEST_RESULT_FAIL;
120         }
121
122         sbjtcache_delete(cache);
123
124         return result;
125 }
126
127 /* test_sbjtcache_3 */
128
129 LOCAL UNITTEST_RESULT test_sbjtcache_3()
130 {
131         sbjtcache_t *cache;
132         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
133         UB *board;
134         W board_len;
135
136         cache = sbjtcache_new();
137
138         sbjtcache_getboard(cache, &board, &board_len);
139         if ((board != NULL)||(board_len != 0)) {
140                 result = UNITTEST_RESULT_FAIL;
141         }
142
143         sbjtcache_delete(cache);
144
145         return result;
146 }
147
148 /* test_sbjtcache_4 */
149
150 LOCAL UNITTEST_RESULT test_sbjtcache_4()
151 {
152         sbjtcache_t *cache;
153         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
154         UB *board;
155         W err, board_len, cmp;
156
157         cache = sbjtcache_new();
158
159         err = sbjtcache_updateboard(cache, test_sbjtcache_testdata_05, strlen(test_sbjtcache_testdata_05));
160         if (err < 0) {
161                 result = UNITTEST_RESULT_FAIL;
162         }
163
164         sbjtcache_getboard(cache, &board, &board_len);
165         cmp = memcmp(board, test_sbjtcache_testdata_05, board_len);
166         if (cmp != 0) {
167                 printf("sbjtcache_getboard: data error\n");
168                 result = UNITTEST_RESULT_FAIL;
169         }
170
171         sbjtcache_delete(cache);
172
173         return result;
174 }
175
176 /* test_sbjtcache_5 */
177
178 LOCAL UNITTEST_RESULT test_sbjtcache_5_testseq()
179 {
180         sbjtcache_t *cache;
181         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
182         UB *header;
183         W err, header_len, cmp;
184
185         cache = sbjtcache_new();
186
187         err = sbjtcache_updatelatestheader(cache, test_sbjtcache_testdata_07, strlen(test_sbjtcache_testdata_07));
188         if (err < 0) {
189                 printf("sbjtcache_updatelataestheade error\n");
190                 sbjtcache_delete(cache);
191                 return UNITTEST_RESULT_FAIL;
192         }
193
194         sbjtcache_getlatestheader(cache, &header, &header_len);
195         if (header_len != strlen(test_sbjtcache_testdata_07)) {
196                 printf("sbjtcache_getlatestheader: length error\n");
197                 result = UNITTEST_RESULT_FAIL;
198         }
199         cmp = memcmp(header, test_sbjtcache_testdata_07, header_len);
200         if (cmp != 0) {
201                 printf("sbjtcache_getlatestheader: data error\n");
202                 result = UNITTEST_RESULT_FAIL;
203         }
204
205         sbjtcache_delete(cache);
206
207         return result;
208 }
209
210 LOCAL UNITTEST_RESULT test_sbjtcache_5()
211 {
212         return test_sbjtcache_5_testseq();
213 }
214
215 /* test_sbjtcache_6 */
216
217 LOCAL UNITTEST_RESULT test_sbjtcache_6_testseq()
218 {
219         sbjtcache_t *cache;
220         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
221         UB *header;
222         W err, header_len, cmp;
223
224         cache = sbjtcache_new();
225
226         err = sbjtcache_updatelatestheader(cache, test_sbjtcache_testdata_07, strlen(test_sbjtcache_testdata_07));
227         if (err < 0) {
228                 printf("sbjtcache_updatelataestheade error\n");
229                 sbjtcache_delete(cache);
230                 return UNITTEST_RESULT_FAIL;
231         }
232         err = sbjtcache_updatelatestheader(cache, test_sbjtcache_testdata_02, strlen(test_sbjtcache_testdata_02));
233         if (err < 0) {
234                 printf("sbjtcache_updatelataestheade error\n");
235                 sbjtcache_delete(cache);
236                 return UNITTEST_RESULT_FAIL;
237         }
238
239         sbjtcache_getlatestheader(cache, &header, &header_len);
240         if (header_len != strlen(test_sbjtcache_testdata_02)) {
241                 printf("sbjtcache_getlatestheader: length error\n");
242                 result = UNITTEST_RESULT_FAIL;
243         }
244         cmp = memcmp(header, test_sbjtcache_testdata_02, header_len);
245         if (cmp != 0) {
246                 printf("sbjtcache_getlatestheader: data error\n");
247                 result = UNITTEST_RESULT_FAIL;
248         }
249
250         sbjtcache_delete(cache);
251
252         return result;
253 }
254
255 LOCAL UNITTEST_RESULT test_sbjtcache_6()
256 {
257         return test_sbjtcache_6_testseq();
258 }
259
260 /* test_sbjtcache_7 */
261
262 LOCAL UNITTEST_RESULT test_sbjtcache_7_testseq()
263 {
264         sbjtcache_t *cache;
265         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
266         UB *header;
267         W header_len;
268
269         cache = sbjtcache_new();
270
271         sbjtcache_getlatestheader(cache, &header, &header_len);
272         if (header != NULL) {
273                 printf("sbjtcache_getlatestheader: data error\n");
274                 result = UNITTEST_RESULT_FAIL;
275         }
276         if (header_len != 0) {
277                 printf("sbjtcache_getlatestheader: length error\n");
278                 result = UNITTEST_RESULT_FAIL;
279         }
280
281         sbjtcache_delete(cache);
282
283         return result;
284 }
285
286 LOCAL UNITTEST_RESULT test_sbjtcache_7()
287 {
288         return test_sbjtcache_7_testseq();
289 }
290
291 /* test_sbjtcache_8 */
292
293 LOCAL UNITTEST_RESULT test_sbjtcache_8_testseq()
294 {
295         W err;
296         sbjtcache_t *cache;
297         sbjtcache_datareadcontext_t *context;
298         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
299         Bool ok;
300
301         cache = sbjtcache_new();
302
303         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_1, strlen(test_sbjtcache_testdata_01_1));
304         if (err < 0) {
305                 printf("sbjtcache_appenddata error 1\n");
306                 sbjtcache_delete(cache);
307                 return UNITTEST_RESULT_FAIL;
308         }
309         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_2, strlen(test_sbjtcache_testdata_01_2));
310         if (err < 0) {
311                 printf("sbjtcache_appenddata error 2\n");
312                 sbjtcache_delete(cache);
313                 return UNITTEST_RESULT_FAIL;
314         }
315         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_3, strlen(test_sbjtcache_testdata_01_3));
316         if (err < 0) {
317                 printf("sbjtcache_appenddata error 3\n");
318                 sbjtcache_delete(cache);
319                 return UNITTEST_RESULT_FAIL;
320         }
321         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_4, strlen(test_sbjtcache_testdata_01_4));
322         if (err < 0) {
323                 printf("sbjtcache_appenddata error 4\n");
324                 sbjtcache_delete(cache);
325                 return UNITTEST_RESULT_FAIL;
326         }
327
328         context = sbjtcache_startdataread(cache, 0);
329         if (context == NULL) {
330                 printf("sbjtcache_startdataread error\n");
331                 sbjtcache_delete(cache);
332                 return UNITTEST_RESULT_FAIL;
333         }
334         ok = test_sbjtcache_util_cmp_ctx_str(context, test_sbjtcache_testdata_01, strlen(test_sbjtcache_testdata_01));
335         if (ok != True) {
336                 result = UNITTEST_RESULT_FAIL;
337         }
338         sbjtcache_enddataread(cache, context);
339
340         sbjtcache_delete(cache);
341
342         return result;
343 }
344
345 LOCAL UNITTEST_RESULT test_sbjtcache_8()
346 {
347         return test_sbjtcache_8_testseq();
348 }
349
350 /* test_sbjtcache_9 */
351
352 LOCAL UNITTEST_RESULT test_sbjtcache_9_testseq()
353 {
354         W err;
355         sbjtcache_t *cache;
356         sbjtcache_datareadcontext_t *context;
357         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
358         UB *bin_cache;
359         W len_cache;
360         Bool ok;
361
362         cache = sbjtcache_new();
363
364         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_1, strlen(test_sbjtcache_testdata_01_1));
365         if (err < 0) {
366                 printf("sbjtcache_appenddata error 1\n");
367                 sbjtcache_delete(cache);
368                 return UNITTEST_RESULT_FAIL;
369         }
370         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_2, strlen(test_sbjtcache_testdata_01_2));
371         if (err < 0) {
372                 printf("sbjtcache_appenddata error 2\n");
373                 sbjtcache_delete(cache);
374                 return UNITTEST_RESULT_FAIL;
375         }
376         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_3, strlen(test_sbjtcache_testdata_01_3));
377         if (err < 0) {
378                 printf("sbjtcache_appenddata error 3\n");
379                 sbjtcache_delete(cache);
380                 return UNITTEST_RESULT_FAIL;
381         }
382         err = sbjtcache_appenddata(cache, test_sbjtcache_testdata_01_4, strlen(test_sbjtcache_testdata_01_4));
383         if (err < 0) {
384                 printf("sbjtcache_appenddata error 4\n");
385                 sbjtcache_delete(cache);
386                 return UNITTEST_RESULT_FAIL;
387         }
388
389         context = sbjtcache_startdataread(cache, strlen(test_sbjtcache_testdata_01)+5);
390         if (context == NULL) {
391                 sbjtcache_delete(cache);
392                 return UNITTEST_RESULT_FAIL;
393         }
394         ok = sbjtcache_datareadcontext_nextdata(context, &bin_cache, &len_cache);
395         if (ok == True) {
396                 result = UNITTEST_RESULT_FAIL;
397         }
398         sbjtcache_enddataread(cache, context);
399
400         sbjtcache_delete(cache);
401
402         return result;
403 }
404
405 LOCAL UNITTEST_RESULT test_sbjtcache_9()
406 {
407         return test_sbjtcache_9_testseq();
408 }
409
410 EXPORT VOID test_sbjtcache_main(unittest_driver_t *driver)
411 {
412         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_1);
413         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_2);
414         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_3);
415         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_4);
416         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_5);
417         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_6);
418         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_7);
419         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_8);
420         UNITTEST_DRIVER_REGIST(driver, test_sbjtcache_9);
421 }