aboutsummaryrefslogtreecommitdiff
path: root/re2/testing/compile_test.cc
blob: 8d92105e21501ea9003c4ab2bdb2bda6a94884a8 (plain)
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
// Copyright 2007 The RE2 Authors.  All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test prog.cc, compile.cc

#include <string>
#include <vector>
#include "util/test.h"
#include "re2/regexp.h"
#include "re2/prog.h"

DEFINE_string(show, "", "regular expression to compile and dump");

namespace re2 {

// Simple input/output tests checking that
// the regexp compiles to the expected code.
// These are just to sanity check the basic implementation.
// The real confidence tests happen by testing the NFA/DFA
// that run the compiled code.

struct Test {
  const char* regexp;
  const char* code;
};

static Test tests[] = {
  { "a",
    "1. byte [61-61] -> 2\n"
    "2. match! 0\n" },
  { "ab",
    "1. byte [61-61] -> 2\n"
    "2. byte [62-62] -> 3\n"
    "3. match! 0\n" },
  { "a|c",
    "3. alt -> 1 | 2\n"
    "1. byte [61-61] -> 4\n"
    "2. byte [63-63] -> 4\n"
    "4. match! 0\n" },
  { "a|b",
    "1. byte [61-62] -> 2\n"
    "2. match! 0\n" },
  { "[ab]",
    "1. byte [61-62] -> 2\n"
    "2. match! 0\n" },
  { "a+",
    "1. byte [61-61] -> 2\n"
    "2. alt -> 1 | 3\n"
    "3. match! 0\n" },
  { "a+?",
    "1. byte [61-61] -> 2\n"
    "2. alt -> 3 | 1\n"
    "3. match! 0\n" },
  { "a*",
    "2. alt -> 1 | 3\n"
    "1. byte [61-61] -> 2\n"
    "3. match! 0\n" },
  { "a*?",
    "2. alt -> 3 | 1\n"
    "3. match! 0\n"
    "1. byte [61-61] -> 2\n" },
  { "a?",
    "2. alt -> 1 | 3\n"
    "1. byte [61-61] -> 3\n"
    "3. match! 0\n" },
  { "a??",
    "2. alt -> 3 | 1\n"
    "3. match! 0\n"
    "1. byte [61-61] -> 3\n" },
  { "a{4}",
    "1. byte [61-61] -> 2\n"
    "2. byte [61-61] -> 3\n"
    "3. byte [61-61] -> 4\n"
    "4. byte [61-61] -> 5\n"
    "5. match! 0\n" },
  { "(a)",
    "2. capture 2 -> 1\n"
    "1. byte [61-61] -> 3\n"
    "3. capture 3 -> 4\n"
    "4. match! 0\n" },
  { "(?:a)",
    "1. byte [61-61] -> 2\n"
    "2. match! 0\n" },
  { "",
    "2. match! 0\n" },
  { ".",
    "3. alt -> 1 | 2\n"
    "1. byte [00-09] -> 4\n"
    "2. byte [0b-ff] -> 4\n"
    "4. match! 0\n" },
  { "[^ab]",
    "5. alt -> 3 | 4\n"
    "3. alt -> 1 | 2\n"
    "4. byte [63-ff] -> 6\n"
    "1. byte [00-09] -> 6\n"
    "2. byte [0b-60] -> 6\n"
    "6. match! 0\n" },
  { "[Aa]",
    "1. byte/i [61-61] -> 2\n"
    "2. match! 0\n" },
};

TEST(TestRegexpCompileToProg, Simple) {
  int failed = 0;
  for (int i = 0; i < arraysize(tests); i++) {
    const re2::Test& t = tests[i];
    Regexp* re = Regexp::Parse(t.regexp, Regexp::PerlX|Regexp::Latin1, NULL);
    if (re == NULL) {
      LOG(ERROR) << "Cannot parse: " << t.regexp;
      failed++;
      continue;
    }
    Prog* prog = re->CompileToProg(0);
    if (prog == NULL) {
      LOG(ERROR) << "Cannot compile: " << t.regexp;
      re->Decref();
      failed++;
      continue;
    }
    CHECK(re->CompileToProg(1) == NULL);
    string s = prog->Dump();
    if (s != t.code) {
      LOG(ERROR) << "Incorrect compiled code for: " << t.regexp;
      LOG(ERROR) << "Want:\n" << t.code;
      LOG(ERROR) << "Got:\n" << s;
      failed++;
    }
    delete prog;
    re->Decref();
  }
  EXPECT_EQ(failed, 0);
}

// The distinct byte ranges involved in the UTF-8 dot ([^\n]).
// Once, erroneously split between 0x3f and 0x40 because it is
// a 6-bit boundary.
static struct UTF8ByteRange {
  int lo;
  int hi;
} utf8ranges[] = {
  { 0x00, 0x09 },
  { 0x0A, 0x0A },
  { 0x10, 0x7F },
  { 0x80, 0x8F },
  { 0x90, 0x9F },
  { 0xA0, 0xBF },
  { 0xC0, 0xC1 },
  { 0xC2, 0xDF },
  { 0xE0, 0xE0 },
  { 0xE1, 0xEF },
  { 0xF0, 0xF0 },
  { 0xF1, 0xF3 },
  { 0xF4, 0xF4 },
  { 0xF5, 0xFF },
};

TEST(TestCompile, ByteRanges) {
  Regexp* re = Regexp::Parse(".", Regexp::PerlX, NULL);
  EXPECT_TRUE(re != NULL);
  Prog* prog = re->CompileToProg(0);
  EXPECT_TRUE(prog != NULL);
  EXPECT_EQ(prog->bytemap_range(), arraysize(utf8ranges));
  for (int i = 0; i < arraysize(utf8ranges); i++)
    for (int j = utf8ranges[i].lo; j <= utf8ranges[i].hi; j++)
      EXPECT_EQ(prog->bytemap()[j], i) << " byte " << j;
  delete prog;
  re->Decref();
}

}  // namespace re2