From 0ca7087f72d70137aac20c012b0a792c444b3e45 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Fri, 24 May 2019 19:05:29 +0200 Subject: [PATCH] Added tests for substring and regular expression functions. --- src/Makefile.am | 2 +- src/Makefile.in | 5 +++-- src/String.h | 1 + src/string.cpp | 1 + test/Makefile.am | 12 +++++++----- test/Makefile.in | 43 ++++++++++++++++++++++++++++++++++++++++--- test/string_assign.cpp | 1 + test/string_regex.cpp | 37 +++++++++++++++++++++++++++++++++++++ test/string_regex.exp | 3 +++ test/string_substring.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 test/string_regex.cpp create mode 100644 test/string_regex.exp create mode 100644 test/string_substring.cpp diff --git a/src/Makefile.am b/src/Makefile.am index c18be9a..1f96230 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES = libACL.la -libACL_la_SOURCES = string.cpp +libACL_la_SOURCES = string.cpp regex.cpp include_HEADERS = String.h diff --git a/src/Makefile.in b/src/Makefile.in index 30feb48..1a3603d 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -133,7 +133,7 @@ am__uninstall_files_from_dir = { \ am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)" LTLIBRARIES = $(lib_LTLIBRARIES) libACL_la_LIBADD = -am_libACL_la_OBJECTS = string.lo +am_libACL_la_OBJECTS = string.lo regex.lo libACL_la_OBJECTS = $(am_libACL_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -326,7 +326,7 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ lib_LTLIBRARIES = libACL.la -libACL_la_SOURCES = string.cpp +libACL_la_SOURCES = string.cpp regex.cpp include_HEADERS = String.h all: all-am @@ -406,6 +406,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/regex.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string.Plo@am__quote@ .cpp.o: diff --git a/src/String.h b/src/String.h index cb2ae1d..8c154be 100644 --- a/src/String.h +++ b/src/String.h @@ -304,6 +304,7 @@ public: friend bool operator == (const String &s, const regex &r); friend bool operator == (const regex &r, const String &s); + // TODO: The != operator }; /* * Other operators to think about... diff --git a/src/string.cpp b/src/string.cpp index 591d78b..8644e66 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -427,6 +427,7 @@ std::istream& operator>>(std::istream& s, String& x) */ // TODO: boundary check. +// TODO: Assignment to an individual character does not decouple references (BUG) char& String::operator[](int i) { diff --git a/test/Makefile.am b/test/Makefile.am index 1fa5631..db615db 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,9 +2,11 @@ TESTS = $(check_PROGRAMS) check_output AM_CPPFLAGS = -I../src LDADD = ../src/.libs/libACL.la -check_PROGRAMS = string_assign string_basics string_compare string_cat +check_PROGRAMS = string_assign string_basics string_compare string_cat string_substring string_regex -string_assign_SOURCES = string_assign.cpp -string_basics_SOURCES = string_basics.cpp -string_compare_SOURCES = string_compare.cpp -string_cat_SOURCES = string_cat.cpp +string_assign_SOURCES = string_assign.cpp +string_basics_SOURCES = string_basics.cpp +string_compare_SOURCES = string_compare.cpp +string_cat_SOURCES = string_cat.cpp +string_substring_SOURCES = string_substring.cpp +string_regex_SOURCES = string_regex.cpp diff --git a/test/Makefile.in b/test/Makefile.in index 846db55..6148396 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -88,7 +88,8 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ check_PROGRAMS = string_assign$(EXEEXT) string_basics$(EXEEXT) \ - string_compare$(EXEEXT) string_cat$(EXEEXT) + string_compare$(EXEEXT) string_cat$(EXEEXT) \ + string_substring$(EXEEXT) string_regex$(EXEEXT) subdir = test ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \ @@ -122,6 +123,14 @@ am_string_compare_OBJECTS = string_compare.$(OBJEXT) string_compare_OBJECTS = $(am_string_compare_OBJECTS) string_compare_LDADD = $(LDADD) string_compare_DEPENDENCIES = ../src/.libs/libACL.la +am_string_regex_OBJECTS = string_regex.$(OBJEXT) +string_regex_OBJECTS = $(am_string_regex_OBJECTS) +string_regex_LDADD = $(LDADD) +string_regex_DEPENDENCIES = ../src/.libs/libACL.la +am_string_substring_OBJECTS = string_substring.$(OBJEXT) +string_substring_OBJECTS = $(am_string_substring_OBJECTS) +string_substring_LDADD = $(LDADD) +string_substring_DEPENDENCIES = ../src/.libs/libACL.la AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -157,9 +166,11 @@ am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) am__v_CXXLD_0 = @echo " CXXLD " $@; am__v_CXXLD_1 = SOURCES = $(string_assign_SOURCES) $(string_basics_SOURCES) \ - $(string_cat_SOURCES) $(string_compare_SOURCES) + $(string_cat_SOURCES) $(string_compare_SOURCES) \ + $(string_regex_SOURCES) $(string_substring_SOURCES) DIST_SOURCES = $(string_assign_SOURCES) $(string_basics_SOURCES) \ - $(string_cat_SOURCES) $(string_compare_SOURCES) + $(string_cat_SOURCES) $(string_compare_SOURCES) \ + $(string_regex_SOURCES) $(string_substring_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -521,6 +532,8 @@ string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp string_compare_SOURCES = string_compare.cpp string_cat_SOURCES = string_cat.cpp +string_substring_SOURCES = string_substring.cpp +string_regex_SOURCES = string_regex.cpp all: all-am .SUFFIXES: @@ -580,6 +593,14 @@ string_compare$(EXEEXT): $(string_compare_OBJECTS) $(string_compare_DEPENDENCIES @rm -f string_compare$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(string_compare_OBJECTS) $(string_compare_LDADD) $(LIBS) +string_regex$(EXEEXT): $(string_regex_OBJECTS) $(string_regex_DEPENDENCIES) $(EXTRA_string_regex_DEPENDENCIES) + @rm -f string_regex$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(string_regex_OBJECTS) $(string_regex_LDADD) $(LIBS) + +string_substring$(EXEEXT): $(string_substring_OBJECTS) $(string_substring_DEPENDENCIES) $(EXTRA_string_substring_DEPENDENCIES) + @rm -f string_substring$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(string_substring_OBJECTS) $(string_substring_LDADD) $(LIBS) + mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -590,6 +611,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_basics.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_cat.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_compare.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_regex.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string_substring.Po@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< @@ -839,6 +862,20 @@ string_cat.log: string_cat$(EXEEXT) --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +string_substring.log: string_substring$(EXEEXT) + @p='string_substring$(EXEEXT)'; \ + b='string_substring'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) +string_regex.log: string_regex$(EXEEXT) + @p='string_regex$(EXEEXT)'; \ + b='string_regex'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) check_output.log: check_output @p='check_output'; \ b='check_output'; \ diff --git a/test/string_assign.cpp b/test/string_assign.cpp index ebb4041..732523d 100644 --- a/test/string_assign.cpp +++ b/test/string_assign.cpp @@ -16,6 +16,7 @@ int main() std::cout << "The default contructor makes an empty string: \"" << s0 << "\"\n"; assert(~s0 == 0); + assert(s0 == ""); // A string from a signle character String s1('p'); diff --git a/test/string_regex.cpp b/test/string_regex.cpp new file mode 100644 index 0000000..86ba26d --- /dev/null +++ b/test/string_regex.cpp @@ -0,0 +1,37 @@ +/******************************************************* + * Unit test for the String class + * + * test regular expression operations + ****************************************************** + * + */ + +#include "String.h" +#include + +int main() +{ + // TODO: test regex coinstructors + + // A simple regular expression + regex nr("[0-9]+"); + + // Regular expression matching + String s1 = "abcdef"; + String s2 = "abd123def"; + + std::cout << "Regular expression matching.\n"; + assert((s1 == nr) == false); + assert((s2 == nr) == true); + + // Select a substring with a regex + + String match; + + match = s2(nr); + std::cout << "The matching part of \"" << s2 << "\" is \"" << match << "\"\n"; + assert(match == "123"); + + return 0; +} + diff --git a/test/string_regex.exp b/test/string_regex.exp new file mode 100644 index 0000000..5b5c94b --- /dev/null +++ b/test/string_regex.exp @@ -0,0 +1,3 @@ +Regular expression matching. +The matching part of "abd123def" is "123" +PASS string_regex (exit status: 0) diff --git a/test/string_substring.cpp b/test/string_substring.cpp new file mode 100644 index 0000000..ec2df92 --- /dev/null +++ b/test/string_substring.cpp @@ -0,0 +1,47 @@ +/******************************************************* + * Unit test for the String class + * + * test substring operations + ****************************************************** + * + */ + +#include "String.h" +#include + +int main() +{ + // Default contructor: empty string + String s0; + + // Select a substring + String s1 = "abcdefghijkl"; + String s2 = s1(3,5); // s2 = "defgh" + + std::cout << "The substring (3,5) of " << s1 << " is " << s2 << "\n"; + assert(s2 == "defgh"); + + String s3 = "abcdefghijkl"; + String s4 = "12345678"; + + std::cout << "Replace substring (3,5) of " << s3 << " with " << s4 << " : "; + s3(3,5) = s4; // s3 = "abc12345678ijkl" + std::cout << s3 << "\n"; + assert(s3 == "abc12345678ijkl"); + + String s5 = "abcdefghijkl"; + String s6 = "12345678"; + + std::cout << "Insert \"" << s6 << "\" into \"" << s5 << "\" : "; + s5(3, 0) = s6; + std::cout << s5 << "\n"; + assert(s5 == "abc12345678defghijkl"); + + std::cout << "Remove part of \"" << s5 << "\" : "; + s5(11,5) = ""; + std::cout << s5 << "\n"; + assert(s5 == "abc12345678ijkl"); + + return 0; +} + -- 2.11.0