patmatch_test.c 2.92 KB
Newer Older
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
/*
 *	BIRD Library -- Pattern Matching Tests
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "test/birdtest.h"

#include "nest/bird.h"
#include "lib/string.h"

#define MATCH		(int) { 1 }
#define NOMATCH		(int) { 0 }

struct match_pair {
  byte *pattern;
  byte *data;
};

static int
test_matching(void *out_, const void *in_, const void *expected_out_)
{
  int *out = out_;
  const struct match_pair *in = in_;
  const int *expected_out = expected_out_;

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

31
  return *out == *expected_out;
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
}

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

static void
fmt_match_result(char *buf, size_t size, const void *data)
{
  const int *result = data;
  snprintf(buf, size, *result ? "match" : "no-match");
}

static int
t_matching(void)
{
  struct bt_pair test_vectors[] = {
    {
      .in  = & (struct match_pair) {
	.pattern = "",
	.data    = "",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "*",
	.data    = "",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "\\*",
	.data    = "*",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "\\*",
	.data    = "a",
      },
      .out = & NOMATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "?",
	.data    = "",
      },
      .out = & NOMATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "abcdefghijklmnopqrstuvwxyz",
	.data    = "abcdefghijklmnopqrstuvwxyz",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "??????????????????????????",
	.data    = "abcdefghijklmnopqrstuvwxyz",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "*abcdefghijklmnopqrstuvwxyz*",
	.data    =  "abcdefghijklmnopqrstuvwxyz",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "ab?defg*jklmnop*stu*wxy*z",
	.data    = "abcdefghijklmnopqrstuvwxyz",
      },
      .out = & MATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "abcdefghijklmnopqrstuvwxyz",
	.data    = "abcdefghijklmnopqrtuvwxyz",
      },
      .out = & NOMATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "abcdefghijklmnopqr?uvwxyz",
	.data    = "abcdefghijklmnopqrstuvwxyz",
      },
      .out = & NOMATCH,
    },
    {
      .in  = & (struct match_pair) {
	.pattern = "aa*aaaaa?aaaaaaaaaaaaaaaaaaa",
	.data    = "aaaaaaaaaaaaaaaaaaaaaaaaaa",
      },
      .out = & NOMATCH,
    },
  };

  return bt_assert_batch(test_vectors, test_matching, fmt_match_pair, fmt_match_result);
}

int
main(int argc, char *argv[])
{
  bt_init(argc, argv);

  bt_test_suite(t_matching, "Pattern matching");

  return bt_exit_value();
}