Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / patmatch_test.c @ 6b3f1a54

History | View | Annotate | Download (2.92 KB)

1
/*
2
 *        BIRD Library -- Pattern Matching Tests
3
 *
4
 *        (c) 2015 CZ.NIC z.s.p.o.
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "test/birdtest.h"
10

    
11
#include "nest/bird.h"
12
#include "lib/string.h"
13

    
14
#define MATCH                (int) { 1 }
15
#define NOMATCH                (int) { 0 }
16

    
17
struct match_pair {
18
  byte *pattern;
19
  byte *data;
20
};
21

    
22
static int
23
test_matching(void *out_, const void *in_, const void *expected_out_)
24
{
25
  int *out = out_;
26
  const struct match_pair *in = in_;
27
  const int *expected_out = expected_out_;
28

    
29
  *out = patmatch(in->pattern, in->data);
30

    
31
  return *out == *expected_out;
32
}
33

    
34
static void
35
fmt_match_pair(char *buf, size_t size, const void *data)
36
{
37
  const struct match_pair *mp = data;
38
  snprintf(buf, size, "pattern: '%s', subject: '%s'", mp->pattern, mp->data);
39
}
40

    
41
static void
42
fmt_match_result(char *buf, size_t size, const void *data)
43
{
44
  const int *result = data;
45
  snprintf(buf, size, *result ? "match" : "no-match");
46
}
47

    
48
static int
49
t_matching(void)
50
{
51
  struct bt_pair test_vectors[] = {
52
    {
53
      .in  = & (struct match_pair) {
54
        .pattern = "",
55
        .data    = "",
56
      },
57
      .out = & MATCH,
58
    },
59
    {
60
      .in  = & (struct match_pair) {
61
        .pattern = "*",
62
        .data    = "",
63
      },
64
      .out = & MATCH,
65
    },
66
    {
67
      .in  = & (struct match_pair) {
68
        .pattern = "\\*",
69
        .data    = "*",
70
      },
71
      .out = & MATCH,
72
    },
73
    {
74
      .in  = & (struct match_pair) {
75
        .pattern = "\\*",
76
        .data    = "a",
77
      },
78
      .out = & NOMATCH,
79
    },
80
    {
81
      .in  = & (struct match_pair) {
82
        .pattern = "?",
83
        .data    = "",
84
      },
85
      .out = & NOMATCH,
86
    },
87
    {
88
      .in  = & (struct match_pair) {
89
        .pattern = "abcdefghijklmnopqrstuvwxyz",
90
        .data    = "abcdefghijklmnopqrstuvwxyz",
91
      },
92
      .out = & MATCH,
93
    },
94
    {
95
      .in  = & (struct match_pair) {
96
        .pattern = "??????????????????????????",
97
        .data    = "abcdefghijklmnopqrstuvwxyz",
98
      },
99
      .out = & MATCH,
100
    },
101
    {
102
      .in  = & (struct match_pair) {
103
        .pattern = "*abcdefghijklmnopqrstuvwxyz*",
104
        .data    =  "abcdefghijklmnopqrstuvwxyz",
105
      },
106
      .out = & MATCH,
107
    },
108
    {
109
      .in  = & (struct match_pair) {
110
        .pattern = "ab?defg*jklmnop*stu*wxy*z",
111
        .data    = "abcdefghijklmnopqrstuvwxyz",
112
      },
113
      .out = & MATCH,
114
    },
115
    {
116
      .in  = & (struct match_pair) {
117
        .pattern = "abcdefghijklmnopqrstuvwxyz",
118
        .data    = "abcdefghijklmnopqrtuvwxyz",
119
      },
120
      .out = & NOMATCH,
121
    },
122
    {
123
      .in  = & (struct match_pair) {
124
        .pattern = "abcdefghijklmnopqr?uvwxyz",
125
        .data    = "abcdefghijklmnopqrstuvwxyz",
126
      },
127
      .out = & NOMATCH,
128
    },
129
    {
130
      .in  = & (struct match_pair) {
131
        .pattern = "aa*aaaaa?aaaaaaaaaaaaaaaaaaa",
132
        .data    = "aaaaaaaaaaaaaaaaaaaaaaaaaa",
133
      },
134
      .out = & NOMATCH,
135
    },
136
  };
137

    
138
  return bt_assert_batch(test_vectors, test_matching, fmt_match_pair, fmt_match_result);
139
}
140

    
141
int
142
main(int argc, char *argv[])
143
{
144
  bt_init(argc, argv);
145

    
146
  bt_test_suite(t_matching, "Pattern matching");
147

    
148
  return bt_exit_value();
149
}