Cleaning up forgotten symbols. Modifying simple test to use test harness. Added comments for more verbose descriptions of what each test is accomplishing.

This commit is contained in:
Clint Herron 2024-04-29 14:29:42 -04:00
parent 7fe2fb3fed
commit b712ae4047

View file

@ -52,8 +52,8 @@ static bool match_string(const std::string & input, llama_grammar* grammar) {
return false; return false;
} }
static void test_grammar(const std::string & grammar_str, const std::vector<std::string> & passing_strings, const std::vector<std::string> & failing_strings) { static void test_grammar(const std::string & test_desc, const std::string & grammar_str, const std::vector<std::string> & passing_strings, const std::vector<std::string> & failing_strings) {
fprintf(stderr, "⚪ Testing grammar: %s\n", grammar_str.c_str()); fprintf(stderr, "⚪ Testing %s. Grammar: %s\n", test_desc.c_str(), grammar_str.c_str());
fflush(stderr); fflush(stderr);
auto grammar = build_grammar(grammar_str); auto grammar = build_grammar(grammar_str);
@ -61,7 +61,7 @@ static void test_grammar(const std::string & grammar_str, const std::vector<std:
// Save the original grammar stacks so that we can reset after every new string we want to test // Save the original grammar stacks so that we can reset after every new string we want to test
auto original_stacks = grammar->stacks; auto original_stacks = grammar->stacks;
fprintf(stderr, " Checking valid strings:\n"); fprintf(stderr, " Valid strings:\n");
// Passing strings // Passing strings
for (const auto & test_string : passing_strings) { for (const auto & test_string : passing_strings) {
@ -82,7 +82,7 @@ static void test_grammar(const std::string & grammar_str, const std::vector<std:
grammar->stacks = original_stacks; grammar->stacks = original_stacks;
} }
fprintf(stderr, " Checking invalid strings:\n"); fprintf(stderr, " Invalid strings:\n");
// Failing strings // Failing strings
for (const auto & test_string : failing_strings) { for (const auto & test_string : failing_strings) {
@ -108,24 +108,33 @@ static void test_grammar(const std::string & grammar_str, const std::vector<std:
static void test_simple_grammar() { static void test_simple_grammar() {
// Test case for a simple grammar // Test case for a simple grammar
const std::string grammar_str = R"""(root ::= expr test_grammar(
expr ::= term ("+" term)* "simple grammar",
term ::= number R"""(
number ::= [0-9]+)"""; root ::= expr
expr ::= term ("+" term)*
auto grammar = build_grammar(grammar_str); term ::= number
number ::= [0-9]+)""",
bool matched = match_string("123+456", grammar); // Passing strings
{
assert(matched); "42",
"1+2+3+4+5",
// Clean up allocated memory "123+456",
llama_grammar_free(grammar); },
// Failing strings
{
"+",
"/ 3",
"1+2+3+4+5+",
"12a45",
}
);
} }
static void test_complex_grammar() { static void test_complex_grammar() {
// Test case for a more complex grammar, with both failure strings and success strings // Test case for a more complex grammar, with both failure strings and success strings
test_grammar( test_grammar(
"medium complexity grammar",
// Grammar // Grammar
R"""( R"""(
root ::= expression root ::= expression
@ -187,6 +196,7 @@ static void test_quantifiers() {
// A collection of tests to exercise * + and ? quantifiers // A collection of tests to exercise * + and ? quantifiers
test_grammar( test_grammar(
"* quantifier",
// Grammar // Grammar
R"""(root ::= "a"*)""", R"""(root ::= "a"*)""",
// Passing strings // Passing strings
@ -207,6 +217,7 @@ static void test_quantifiers() {
} }
); );
test_grammar( test_grammar(
"+ quantifier",
// Grammar // Grammar
R"""(root ::= "a"+)""", R"""(root ::= "a"+)""",
// Passing strings // Passing strings
@ -227,6 +238,7 @@ static void test_quantifiers() {
} }
); );
test_grammar( test_grammar(
"? quantifier",
// Grammar // Grammar
R"""(root ::= "a"?)""", R"""(root ::= "a"?)""",
// Passing strings // Passing strings
@ -243,6 +255,7 @@ static void test_quantifiers() {
} }
); );
test_grammar( test_grammar(
"mixed quantifiers",
// Grammar // Grammar
R"""( R"""(
root ::= cons+ vowel* cons? (vowel cons)* root ::= cons+ vowel* cons? (vowel cons)*
@ -269,7 +282,7 @@ static void test_quantifiers() {
} }
static void test_failure_missing_root() { static void test_failure_missing_root() {
fprintf(stderr, "🟢 Testing for missing root node:\n"); fprintf(stderr, "⚪ Testing missing root node:\n");
// Test case for a grammar that is missing a root rule // Test case for a grammar that is missing a root rule
const std::string grammar_str = R"""(rot ::= expr const std::string grammar_str = R"""(rot ::= expr
expr ::= term ("+" term)* expr ::= term ("+" term)*
@ -287,7 +300,7 @@ number ::= [0-9]+)""";
} }
static void test_failure_missing_reference() { static void test_failure_missing_reference() {
fprintf(stderr, "🟢 Testing for missing reference node:\n"); fprintf(stderr, "⚪ Testing missing reference node:\n");
// Test case for a grammar that is missing a referenced rule // Test case for a grammar that is missing a referenced rule
const std::string grammar_str = const std::string grammar_str =