This repository was archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
359 lines (286 loc) · 9.15 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "ss.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static bool testMatch( ss_Context* ctx, ss_Format fmt, char const* p, char const* s ) {
ss_Pattern* pat = NULL;
ss_Scanner* scanner = NULL;
ss_Match* match = NULL;
pat = ss_compile( ctx, fmt, p, strlen( p ) );
if( ss_error( ctx ) )
goto fail;
scanner = ss_start( ctx, fmt, pat, s, strlen( s ) );
if( ss_error( ctx ) )
goto fail;
match = ss_match( scanner );
if( !match )
goto fail;
if( ss_loc( match ) != s )
goto fail;
if( ss_end( match ) != s + strlen( s ) )
goto fail;
ss_release( scanner );
ss_release( match );
ss_release( pat );
return true;
fail:
if( scanner )
ss_release( scanner );
if( match )
ss_release( match );
if( pat )
ss_release( pat );
return false;
}
static bool test1( void ) {
ss_Context* ctx = ss_init();
bool result = testMatch( ctx, ss_BYTES,
"Literal text, ( 'not literal' ).",
"Literal text, not literal."
);
ss_release( ctx );
return result;
}
static bool test2( void ) {
ss_Context* ctx = ss_init();
char const* p = "I have an ( 'apple' | 'orange' | 'almond' ).";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I have an apple." );
result &= testMatch( ctx, ss_BYTES, p, "I have an orange." );
result &= testMatch( ctx, ss_BYTES, p, "I have an almond." );
ss_release( ctx );
return result;
}
static bool test3( void ) {
ss_Context* ctx = ss_init();
char const* p = "I have ( 'two ' 'apples' | 'three oranges' ).";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I have two apples." );
result &= testMatch( ctx, ss_BYTES, p, "I have three oranges." );
ss_release( ctx );
return result;
}
static bool test4( void ) {
ss_Context* ctx = ss_init();
char const* p = "I eat [ 'blueberry ' ]pancakes.";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I eat pancakes." );
result &= testMatch( ctx, ss_BYTES, p, "I eat blueberry pancakes." );
ss_release( ctx );
return result;
}
static bool test5( void ) {
ss_Context* ctx = ss_init();
char const* p = "I < 'love ' >food!";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I love food!" );
result &= testMatch( ctx, ss_BYTES, p, "I love love food!" );
result &= testMatch( ctx, ss_BYTES, p, "I love love love food!" );
ss_release( ctx );
return result;
}
static bool test6( void ) {
ss_Context* ctx = ss_init();
char const* p = "I sleep{ ' a' | ' lot' | ' very' | ' little' }.";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I sleep." );
result &= testMatch( ctx, ss_BYTES, p, "I sleep a lot." );
result &= testMatch( ctx, ss_BYTES, p, "I sleep a little." );
result &= testMatch( ctx, ss_BYTES, p, "I sleep very little." );
ss_release( ctx );
return result;
}
static bool test7( void ) {
ss_Context* ctx = ss_init();
char const* p = "I drink~( ' wine' )[ ' water' | ' beer' ].";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I drink water." );
result &= testMatch( ctx, ss_BYTES, p, "I drink beer." );
result &= !testMatch( ctx, ss_BYTES, p, "I drink wine." );
ss_release( ctx );
return result;
}
static bool test8( void ) {
ss_Context* ctx = ss_init();
char const* p = "I eat ^( 't' )( 'tacos' | 'enchiladas' | 'fries' ).";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I eat tacos." );
result &= !testMatch( ctx, ss_BYTES, p, "I eat enchiladas." );
result &= !testMatch( ctx, ss_BYTES, p, "I eat fries." );
ss_release( ctx );
return result;
}
static bool test9( void ) {
ss_Context* ctx = ss_init();
char const* p = "I ate ( digit ) tacos.";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p, "I ate 1 tacos." );
result &= testMatch( ctx, ss_BYTES, p, "I ate 2 tacos." );
result &= testMatch( ctx, ss_BYTES, p, "I ate 3 tacos." );
result &= testMatch( ctx, ss_BYTES, p, "I ate 9 tacos." );
result &= !testMatch( ctx, ss_BYTES, p, "I ate N tacos." );
ss_release( ctx );
return result;
}
static bool test10( void ) {
ss_Context* ctx = ss_init();
char const* p = "( 104 101 108 108 111 )";
bool result = testMatch( ctx, ss_BYTES, p, "hello" );
ss_release( ctx );
return result;
}
static bool test11( void ) {
ss_Context* ctx = ss_init();
char const splatSrc[] = "< ~'/' ~'.' char >";
ss_Pattern* splatPat = ss_compile( ctx, ss_BYTES, splatSrc, strlen( splatSrc ) );
ss_define( ctx, "splat", splatPat );
ss_release( splatPat );
char const quarkSrc[] = "(char)";
ss_Pattern* quarkPat = ss_compile( ctx, ss_BYTES, quarkSrc, strlen( quarkSrc ) );
ss_define( ctx, "quark", quarkPat );
ss_release( quarkPat );
char const* p1 = "*/file.???";
bool result = true;
result &= testMatch( ctx, ss_BYTES, p1, "dir1/file.txt" );
result &= testMatch( ctx, ss_BYTES, p1, "dir2/file.csv" );
result &= testMatch( ctx, ss_BYTES, p1, "dir3/file.dat" );
result &= !testMatch( ctx, ss_BYTES, p1, "dir1/dir2/file.txt" );
char const* p2 = "*/*/*.txt";
result &= testMatch( ctx, ss_BYTES, p2, "dir1/dir2/thing.txt" );
ss_release( ctx );
return result;
}
static bool test12( void ) {
ss_Context* ctx = ss_init();
char const* p = "I have two ( 'apples':apples | 'oranges':oranges ):fruit.";
char const* s = "I have two apples.";
ss_Pattern* pat = NULL;
ss_Scanner* scanner = NULL;
ss_Match* match = NULL;
ss_Match* fruit = NULL;
ss_Match* apples = NULL;
ss_Match* oranges = NULL;
pat = ss_compile( ctx, ss_BYTES, p, strlen( p ) );
if( ss_error( ctx ) )
goto fail;
scanner = ss_start( ctx, ss_BYTES, pat, s, strlen( s ) );
if( ss_error( ctx ) )
goto fail;
match = ss_match( scanner );
if( !match )
goto fail;
fruit = ss_get( match, "fruit" );
if( !fruit )
goto fail;
if( ss_loc( fruit ) != s + 11 )
goto fail;
if( ss_end( fruit ) != s + 17 )
goto fail;
apples = ss_get( fruit, "apples" );
if( !apples )
goto fail;
oranges = ss_get( fruit, "oranges" );
if( oranges )
goto fail;
ss_release( apples );
ss_release( fruit );
ss_release( scanner );
ss_release( match );
ss_release( pat );
ss_release( ctx );
return true;
fail:
if( apples )
ss_release( apples );
if( oranges )
ss_release( oranges );
if( fruit )
ss_release( fruit );
if( scanner )
ss_release( scanner );
if( match )
ss_release( match );
if( pat )
ss_release( pat );
ss_release( ctx );
return false;
}
static bool test13( void ) {
ss_Context* ctx = ss_init();
char const* p = "( 'apple' | 'orange' | 'pear' )";
char const* s = "I ate an apple.";
ss_Pattern* pat = NULL;
ss_Scanner* scanner = NULL;
ss_Match* match = NULL;
pat = ss_compile( ctx, ss_BYTES, p, strlen( p ) );
if( ss_error( ctx ) )
goto fail;
scanner = ss_start( ctx, ss_BYTES, pat, s, strlen( s ) );
if( ss_error( ctx ) )
goto fail;
match = ss_find( scanner );
if( !match )
goto fail;
if( ss_loc( match ) != s + 9 )
goto fail;
if( ss_end( match ) != s + 14 )
goto fail;
ss_release( scanner );
ss_release( match );
ss_release( pat );
ss_release( ctx );
return true;
fail:
if( scanner )
ss_release( scanner );
if( match )
ss_release( match );
if( pat )
ss_release( pat );
ss_release( ctx );
return false;
}
static bool test14( void ) {
ss_Context* ctx = ss_init();
char const* p = "( 20170 26085 12399 )";
bool result = testMatch( ctx, ss_CHARS, p, "今日は" );
ss_release( ctx );
return result;
}
static bool test15( void ) {
ss_Context* ctx = ss_init();
bool result = true;
char const* p1 = "This is ( '(' )not( ')' ) very interesting.";
result &= testMatch( ctx, ss_CHARS, p1, "This is (not) very interesting." );
char const* p2 = "This is \\[not\\] very interesting.";
result &= testMatch( ctx, ss_CHARS, p2, "This is [not] very interesting." );
ss_release( ctx );
return result;
}
int main( void ) {
bool passing = true;
passing &= test1();
passing &= test2();
passing &= test3();
passing &= test4();
passing &= test5();
passing &= test6();
passing &= test7();
passing &= test8();
passing &= test9();
passing &= test10();
passing &= test11();
passing &= test12();
passing &= test13();
passing &= test14();
passing &= test15();
if( passing ) {
printf( "PASSED\n" );
return 0;
}
else {
printf( "FAILED\n" );
return 1;
}
}