OSDN Git Service

refactoring insert filter api.
[bbk/bchanf.git] / src / control / test_texteditor_insertfilter.c
1 /*
2  * test_texteditor_insertfilter.c
3  *
4  * Copyright (c) 2014 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_control.h"
28
29 #include "texteditor_insertfilter.h"
30
31 #include        <basic.h>
32 #include        <bstdio.h>
33 #include        <bstdlib.h>
34 #include        <bstring.h>
35 #include        <tstring.h>
36 #include        <tcode.h>
37
38 #include    <tad/tadlangcode.h>
39 #include    <tad/taddecoder.h>
40
41 #include    <unittest_driver.h>
42
43 typedef struct {
44         UB *data;
45         W len;
46 } test_texteditor_insertfilter_expected;
47
48 LOCAL Bool test_texteditor_insertfilter_cmpsegment(test_texteditor_insertfilter_expected *expected, tadsegment *result)
49 {
50         Bool ok = True;
51
52         switch (result->type) {
53         case TADSEGMENT_TYPE_VARIABLE:
54                 if (result->value.variable.rawlen != expected->len) {
55                         printf("VAR: return length failure: expected = %d, result = %d\n", expected->len, result->value.variable.rawlen);
56                         ok = False;
57                 } else if (memcmp(result->value.variable.raw, expected->data, expected->len) != 0) {
58                         printf("VAR: return data failure\n");
59                         ok = False;
60                 }
61                 return ok;
62         case TADSEGMENT_TYPE_CHARACTOR:
63                 if (expected->len != sizeof(TC)) {
64                         printf("CH: return length failure: expected = %d\n", expected->len);
65                         ok = False;
66                 } else if (memcmp((UB*)&result->value.ch, expected->data, expected->len) != 0) {
67                         printf("CH: return data failure: expected = %04x, result = %04x\n", *(TC*)expected->data, result->value.ch);
68                         ok = False;
69                 }
70                 return ok;
71         case TADSEGMENT_TYPE_LANGCODE:
72                 ok = tadlangcodecmpTC((TC*)expected->data, expected->len/sizeof(TC), &result->value.lang);
73                 if (ok == False) {
74                         printf("LANG: langcode faiure\n");
75                 }
76                 return ok;
77         }
78         return False;
79 }
80
81 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_common(tadlangcode lang, RATIO w_ratio, UB *data, W len, test_texteditor_insertfilter_expected *expected, W expected_len)
82 {
83         texteditor_insertfilter_t filter;
84         texteditor_insertfilterresult_t *result;
85         taddecoder_t decoder;
86         tadsegment segment, *filterd;
87         Bool cont, ok;
88         W i, err;
89         UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
90
91         texteditor_insertfilter_initialize(&filter, &lang, w_ratio);
92
93         taddecoder_initialize(&decoder, (TC*)data, len/sizeof(TC));
94
95         for (i = 0;;) {
96                 cont = taddecoder_next(&decoder, &segment);
97                 if (cont == False) {
98                         break;
99                 }
100                 err = texteditor_insertfilter_put(&filter, &segment, &result);
101                 if (err < 0) {
102                         ret = UNITTEST_RESULT_FAIL;
103                         break;
104                 }
105                 for (;; i++) {
106                         cont = texteditor_insertfilterresult_next(result, &filterd);
107                         if (cont == False) {
108                                 break;
109                         }
110                         ok = test_texteditor_insertfilter_cmpsegment(expected + i, filterd);
111                         if (ok == False) {
112                                 printf("segment[%d] fail\n", i);
113                                 ret = UNITTEST_RESULT_FAIL;
114                         }
115                 }
116         }
117         texteditor_insertfilter_endinput(&filter, &result);
118         for (;; i++) {
119                 cont = texteditor_insertfilterresult_next(result, &filterd);
120                 if (cont == False) {
121                         break;
122                 }
123                 ok = test_texteditor_insertfilter_cmpsegment(expected + i, filterd);
124                 if (ok == False) {
125                         ret = UNITTEST_RESULT_FAIL;
126                 }
127         }
128
129         if (i != expected_len) {
130                 printf("iteration number failure: expected = %d, result = %d\n", expected_len, i);
131                 ret = UNITTEST_RESULT_FAIL;
132         }
133
134         taddecoder_finalize(&decoder);
135
136         texteditor_insertfilter_finalize(&filter);
137
138         return ret;
139 }
140
141 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_1()
142 {
143         tadlangcode lang = (tadlangcode){0, 0x21};
144         RATIO w_ratio = 0x0101;
145         UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
146         W len = 6;
147         test_texteditor_insertfilter_expected expected[] = {
148                 { (UB*)(TC[]){TK_A}, 2 },
149                 { (UB*)(TC[]){TK_B}, 2 },
150                 { (UB*)(TC[]){TK_C}, 2 },
151         };
152         W expected_len = 3;
153         return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
154 }
155
156 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_2()
157 {
158         tadlangcode lang = (tadlangcode){0, 0x22};
159         RATIO w_ratio = 0x0101;
160         UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
161         W len = 6;
162         test_texteditor_insertfilter_expected expected[] = {
163                 { (UB*)(TC[]){0xFE21}, 2 },
164                 { (UB*)(TC[]){TK_A}, 2 },
165                 { (UB*)(TC[]){TK_B}, 2 },
166                 { (UB*)(TC[]){TK_C}, 2 },
167                 { (UB*)(TC[]){0xFE22}, 2 },
168         };
169         W expected_len = 5;
170         return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
171 }
172
173 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_3()
174 {
175         tadlangcode lang = (tadlangcode){0, 0x21};
176         RATIO w_ratio = 0x0102;
177         UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
178         W len = 6;
179         test_texteditor_insertfilter_expected expected[] = {
180                 { (UB*)(TC[]){ 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0101 }, 10 },
181                 { (UB*)(TC[]){TK_A}, 2 },
182                 { (UB*)(TC[]){TK_B}, 2 },
183                 { (UB*)(TC[]){TK_C}, 2 },
184                 { (UB*)(TC[]){ 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0102 }, 10 },
185         };
186         W expected_len = 5;
187         return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
188 }
189
190 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_4()
191 {
192         tadlangcode lang = (tadlangcode){0, 0x21};
193         RATIO w_ratio = 0x0101;
194         UB *data = (UB*)(TC[]){TK_A, TK_B, 0xFE22, TK_C};
195         W len = 8;
196         test_texteditor_insertfilter_expected expected[] = {
197                 { (UB*)(TC[]){TK_A}, 2 },
198                 { (UB*)(TC[]){TK_B}, 2 },
199                 { (UB*)(TC[]){0xFE22}, 2 },
200                 { (UB*)(TC[]){TK_C}, 2 },
201                 { (UB*)(TC[]){0xFE21}, 2 },
202         };
203         W expected_len = 5;
204         return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
205 }
206
207 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_5()
208 {
209         tadlangcode lang = (tadlangcode){0, 0x21};
210         RATIO w_ratio = 0x0101;
211         UB *data = (UB*)(TC[]){TK_A, TK_B, 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0102, TK_C};
212         W len = 16;
213         test_texteditor_insertfilter_expected expected[] = {
214                 { (UB*)(TC[]){TK_A}, 2 },
215                 { (UB*)(TC[]){TK_B}, 2 },
216                 { (UB*)(TC[]){ 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0102 }, 10 },
217                 { (UB*)(TC[]){TK_C}, 2 },
218                 { (UB*)(TC[]){ 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0101 }, 10 },
219         };
220         W expected_len = 5;
221         return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
222 }
223
224 EXPORT VOID test_texteditor_insertfilter_main(unittest_driver_t *driver)
225 {
226         UNITTEST_DRIVER_REGIST(driver, test_texteditor_insertfilter_1);
227         UNITTEST_DRIVER_REGIST(driver, test_texteditor_insertfilter_2);
228         UNITTEST_DRIVER_REGIST(driver, test_texteditor_insertfilter_3);
229         UNITTEST_DRIVER_REGIST(driver, test_texteditor_insertfilter_4);
230         UNITTEST_DRIVER_REGIST(driver, test_texteditor_insertfilter_5);
231 }