2 * test_texteditor_insertfilter.c
4 * Copyright (c) 2014 project bchan
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.
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:
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.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
27 #include "test_control.h"
29 #include "texteditor_insertfilter.h"
38 #include <tad/tadlangcode.h>
39 #include <tad/taddecoder.h>
41 #include <unittest_driver.h>
46 } test_texteditor_insertfilter_expected;
48 LOCAL Bool test_texteditor_insertfilter_cmpsegment(test_texteditor_insertfilter_expected *expected, tadsegment *result)
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);
57 } else if (memcmp(result->value.variable.raw, expected->data, expected->len) != 0) {
58 printf("VAR: return data failure\n");
62 case TADSEGMENT_TYPE_CHARACTOR:
63 if (expected->len != sizeof(TC)) {
64 printf("CH: return length failure: expected = %d\n", expected->len);
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);
71 case TADSEGMENT_TYPE_LANGCODE:
72 ok = tadlangcodecmpTC((TC*)expected->data, expected->len/sizeof(TC), &result->value.lang);
74 printf("LANG: langcode faiure\n");
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)
83 texteditor_insertfilter_t filter;
84 texteditor_insertfilterresult_t *result;
86 tadsegment segment, *filterd;
89 UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
91 texteditor_insertfilter_initialize(&filter, &lang, w_ratio);
93 taddecoder_initialize(&decoder, (TC*)data, len/sizeof(TC));
96 cont = taddecoder_next(&decoder, &segment);
100 err = texteditor_insertfilter_put(&filter, &segment, &result);
102 ret = UNITTEST_RESULT_FAIL;
106 cont = texteditor_insertfilterresult_next(result, &filterd);
110 ok = test_texteditor_insertfilter_cmpsegment(expected + i, filterd);
112 printf("segment[%d] fail\n", i);
113 ret = UNITTEST_RESULT_FAIL;
117 texteditor_insertfilter_endinput(&filter, &result);
119 cont = texteditor_insertfilterresult_next(result, &filterd);
123 ok = test_texteditor_insertfilter_cmpsegment(expected + i, filterd);
125 ret = UNITTEST_RESULT_FAIL;
129 if (i != expected_len) {
130 printf("iteration number failure: expected = %d, result = %d\n", expected_len, i);
131 ret = UNITTEST_RESULT_FAIL;
134 taddecoder_finalize(&decoder);
136 texteditor_insertfilter_finalize(&filter);
141 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_1()
143 tadlangcode lang = (tadlangcode){0, 0x21};
144 RATIO w_ratio = 0x0101;
145 UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
147 test_texteditor_insertfilter_expected expected[] = {
148 { (UB*)(TC[]){TK_A}, 2 },
149 { (UB*)(TC[]){TK_B}, 2 },
150 { (UB*)(TC[]){TK_C}, 2 },
153 return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
156 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_2()
158 tadlangcode lang = (tadlangcode){0, 0x22};
159 RATIO w_ratio = 0x0101;
160 UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
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 },
170 return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
173 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_3()
175 tadlangcode lang = (tadlangcode){0, 0x21};
176 RATIO w_ratio = 0x0102;
177 UB *data = (UB*)(TC[]){TK_A, TK_B, TK_C};
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 },
187 return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
190 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_4()
192 tadlangcode lang = (tadlangcode){0, 0x21};
193 RATIO w_ratio = 0x0101;
194 UB *data = (UB*)(TC[]){TK_A, TK_B, 0xFE22, TK_C};
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 },
204 return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
207 LOCAL UNITTEST_RESULT test_texteditor_insertfilter_5()
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};
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 },
221 return test_texteditor_insertfilter_common(lang, w_ratio, data, len, expected, expected_len);
224 EXPORT VOID test_texteditor_insertfilter_main(unittest_driver_t *driver)
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);