Skip to content

Commit

Permalink
Tests for the NFA State functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkpro118 committed Jul 1, 2024
1 parent 6b64789 commit 7437106
Showing 1 changed file with 69 additions and 57 deletions.
126 changes: 69 additions & 57 deletions tests/test_nfa_state.c
Original file line number Diff line number Diff line change
@@ -1,92 +1,104 @@
#include <stdbool.h>

#define FAIL_FAST

#include "testlib/asserts.h"
#include "testlib/tests.h"

#include "nfa_state.h"
#include "list.h"

NFAState* state_create(bool is_final);
int state_init(NFAState* state, bool is_final);
void state_free(NFAState* state);
int add_transition(NFAState* from, NFAState* to, char on);
#include <stdio.h>

CREATE_LIST_FOR(NFAState, NFAStateList)

// Global NFAState variable
NFAState state;

int test_state_create(void) {
TEST_BEGIN;

// Case 1: Accepting/Final state
{
NFAState* state = state_create(true);
assert_is_not_null(state);
int test_state_init() {

assert_equals_int(state->is_final, true);
assert_is_not_null(state->transitions);
TEST_BEGIN;

state_free(state);
free(state);
assert_equals_int(state_init(&state, false), 0);
assert_equals_int(state.is_final, false);
for (int i = 0; i < MAX_N_TRANSITIONS; i++) {

assert_is_null(state.transitions[i]);
}

// Case 2: Non-Accepting/Non-Final state
{
NFAState* state = state_create(false);
assert_is_not_null(state);
state_free(&state);
TEST_END;
}

assert_equals_int(state->is_final, false);
assert_is_not_null(state->transitions);
int test_add_transition() {

state_free(state);
free(state);
TEST_BEGIN;
NFAState to;
state_init(&to, true);

int n_transitions = add_transition(&state, &to, 'a');
assert_equals_int(n_transitions, 1);

NFAStateList* list = get_transition(&state, 'a');
assert_is_not_null(list);

int size = NFAStateList_size(get_transition(&state, 'a'));
assert_equals_int(size, 1);

// Add another transition on the same character
NFAState to2;
state_init(&to2, false);

n_transitions = add_transition(&state, &to2, 'a');
assert_equals_int(n_transitions, 2);
size = NFAStateList_size(get_transition(&state, 'a'));
assert_equals_int(size, 2);

// Test invalid transitions
n_transitions = add_transition(NULL, &to, 'b');
assert_equals_int(n_transitions, -1);
n_transitions = add_transition(&state, NULL, 'b');
assert_equals_int(n_transitions, -1);
n_transitions = add_transition(&state, &to, '\n');
assert_equals_int(n_transitions, -1);

// Clean up transitions
for (int i = 0; i < MAX_N_TRANSITIONS; i++) {
if (state.transitions[i] != NULL) {
NFAStateList_free(state.transitions[i], NULL);
free(state.transitions[i]);
state.transitions[i] = NULL;
}
}

state_free(&state);
TEST_END;
}

int test_state_init(void) {
int test_state_free() {
TEST_BEGIN;
NFAState to1, to2;
state_init(&state, false);
state_init(&to1, false);
state_init(&to2, false);

// Case 1: Accepting/Final state
{
int ret = state_init(&state, true);
assert_equals_int(ret, 0);
add_transition(&state, &to1, 'a');
add_transition(&state, &to2, 'b');

assert_equals_int(state.is_final, true);
assert_is_not_null(state.transitions);
// This should free all allocated memory without crashing
state_free(&state);

state_free(state);
// Verify that transitions are freed
for (int i = 0; i < MAX_N_TRANSITIONS; i++) {
assert_is_null(state.transitions[i]);
}

// Case 2: Non-Accepting/Non-Final state
{
int ret = state_init(&state, false);
assert_equals_int(ret, 0);

assert_equals_int(state.is_final, false);
assert_is_not_null(state.transitions);

state_free(state);
}

TEST_END;
}

int test_add_transition(void) {
TEST_BEGIN;

TEST_END;
}


Test tests[] = {
{.name="test_state_create", .func=test_state_create},
{.name="test_state_init", .func=test_state_init},
{.name="test_add_transition", .func=test_add_transition},
{.name=NULL},
{.name="test_state_free", .func=test_state_free},
{.name=NULL, .func=NULL}
};


int main(int argc, char const *argv[]) {
int main(int argc, char* argv[]) {
return default_main(&argv[1], argc - 1);
}

0 comments on commit 7437106

Please sign in to comment.