From dbe0cdddda3bdb37304cfb769fd9de2559a3f09a Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Fri, 29 May 2020 16:16:07 +0200 Subject: [PATCH] String: Fixed conversion to int --- .gitignore | 2 ++ TODO | 4 +-- debian/changelog | 4 +-- debian/copyright | 19 +++++++++++++++ debian/source/options | 1 + doc/Makefile.am | 27 ++++++++++----------- src/String.h | 9 +++++-- test/Makefile.am | 7 +++++- test/string_convert.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ test/string_convert.exp | 7 ++++++ test/string_shift.cpp | 37 ++++++++++++++++++++++++++-- test/string_shift.exp | 6 +++++ 12 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 debian/copyright create mode 100644 debian/source/options create mode 100644 test/string_convert.cpp create mode 100644 test/string_convert.exp diff --git a/.gitignore b/.gitignore index eb6ef18..30b3419 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ Makefile.in aclocal.m4 +autom4te.cache configure missing ylwrap m4 +.*.swp diff --git a/TODO b/TODO index ce2e1d5..b5014a3 100644 --- a/TODO +++ b/TODO @@ -1,12 +1,12 @@ Things to do: ============= -- String: test cases for numerical conversion - String: Implement String != regex operator - String: index and rindex methods with String argument - String: format operator % like in python - String: split and join - String: assign fstream objects to read and write a file +- String: convert to and from std::sstream and std::string - regex: throw an exception when contructing a regex returns an error - date: test parser for invalid dates and syntax errors @@ -15,4 +15,4 @@ Things to do: - UTC: Convert to and from time_t, struct tm -- Add classes: xml, configuration, amount +- xml: Add access to attributes diff --git a/debian/changelog b/debian/changelog index 7d4b4ae..3310131 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,5 +1,5 @@ -libacl (0.3-1) UNRELEASED; urgency=low +libacl (0.3-1) testing; urgency=low - * Initial release. (Closes: #XXXXXX) + * Initial release. -- Arjen Baart Wed, 13 May 2020 08:14:10 +0200 diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..fd29cf0 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,19 @@ +Files: * +Copyright: 2002-2020 Arjen Baart +License: GPL-2+ + + This package is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + . + This package is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + . + You should have received a copy of the GNU General Public License + along with this program. If not, see + . + On Debian systems, the complete text of the GNU General + Public License version 2 can be found in "/usr/share/common-licenses/GPL-2". diff --git a/debian/source/options b/debian/source/options new file mode 100644 index 0000000..862d631 --- /dev/null +++ b/debian/source/options @@ -0,0 +1 @@ +diff-ignore = ".*" diff --git a/doc/Makefile.am b/doc/Makefile.am index 3f50245..cb8ac88 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -16,27 +16,26 @@ EXTRA_DIST = $(XMLS) html-local: manual.html -#ps-local: manual.ps +ps-local: manual.ps -#txt: manual.txt +txt: manual.txt -#all: html txt ps -all: html +all: html txt ps manual.html : $(XMLS) $(IMAGES) - /usr/local/bin/xml2html manual.xml > manual.html + xml2html manual.xml > manual.html -#manual.ps : $(XMLS) $(PICTURES) -# xml2latex manual.xml > manual.tex -# latex manual.tex -# dvips -o manual.ps manual.dvi +manual.ps : $(XMLS) $(PICTURES) + xml2latex manual.xml > manual.tex + latex manual.tex + dvips -o manual.ps manual.dvi -#manual.pdf : $(XMLS) $(PICTURES) -# xml2latex manual.xml > manual.tex -# pdflatex manual.tex +manual.pdf : $(XMLS) $(PICTURES) + xml2latex manual.xml > manual.tex + pdflatex manual.tex -#manual.txt : $(XMLS) -# xml2text manual.xml > manual.txt +manual.txt : $(XMLS) + xml2text manual.xml > manual.txt check: # xmllint --noout --xinclude --loaddtd --noent --schema /usr/local/xslt/doc.xsd manual.xml diff --git a/src/String.h b/src/String.h index 118d855..d32b276 100644 --- a/src/String.h +++ b/src/String.h @@ -105,9 +105,15 @@ public: /* Numerical conversion */ + String(int); // String x(12); x = "12" String(long); // String x(12); x = "12" String(unsigned long); // String x(12); x = "12" - String(int); // String x(12); x = "12" + + operator int() + { + return strtol(p->s, 0, 0); + } + operator long() { return strtol(p->s, 0, 0); @@ -139,7 +145,6 @@ public: return strtod(p->s, 0); } - /* */ char& operator[](size_t i); // Individual character access diff --git a/test/Makefile.am b/test/Makefile.am index a21b337..70fd173 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,7 +2,8 @@ TESTS = $(check_PROGRAMS) date_today hour_now check_output AM_CPPFLAGS = -I../src LDADD = ../src/.libs/libACL.la -check_PROGRAMS = string_assign string_basics string_compare string_cat string_modify string_shift string_substring string_regex \ +check_PROGRAMS = string_assign string_basics string_compare string_cat string_convert string_modify \ + string_shift string_substring string_regex \ date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \ hour_assign hour_parse hour_compare hour_arithmetic hour_check_now \ utc_assign utc_parse utc_compare utc_arithmetic utc_convert \ @@ -14,6 +15,7 @@ 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_convert_SOURCES = string_convert.cpp string_modify_SOURCES = string_modify.cpp string_shift_SOURCES = string_shift.cpp string_substring_SOURCES = string_substring.cpp @@ -55,3 +57,6 @@ xml_elem_SOURCES = xml_elem.cpp configuration_read_SOURCES = configuration_read.cpp configuration_find_SOURCES = configuration_find.cpp + +clean-local: + rm -f xml_test01.out.xml diff --git a/test/string_convert.cpp b/test/string_convert.cpp new file mode 100644 index 0000000..7f37906 --- /dev/null +++ b/test/string_convert.cpp @@ -0,0 +1,54 @@ +/******************************************************* + * Unit test for the String class + * + * test conversion operations + ****************************************************** + * + */ + +#include "String.h" +#include + +int main() +{ + + // Construct a string from a number + + String s1(12); + + std::cout << s1 << " is contructed from the number 12\n"; + assert(s1 == "12"); + + String s2(2.32); + + std::cout << s2 << " is contructed from the number 2.32\n"; + assert(s2 == "2.320"); + + int n; + double f; + + String s3("42"); + n = int(s3); + std::cout << "String " << s3 << " converts into number " << n << "\n"; + assert(n == 42); + + String s4("3.1415"); + f = double(s4); + std::cout << "String " << s4 << " converts into number " << f << "\n"; + assert(f == 3.1415); + + int k; + double x; + + String s5("not a number"); + k = int(s5); + std::cout << "String " << s5 << " converts into number " << k << "\n"; + assert(k == 0); + + x = double(s5); + std::cout << "String " << s5 << " converts into number " << x << "\n"; + assert(x == 0.0); + + return 0; +} + diff --git a/test/string_convert.exp b/test/string_convert.exp new file mode 100644 index 0000000..f825bfd --- /dev/null +++ b/test/string_convert.exp @@ -0,0 +1,7 @@ +12 is contructed from the number 12 +2.320 is contructed from the number 2.32 +String 42 converts into number 42 +String 3.1415 converts into number 3.1415 +String not a number converts into number 0 +String not a number converts into number 0 +PASS string_convert (exit status: 0) diff --git a/test/string_shift.cpp b/test/string_shift.cpp index f8c8a0f..16ee513 100644 --- a/test/string_shift.cpp +++ b/test/string_shift.cpp @@ -14,11 +14,19 @@ int main() String s1 = "abcdefghij"; - String s2 = s1 << 3; + String s2 = s1 << 0; + std::cout << s1 << " << 0 = " << s2 << "\n"; + assert(s2 == "abcdefghij"); + + s2 = s1 << 3; std::cout << s1 << " << 3 = " << s2 << "\n"; assert(s2 == "defghij"); - String s3 = s1 >> 3; + String s3 = s1 >> 0; + std::cout << s1 << " >> 0 = " << s3 << "\n"; + assert(s3 == "abcdefghij"); + + s3 = s1 >> 3; std::cout << s1 << " >> 3 = " << s3 << "\n"; assert(s3 == "abcdefg"); @@ -34,6 +42,31 @@ int main() std::cout << s5 << "\n"; assert(s5 == "abcde"); + // Shift an empty string + + String s6, s7; + s7 = s6 << 4; + std::cout << "Shift left an empty string: \"" << s7 << "\"\n"; + assert(s7 == ""); + + String s8, s9; + s9 = s8 >> 4; + std::cout << "Shift right an empty string: \"" << s9 << "\"\n"; + assert(s9 == ""); + + // Shift more then the string length + + String s10 = "abcdefghji"; + String s11, s12; + + s11 = s10 << 42; + std::cout << s10 << " << 42 = \"" << s11 << "\"\n"; + assert(s11 == ""); + + s12 = s10 >> 42; + std::cout << s10 << " >> 42 = \"" << s12 << "\"\n"; + assert(s12 == ""); + return 0; } diff --git a/test/string_shift.exp b/test/string_shift.exp index c280a95..9d8bc4e 100644 --- a/test/string_shift.exp +++ b/test/string_shift.exp @@ -1,5 +1,11 @@ +abcdefghij << 0 = abcdefghij abcdefghij << 3 = defghij +abcdefghij >> 0 = abcdefghij abcdefghij >> 3 = abcdefg abcdefghij <<= 5 : fghij abcdefghij >>= 5 : abcde +Shift left an empty string: "" +Shift right an empty string: "" +abcdefghji << 42 = "" +abcdefghji >> 42 = "" PASS string_shift (exit status: 0) -- 2.20.1