192 lines
5.1 KiB
C
192 lines
5.1 KiB
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "construct.h"
|
|
#include "testing.h"
|
|
|
|
static bool
|
|
accepts_from_state(const fsa_t *nfa, int state_id, const char *input)
|
|
{
|
|
const fsa_state_t *state = &nfa->states[state_id];
|
|
if ('\0' == *input)
|
|
return state->final;
|
|
|
|
for (int i = 0; i < state->count; ++i) {
|
|
if (EPSILON == state->rules[i].input
|
|
&& accepts_from_state(nfa, state->rules[i].next, input))
|
|
return true;
|
|
if (*input == state->rules[i].input
|
|
&& accepts_from_state(nfa, state->rules[i].next, input + 1))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool accepts(const fsa_t *nfa, const char *input)
|
|
{
|
|
return accepts_from_state(nfa, nfa->initial, input);
|
|
}
|
|
|
|
static void test_empty_expression(void)
|
|
{
|
|
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
|
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
|
terms[0].type = REGEX_TERM_EMPTY;
|
|
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
|
alternatives[0].count = alternatives[0].capacity = 1;
|
|
alternatives[0].contents = terms;
|
|
const regex_t regex
|
|
= { .count = 1, .capacity = 1, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, ""));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
static void test_literal_expression(void)
|
|
{
|
|
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
|
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
|
terms[0].type = REGEX_TERM_LITERAL;
|
|
terms[0].literal = 'a';
|
|
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
|
alternatives[0].count = alternatives[0].capacity = 1;
|
|
alternatives[0].contents = terms;
|
|
const regex_t regex
|
|
= { .count = 1, .capacity = 1, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, "a"));
|
|
ASSERT_FALSE(accepts(&fsa, "b"));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
static void test_sequence(void)
|
|
{
|
|
regex_term_t *terms = malloc(3 * sizeof(regex_term_t));
|
|
terms[0].type = REGEX_TERM_LITERAL;
|
|
terms[0].literal = 'a';
|
|
terms[1].type = REGEX_TERM_LITERAL;
|
|
terms[1].literal = 'b';
|
|
terms[2].type = REGEX_TERM_LITERAL;
|
|
terms[2].literal = 'c';
|
|
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
|
alternatives[0].count = alternatives[0].capacity = 3;
|
|
alternatives[0].contents = terms;
|
|
regex_t regex = { .count = 1, .capacity = 1, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, "abc"));
|
|
ASSERT_FALSE(accepts(&fsa, "a"));
|
|
ASSERT_FALSE(accepts(&fsa, "ab"));
|
|
ASSERT_FALSE(accepts(&fsa, "d"));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
static void test_union(void)
|
|
{
|
|
const char *literals = "abc";
|
|
regex_sequence_t *alternatives = malloc(3 * sizeof(regex_sequence_t));
|
|
for (int i = 0; i < 3; ++i) {
|
|
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
|
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
|
terms[0].type = REGEX_TERM_LITERAL;
|
|
terms[0].literal = literals[i];
|
|
|
|
alternatives[i].count = alternatives[i].capacity = 1;
|
|
alternatives[i].contents = terms;
|
|
}
|
|
regex_t regex = { .count = 3, .capacity = 3, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, "a"));
|
|
ASSERT_TRUE(accepts(&fsa, "b"));
|
|
ASSERT_TRUE(accepts(&fsa, "c"));
|
|
ASSERT_FALSE(accepts(&fsa, "d"));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
static void test_star(void)
|
|
{
|
|
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
|
terms[0].quantifier = REGEX_QUANTIFIER_STAR;
|
|
terms[0].type = REGEX_TERM_LITERAL;
|
|
terms[0].literal = 'a';
|
|
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
|
alternatives[0].count = alternatives[0].capacity = 1;
|
|
alternatives[0].contents = terms;
|
|
regex_t regex = { .count = 1, .capacity = 1, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, ""));
|
|
ASSERT_TRUE(accepts(&fsa, "a"));
|
|
ASSERT_TRUE(accepts(&fsa, "aaaaaa"));
|
|
ASSERT_FALSE(accepts(&fsa, "b"));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
static void test_subexpression(void)
|
|
{
|
|
regex_term_t *inner_terms = malloc(1 * sizeof(regex_term_t));
|
|
inner_terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
|
inner_terms[0].type = REGEX_TERM_LITERAL;
|
|
inner_terms[0].literal = 'a';
|
|
regex_sequence_t *inner_alternatives
|
|
= malloc(1 * sizeof(regex_sequence_t));
|
|
inner_alternatives[0].count = inner_alternatives[0].capacity = 1;
|
|
inner_alternatives[0].contents = inner_terms;
|
|
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
|
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
|
terms[0].type = REGEX_TERM_SUBEXPR;
|
|
terms[0].subexpr.count = terms[0].subexpr.capacity = 1;
|
|
terms[0].subexpr.contents = inner_alternatives;
|
|
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
|
alternatives[0].count = alternatives[0].capacity = 1;
|
|
alternatives[0].contents = terms;
|
|
regex_t regex = { .count = 1, .capacity = 1, .contents = alternatives };
|
|
|
|
fsa_t fsa;
|
|
construct(®ex, &fsa);
|
|
|
|
ASSERT_TRUE(accepts(&fsa, "a"));
|
|
ASSERT_FALSE(accepts(&fsa, "b"));
|
|
|
|
regex_free(®ex);
|
|
fsa_free(&fsa);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
TESTING_BEGIN();
|
|
test_empty_expression();
|
|
test_literal_expression();
|
|
test_sequence();
|
|
test_union();
|
|
test_star();
|
|
test_subexpression();
|
|
return TESTING_END();
|
|
}
|