各種ライブラリ
Revision | 606d379a66e25eabbe9ab55edc3bdd21eaad5a76 (tree) |
---|---|
Time | 2014-09-24 01:50:00 |
Author | ornse01 <ornse01@user...> |
Commiter | ornse01 |
implement filtering for TAD segments nesting.
git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchanf/trunk@630 20a0b8eb-f62a-4a12-8fe1-b598822500fb
@@ -0,0 +1,218 @@ | ||
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 | +} |
@@ -0,0 +1,109 @@ | ||
1 | +/* | |
2 | + * 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 "texteditor_stackfilter.h" | |
28 | + | |
29 | +#include <bstdio.h> | |
30 | + | |
31 | +#include <tad/tadsegment.h> | |
32 | +#include <tad/tadlangcode.h> | |
33 | + | |
34 | +#ifdef BCHAN_CONFIG_DEBUG | |
35 | +# define DP(arg) printf arg | |
36 | +# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err) | |
37 | +#else | |
38 | +# define DP(arg) /**/ | |
39 | +# define DP_ER(msg, err) /**/ | |
40 | +#endif | |
41 | + | |
42 | +IMPORT W texteditor_stackfilter_setinput(texteditor_stackfilter_t *filter, tadsegment *segment) | |
43 | +{ | |
44 | + W nestlevel, err; | |
45 | + TADSTACK_DATATYPE type; | |
46 | + TADSTACK_RESULT result; | |
47 | + | |
48 | + if (filter->state == TEXTEDITOR_STACKFILTER_STATE_HAS_RESULT) { | |
49 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FULL; | |
50 | + } | |
51 | + | |
52 | + result = tadstack_inputsegment(&filter->tadstack, segment); | |
53 | + | |
54 | + nestlevel = tadstack_nestlevel(&filter->tadstack); | |
55 | + if (nestlevel < 0) { | |
56 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FORMAT_ERROR; | |
57 | + } | |
58 | + if (nestlevel > 0) { | |
59 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK; | |
60 | + } | |
61 | + if (result == TADSTACK_RESULT_POP_STACK) { | |
62 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK; | |
63 | + } | |
64 | + err = tadstack_currentdata(&filter->tadstack, &type); | |
65 | + if (err < 0) { | |
66 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FORMAT_ERROR; | |
67 | + } | |
68 | + if (type != TADSTACK_DATATYPE_TEXT) { | |
69 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK; | |
70 | + } | |
71 | + | |
72 | + filter->state = TEXTEDITOR_STACKFILTER_STATE_HAS_RESULT; | |
73 | + filter->result_buffer.segs[0] = *segment; | |
74 | + filter->result_buffer.used = 1; | |
75 | + filter->result_buffer.consumed = 0; | |
76 | + | |
77 | + return TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK; | |
78 | +} | |
79 | + | |
80 | +IMPORT Bool texteditor_stackfilter_getoutput(texteditor_stackfilter_t *filter, tadsegment **segment) | |
81 | +{ | |
82 | + if (filter->state == TEXTEDITOR_STACKFILTER_STATE_WAIT_INPUT) { | |
83 | + return False; | |
84 | + } | |
85 | + | |
86 | + *segment = filter->result_buffer.segs + filter->result_buffer.consumed; | |
87 | + filter->result_buffer.consumed++; | |
88 | + | |
89 | + if (filter->result_buffer.consumed >= filter->result_buffer.used) { | |
90 | + filter->result_buffer.used = 0; | |
91 | + filter->result_buffer.consumed = 0; | |
92 | + filter->state = TEXTEDITOR_STACKFILTER_STATE_WAIT_INPUT; | |
93 | + } | |
94 | + | |
95 | + return True; | |
96 | +} | |
97 | + | |
98 | +IMPORT VOID texteditor_stackfilter_initialize(texteditor_stackfilter_t *filter) | |
99 | +{ | |
100 | + tadstack_initialize(&filter->tadstack); | |
101 | + filter->state = TEXTEDITOR_STACKFILTER_STATE_WAIT_INPUT; | |
102 | + filter->result_buffer.used = 0; | |
103 | + filter->result_buffer.consumed = 0; | |
104 | +} | |
105 | + | |
106 | +IMPORT VOID texteditor_stackfilter_finalize(texteditor_stackfilter_t *filter) | |
107 | +{ | |
108 | + tadstack_finalize(&filter->tadstack); | |
109 | +} |
@@ -0,0 +1,59 @@ | ||
1 | +/* | |
2 | + * texteditor_stackfilter.h | |
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 <basic.h> | |
28 | + | |
29 | +#include <tad/tadsegment.h> | |
30 | +#include <tad/tadstack.h> | |
31 | + | |
32 | +#ifndef __TEXTEDITOR_STACKFILTER_H__ | |
33 | +#define __TEXTEDITOR_STACKFILTER_H__ | |
34 | + | |
35 | +/* Functionality name: texteditor */ | |
36 | +/* Detail name: stackfilter */ | |
37 | +struct texteditor_stackfilter_t_ { | |
38 | + enum { | |
39 | + TEXTEDITOR_STACKFILTER_STATE_WAIT_INPUT, | |
40 | + TEXTEDITOR_STACKFILTER_STATE_HAS_RESULT, | |
41 | + } state; | |
42 | + tadstack_t tadstack; | |
43 | + struct { | |
44 | + tadsegment segs[1]; | |
45 | + W used; | |
46 | + W consumed; | |
47 | + } result_buffer; | |
48 | +}; | |
49 | +typedef struct texteditor_stackfilter_t_ texteditor_stackfilter_t; | |
50 | + | |
51 | +IMPORT VOID texteditor_stackfilter_initialize(texteditor_stackfilter_t *filter); | |
52 | +IMPORT VOID texteditor_stackfilter_finalize(texteditor_stackfilter_t *filter); | |
53 | +IMPORT W texteditor_stackfilter_setinput(texteditor_stackfilter_t *filter, tadsegment *segment); | |
54 | +#define TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_OK (0) | |
55 | +#define TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FULL (1) | |
56 | +#define TEXTEDITOR_STACKFILTER_SETINPUT_RESULT_FORMAT_ERROR (-1) | |
57 | +IMPORT Bool texteditor_stackfilter_getoutput(texteditor_stackfilter_t *filter, tadsegment **segment); | |
58 | + | |
59 | +#endif |