regex-engine/tests/construct_tests.c

144 lines
3.7 KiB
C

/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "construct.h"
#include "testing.h"
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(&regex, &fsa);
ASSERT_EQ(1, fsa.count);
ASSERT_TRUE(fsa.states[fsa.initial].final);
ASSERT_EQ(0, fsa.states[fsa.initial].count);
regex_free(&regex);
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(&regex, &fsa);
const fsa_state_t *initial = &fsa.states[fsa.initial];
ASSERT_EQ(2, fsa.count);
ASSERT_EQ(1, initial->count);
ASSERT_EQ('a', initial->rules[0].input);
const int next = initial->rules[0].next;
ASSERT_TRUE(fsa.states[next].final);
ASSERT_EQ(0, fsa.states[next].count);
regex_free(&regex);
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(&regex, &fsa);
int next = fsa.initial;
const fsa_state_t *state;
state = &fsa.states[next];
ASSERT_FALSE(state->final);
ASSERT_EQ(1, state->count);
ASSERT_EQ('a', state->rules[0].input);
next = state->rules[0].next;
state = &fsa.states[next];
ASSERT_FALSE(state->final);
ASSERT_EQ(1, state->count);
ASSERT_EQ('b', state->rules[0].input);
next = state->rules[0].next;
state = &fsa.states[next];
ASSERT_FALSE(state->final);
ASSERT_EQ(1, state->count);
ASSERT_EQ('c', state->rules[0].input);
next = state->rules[0].next;
state = &fsa.states[next];
ASSERT_TRUE(state->final);
ASSERT_EQ(0, state->count);
regex_free(&regex);
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(&regex, &fsa);
const fsa_state_t *initial = &fsa.states[fsa.initial];
ASSERT_EQ(3, initial->count);
for (int i = 0; i < 3; ++i) {
ASSERT_EQ(literals[i], initial->rules[i].input);
const int next = initial->rules[i].next;
ASSERT_TRUE(fsa.states[next].final);
}
regex_free(&regex);
fsa_free(&fsa);
}
int main(void)
{
TESTING_BEGIN();
test_empty_expression();
test_literal_expression();
test_sequence();
test_union();
return TESTING_END();
}