From 6dbd2070503dc0c2dd5f58cfa10bcff38d54844d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Sch=C3=B6bel?= <jonathan@xn--schbel-yxa.info> Date: Sun, 31 Mar 2024 15:48:38 +0200 Subject: [PATCH] add Rule This will provide methods to manipulate a rule. It is separated from a grammar in order to simplify it. Essentially it is just a wrapper for two arrays. --- gsat.geany | 21 ++++++----- src/lib/Makefile.am | 1 + src/lib/rule.c | 61 ++++++++++++++++++++++++++++++ src/lib/rule.h | 44 ++++++++++++++++++++++ tests/Makefile.am | 6 +++ tests/test_rule.c | 91 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 215 insertions(+), 9 deletions(-) create mode 100644 src/lib/rule.c create mode 100644 src/lib/rule.h create mode 100644 tests/test_rule.c diff --git a/gsat.geany b/gsat.geany index 7a08dff..b2ab196 100644 --- a/gsat.geany +++ b/gsat.geany @@ -28,14 +28,14 @@ long_line_behaviour=1 long_line_column=72 [files] -current_page=12 +current_page=14 FILE_NAME_0=309;None;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2FREADME;0;8 FILE_NAME_1=47;None;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2F.gitignore;0;8 FILE_NAME_2=1770;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fconfigure.ac;0;8 FILE_NAME_3=100;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2FMakefile.am;0;8 FILE_NAME_4=74;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2FMakefile.am;0;8 FILE_NAME_5=887;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Fmain.c;0;8 -FILE_NAME_6=335;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2FMakefile.am;0;8 +FILE_NAME_6=350;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2FMakefile.am;0;8 FILE_NAME_7=947;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fmacro.h;0;8 FILE_NAME_8=4638;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fstatus.h;0;8 FILE_NAME_9=881;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Flog.h;0;8 @@ -43,13 +43,16 @@ FILE_NAME_10=8070;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fp FILE_NAME_11=957;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fgrammar.h;0;8 FILE_NAME_12=6482;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Ffile.c;0;8 FILE_NAME_13=1457;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Ffile.h;0;8 -FILE_NAME_14=1282;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fword.c;0;8 -FILE_NAME_15=908;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fword.h;0;8 -FILE_NAME_16=919;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2FMakefile.am;0;8 -FILE_NAME_17=0;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Fno_test.sh;0;8 -FILE_NAME_18=51550;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_grammar.c;0;8 -FILE_NAME_19=17996;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_file.c;0;8 -FILE_NAME_20=1731;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_word.c;0;8 +FILE_NAME_14=968;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Frule.c;0;8 +FILE_NAME_15=1068;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Frule.h;0;8 +FILE_NAME_16=1282;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fword.c;0;8 +FILE_NAME_17=908;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Fsrc%2Flib%2Fword.h;0;8 +FILE_NAME_18=433;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2FMakefile.am;0;8 +FILE_NAME_19=0;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Fno_test.sh;0;8 +FILE_NAME_20=51550;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_grammar.c;0;8 +FILE_NAME_21=17996;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_file.c;0;8 +FILE_NAME_22=1280;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_rule.c;0;8 +FILE_NAME_23=1731;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDocuments%2Fprojects%2Fprgm%2Flanguage%2FGSAT%2Ftests%2Ftest_word.c;0;8 [prjorg] source_patterns=*.c; diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index e17897d..d3d7ad3 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -10,6 +10,7 @@ libgsat_la_SOURCES += macro.h libgsat_la_SOURCES += log.h libgsat_la_SOURCES += grammar.c grammar.h libgsat_la_SOURCES += file.c file.h +libgsat_la_SOURCES += rule.c rule.h libgsat_la_SOURCES += word.c word.h libgsat_la_LDFLAGS = -version_info 0:0:0 diff --git a/src/lib/rule.c b/src/lib/rule.c new file mode 100644 index 0000000..e59af62 --- /dev/null +++ b/src/lib/rule.c @@ -0,0 +1,61 @@ +/* + * rule.c + * + * Copyright 2024 Jonathan Schöbel <jonathan@xn--schbel-yxa.info> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + + +#include <stdlib.h> + +#include "macro.h" +#include "status.h" + +#include "rule.h" + + +struct GR_Rule +{ +}; + + +/*@null@*/ +/*@only@*/ +struct GR_Rule * +GR_Rule_new (/*@null@*/ /*@out@*/ struct GR_Status * status) +{ + struct GR_Rule * rule; + + rule = malloc (sizeof (struct GR_Rule)); + if (NULL == rule) + { + set_status (status, E_ALLOC, 3, "malloc failed"); + return NULL; + } + + set_success (status); + return rule; +} + +void +GR_Rule_free (/*@only@*/ struct GR_Rule * rule) +{ + free (rule); + return; +} diff --git a/src/lib/rule.h b/src/lib/rule.h new file mode 100644 index 0000000..5b8d339 --- /dev/null +++ b/src/lib/rule.h @@ -0,0 +1,44 @@ +/* + * rule.h + * + * Copyright 2024 Jonathan Schöbel <jonathan@xn--schbel-yxa.info> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + + +#include <limits.h> + +#include "status.h" + + +#ifndef GRAMMAR_RULE_H +#define GRAMMAR_RULE_H + +typedef struct GR_Rule GR_Rule; + + +/*@null@*/ +/*@only@*/ +GR_Rule * +GR_Rule_new (/*@null@*/ /*@out@*/ struct GR_Status * status); + +void +GR_Rule_free (/*@only@*/ GR_Rule * rule); + +#endif /* GRAMMAR_RULE_H */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 2828a10..529883e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -15,6 +15,7 @@ if !MISSING_CHECK check_PROGRAMS = check_PROGRAMS += grammar_test check_PROGRAMS += file_test +check_PROGRAMS += rule_test check_PROGRAMS += word_test endif @@ -40,6 +41,11 @@ file_test_LDADD = file_test_LDADD += $(OBJECT_PREFIX)grammar.o file_test_LDADD += $(tests_LDADD) +rule_test_SOURCES = test_rule.c +rule_test_CPPFLAGS = $(tests_CPPFLAGS) +rule_test_LDADD = +rule_test_LDADD += $(tests_LDADD) + word_test_SOURCES = test_word.c word_test_CPPFLAGS = $(tests_CPPFLAGS) word_test_LDADD = diff --git a/tests/test_rule.c b/tests/test_rule.c new file mode 100644 index 0000000..fc4207d --- /dev/null +++ b/tests/test_rule.c @@ -0,0 +1,91 @@ +/* + * test_rule.c + * + * Copyright 2024 Jonathan Schöbel <jonathan@xn--schbel-yxa.info> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ + + +#include <check.h> +#include <stdlib.h> + + +#include "status.h" + +#include "rule.c" + + +START_TEST(test_rule_no_status) +{ + struct GR_Rule * rule; + + rule = GR_Rule_new (NULL); + ck_assert_ptr_ne (NULL, rule); + + GR_Rule_free (rule); +} +END_TEST + +START_TEST(test_rule_with_status) +{ + struct GR_Status status; + struct GR_Rule * rule; + + _status_preinit(status); + rule = GR_Rule_new (&status); + ck_assert_ptr_ne (NULL, rule); + ck_assert_int_eq (SUCCESS, status.status); + + GR_Rule_free (rule); +} +END_TEST + + +Suite * test_suite (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create ("Testsuite GSAT Rule"); + + /* Core test case */ + tc_core = tcase_create ("Core"); + + tcase_add_test (tc_core, test_rule_no_status); + tcase_add_test (tc_core, test_rule_with_status); + suite_add_tcase (s, tc_core); + + return s; +} + +int main (void) +{ + int number_failed; + Suite *s; + SRunner *sr; + + s = test_suite (); + sr = srunner_create (s); + + srunner_run_all (sr, CK_NORMAL); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} -- GitLab