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
|
#include "grok.h"
#include "test.h"
void test_grok_pattern_add_and_find_work(void) {
INIT;
const char *regexp = NULL;
size_t len = 0;
grok_pattern_add(&grok, "WORD", 5, "\\w+", 3);
grok_pattern_add(&grok, "TEST", 5, "TEST", 4);
grok_pattern_find(&grok, "WORD", 5, ®exp, &len);
CU_ASSERT(regexp != NULL);
CU_ASSERT(len == 3);
CU_ASSERT(!strncmp(regexp, "\\w+", len));
//free(regexp);
grok_pattern_find(&grok, "TEST", 5, ®exp, &len);
CU_ASSERT(regexp != NULL);
CU_ASSERT(len == 4);
CU_ASSERT(!strncmp(regexp, "TEST", len));
//free(regexp);
CLEANUP;
}
void test_pattern_parse(void) {
const char *name, *regexp;
size_t name_len, regexp_len;
_pattern_parse_string("WORD \\w+", &name, &name_len, ®exp, ®exp_len);
CU_ASSERT(!strncmp(name, "WORD", name_len));
CU_ASSERT(!strncmp(regexp, "\\w+", regexp_len));
CU_ASSERT(name_len == 4);
CU_ASSERT(regexp_len == 3);
_pattern_parse_string(" NUM numtest", &name, &name_len, ®exp, ®exp_len);
CU_ASSERT(!strncmp(name, "NUM", name_len));
CU_ASSERT(!strncmp(regexp, "numtest", regexp_len));
CU_ASSERT(name_len == 3);
CU_ASSERT(regexp_len == 7);
_pattern_parse_string(" NUMNUM numtest",
&name, &name_len, ®exp, ®exp_len);
CU_ASSERT(!strncmp(name, "NUMNUM", name_len));
CU_ASSERT(!strncmp(regexp, "numtest", regexp_len));
CU_ASSERT(name_len == 6);
CU_ASSERT(regexp_len == 7);
}
void test_pattern_import_from_string(void) {
INIT;
char *buf = "WORD \\w+\n"
"TEST test\n"
"# This is a comment\n"
"FOO bar\n";
buf = strdup(buf);
grok_patterns_import_from_string(&grok, buf);
//CU_ASSERT(!strcmp(grok_pattern_find(&grok, "WORD", 5), "\\w+"));
//CU_ASSERT(!strcmp(grok_pattern_find(&grok, "TEST", 5), "test"));
//CU_ASSERT(!strcmp(grok_pattern_find(&grok, "FOO", 4), "bar"));
free(buf);
CLEANUP;
}
|