add test cases.
@@ -34,8 +34,10 @@ | ||
34 | 34 | #include <bstring.h> |
35 | 35 | #include <btron/dp.h> |
36 | 36 | #include <tad.h> |
37 | +#include <tcode.h> | |
37 | 38 | |
38 | 39 | #include "tadsegment.h" |
40 | +#include "tadlangcode.h" | |
39 | 41 | |
40 | 42 | #include <unittest_driver.h> |
41 | 43 |
@@ -99,7 +101,162 @@ | ||
99 | 101 | return UNITTEST_RESULT_PASS; |
100 | 102 | } |
101 | 103 | |
104 | +typedef struct { | |
105 | + TC *data; | |
106 | + W len; | |
107 | +} test_taddecoder_expected_t; | |
108 | + | |
109 | +typedef struct { | |
110 | + TC *src; | |
111 | + W src_len; | |
112 | + test_taddecoder_expected_t *expected; | |
113 | + W expected_len; | |
114 | +} test_taddecoder_t; | |
115 | + | |
116 | +LOCAL UNITTEST_RESULT test_taddecoder_common(test_taddecoder_t *testdata) | |
117 | +{ | |
118 | + taddecoder_t iter; | |
119 | + tadsegment result; | |
120 | + Bool cont, ok; | |
121 | + test_taddecoder_expected_t *expected; | |
122 | + UNITTEST_RESULT ret = UNITTEST_RESULT_PASS; | |
123 | + W i = 0; | |
124 | + | |
125 | + taddecoder_initialize(&iter, testdata->src, testdata->src_len); | |
126 | + | |
127 | + for (i = 0;; i++) { | |
128 | + cont = taddecoder_next(&iter, &result); | |
129 | + if (cont == False) { | |
130 | + break; | |
131 | + } | |
132 | + | |
133 | + if (i >= testdata->expected_len) { | |
134 | + continue; | |
135 | + } | |
136 | + | |
137 | + expected = testdata->expected + i; | |
138 | + | |
139 | + switch (result.type) { | |
140 | + case TADSEGMENT_TYPE_VARIABLE: | |
141 | + if (result.value.variable.rawlen != expected->len * sizeof(TC)) { | |
142 | + ret = UNITTEST_RESULT_FAIL; | |
143 | + printf("VARIABLE length failure: expected = %d, result = %d\n", expected->len * sizeof(TC), result.value.variable.rawlen); | |
144 | + } else if (memcmp(result.value.variable.raw, expected->data, result.value.variable.rawlen) != 0) { | |
145 | + ret = UNITTEST_RESULT_FAIL; | |
146 | + printf("VARIABLE data failure\n"); | |
147 | + { | |
148 | + W j; | |
149 | + for (j = 0; j < expected->len; j++) { | |
150 | + printf("%02x, %02x\n", result.value.variable.raw[j], ((UB*)expected->data)[j]); | |
151 | + } | |
152 | + } | |
153 | + } | |
154 | + break; | |
155 | + case TADSEGMENT_TYPE_CHARACTOR: | |
156 | + if (expected->len != 1) { | |
157 | + ret = UNITTEST_RESULT_FAIL; | |
158 | + printf("CHARACTOR length failure: expected = %d, result = %d\n", sizeof(TC), expected->len * sizeof(TC)); | |
159 | + } else if (memcmp(&result.value.ch, expected->data, sizeof(TC)) != 0) { | |
160 | + printf("CHARACTOR data failure: expected = %04x, result = %04x\n", result.value.ch, expected->data[0]); | |
161 | + } | |
162 | + break; | |
163 | + case TADSEGMENT_TYPE_LANGCODE: | |
164 | + ok = tadlangcodecmpTC(expected->data, expected->len, &result.value.lang); | |
165 | + if (ok == False) { | |
166 | + ret = UNITTEST_RESULT_FAIL; | |
167 | + printf("LANGCODE failure\n"); | |
168 | + } | |
169 | + break; | |
170 | + default: | |
171 | + ret = UNITTEST_RESULT_FAIL; | |
172 | + printf("invalid tadsegment->type = %d\n", result.type); | |
173 | + break; | |
174 | + } | |
175 | + } | |
176 | + | |
177 | + taddecoder_finalize(&iter); | |
178 | + | |
179 | + if (i != testdata->expected_len) { | |
180 | + printf("iteration coun fail: expected = %d, result = %d\n", testdata->expected_len, i); | |
181 | + ret = UNITTEST_RESULT_FAIL; | |
182 | + } | |
183 | + | |
184 | + return ret; | |
185 | +} | |
186 | + | |
187 | +LOCAL UNITTEST_RESULT test_taddecoder_2() | |
188 | +{ | |
189 | + TC src[] = (TC[]){TK_A, TK_B, TK_C}; | |
190 | + W src_len = sizeof(src) / sizeof(TC); | |
191 | + test_taddecoder_expected_t expected[] = { | |
192 | + { (TC[]){TK_A}, 1 }, | |
193 | + { (TC[]){TK_B}, 1 }, | |
194 | + { (TC[]){TK_C}, 1 }, | |
195 | + }; | |
196 | + W expected_len = 3; | |
197 | + test_taddecoder_t testdata = { | |
198 | + src, src_len, | |
199 | + expected, expected_len | |
200 | + }; | |
201 | + return test_taddecoder_common(&testdata); | |
202 | +} | |
203 | + | |
204 | +LOCAL UNITTEST_RESULT test_taddecoder_3() | |
205 | +{ | |
206 | + TC src[] = (TC[]){TK_A, 0xFE21, TK_C}; | |
207 | + W src_len = sizeof(src) / sizeof(TC); | |
208 | + test_taddecoder_expected_t expected[] = { | |
209 | + { (TC[]){TK_A}, 1 }, | |
210 | + { (TC[]){0xFE21}, 1 }, | |
211 | + { (TC[]){TK_C}, 1 }, | |
212 | + }; | |
213 | + W expected_len = 3; | |
214 | + test_taddecoder_t testdata = { | |
215 | + src, src_len, | |
216 | + expected, expected_len | |
217 | + }; | |
218 | + return test_taddecoder_common(&testdata); | |
219 | +} | |
220 | + | |
221 | +LOCAL UNITTEST_RESULT test_taddecoder_4() | |
222 | +{ | |
223 | + TC src[] = (TC[]){TK_A, 0xFEFE, 0xFE21, TK_C}; | |
224 | + W src_len = sizeof(src) / sizeof(TC); | |
225 | + test_taddecoder_expected_t expected[] = { | |
226 | + { (TC[]){TK_A}, 1 }, | |
227 | + { (TC[]){0xFEFE, 0xFE21}, 2 }, | |
228 | + { (TC[]){TK_C}, 1 }, | |
229 | + }; | |
230 | + W expected_len = 3; | |
231 | + test_taddecoder_t testdata = { | |
232 | + src, src_len, | |
233 | + expected, expected_len | |
234 | + }; | |
235 | + return test_taddecoder_common(&testdata); | |
236 | +} | |
237 | + | |
238 | +LOCAL UNITTEST_RESULT test_taddecoder_5() | |
239 | +{ | |
240 | + TC src[] = (TC[]){TK_A, 0xFFA2, 0x0006, 0x0300, 0x0101, 0x0102, TK_C}; | |
241 | + W src_len = sizeof(src) / sizeof(TC); | |
242 | + test_taddecoder_expected_t expected[] = { | |
243 | + { (TC[]){TK_A}, 1 }, | |
244 | + { (TC[]){0xFFA2, 0x0006, 0x0300, 0x0101, 0x0102}, 5 }, | |
245 | + { (TC[]){TK_C}, 1 }, | |
246 | + }; | |
247 | + W expected_len = 3; | |
248 | + test_taddecoder_t testdata = { | |
249 | + src, src_len, | |
250 | + expected, expected_len | |
251 | + }; | |
252 | + return test_taddecoder_common(&testdata); | |
253 | +} | |
254 | + | |
102 | 255 | EXPORT VOID test_taddecoder_main(unittest_driver_t *driver) |
103 | 256 | { |
104 | 257 | UNITTEST_DRIVER_REGIST(driver, test_taddecoder_1); |
258 | + UNITTEST_DRIVER_REGIST(driver, test_taddecoder_2); | |
259 | + UNITTEST_DRIVER_REGIST(driver, test_taddecoder_3); | |
260 | + UNITTEST_DRIVER_REGIST(driver, test_taddecoder_4); | |
261 | + UNITTEST_DRIVER_REGIST(driver, test_taddecoder_5); | |
105 | 262 | } |