Implementing suggestions from @ochafik -- grammars and test strings now print and flush before tests to aid in debugging segfaults and whatnot.
This commit is contained in:
parent
a273de3c5a
commit
7fe2fb3fed
1 changed files with 16 additions and 5 deletions
|
@ -53,21 +53,27 @@ static bool match_string(const std::string & input, llama_grammar* grammar) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 & 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 grammar: %s\n", grammar_str.c_str());
|
||||||
|
fflush(stderr);
|
||||||
|
|
||||||
auto grammar = build_grammar(grammar_str);
|
auto grammar = build_grammar(grammar_str);
|
||||||
|
|
||||||
// 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");
|
||||||
|
|
||||||
// Passing strings
|
// Passing strings
|
||||||
for (const auto & test_string : passing_strings) {
|
for (const auto & test_string : passing_strings) {
|
||||||
|
fprintf(stderr, " \"%s\" ", test_string.c_str());
|
||||||
|
fflush(stderr);
|
||||||
|
|
||||||
bool matched = match_string(test_string, grammar);
|
bool matched = match_string(test_string, grammar);
|
||||||
|
|
||||||
if (!matched) {
|
if (!matched) {
|
||||||
fprintf(stderr, " ❌ Failed to match string: \"%s\"\n", test_string.c_str());
|
fprintf(stderr, "❌ (failed to match)\n");
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, " ✅︎ Matched string: \"%s\"\n", test_string.c_str());
|
fprintf(stdout, "✅︎\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(matched);
|
assert(matched);
|
||||||
|
@ -76,14 +82,19 @@ 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");
|
||||||
|
|
||||||
// Failing strings
|
// Failing strings
|
||||||
for (const auto & test_string : failing_strings) {
|
for (const auto & test_string : failing_strings) {
|
||||||
|
fprintf(stderr, " \"%s\" ", test_string.c_str());
|
||||||
|
fflush(stderr);
|
||||||
|
|
||||||
bool matched = match_string(test_string, grammar);
|
bool matched = match_string(test_string, grammar);
|
||||||
|
|
||||||
if (matched) {
|
if (matched) {
|
||||||
fprintf(stderr, " ❌ Improperly matched string: \"%s\"\n", test_string.c_str());
|
fprintf(stderr, "❌ (incorrectly matched)\n");
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, " ✅︎ Correctly did not match string: \"%s\"\n", test_string.c_str());
|
fprintf(stdout, "✅︎\n");
|
||||||
}
|
}
|
||||||
assert(!matched);
|
assert(!matched);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue