Added Tests!!!

This commit is contained in:
Stephen Toth 2023-09-30 18:05:49 -04:00
parent 62c15ab976
commit 1c53c5f054
6 changed files with 176 additions and 19 deletions

15
.gitignore vendored
View File

@ -1,17 +1,14 @@
# Ignore the build and lib dirs
*.i *.i
*.s *.s
build/* *.lp
testing/*.lp
#lib/*
# Ignore any executables # Ignore temp files
bin/*
# Ignore temp editor files
*.swp *.swp
*.tmp *.tmp
.vscode/* # Ignore the build and bin dirs
bin/*
build/*
.vscode/*
!.gitkeep !.gitkeep

View File

@ -1,8 +1,13 @@
CC := gcc CC := gcc
SRCDIR := src SRCDIR := src
BUILDDIR := build BUILDDIR := build
TESTDIR := testing BINDIR := bin
TESTDIR := tests
TARGET := bin/cterm TARGET := bin/cterm
TESTBIN := bin/testsuite
SRCEXT := c SRCEXT := c
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
@ -23,19 +28,22 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(BUILDDIR) @mkdir -p $(BUILDDIR)
@echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $< -save-temps -O3 @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: clean
debug: CFLAGS += -ggdb -O0 -Wall -Wextra -pedantic -Wno-unused-variable -Wno-unused-parameter 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: CFLAGS += -D"DEBUG=1" -DGITBRANCH=\"$(shell git branch --show-current)\"
debug: $(TARGET) debug: $(TARGET)
test: run:
$(TARGET) /bin/sh $(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: trace:
cat /dev/null > $(TESTDIR)/trace.lp; cat /dev/null > $(TESTDIR)/trace.lp;
strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh
@ -44,4 +52,9 @@ tracef:
cat /dev/null > $(TESTDIR)/trace.lp; cat /dev/null > $(TESTDIR)/trace.lp;
strace -fo $(TESTDIR)/trace.lp $(TARGET) /bin/sh 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 .PHONY: clean

View File

@ -6,7 +6,7 @@
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_HOTFIX 0 #define VERSION_HOTFIX 0
#define VERSION_BUILD 350 #define VERSION_BUILD 354
//#define DEFAULT_CMD "/bin/sh" //#define DEFAULT_CMD "/bin/sh"
#define CONF_DO_CMD_ACK 1 #define CONF_DO_CMD_ACK 1

View File

@ -24,6 +24,7 @@
//temporary //temporary
ct_match replace_rules[]= { ct_match replace_rules[]= {
//gotta be careful right now because earlier replaces also effect later ones
{ "alpine", 0, 0, KMAG"alpine"KNRM }, { "alpine", 0, 0, KMAG"alpine"KNRM },
{ "r--", 0, 0, KGRN"r--"KNRM }, { "r--", 0, 0, KGRN"r--"KNRM },
{ "rw-", 0, 0, KRED"rw-"KNRM }, { "rw-", 0, 0, KRED"rw-"KNRM },
@ -47,6 +48,7 @@ ct_line processline(ct_line current_line){
regex_t comp_regex; 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 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(&current_line, &comp_regex, replace_rules[i].replace_group, replace_rules[i].replace_with, 255); int regex_result = line_regex_replace(&current_line, &comp_regex, replace_rules[i].replace_group, replace_rules[i].replace_with, 255);
regfree(&comp_regex);
} }

47
tests/minunit.h Normal file
View File

@ -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;

98
tests/tests.c Normal file
View File

@ -0,0 +1,98 @@
#include "minunit.h"
int units_run = 0;
int cases_run = 0;
#include <stdio.h>
#include <regex.h>
#include <structdef.h>
#include <config.h>
#include <linework.c>
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<len_tcs; i++){
char *fun = str_index_replace(tcs[i].is, tcs[i].rw, tcs[i].si, tcs[i].ei);
int test = (strcmp(fun, tcs[i].assert)==0);
mu_assert("Case %d\n\tstr_index_replace( %s, %s, %d, %d ): \"%s\" != \"%s\"", test, i, tcs[i].is, tcs[i].rw, tcs[i].si, tcs[i].ei, fun, tcs[i].assert);
free(fun);
//if (err) break;
}
MU_UNIT_EXIT();
}
#define REG_NOFLAG 0
static unit_result test_line_regex_replace() {
MU_UNIT_INIT();
struct function_options {
char *is; //input string
char* pat; //regex pattern
int regflag;
int tcg; //target_capture_group
char* with; //with
int mi; //max_instances
char *assert; //result
};
struct function_options tcs[]= {
{ "drwxr-xr-x. 1 username group 44 Sep 30 17:11 bin", "^d", REG_EXTENDED, 0, "D", 0,
"Drwxr-xr-x. 1 username group 44 Sep 30 17:11 bin"},
{ "drwxr-xr--. 1 username group 44 Sep 30 17:11 bin", "r--", REG_NOFLAG, 0, "[r--]", 0,
"drwxr-x[r--]. 1 username group 44 Sep 30 17:11 bin"},
{ "alpine alpine01 alpine12 alpine23 alpineA long UTF-8-demo.txt", "(alpine)[[:blank:]01]", REG_EXTENDED, 1, "[alpine]", 255,
"[alpine] [alpine]01 [alpine]12 alpine23 alpineA long UTF-8-demo.txt"},
};
int len_tcs = lenofarr(tcs);
for(int i=0; i<len_tcs; i++){
regex_t le_regex;
regcomp(&le_regex, tcs[i].pat, tcs[i].regflag);
ct_line test_line = {tcs[i].is, 0};
line_regex_replace(&test_line, &le_regex, tcs[i].tcg, tcs[i].with, tcs[i].mi);
int test = (strcmp(test_line.line_str, tcs[i].assert)==0);
mu_assert("Case %d\n\tline_regex_replace( \"%s\", \"%s\", %d, %s, %d ):\n\t \"%s\" != \n\t \"%s\"", test, i, \
tcs[i].is, tcs[i].pat, tcs[i].tcg, tcs[i].with, tcs[i].mi, test_line.line_str, tcs[i].assert);
if (test_line.need_free)free(test_line.line_str);
regfree(&le_regex);
//if (err) break;
}
MU_UNIT_EXIT();
}
static int all_units() {
int err = 0;
mu_run_test(test_str_index_replace);
mu_run_test(test_line_regex_replace);
return err;
}
//=============================================//
int main(int argc, char **argv) {
printf(blue("=== RUNNING TESTS ===")"\n\n");
int result = all_units();
if (result != 0) {
printf(red("=== SUITE FAILED ===")"\n");
}
else {
printf(green("=== ALL TESTS PASSED ===")"\n");
}
printf("=== Units run: %d\n", units_run);
return 0;
return result != 0;
}