From 1c53c5f0549779e5da667eeb758aadac6cc924b3 Mon Sep 17 00:00:00 2001 From: Stephen Toth Date: Sat, 30 Sep 2023 18:05:49 -0400 Subject: [PATCH] Added Tests!!! --- .gitignore | 17 ++++----- Makefile | 29 ++++++++++---- include/config.h | 2 +- src/main.c | 2 + tests/minunit.h | 47 +++++++++++++++++++++++ tests/tests.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 tests/minunit.h create mode 100644 tests/tests.c diff --git a/.gitignore b/.gitignore index 6f3ba1d..61303d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,14 @@ -# Ignore the build and lib dirs *.i *.s -build/* -testing/*.lp -#lib/* +*.lp -# Ignore any executables -bin/* - -# Ignore temp editor files +# Ignore temp files *.swp *.tmp -.vscode/* +# Ignore the build and bin dirs +bin/* +build/* -!.gitkeep \ No newline at end of file +.vscode/* +!.gitkeep diff --git a/Makefile b/Makefile index 4fc6a34..6088237 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,13 @@ CC := gcc + SRCDIR := src BUILDDIR := build -TESTDIR := testing +BINDIR := bin +TESTDIR := tests + TARGET := bin/cterm +TESTBIN := bin/testsuite + SRCEXT := c SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) @@ -13,7 +18,7 @@ INC := -I include $(TARGET): $(OBJECTS) @echo " Incrementing Build #"; make/genbuildno.sh - @echo "Build number: $(shell grep BUILD include/config.h | cut -d' ' -f 3)" + @echo " Build number: $(shell grep BUILD include/config.h | cut -d' ' -f 3)" @echo " Linking..." @echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) -O3 @echo " Done!" @@ -23,19 +28,22 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) @mkdir -p $(BUILDDIR) @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $< -save-temps -O3 -clean: - @echo " Cleaning..."; - @echo " cat /dev/null > $(TESTDIR)/trace.lp"; cat /dev/null > $(TESTDIR)/trace.lp; - @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR)/* $(TARGET); - #debug: clean debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -Wno-unused-variable -Wno-unused-parameter debug: CFLAGS += -D"DEBUG=1" -DGITBRANCH=\"$(shell git branch --show-current)\" debug: $(TARGET) -test: +run: $(TARGET) /bin/sh +$(TESTBIN): $(TESTDIR)/tests.c + @echo " Building Testsuite..."; $(CC) $(CFLAGS) -O0 $(INC) -I$(SRCDIR) $(TESTDIR)/tests.c -o $(TESTBIN) + @echo " Running Tests..." + bin/testsuite + +test: $(TESTBIN) + + trace: cat /dev/null > $(TESTDIR)/trace.lp; strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh @@ -44,4 +52,9 @@ tracef: cat /dev/null > $(TESTDIR)/trace.lp; strace -fo $(TESTDIR)/trace.lp $(TARGET) /bin/sh +clean: + @echo " Cleaning..."; + @echo " cat /dev/null > $(TESTDIR)/trace.lp"; cat /dev/null > $(TESTDIR)/trace.lp; + @echo " $(RM) -r $(BUILDDIR)/* $(BINDIR)/*"; $(RM) -r $(BUILDDIR)/* $(BINDIR)/*; + .PHONY: clean diff --git a/include/config.h b/include/config.h index a196aad..b1dcc4e 100644 --- a/include/config.h +++ b/include/config.h @@ -6,7 +6,7 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_HOTFIX 0 -#define VERSION_BUILD 350 +#define VERSION_BUILD 354 //#define DEFAULT_CMD "/bin/sh" #define CONF_DO_CMD_ACK 1 diff --git a/src/main.c b/src/main.c index 310a28a..16538dd 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ //temporary ct_match replace_rules[]= { + //gotta be careful right now because earlier replaces also effect later ones { "alpine", 0, 0, KMAG"alpine"KNRM }, { "r--", 0, 0, KGRN"r--"KNRM }, { "rw-", 0, 0, KRED"rw-"KNRM }, @@ -47,6 +48,7 @@ ct_line processline(ct_line current_line){ regex_t comp_regex; int match1_comp_result = regcomp(&comp_regex, replace_rules[i].match_regex, replace_rules[i].match_cflags); //unused but needed int regex_result = line_regex_replace(¤t_line, &comp_regex, replace_rules[i].replace_group, replace_rules[i].replace_with, 255); + regfree(&comp_regex); } diff --git a/tests/minunit.h b/tests/minunit.h new file mode 100644 index 0000000..acd9630 --- /dev/null +++ b/tests/minunit.h @@ -0,0 +1,47 @@ +#define KNRM "\x1B[0m" +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define KBLU "\x1B[34m" + +#define red(str) KRED str KNRM +#define green(str) KGRN str KNRM +#define blue(str) KBLU str KNRM + +#define ALERT KRED"[!]"KNRM +#define INFO KNRM"[~]"KNRM +#define GOOD KGRN"[*]"KNRM + +#define lenofarr(axx) (sizeof(axx)/sizeof(axx[0])) + +typedef struct { + int error; + int cases_passed; +} unit_result; + +#define MU_UNIT_INIT() int err = 0; int unit_cases_run = 0; +#define MU_UNIT_EXIT() return (unit_result){err, unit_cases_run}; + + + +#define mu_assert(error_format, test, ...) \ + do { \ + unit_cases_run++; \ + if (!(test)) { \ + printf( ALERT"\t%s: " error_format "\n\n", __func__, ##__VA_ARGS__); \ + err = 1; \ + }\ + } while (0) + +#define mu_run_test(unit) \ + do { \ + printf(INFO" Testing "#unit"\n"); \ + unit_result result = unit();\ + units_run++; \ + /*if (result.error) return result.error; */\ + if (result.error){ printf(ALERT" Unit Failed \n"); err = 1; break;}\ + printf(GOOD" Unit Passed \n"); \ + } while (0) + + +extern int units_run; +extern int unit_cases_run; \ No newline at end of file diff --git a/tests/tests.c b/tests/tests.c new file mode 100644 index 0000000..78294ac --- /dev/null +++ b/tests/tests.c @@ -0,0 +1,98 @@ +#include "minunit.h" +int units_run = 0; +int cases_run = 0; + +#include +#include +#include +#include +#include + +static unit_result test_str_index_replace() { + MU_UNIT_INIT(); + struct function_options { + char *is; //input string + char *rw; //replace with + int si; //start index + int ei; //end index + char *assert; //result + }; + struct function_options tcs[]= { + {"----....----", "````", 4, 4+4, "----````----"}, + {"1234567890", "qwerty", 1, 4, "1qwerty567890"}, + {"1234567890", "qwerty", 1, 4, "1qwerty567890"}, + }; + int len_tcs = lenofarr(tcs); + + for(int i=0; i