OSDN Git Service

e994c3a6ddd746255dce374c0c37d04a35d3ad66
[bbk/bchanf.git] / src / control / test_texteditor_stackfilter.c
1 /*
2  * test_texteditor_stackfilter.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_stackfilter.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/taddecoder.h>
39 #include    <tad/tadfragment.h>
40
41 #include    <unittest_driver.h>
42
43 typedef struct {
44         TC *src;
45         W src_len;
46         TC *expected;
47         W expected_len;
48 } test_texteditor_stackfilter_testdata_t;
49
50 LOCAL UNITTEST_RESULT test_texteditor_stackfilter_common(test_texteditor_stackfilter_testdata_t *testdata)
51 {
52         texteditor_stackfilter_t filter;
53         taddecoder_t decoder;
54         tadsegment segment, *filterd;
55         tadfragment_t fragment;
56         tadfragment_cursor_t cursor;
57         UB *buf;
58         Bool cont;
59         W input_result, err, len;
60         UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
61
62         err = tadfragment_initialize(&fragment);
63         if (err < 0) {
64                 return UNITTEST_RESULT_FAIL;
65         }
66
67         texteditor_stackfilter_initialize(&filter);
68
69         taddecoder_initialize(&decoder, testdata->src, testdata->src_len);
70
71         tadfragment_cursor_initialize(&cursor, &fragment);
72         for (;;) {
73                 cont = taddecoder_next(&decoder, &segment);
74                 if (cont == False) {
75                         break;
76                 }
77                 input_result = texteditor_stackfilter_setinput(&filter, &segment);
78                 if (input_result == TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FULL) {
79                         for (;;) {
80                                 cont = texteditor_stackfilter_getoutput(&filter, &filterd);
81                                 if (cont == False) {
82                                         break;
83                                 }
84                                 tadfragment_cursor_insertsegment(&cursor, filterd);
85                         }
86                         input_result = texteditor_stackfilter_setinput(&filter, &segment);
87                 }
88                 if (input_result != TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK) {
89                         result = UNITTEST_RESULT_FAIL;
90                         break;
91                 }
92         }
93         for (;;) {
94                 cont = texteditor_stackfilter_getoutput(&filter, &filterd);
95                 if (cont == False) {
96                         break;
97                 }
98                 tadfragment_cursor_insertsegment(&cursor, filterd);
99         }
100         tadfragment_cursor_finalize(&cursor);
101
102         taddecoder_finalize(&decoder);
103
104         texteditor_stackfilter_finalize(&filter);
105
106         buf = tadfragment_getbuffer(&fragment);
107         len = tadfragment_getbufferlength(&fragment);
108
109         if (len != testdata->expected_len * sizeof(TC)) {
110                 printf("length error: expected = %d, result = %d\n", testdata->expected_len * sizeof(TC), len);
111                 result = UNITTEST_RESULT_FAIL;
112         } else if (memcmp(buf, testdata->expected, len) != 0) {
113                 printf("buffer error\n");
114                 result = UNITTEST_RESULT_FAIL;
115         }
116
117         tadfragment_finalize(&fragment);
118
119         return result;
120 }
121
122 LOCAL UNITTEST_RESULT test_texteditor_stackfilter_1()
123 {
124         TC src[] = (TC[]){
125                 0xffe1, 0x0018, 0x0000, 0x0000,
126                 0x0000, 0x0000, 0x0000, 0x0000,
127                 0x0000, 0x0000, 0xff88, 0xff88,
128                 0x0021, 0x0000, 0x2422, 0xffa2,
129                 0x0006, 0x0300, 0x0101, 0x0101,
130                 0x2422,
131         };
132         W src_len = 21;
133         TC expected[] = (TC[]){
134                 0xffe1, 0x0018, 0x0000, 0x0000,
135                 0x0000, 0x0000, 0x0000, 0x0000,
136                 0x0000, 0x0000, 0xff88, 0xff88,
137                 0x0021, 0x0000, 0x2422, 0xffa2,
138                 0x0006, 0x0300, 0x0101, 0x0101,
139                 0x2422,
140         };
141         W expected_len = 21;
142         test_texteditor_stackfilter_testdata_t testdata = {
143                 src, src_len, expected, expected_len
144         };
145         return test_texteditor_stackfilter_common(&testdata);
146 }
147
148 LOCAL UNITTEST_RESULT test_texteditor_stackfilter_2()
149 {
150         TC src[] = (TC[]){
151                 0x2422, 0x2422,
152         };
153         W src_len = 2;
154         TC expected[] = (TC[]){
155                 0x2422, 0x2422,
156         };
157         W expected_len = 2;
158         test_texteditor_stackfilter_testdata_t testdata = {
159                 src, src_len, expected, expected_len
160         };
161         return test_texteditor_stackfilter_common(&testdata);
162 }
163
164 LOCAL UNITTEST_RESULT test_texteditor_stackfilter_3()
165 {
166         TC src[] = (TC[]){
167                 0x2422,
168                 0xffe3, 0x0018, 0x0000, 0x0000,
169                 0x0000, 0x0000, 0x0000, 0x0000,
170                 0x0000, 0x0000, 0xff88, 0xff88,
171                 0x0000, 0x0000,
172                 0xffb0, 0x0012, 0x0000, 0x0000,
173                 0x0000, 0x0000, 0x0000, 0x0000,
174                 0x0000, 0x0000, 0x0000,
175                 0xffe4, 0x0000,
176                 0x2422,
177         };
178         W src_len = 29;
179         TC expected[] = (TC[]){
180                 0x2422, 0x2422,
181         };
182         W expected_len = 2;
183         test_texteditor_stackfilter_testdata_t testdata = {
184                 src, src_len, expected, expected_len
185         };
186         return test_texteditor_stackfilter_common(&testdata);
187 }
188
189 LOCAL UNITTEST_RESULT test_texteditor_stackfilter_4()
190 {
191         TC src[] = (TC[]){
192                 0x2422,
193                 0xffe1, 0x0018, 0x0000, 0x0000,
194                 0x0000, 0x0000, 0x0000, 0x0000,
195                 0x0000, 0x0000, 0xff88, 0xff88,
196                 0x0021, 0x0000,
197                 0x2422,
198                 0xffe2, 0x0000,
199                 0x2422,
200         };
201         W src_len = 19;
202         TC expected[] = (TC[]){
203                 0x2422, 0x2422,
204         };
205         W expected_len = 2;
206         test_texteditor_stackfilter_testdata_t testdata = {
207                 src, src_len, expected, expected_len
208         };
209         return test_texteditor_stackfilter_common(&testdata);
210 }
211
212 EXPORT VOID test_texteditor_stackfilter_main(unittest_driver_t *driver)
213 {
214         UNITTEST_DRIVER_REGIST(driver, test_texteditor_stackfilter_1);
215         UNITTEST_DRIVER_REGIST(driver, test_texteditor_stackfilter_2);
216         UNITTEST_DRIVER_REGIST(driver, test_texteditor_stackfilter_3);
217         UNITTEST_DRIVER_REGIST(driver, test_texteditor_stackfilter_4);
218 }