From f5803e9b577adc39139403f47ecb11db11abb395 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Fri, 12 Jun 2020 10:07:38 +0200 Subject: [PATCH] Introduction of class SuperString --- ChangeLog | 5 ++ TODO | 1 + doc/Makefile.am | 2 +- doc/UTC.html | 68 -------------- doc/complex.html | 25 ------ doc/hour.html | 82 ----------------- doc/index.html | 16 ---- doc/manual.xml | 2 + doc/string.html | 173 ------------------------------------ doc/string.xml | 1 + doc/superstring.xml | 124 ++++++++++++++++++++++++++ src/Makefile.am | 2 +- src/String.h | 46 ++++++++++ src/string.cpp | 34 ++++++- src/superstring.cpp | 44 +++++++++ test/Makefile.am | 6 ++ test/string_cat.cpp | 2 +- test/string_split_join.cpp | 42 +++++++++ test/superstring_assign.cpp | 92 +++++++++++++++++++ test/superstring_assign.exp | 5 ++ test/superstring_basics.cpp | 98 ++++++++++++++++++++ test/superstring_basics.exp | 12 +++ test/superstring_cat.cpp | 58 ++++++++++++ 23 files changed, 572 insertions(+), 368 deletions(-) delete mode 100644 doc/UTC.html delete mode 100644 doc/complex.html delete mode 100644 doc/hour.html delete mode 100644 doc/index.html delete mode 100644 doc/string.html create mode 100644 doc/superstring.xml create mode 100644 src/superstring.cpp create mode 100644 test/string_split_join.cpp create mode 100644 test/superstring_assign.cpp create mode 100644 test/superstring_assign.exp create mode 100644 test/superstring_basics.cpp create mode 100644 test/superstring_basics.exp create mode 100644 test/superstring_cat.cpp diff --git a/ChangeLog b/ChangeLog index b959431..0b1e076 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Jun 11, 2020 - Release 0.3.1 +============================ + + - Introduction of class SuperString + Feb 07, 2020 - Release 0.3 ============================ diff --git a/TODO b/TODO index b5014a3..12b3b72 100644 --- a/TODO +++ b/TODO @@ -16,3 +16,4 @@ Things to do: - UTC: Convert to and from time_t, struct tm - xml: Add access to attributes +- xml: Xinclude processing diff --git a/doc/Makefile.am b/doc/Makefile.am index cb8ac88..ad337d8 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -7,7 +7,7 @@ SUFFIXES = .obj .eps .png .obj.eps: tgif -print -eps -color $< -XMLS = manual.xml string.xml date.xml hour.xml utc.xml Integer.xml xml.xml +XMLS = manual.xml string.xml superstring.xml date.xml hour.xml utc.xml Integer.xml xml.xml IMAGES= PICTURES= diff --git a/doc/UTC.html b/doc/UTC.html deleted file mode 100644 index 9575996..0000000 --- a/doc/UTC.html +++ /dev/null @@ -1,68 +0,0 @@ - - -UTC - ANDROMEDA CLASS LIBRARY - - - -

NAME

-UTC - Universal Time Coordinated class -

SYNOPSIS

-
-#include 
-
-UTC clock;
-
-

DESCRIPTION

- - -

Construction

- -
UTC()
-
UTC(date d, hour t)
-
UTC(string)
- -

Assignment

- -
operator=(string &)
- -

Conversion

- -
string()
- -

Relational operations

- -
operator==(UTC &)
-
operator!=(UTC &)
-
operator<(UTC &)
-
operator<=(UTC &)
-
operator>(UTC &)
-
operator>=(UTC &)
- -

Attributes

- -
date day()
-
hour hour()
- -

Arithmetic

- -
UTC operator+(UTC &t1, UTC &t2)
-
UTC operator-(UTC &t1, UTC &t2)
-
UTC &operator+=(UTC &t)
-
UTC &operator-=(UTC &t)
-
UTC &operator++()
-
UTC &operator--()
- -

Stream I/O

- -
ostream &operator<<(ostream &str, UTC &t)
-
istream &operator>>(istream &str, UTC &t)
- -

SEE ALSO

- -date -hour - -

DIAGNOSTICS

- - - diff --git a/doc/complex.html b/doc/complex.html deleted file mode 100644 index a73eb32..0000000 --- a/doc/complex.html +++ /dev/null @@ -1,25 +0,0 @@ - - -complex - ANDROMEDA CLASS LIBRARY - - - -

NAME

-

complex - Complex number arithmetic

-

SYNOPSIS

-

complex z;

-

DESCRIPTION

-

The complex class encapsulates a comples number consisting of a real -and an imaginary part.

-

Construction

-

Assignment

-

Conversion

-

Relational Operators

-

Attributes

-

Arithmetic

-

Mathematical operations

-

Stream I/O

-

SEE ALSO

-

DIAGNOSTICS

- - diff --git a/doc/hour.html b/doc/hour.html deleted file mode 100644 index 5539b9d..0000000 --- a/doc/hour.html +++ /dev/null @@ -1,82 +0,0 @@ - - -hour - ANDROMEDA CLASS LIBRARY - - - -

NAME

-hour - Time class -

SYNOPSIS

-
-#include 
-
-hour t;
-
-

DESCRIPTION

- -The hour class encapsulates a time in hours, minutes and seconds. - -

Construction

- -
hour()
-
hour(int hour, short minute, short second)
-
hour(string)
- -Format is HH:MM:SS - -
hour(long)
- -A number of seconds. - -

Assignment

- -
operator=(hour &)
-
operator=(string &)
-
operator=(long)
- -

Conversion

- -
long()
- -Converts to a number of seconds. - -
string()
- -

Relational operations

- -
operator==(hour &)
-
operator!=(hour &)
-
operator<(hour &)
-
operator<=(hour &)
-
operator>(hour &)
-
operator>=(hour &)
- -

Attributes

- -
int hour()
-
short minute()
-
short second()
- -

Arithmetic

- -
hour operator+(hour &t1, hour &t2)
-
hour operator-(hour &t1, hour &t2)
-
hour &operator+=(hour &t)
-
hour &operator-=(hour &t)
-
hour &operator++()
-
hour &operator--()
- -

Stream I/O

- -
ostream &operator<<(ostream &str, hour &t)
-
istream &operator>>(istream &str, hour &t)
- -

SEE ALSO

- -date -UTC - -

DIAGNOSTICS

- - - diff --git a/doc/index.html b/doc/index.html deleted file mode 100644 index d5668a1..0000000 --- a/doc/index.html +++ /dev/null @@ -1,16 +0,0 @@ - - -ANDROMEDA CLASS LIBRARY Class index - - - - - - diff --git a/doc/manual.xml b/doc/manual.xml index 32e8d08..629da30 100644 --- a/doc/manual.xml +++ b/doc/manual.xml @@ -7,6 +7,8 @@ + - -string - ANDROMEDA CLASS LIBRARY - - - -

class string

-
-
-
-

NAME

-

String - C++ string class

-

SYNOPSIS

-
-

#include <Stringc.h>

-
-

DESCRIPTION

-

The C++ String class can be used to represent character strings and perform -operation upon them. Many operations are defined for normal operators but may -have different semantics. E.g. the + operator is used to concatenate (add) -strings together.

-

Substrings may be selected from strings to create other strings. Also, -substrings have lvalue semantics, meaning a string may be assigned to a -substring expression.

-

Construction and assignment.

-

String()

-

The default constructor creates an empty string.

-

String(const char *)

-

String(char)

-

String(const String &)

-

Other constructors are provided to initialize strings from character -pointers, single characters or other strings. These constuctors also take care -of typecasting:

-
-

String x; // Contruct an empty string -String abc("abc"); // Convert a char * to a string -String p('p'); // A string with one character in it. -

-
-

Another string (or string expression), a char* or a single character can be -assigned to a string:

-
-

String x, y; - -x = "abc"; -y = x; -x = 'p';

-
-

Relational operators.

-

All relational operators (==, != , <, <=, > and >=) are -available to compare strings alphabethically. Note that the comparison is done -in the local character set, which will be ASCII in most cases. This also means -that case is significant.

-

Adding strings together.

-

String can be added together, i.e. concatenated, with the + -operator. Of course, the += operator can be used as well. Example: -

-
-

String x, y, z; - -x = "abc" -y = "def" -z = x + y; // z = "abcdef" -y += x; // y = "defabc"

-
-

String length, testing and character access

-

The length of a string can be determined with the ~ operator. Do not -confuse this with the bit-wise inversion operator !

-
-

String x = "Hello"; -int l = ~x; // l = 5

-
-

The test for an empty string is done with the ! operator. Note that -there is no test for a non-empty string.

-
-

if (!x) - cout << "String x is empty\n";

-
-

Access to individual characters is provided like in ordinary C strings. The -[] operator selects an individual character for both read and write access: -

-
-

String x = "Hello"; - -char c = x[1]; // c = 'e' -x[2] = 'L'; // x = "HeLlo"

-
-

Substring expressions.

-

A substring is a part of a string denoted by its start index and its -length. The start index is counted from zero. To extract a substring from a -string use:

-
-

String x = "abcdefghijkl"; -String y = x(3,5); // y = "defgh"

-
-

A substring expression can also be used as an lvalue. This means that a -string can be assigned to a substring. The length of the substring does not -need to be equal to the length of the string that is assigned to it.

-
-

String x = "abcdefghijkl"; -String y = "12345678"; - -x(3,5) = y; // x = "abc12345678ijkl"

-
-

Note that assigning a string to a zero-length substring will simply insert -the a string into another string. Reversely, assigning an empty string to a -substring will remove the characters from the original string. This property -can be used to truncate a string to its first n characters by:

-
-

x(n, ~x-n) = "";

-
-

Numerical Conversion

-

Strings are coverted to and from numerical types (integer or floating -point) by constructors and conversion operators. A numerical value is -converted to a string with the number constructor:

-
-

String n(12); // n = "12"

-

String x(2.32); // x = "2.32"

-
-

These functions use a default format to convert the number to a string. -Specifically, an integer is converted with the "%d" format from -printf and a floating point is converted with the "%g" format. -

-

A string is converted to a number with the type-casting operators: operator -long() and operator double(). The conversion to a long integer recognizes C -syntax for numerical literals. Numbers starting with '0' are regarded as octal -and numbers starting with '0x' are regarded as hexadecimal.

-

Special member functions allow conversion of strings to and from numbers in -a specific format. The functions dec(), oct() and hex() convert the string -using decimal, octal and hexadecimal, respectively.

-

Shifting

-

Shifting a string moves all characters in the string a number of places to -the left or to the right. Characters on the other end of the string simply -fall off. Unlike shifting a binary number, shifting a string does not pad the -string. Rather, shifting a string n places either to the left or to the -right, renders the string n characters shorter. Example:

-
-

"abcdefgh" << 3 = "defgh"

-

"abcdefgh" >> 3 = "abcde"

-
-

The shift operators << and >> can be combined with assignment: -<<= and >>=.

-

Do not confuse these shift operators with stream I/O operators.

-

Operations

-
String upper(void)
-

Convert the string to all upper-case characters.

-
String lower(void)
-

Convert the string to all lower-case characters.

-

Pattern matching

-
int in(String &x)
-

Find the string x within the string. Returns the index where x was -found. Returns -1 if x was not found in the string. see also strstr(). -

-

Stream I/O.

-

A string can be written to a stream like any other type with the << -operator. The left hand side of the expression must be a stream. To read a -string from a stream, use the >> operator, with a stream on the -left hand side:

-
-

String x; - -cin >> x; -cout << x;

-
-

Note that reading a string from a istream behaves like reading a character -array from an istream. I.e., characters are read from the stream until the -first whitespace character.

-

SEE ALSO

-

Bjarne Stroustrup, Section 6.9,
-DDJ October 1991, pg 24

-

DIAGNOSTICS

- - diff --git a/doc/string.xml b/doc/string.xml index cf53e99..998bfcd 100644 --- a/doc/string.xml +++ b/doc/string.xml @@ -111,6 +111,7 @@ char c = x[1]; // c = 'e' x[2] = 'L'; // x = "HeLlo" +
Substring expressions. diff --git a/doc/superstring.xml b/doc/superstring.xml new file mode 100644 index 0000000..467c767 --- /dev/null +++ b/doc/superstring.xml @@ -0,0 +1,124 @@ + + +class SuperString + +A SuperString is a sequence of Strings, just like a String is a sequence of characters. +A SuperString is in fact a string of Strings. +Many operations that can be applied to String objects are also available for SuperSting objects, +for example realtional operators, adding to SuperString objects, access to individual characters +and 'substring' expressions. + +Split and join operations convert Strings into SuperStrings and vice versa. +In that sense, split and join are complementary operations. +The split() method of a string creates a SuperString. +Conversily, the join() method of a SuperString creates a String. + + +
+Construction and assignment. + +The default constructor creates an empty superstring, one that does not contain any strings. +A superstring constructed from a string creates a superstring with one element. +Other contructors can create a SuperString from STL collectors (list and vector) of strings. + + + + The default constructor creates an empty string. + + +Create a SuperSting with one element. + + +Create a SuperSting with n empty elements. + + + + +Another SuperString, String or SuperString expression can be +assigned to a SuperString: + + +SuperString x, y; +String z("abc"); + +x = z; // Create a SuperString with one element +y = x; + +
+ +
+SuperString length, testing and element access + + +The length of a SuperString can be determined with the ~ operator. +This is the number of Strings in the SuperString. +Do not confuse this with the bit-wise inversion operator ! + + + +SuperString xx(5); +int l = ~xx; // l = 5 + + + +The test for an empty string is done with the booloperator. +This operator returns true if the SuperString object contains 1 or more Strings. +It does not mean, however, that the String objects inside the SuperSting are not empty. + + +if (!x) + cout << "String x is empty\n"; + + +Access to individual String elements is provided like in ordinary arrays. The +[] operator selects an individual String for both read and write access: + + +SuperString xx(3); + +String s = "Hello"; +xx[2] = s; + + +Access out of range will throw a std::out_of_range exception. + +
+ +
+Adding to SuperStrings + + +SuperString objects can be added together, i.e. concatenated, with the + +operator. Of course, the += operator can be used as well. Example: + + +SuperString x, y, z; + +SuperString x("abc"); +SuperString y("def"); + +z = x + y; // z = [ "abc", "def" ] +y += z; // y = [ "def", "abc", "def" ] + +
+ + split on a regex ? how to split on an empty string ? + +split and join for csv formats. quoting and escape , ? + + + +the + operater can add strings and superstrings to append or prepend the one to the other. the result us always a superstring. + +add characters and strings ? -> TODO + +stream io is not possible. the split and join functions must be used instead + +the substring expression creates a selection of strings of the superstring just like the substring expression of a string creates a selection of characters. +a substring expression with regex argument selects all strings that match the expression. implements a sort of grep function. this means the substring may not be a consequitive subset of the superstring. + +assigning to a substring repleces all strings in the substring + +convert to and from a std vector or list + +
diff --git a/src/Makefile.am b/src/Makefile.am index 1371d53..de2e50a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libACL.la -libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c \ +libACL_la_SOURCES = string.cpp regex.cpp superstring.cpp date.cpp parsedate.c dateyacc.y datelex.c \ hour.cpp utc.cpp \ Integer.cpp \ xml.cpp \ diff --git a/src/String.h b/src/String.h index d32b276..2fb6f18 100644 --- a/src/String.h +++ b/src/String.h @@ -45,6 +45,7 @@ #include #include +#include #include #include @@ -52,6 +53,7 @@ // Forward declarations. class substring; class regex; +class SuperString; /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ** NAME : String - Character String class. @@ -311,6 +313,9 @@ public: friend bool operator == (const String &s, const regex &r); friend bool operator == (const regex &r, const String &s); // TODO: The != operator + + SuperString split(const String &separator); + }; /* * Other operators to think about... @@ -404,4 +409,45 @@ public: } }; +class SuperString +{ + std::vector _ss; + +public: + + SuperString() {}; + + SuperString(const String &s) + { + std::vector __ss(1, s); + _ss = __ss; + } + + SuperString(const int n) + { + std::vector __ss(n); + _ss = __ss; + } + + operator bool() // Test for empty SuperString + { + return _ss.size() != 0; + } + + int operator~() const // Length of the SuperString + { + return _ss.size(); + } + + String& operator[](size_t i) // Individual String access + { + return _ss.at(i); + } + + // Adding Strings and SuperStrings + + friend SuperString operator+(const SuperString&, const String&); + SuperString& operator+=(const String&); +}; + #endif /* STRING_H */ diff --git a/src/string.cpp b/src/string.cpp index dd159d0..6ad0fd3 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -432,7 +432,6 @@ std::istream& operator>>(std::istream& s, String& x) * The subscript operator is provided for access to individual characters */ -// TODO: boundary check. // TODO: Assignment to an individual character does not decouple references (BUG) char& String::operator[](size_t i) @@ -717,3 +716,36 @@ int String::in(String & x) else return -1; } + +SuperString String::split(const String &separator) +{ + SuperString splitted; + char *sep, *part; + + part = p->s; + sep = strstr(p->s, separator.p->s); + + while (sep != NULL) + { + // Create a new string from the part until the separator + + int len = sep - part; + char *str = new char[len + 1]; + + strncpy(str, part, len); + str[len] = '\0'; + splitted += String(str); + + // Look for the next separator + + sep += ~separator; + part = sep; + sep = strstr(sep, separator.p->s); + } + + // Add the leftover after the last separator. + + splitted += String(part); + + return splitted; +} diff --git a/src/superstring.cpp b/src/superstring.cpp new file mode 100644 index 0000000..5715ac9 --- /dev/null +++ b/src/superstring.cpp @@ -0,0 +1,44 @@ +/************************************************************************** +** (c) Copyright 1997, Andromeda Technology & Automation +*************************************************************************** +** MODULE INFORMATION * +*********************** +** FILE NAME : superstring.cpp +** SYSTEM NAME : Andromeda X-Windows Encapsulation +** VERSION NUMBER : $Revision: 1.5 $ +** +** DESCRIPTION : SuperString class implementation. +** +** EXPORTED OBJECTS : +** LOCAL OBJECTS : +** MODULES USED : +*************************************************************************** +** ADMINISTRATIVE INFORMATION * +******************************** +** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl +** CREATION DATE : Jun 10, 2020 +** LAST UPDATE : Jun 10, 2020 +** MODIFICATIONS : +**************************************************************************/ + +#include +#include +#include "String.h" + +// Addition operators + +SuperString operator+(const SuperString& ss, const String& s) +{ + SuperString SS(ss); + + SS._ss.push_back(s); + + return SS; +} + +SuperString& SuperString::operator+=(const String& x) +{ + _ss.push_back(x); + + return *this; +} diff --git a/test/Makefile.am b/test/Makefile.am index 70fd173..7313716 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,6 +4,7 @@ AM_CPPFLAGS = -I../src LDADD = ../src/.libs/libACL.la check_PROGRAMS = string_assign string_basics string_compare string_cat string_convert string_modify \ string_shift string_substring string_regex \ + superstring_assign superstring_basics superstring_cat string_split_join \ 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 \ @@ -21,6 +22,11 @@ string_shift_SOURCES = string_shift.cpp string_substring_SOURCES = string_substring.cpp string_regex_SOURCES = string_regex.cpp +superstring_assign_SOURCES = superstring_assign.cpp +superstring_basics_SOURCES = superstring_basics.cpp +superstring_cat_SOURCES = superstring_cat.cpp +string_split_join_SOURCES = string_split_join.cpp + date_assign_SOURCES = date_assign.cpp date_parse_SOURCES = date_parse.cpp date_compare_SOURCES = date_compare.cpp diff --git a/test/string_cat.cpp b/test/string_cat.cpp index 56c1da8..71eff4d 100644 --- a/test/string_cat.cpp +++ b/test/string_cat.cpp @@ -1,7 +1,7 @@ /******************************************************* * Unit test for the String class * - * test relational operators + * test addition operators ****************************************************** * */ diff --git a/test/string_split_join.cpp b/test/string_split_join.cpp new file mode 100644 index 0000000..63aa7d9 --- /dev/null +++ b/test/string_split_join.cpp @@ -0,0 +1,42 @@ +/******************************************************* + * Unit test for the SuperString class + * + * test split and join of String and SuperString + ****************************************************** + * + */ + +#include "String.h" +#include + +void print_ss(SuperString ss) +{ + std::cout << "["; + for (int i = 0; i < ~ss; i++) + { + if (i != 0) + std::cout << ","; + std::cout << "\"" << ss[i] << "\""; + } + std::cout << "]"; +} + +int main() +{ + String s0("The_String_To_Split"); + SuperString ss0; + + ss0 = s0.split("_"); + + std::cout << "The string " << s0 << " split on \"_\":\n"; + print_ss(ss0); + std::cout << "\n"; + + assert(ss0[0] == "The"); + assert(ss0[1] == "String"); + assert(ss0[2] == "To"); + assert(ss0[3] == "Split"); + + return 0; +} + diff --git a/test/superstring_assign.cpp b/test/superstring_assign.cpp new file mode 100644 index 0000000..3a9fbca --- /dev/null +++ b/test/superstring_assign.cpp @@ -0,0 +1,92 @@ +/******************************************************* + * Unit test for the SuperString class + * + * test contrustor and assignment of SuperSting objects + ****************************************************** + * + */ + +#include "String.h" +#include + +int main() +{ + // Default contructor: empty string + SuperString s0; + + std::cout << "The default contructor makes an empty superstring.\n"; + assert(~s0 == 0); + + String s1("abc"); + SuperString ss1(s1); + + std::cout << "Contructor from a String makes a superstring with 1 element.\n"; + assert(~ss1 == 1); + + SuperString ss2(4); + + std::cout << "Contructor with an integer argument makes a superstring with n elements.\n"; + assert(~ss2 == 4); + + // The copy contructor + + SuperString ss3(ss2); + + std::cout << "The copy contructor makes a bitwise copy.\n"; + assert(~ss3 == 4); + + // Assignment to a SuperSting with n elements should remove the old elements. + + return 0; + + // A string from a literal string + String s2("abc"); + + std::cout << "A string from a literal string \"abc\": \"" << s2 << "\"\n"; + assert(~s2 == 3); + + // The copy constructor + String s3(s2); + + std::cout << "A string copied from the previous string \"abc\": \"" << s3 << "\"\n"; + assert(~s3 == 3); + + // Assign a single character + String s4; + s4 = 'q'; + std::cout << "A string assigned a single charater 'q': \"" << s4 << "\"\n"; + assert(~s4 == 1); + + // Initialized with a single character + String s5 = 'r'; + + std::cout << "A string initialized with a single charater 'r': \"" << s5 << "\"\n"; + assert(~s5 == 1); + + // Assign a literal string + String s6; + s6 = "ijk"; + std::cout << "A string assigned a literal string \"ijk\": \"" << s6 << "\"\n"; + assert(~s6 == 3); + + // Initialized a literal string + String s7 = "lmn"; + + std::cout << "A string initialized with a literal string \"lmn\": \"" << s7 << "\"\n"; + assert(~s7 == 3); + + // Assign a String object + String s8; + s8 = s6; + std::cout << "A string assigned a String object \"ijk\": \"" << s8 << "\"\n"; + assert(~s8 == 3); + + // Initialized a String object + String s9 = s7; + + std::cout << "A string initialized with a String object \"lmn\": \"" << s9 << "\"\n"; + assert(~s9 == 3); + + return 0; +} + diff --git a/test/superstring_assign.exp b/test/superstring_assign.exp new file mode 100644 index 0000000..49aff2a --- /dev/null +++ b/test/superstring_assign.exp @@ -0,0 +1,5 @@ +The default contructor makes an empty superstring. +Contructor from a String makes a superstring with 1 element. +Contructor with an integer argument makes a superstring with n elements. +The copy contructor makes a bitwise copy. +PASS superstring_assign (exit status: 0) diff --git a/test/superstring_basics.cpp b/test/superstring_basics.cpp new file mode 100644 index 0000000..72b777e --- /dev/null +++ b/test/superstring_basics.cpp @@ -0,0 +1,98 @@ +/******************************************************* + * Unit test for the SuperString class + * + * test basic SuperString operations + ****************************************************** + * + */ + +#include "String.h" +#include + +int main() +{ + // Default contructor: empty SuperString + SuperString ss0; + + assert(~ss0 == 0); + + // The length of a string with the ~ operator + String s1("abcde"); + SuperString ss1(s1); + + // check boolean opareations that check for empty strings + + if (ss0) + { + std::cout << " ss0 is not empty.\n"; + } + else + { + std::cout << " ss0 is empty.\n"; + } + if (!ss0) + { + std::cout << " ss0 is empty.\n"; + } + else + { + std::cout << " ss0 is not empty.\n"; + } + + if (ss1) + { + std::cout << " ss1 is not empty.\n"; + } + else + { + std::cout << " ss1 is empty.\n"; + } + if (!ss1) + { + std::cout << " ss1 is empty.\n"; + } + else + { + std::cout << " ss1 is not empty.\n"; + } + + int length; + SuperString ss2(5); + length = ~ss2; + std::cout << "The length of ss2 is " << length << "\n"; + assert(length == 5); + + // test String access, also outside boundaries (e.g. -1, 666) + + String s2; + s2 = ss1[0]; + std::cout << "First string of ss1 is \"" << s2 << "\"\n"; + assert(s2 == "abcde"); + + // An out of bounds index throws an exception + + try + { + s2 = ss1[3]; + std::cout << "Fourth string of ss1 is \"" << s2 << "\"\n"; + } + catch (std::out_of_range e) + { + std::cout << "SuperString exception: " << e.what() << "\n"; + } + + // Fill up a SuperSting with some content. + + SuperString ss3(4); + ss3[0] = "abc"; + ss3[1] = "def"; + ss3[3] = "xyz"; + + for (int i = 0; i < ~ss3; i++) + { + std::cout << "ss3[" << i << "] = " << ss3[i] << "\n"; + } + return 0; + +} + diff --git a/test/superstring_basics.exp b/test/superstring_basics.exp new file mode 100644 index 0000000..054d1e5 --- /dev/null +++ b/test/superstring_basics.exp @@ -0,0 +1,12 @@ + ss0 is empty. + ss0 is empty. + ss1 is not empty. + ss1 is not empty. +The length of ss2 is 5 +First string of ss1 is "abcde" +SuperString exception: vector::_M_range_check: __n (which is 3) >= this->size() (which is 1) +ss3[0] = abc +ss3[1] = def +ss3[2] = +ss3[3] = xyz +PASS superstring_basics (exit status: 0) diff --git a/test/superstring_cat.cpp b/test/superstring_cat.cpp new file mode 100644 index 0000000..ff16ae0 --- /dev/null +++ b/test/superstring_cat.cpp @@ -0,0 +1,58 @@ +/******************************************************* + * Unit test for the SuperString class + * + * test addition operators + ****************************************************** + * + */ + +#include "String.h" +#include + +void print_ss(SuperString ss) +{ + std::cout << "["; + for (int i = 0; i < ~ss; i++) + { + if (i != 0) + std::cout << ","; + std::cout << "\"" << ss[i] << "\""; + } + std::cout << "]"; +} + +int main() +{ + // Add a String to a SuperString, + operator + + SuperString ss0; + + String s1("abc"); + String s2("def"); + SuperString ss1(s1); + + ss0 = ss1 + s2; + + print_ss(ss1); + std::cout << " + " << s2 << " = "; + print_ss(ss0); + std::cout << "\n"; + + // Add a String to a SuperString, += operator + + SuperString ss2; + + String s3("uvw"); + String s4("xyz"); + + ss2 += s3; + print_ss(ss2); + std::cout << "\n"; + + ss2 += s4; + print_ss(ss2); + std::cout << "\n"; + + return 0; +} + -- 2.20.1