99 lines
2.9 KiB
C
99 lines
2.9 KiB
C
#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;
|
|
}
|