From e3f869fc21836859061abebd2f9af16231cccc66 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Sun, 17 Jan 2016 21:50:53 -0500 Subject: [PATCH] split out unit_test/test_mlrregex from unit_test/test_mlrutil --- .gitignore | 2 + c/Makefile.no-autoconfig | 12 ++++- c/unit_test/Makefile.am | 4 ++ c/unit_test/test_mlrregex.c | 92 +++++++++++++++++++++++++++++++++++++ c/unit_test/test_mlrutil.c | 62 ------------------------- 5 files changed, 109 insertions(+), 63 deletions(-) create mode 100644 c/unit_test/test_mlrregex.c diff --git a/.gitignore b/.gitignore index f55f5a8cd..2168f0fd5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ mlrp mlr-unit-tester test-argparse test-mlrutil +test-mlrregex test-string-builder test-byte-readers test-peek-file-reader @@ -66,6 +67,7 @@ test_byte_readers test_join_bucket_keeper test_lrec test_mlrutil +test_mlrregex test_multiple_containers test_parse_trie test_peek_file_reader diff --git a/c/Makefile.no-autoconfig b/c/Makefile.no-autoconfig index 25d427f84..e4be150d0 100644 --- a/c/Makefile.no-autoconfig +++ b/c/Makefile.no-autoconfig @@ -137,6 +137,12 @@ TEST_MULTIPLE_CONTAINERS_SRCS = \ unit_test/test_multiple_containers.c TEST_MLRUTIL_SRCS = \ + lib/mlr_globals.c \ + lib/mlrutil.c \ + lib/string_builder.c \ + unit_test/test_mlrutil.c + +TEST_MLRREGEX_SRCS = \ lib/mlr_globals.c \ lib/mlrutil.c \ lib/mlrregex.c \ @@ -263,8 +269,9 @@ mlrp: .always dsls # ================================================================ tests: unit-test reg-test -unit-test: test-mlrutil test-argparse test-byte-readers test-peek-file-reader test-parse-trie test-lrec test-multiple-containers test-string-builder test-lrec-evaluators test-join-bucket-keeper +unit-test: test-mlrutil test-mlrregex test-argparse test-byte-readers test-peek-file-reader test-parse-trie test-lrec test-multiple-containers test-string-builder test-lrec-evaluators test-join-bucket-keeper ./test-mlrutil + ./test-mlrregex ./test-argparse ./test-byte-readers ./test-peek-file-reader @@ -307,6 +314,9 @@ test-multiple-containers: .always test-mlrutil: .always $(CCDEBUG) -D__TEST_MLRUTIL_MAIN__ $(TEST_MLRUTIL_SRCS) -o test-mlrutil +test-mlrregex: .always + $(CCDEBUG) -D__TEST_MLRREGEX_MAIN__ $(TEST_MLRREGEX_SRCS) -o test-mlrregex + test-string-builder: .always $(CCDEBUG) -D__TEST_STRING_BUILDER_MAIN__ $(TEST_STRING_BUILDER_SRCS) -o test-string-builder diff --git a/c/unit_test/Makefile.am b/c/unit_test/Makefile.am index 36d6121b1..36eaaf8a5 100644 --- a/c/unit_test/Makefile.am +++ b/c/unit_test/Makefile.am @@ -1,6 +1,7 @@ TESTS= $(check_PROGRAMS) check_PROGRAMS = test_mlrutil \ + test_mlrregex \ test_argparse \ test_byte_readers \ test_peek_file_reader \ @@ -29,6 +30,9 @@ all_ldadd= \ test_mlrutil_CFLAGS= -std=gnu99 -g ${AM_CFLAGS} test_mlrutil_LDADD= ${all_ldadd} +test_mlrregex_CFLAGS= -std=gnu99 -g ${AM_CFLAGS} +test_mlrregex_LDADD= ${all_ldadd} + test_argparse_CFLAGS= -std=gnu99 -g ${AM_CFLAGS} test_argparse_LDADD= ${all_ldadd} diff --git a/c/unit_test/test_mlrregex.c b/c/unit_test/test_mlrregex.c new file mode 100644 index 000000000..dcecf4e50 --- /dev/null +++ b/c/unit_test/test_mlrregex.c @@ -0,0 +1,92 @@ +#include +#include +#include "lib/minunit.h" +#include "lib/mlrutil.h" +#include "lib/mlrregex.h" + +int tests_run = 0; +int tests_failed = 0; +int assertions_run = 0; +int assertions_failed = 0; + +// ---------------------------------------------------------------- +static char * test_interpolate_regex_captures() { + int was_allocated = FALSE; + + char* output = interpolate_regex_captures("hello", NULL, &was_allocated); + mu_assert_lf(streq(output, "hello")); + mu_assert_lf(was_allocated == FALSE); + + string_array_t* psa = string_array_alloc(0); + output = interpolate_regex_captures("hello", psa, &was_allocated); + mu_assert_lf(streq(output, "hello")); + mu_assert_lf(was_allocated == FALSE); + string_array_free(psa); + + // captures are indexed 1-up so the X is a placeholder at index 0 + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("hello", psa, &was_allocated); + mu_assert_lf(streq(output, "hello")); + mu_assert_lf(was_allocated == FALSE); + string_array_free(psa); + + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("h\\3ello", psa, &was_allocated); + printf("output=[%s]\n", output); + mu_assert_lf(streq(output, "hcello")); + mu_assert_lf(was_allocated == TRUE); + string_array_free(psa); + + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("h\\1ello", psa, &was_allocated); + printf("output=[%s]\n", output); + mu_assert_lf(streq(output, "haello")); + mu_assert_lf(was_allocated == TRUE); + string_array_free(psa); + + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("h\\4ello", psa, &was_allocated); + printf("output=[%s]\n", output); + mu_assert_lf(streq(output, "h\\4ello")); + mu_assert_lf(was_allocated == FALSE); + string_array_free(psa); + + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("h\\0ello", psa, &was_allocated); + printf("output=[%s]\n", output); + mu_assert_lf(streq(output, "h\\0ello")); + mu_assert_lf(was_allocated == FALSE); + string_array_free(psa); + + psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); + output = interpolate_regex_captures("h\\3e\\1l\\2l\\4o", psa, &was_allocated); + printf("output=[%s]\n", output); + mu_assert_lf(streq(output, "hcealbl\\4o")); + mu_assert_lf(was_allocated == TRUE); + string_array_free(psa); + + return 0; +} + + +// ================================================================ +static char * all_tests() { + mu_run_test(test_interpolate_regex_captures); + return 0; +} + +int main(int argc, char **argv) { + printf("TEST_MLRREGEX ENTER\n"); + char *result = all_tests(); + printf("\n"); + if (result != 0) { + printf("Not all unit tests passed\n"); + } + else { + printf("TEST_MLRREGEX: ALL UNIT TESTS PASSED\n"); + } + printf("Tests passed: %d of %d\n", tests_run - tests_failed, tests_run); + printf("Assertions passed: %d of %d\n", assertions_run - assertions_failed, assertions_run); + + return result != 0; +} diff --git a/c/unit_test/test_mlrutil.c b/c/unit_test/test_mlrutil.c index 585156132..a5f7143e9 100644 --- a/c/unit_test/test_mlrutil.c +++ b/c/unit_test/test_mlrutil.c @@ -2,7 +2,6 @@ #include #include "lib/minunit.h" #include "lib/mlrutil.h" -#include "lib/mlrregex.h" int tests_run = 0; int tests_failed = 0; @@ -143,66 +142,6 @@ static char * test_unbackslash() { return 0; } -// ---------------------------------------------------------------- -static char * test_interpolate_regex_captures() { - int was_allocated = FALSE; - - char* output = interpolate_regex_captures("hello", NULL, &was_allocated); - mu_assert_lf(streq(output, "hello")); - mu_assert_lf(was_allocated == FALSE); - - string_array_t* psa = string_array_alloc(0); - output = interpolate_regex_captures("hello", psa, &was_allocated); - mu_assert_lf(streq(output, "hello")); - mu_assert_lf(was_allocated == FALSE); - string_array_free(psa); - - // captures are indexed 1-up so the X is a placeholder at index 0 - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("hello", psa, &was_allocated); - mu_assert_lf(streq(output, "hello")); - mu_assert_lf(was_allocated == FALSE); - string_array_free(psa); - - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("h\\3ello", psa, &was_allocated); - printf("output=[%s]\n", output); - mu_assert_lf(streq(output, "hcello")); - mu_assert_lf(was_allocated == TRUE); - string_array_free(psa); - - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("h\\1ello", psa, &was_allocated); - printf("output=[%s]\n", output); - mu_assert_lf(streq(output, "haello")); - mu_assert_lf(was_allocated == TRUE); - string_array_free(psa); - - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("h\\4ello", psa, &was_allocated); - printf("output=[%s]\n", output); - mu_assert_lf(streq(output, "h\\4ello")); - mu_assert_lf(was_allocated == FALSE); - string_array_free(psa); - - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("h\\0ello", psa, &was_allocated); - printf("output=[%s]\n", output); - mu_assert_lf(streq(output, "h\\0ello")); - mu_assert_lf(was_allocated == FALSE); - string_array_free(psa); - - psa = string_array_from_line(mlr_strdup_or_die("X,a,b,c"), ','); - output = interpolate_regex_captures("h\\3e\\1l\\2l\\4o", psa, &was_allocated); - printf("output=[%s]\n", output); - mu_assert_lf(streq(output, "hcealbl\\4o")); - mu_assert_lf(was_allocated == TRUE); - string_array_free(psa); - - return 0; -} - - // ================================================================ static char * all_tests() { mu_run_test(test_canonical_mod); @@ -212,7 +151,6 @@ static char * all_tests() { mu_run_test(test_scanners); mu_run_test(test_paste); mu_run_test(test_unbackslash); - mu_run_test(test_interpolate_regex_captures); return 0; }