From 74371060552af03da8896688116bacc71d7dea6d Mon Sep 17 00:00:00 2001 From: M K Date: Mon, 1 Jul 2024 16:40:01 -0500 Subject: [PATCH] Tests for the NFA State functions --- tests/test_nfa_state.c | 126 ++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/tests/test_nfa_state.c b/tests/test_nfa_state.c index 7917693..8ad0448 100644 --- a/tests/test_nfa_state.c +++ b/tests/test_nfa_state.c @@ -1,92 +1,104 @@ -#include - -#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 +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); }