OSDN Git Service

update version number.
[bbk/bchanl.git] / src / test_bbsmenufilter.c
1 /*
2  * test_bbsmenufilter.c
3  *
4  * Copyright (c) 2010-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    "bbsmenufilter.h"
30
31 #include    <bstdio.h>
32
33 #include    <unittest_driver.h>
34
35 #include    "bbsmenuparser.h"
36 #include    "bbsmenucache.h"
37
38 LOCAL UB test_bbsmnfilter_testdata_01[] = {
39 "
40 <HTML>
41 <HEAD>
42 <TITLE>BBS MENU for test</TITLE>
43 </HEAD>
44
45 <BODY>
46 <BR>
47 <font size=2>
48
49 <A HREF=http://xxx.2ch.net/AAA/>AAA</A>
50
51 <BR><BR><B>Cat 1</B><BR>
52 <A HREF=http://xxx.2ch.net/BBB/>BBB</A><br>
53 <A HREF=http://yyy.2ch.net/CCC/>CCC</A><br>
54 <A HREF=http://yyy.2ch.net/DDD/>DDD</A><br>
55 <A HREF=http://yyy.2ch.net/EEE/>EEE</A>
56
57 <BR><BR><B>Cat 2</B><BR>
58 <A HREF=http://xxx.2ch.net/FFF/>FFF</A><br>
59 <A HREF=http://xxx.2ch.net/GGG/>GGG</A><br>
60 <A HREF=http://xxx.2ch.net/HHH/>HHH</A><br>
61 <A HREF=http://xxx.2ch.net/III/>III</A><br>
62
63 <BR><BR><B>Cat 3</B><BR>
64 <A HREF=http://xxx.2ch.net/>other</A><br>
65 <A HREF=http://yyy.2ch.net/>other2</A><br>
66 <A HREF=http://zzz.2ch.net/zzz/zzz.php>other3</A><br>
67
68 <BR><BR><B>Cat 4</B><BR>
69 <A HREF=http://www.chokanji.com/ TARGET=_blank>ck4</A><br>
70 <A HREF=http://www.parsonal-media.co.jp/ TARGET=_blank>ck4</A><br>
71
72 </BODY></HTML>
73 "};
74
75 LOCAL UNITTEST_RESULT test_bbsmnfilter_1()
76 {
77         W ret,err;
78         bbsmncache_t *cache;
79         bbsmnparser_t *parser;
80         bbsmnparser_item_t *item = NULL;
81         bbsmnfilter_t *filter = NULL;
82         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
83
84         cache = bbsmncache_new();
85         bbsmncache_appenddata(cache, test_bbsmnfilter_testdata_01, strlen(test_bbsmnfilter_testdata_01));
86         parser = bbsmnparser_new(cache);
87
88         filter = bbsmnfilter_new();
89
90         for (;;) {
91                 err = bbsmnparser_getnextitem(parser, &item);
92                 if (err != 1) {
93                         break;
94                 }
95                 bbsmnfilter_inputitem(filter, item);
96                 for (;;) {
97                         ret = bbsmnfilter_outputitem(filter, &item);
98                         if (item != NULL) {
99                                 if (item->category != NULL) {
100                                         printf("category:\n%S\n", item->category);
101                                 }
102                                 if (item->url != NULL) {
103                                         printf("url:\n%s\n", item->url);
104                                 }
105                                 if (item->title != NULL) {
106                                         printf("title:\n%S\n", item->title);
107                                 }
108                                 bbsmnparser_item_delete(item);
109                         } else {
110                                 break;
111                         }
112                         if (ret != BBSMNFILTER_OUTPUTITEM_CONTINUE) {
113                                 break;
114                         }
115                 }
116         }
117
118         bbsmnfilter_delete(filter);
119
120         bbsmnparser_delete(parser);
121         bbsmncache_delete(cache);
122
123         return result;
124 }
125
126 LOCAL UNITTEST_RESULT test_bbsmnfilter_2_checkitem_index(bbsmnparser_item_t *item, W i)
127 {
128         switch(i) {
129         case 0:
130         case 5:
131                 if (item->category == NULL) {
132                         return UNITTEST_RESULT_FAIL;
133                 }
134                 break;
135         case 1:
136         case 2:
137         case 3:
138         case 4:
139         case 6:
140         case 7:
141         case 8:
142         case 9:
143                 if ((item->category != NULL)||(item->url == NULL)||(item->title == NULL)) {
144                         return UNITTEST_RESULT_FAIL;
145                 }
146                 break;
147         default:
148                 return UNITTEST_RESULT_FAIL;
149                 break;
150         }
151         return UNITTEST_RESULT_PASS;
152 }
153
154 LOCAL UNITTEST_RESULT test_bbsmnfilter_2()
155 {
156         W i = 0, ret,err;
157         bbsmncache_t *cache;
158         bbsmnparser_t *parser;
159         bbsmnparser_item_t *item = NULL;
160         bbsmnfilter_t *filter = NULL;
161         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
162
163         cache = bbsmncache_new();
164         bbsmncache_appenddata(cache, test_bbsmnfilter_testdata_01, strlen(test_bbsmnfilter_testdata_01));
165         parser = bbsmnparser_new(cache);
166
167         filter = bbsmnfilter_new();
168
169         for (;;) {
170                 err = bbsmnparser_getnextitem(parser, &item);
171                 if (err != 1) {
172                         break;
173                 }
174                 bbsmnfilter_inputitem(filter, item);
175                 for (;;) {
176                         ret = bbsmnfilter_outputitem(filter, &item);
177                         if (item != NULL) {
178                                 result = test_bbsmnfilter_2_checkitem_index(item, i);
179                                 i++;
180                                 bbsmnparser_item_delete(item);
181
182                                 if (result == UNITTEST_RESULT_FAIL) {
183                                         printf("fail i = %d\n", i-1);
184                                         break;
185                                 }
186                         } else {
187                                 if ((ret != BBSMNFILTER_OUTPUTITEM_WAITNEXT)&&(ret != BBSMNFILTER_OUTPUTITEM_END)) {
188                                         printf("invalid return value\n");
189                                         result = UNITTEST_RESULT_FAIL;
190                                 }
191                                 break;
192                         }
193                         if (ret != BBSMNFILTER_OUTPUTITEM_CONTINUE) {
194                                 break;
195                         }
196                 }
197                 if (result == UNITTEST_RESULT_FAIL) {
198                         break;
199                 }
200         }
201
202         bbsmnfilter_inputitem(filter, NULL);
203         for (;;) {
204                 ret = bbsmnfilter_outputitem(filter, &item);
205                 if (item != NULL) {
206                         result = test_bbsmnfilter_2_checkitem_index(item, i);
207                         i++;
208                         bbsmnparser_item_delete(item);
209
210                         if (result == UNITTEST_RESULT_FAIL) {
211                                 printf("fail i = %d\n", i-1);
212                                 break;
213                         }
214                 }
215                 if (ret == BBSMNFILTER_OUTPUTITEM_CONTINUE) {
216                         continue;
217                 }
218                 if (ret == BBSMNFILTER_OUTPUTITEM_END) {
219                         break;
220                 }
221                 result = UNITTEST_RESULT_FAIL;
222                 printf("invalid return value\n");
223                 break;
224         }
225         if (i != 10) {
226                 result = UNITTEST_RESULT_FAIL;
227                 printf("fail: too meny items\n");
228         }
229
230         bbsmnfilter_delete(filter);
231
232         bbsmnparser_delete(parser);
233         bbsmncache_delete(cache);
234
235         return result;
236 }
237
238 EXPORT VOID test_bbsmnfilter_main(unittest_driver_t *driver)
239 {
240         UNITTEST_DRIVER_REGIST(driver, test_bbsmnfilter_1);
241         UNITTEST_DRIVER_REGIST(driver, test_bbsmnfilter_2);
242 }