@@ -27,27 +27,39 @@ using ByteNfa = log_surgeon::finite_automata::Nfa<ByteNfaState>;
27
27
28
28
namespace {
29
29
/* *
30
- * Helper function to compare the actual and expected DFA serialized strings, and compare them line
31
- * by line to ensure the serialized DFA output is correct .
30
+ * Generates a DFA for the given variable schemas, then serializes the DFA and compares it with
31
+ * `expected_serialized_dfa` .
32
32
*
33
- * @param actual_dfa The actual DFA serialized string to be compared .
34
- * @param expected_serialized_dfa The expected DFA serialized string for comparison .
33
+ * @param var_schemas Vector of variable schemas from which to construct the DFA .
34
+ * @param expected_serialized_dfa Expected serialized string representation of the DFA .
35
35
*/
36
- auto compare_serialized_dfa (ByteDfa const & actual_dfa, std:: string const & expected_serialized_dfa)
36
+ auto test_dfa (std::vector<string> const & var_schemas, string const & expected_serialized_dfa)
37
37
-> void;
38
38
39
- auto compare_serialized_dfa (ByteDfa const & actual_dfa, std:: string const & expected_serialized_dfa)
39
+ auto test_dfa (std::vector<string> const & var_schemas, string const & expected_serialized_dfa)
40
40
-> void {
41
- auto const optional_actual_serialized_dfa = actual_dfa.serialize ();
42
- REQUIRE (optional_actual_serialized_dfa.has_value ());
43
- auto const & actual_serialized_dfa = optional_actual_serialized_dfa.value ();
41
+ Schema schema;
42
+ for (auto const & var_schema : var_schemas) {
43
+ schema.add_variable (var_schema, -1 );
44
+ }
45
+ auto const schema_ast = schema.release_schema_ast_ptr ();
46
+ vector<ByteLexicalRule> rules;
47
+ for (size_t i{0 }; i < var_schemas.size (); i++) {
48
+ auto & capture_rule_ast = dynamic_cast <SchemaVarAST&>(*schema_ast->m_schema_vars [i]);
49
+ rules.emplace_back (i, std::move (capture_rule_ast.m_regex_ptr ));
50
+ }
51
+ ByteNfa const nfa{rules};
52
+ ByteDfa const dfa{nfa};
44
53
45
- stringstream ss_actual{actual_serialized_dfa};
54
+ // Compare expected and actual line-by-line
55
+ auto const optional_actual_serialized_dfa = dfa.serialize ();
56
+ REQUIRE (optional_actual_serialized_dfa.has_value ());
57
+ stringstream ss_actual{optional_actual_serialized_dfa.value ()};
46
58
stringstream ss_expected{expected_serialized_dfa};
47
59
string actual_line;
48
60
string expected_line;
49
61
50
- CAPTURE (actual_serialized_dfa );
62
+ CAPTURE (optional_actual_serialized_dfa. value () );
51
63
CAPTURE (expected_serialized_dfa);
52
64
while (getline (ss_actual, actual_line) && getline (ss_expected, expected_line)) {
53
65
REQUIRE (actual_line == expected_line);
@@ -60,18 +72,7 @@ auto compare_serialized_dfa(ByteDfa const& actual_dfa, std::string const& expect
60
72
} // namespace
61
73
62
74
TEST_CASE (" Test Simple Untagged DFA" , " [DFA]" ) {
63
- Schema schema;
64
- string const var_name{" capture" };
65
- string const var_schema{var_name + " :" + " userID=123" };
66
- schema.add_variable (var_schema, -1 );
67
-
68
- auto const schema_ast = schema.release_schema_ast_ptr ();
69
- auto & capture_rule_ast = dynamic_cast <SchemaVarAST&>(*schema_ast->m_schema_vars .at (0 ));
70
- vector<ByteLexicalRule> rules;
71
- rules.emplace_back (0 , std::move (capture_rule_ast.m_regex_ptr ));
72
- ByteNfa const nfa{rules};
73
- ByteDfa const dfa{nfa};
74
-
75
+ string const var_schema{" capture:userID=123" };
75
76
string const expected_serialized_dfa{
76
77
" 0:byte_transitions={u-()->1}\n "
77
78
" 1:byte_transitions={s-()->2}\n "
@@ -85,23 +86,11 @@ TEST_CASE("Test Simple Untagged DFA", "[DFA]") {
85
86
" 9:byte_transitions={3-()->10}\n "
86
87
" 10:accepting_tags={0},accepting_operations={},byte_transitions={}\n "
87
88
};
88
-
89
- compare_serialized_dfa (dfa, expected_serialized_dfa);
89
+ test_dfa ({var_schema}, expected_serialized_dfa);
90
90
}
91
91
92
92
TEST_CASE (" Test Complex Untagged DFA" , " [DFA]" ) {
93
- Schema schema;
94
- string const var_name{" capture" };
95
- string const var_schema{var_name + " :" + " Z|(A[abcd]B\\ d+C)" };
96
- schema.add_variable (var_schema, -1 );
97
-
98
- auto const schema_ast = schema.release_schema_ast_ptr ();
99
- auto & capture_rule_ast = dynamic_cast <SchemaVarAST&>(*schema_ast->m_schema_vars .at (0 ));
100
- vector<ByteLexicalRule> rules;
101
- rules.emplace_back (0 , std::move (capture_rule_ast.m_regex_ptr ));
102
- ByteNfa const nfa{rules};
103
- ByteDfa const dfa{nfa};
104
-
93
+ string const var_schema{" capture:Z|(A[abcd]B\\ d+C)" };
105
94
string const expected_serialized_dfa{
106
95
" 0:byte_transitions={A-()->1,Z-()->2}\n "
107
96
" 1:byte_transitions={a-()->3,b-()->3,c-()->3,d-()->3}\n "
@@ -112,24 +101,11 @@ TEST_CASE("Test Complex Untagged DFA", "[DFA]") {
112
101
" 5:byte_transitions={0-()->5,1-()->5,2-()->5,3-()->5,4-()->5,5-()->5,6-()->5,7-()->5,"
113
102
" 8-()->5,9-()->5,C-()->2}\n "
114
103
};
115
-
116
- compare_serialized_dfa (dfa, expected_serialized_dfa);
104
+ test_dfa ({var_schema}, expected_serialized_dfa);
117
105
}
118
106
119
107
TEST_CASE (" Test Simple Tagged DFA" , " [DFA]" ) {
120
- Schema schema;
121
- string const var_name{" capture" };
122
- string const var_schema{var_name + " :" + " userID=(?<uID>123)" };
123
-
124
- schema.add_variable (var_schema, -1 );
125
-
126
- auto const schema_ast = schema.release_schema_ast_ptr ();
127
- auto & capture_rule_ast = dynamic_cast <SchemaVarAST&>(*schema_ast->m_schema_vars .at (0 ));
128
- vector<ByteLexicalRule> rules;
129
- rules.emplace_back (0 , std::move (capture_rule_ast.m_regex_ptr ));
130
- ByteNfa const nfa{rules};
131
- ByteDfa const dfa{nfa};
132
-
108
+ string const var_schema{" capture:userID=(?<uID>123)" };
133
109
string const expected_serialized_dfa{
134
110
" 0:byte_transitions={u-()->1}\n "
135
111
" 1:byte_transitions={s-()->2}\n "
@@ -143,27 +119,12 @@ TEST_CASE("Test Simple Tagged DFA", "[DFA]") {
143
119
" 9:byte_transitions={3-()->10}\n "
144
120
" 10:accepting_tags={0},accepting_operations={2c4,3p},byte_transitions={}\n "
145
121
};
146
-
147
- compare_serialized_dfa (dfa, expected_serialized_dfa);
122
+ test_dfa ({var_schema}, expected_serialized_dfa);
148
123
}
149
124
150
125
TEST_CASE (" Test Complex Tagged DFA" , " [DFA]" ) {
151
- Schema schema;
152
- string const var_name{" capture" };
153
- string const var_schema{
154
- var_name + " :"
155
- + " Z|(A(?<letter>((?<letter1>(a)|(b))|(?<letter2>(c)|(d))))B(?"
156
- " <containerID>\\ d+)C)"
157
- };
158
- schema.add_variable (var_schema, -1 );
159
-
160
- auto const schema_ast = schema.release_schema_ast_ptr ();
161
- auto & capture_rule_ast = dynamic_cast <SchemaVarAST&>(*schema_ast->m_schema_vars .at (0 ));
162
- vector<ByteLexicalRule> rules;
163
- rules.emplace_back (0 , std::move (capture_rule_ast.m_regex_ptr ));
164
- ByteNfa const nfa{rules};
165
- ByteDfa const dfa{nfa};
166
-
126
+ string const var_schema{" capture:Z|(A(?<letter>((?<letter1>(a)|(b))|(?<letter2>(c)|(d))))B(?<"
127
+ " containerID>\\ d+)C)" };
167
128
string const expected_serialized_dfa{
168
129
" 0:byte_transitions={A-()->1,Z-()->2}\n "
169
130
" 1:byte_transitions={a-(16p,17p)->3,b-(16p,17p)->3,c-(18p,17p)->4,d-(18p,17p)->4}\n "
@@ -178,6 +139,20 @@ TEST_CASE("Test Complex Tagged DFA", "[DFA]") {
178
139
" 7:accepting_tags={0},accepting_operations={8c16,9c19,10c20,11c21,12c17,13c22,14c27,"
179
140
" 15c28},byte_transitions={}\n "
180
141
};
142
+ test_dfa ({var_schema}, expected_serialized_dfa);
143
+ }
181
144
182
- compare_serialized_dfa (dfa, expected_serialized_dfa);
145
+ TEST_CASE (" Test Repetition Tagged DFA" , " [DFA]" ) {
146
+ string const var_schema{" capture:([a]+=(?<val>1+),)+" };
147
+ string const expected_serialized_dfa{
148
+ " 0:byte_transitions={a-()->1}\n "
149
+ " 1:byte_transitions={=-()->2,a-()->1}\n "
150
+ " 2:byte_transitions={1-(4p)->3}\n "
151
+ " 3:byte_transitions={,-(5p)->4,1-()->3}\n "
152
+ " 4:accepting_tags={0},accepting_operations={2c4,3c5},byte_transitions={a-()->5}\n "
153
+ " 5:byte_transitions={=-()->6,a-()->5}\n "
154
+ " 6:byte_transitions={1-(6p)->7}\n "
155
+ " 7:byte_transitions={,-(5p,4c6)->4,1-()->7}\n "
156
+ };
157
+ test_dfa ({var_schema}, expected_serialized_dfa);
183
158
}
0 commit comments