Implements utils and tests.

This commit is contained in:
2023-04-17 22:58:50 -07:00
parent d7df3a2be4
commit 1f6df1227c
3 changed files with 227 additions and 34 deletions

View File

@@ -21,7 +21,7 @@ namespace SBF {
using std::make_tuple;
using std::vector;
string word_wrap(const string& text, int maxWidth) {
string word_wrap(const string& text, const size_t max_width) {
string output = "";
string thisLine = "";
string nextChunk = "";
@@ -46,13 +46,13 @@ namespace SBF {
thisLine = thisLine + " ";
}
thisLineLength = thisLine.size();
if (nextChunkLength > maxWidth) {
nextChunk = get_substring(text, thisLineCurrentPosition, maxWidth - thisLineLength);
nextSpace = thisLineStartPosition + maxWidth;
if (nextChunkLength > max_width) {
nextChunk = get_substring(text, thisLineCurrentPosition, max_width - thisLineLength);
nextSpace = thisLineStartPosition + max_width;
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace;
} else if(thisLineLength + nextChunkLength > maxWidth) {
thisLine = make_fit_l(thisLine, maxWidth, L' ');
} else if(thisLineLength + nextChunkLength > max_width) {
thisLine = make_fit_l(thisLine, max_width, L' ');
} else {
thisLine = thisLine + nextChunk;
thisLineCurrentPosition = nextSpace + 1;
@@ -61,11 +61,11 @@ namespace SBF {
} else {
thisLineCurrentPosition = nextSpace + 1;
}
if (thisLineLength >= maxWidth || thisLineCurrentPosition > textLength) {
if (thisLineLength >= max_width || thisLineCurrentPosition > textLength) {
if (thisLineCurrentPosition > textLength) {
done = true;
}
thisLine = make_fit_l(thisLine, maxWidth, L'_');
thisLine = make_fit_l(thisLine, max_width, L'_');
output += thisLine + (done ? "" : "\n");
thisLine = "";
thisLineLength = thisLine.size();
@@ -76,7 +76,11 @@ namespace SBF {
return output;
}
string string_dollar(size_t length, char ch) {
string string_dollar(const size_t length, const char ch) {
if (ch == '\0') {
return string_dollar(length, ' ');
}
string str = "";
for (size_t i = 0; i<length; i++) {
str += ch;
@@ -84,19 +88,19 @@ namespace SBF {
return str;
}
string left(const string& text, size_t length) {
string left(const string& text, const size_t length) {
return text.substr(0, length);
}
string make_fit_l(const string& text, size_t length, char paddCh) {
return left(text + string_dollar(length, paddCh), length);
string make_fit_l(const string& text, const size_t length, const char pad_character) {
return left(text + string_dollar(length, pad_character != '\0' ? pad_character : ' '), length);
}
string get_substring(const string& text, const int32_t var1, const int32_t var2) {
return text.substr(std::min<int32_t>(var1, text.length()-1), std::max(var2, 0));
string get_substring(const string& text, const size_t start, const size_t length) {
return text.substr(std::min<size_t>(start, text.length()), std::max<size_t>(length, 0));
}
size_t get_index_of(const string& text, const string& search, size_t start) {
size_t get_index_of(const string& text, const string& search, const size_t start) {
return text.find(search, start);
}
} // End namespace SBF

View File

@@ -16,13 +16,44 @@
namespace SBF {
using std::string;
size_t get_index_of(const string& text, const string& search, size_t start);
string word_wrap(const string& text, int maxWidth);
string get_substring(const string& text, int32_t var1, int32_t var2);
string make_fit_l(const string& text, size_t length, char paddCh);
string left(const string& text, size_t length);
string string_dollar(size_t length, char ch);
/// @brief Gets the first index of search in text starting at start.
/// @param text The text to search.
/// @param search The text to search for.
/// @param start The position to start searching at.
/// @return The position of the string if found and std::npos if not found.
size_t get_index_of(const string& text, const string& search, const size_t start);
/// @brief Collapses white space and attempts to word wrap text to a max of max_width columns.
/// @param text The text to wrap.
/// @param max_width The number of columns to wrap to.
/// @return The wrapped text.
string word_wrap(const string& text, const size_t max_width);
/// @brief Gets a substring of another string.
/// @param text The text to split.
/// @param start The starting position.
/// @param length The length of the substring.
/// @return The sub string of text.
string get_substring(const string& text, const size_t start, const size_t length);
/// @brief Pads or truncates text to length using pad_character.
/// @param text The text to operate on.
/// @param length The desired length to make text.
/// @param pad_character The character to pad with.
/// @return The modified string.
string make_fit_l(const string& text, const size_t length, const char pad_character);
/// @brief Gets the leftmost length characters of text.
/// @param text The text to operate on.
/// @param length The maximum number of characters to return.
/// @return The leftmost n characters of text where n is the lesser of text.size and length.
string left(const string& text, const size_t length);
/// @brief Gets a string made by repeating a character.
/// @param length The length of the string to return.
/// @param ch The character to repeat.
/// @return The repeated string.
string string_dollar(const size_t length, const char ch);
} // End namespace SBF
/** @}*/
#endif // End !defined UTILS_H__

View File

@@ -32,37 +32,195 @@ TestResults main_test_Utils(int argc, char** argv) {
}
namespace Test::Utils {
//size_t get_index_of(const string& text, const string& search, size_t start);
TestResults test_get_index_of() {
string long_text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
return execute_suite<size_t, string, string, size_t>(make_test_suite(
"SBF::get_index_of",
get_index_of,
vector<TestTuple<size_t, string, string, size_t>>({
make_test<size_t, string, string, size_t>(
"should get {0, \"\"} for invalid rank id 0",
1,
make_tuple(string(""), string(""), size_t(0))),
"should get 0 for the first word in a string",
0,
make_tuple(long_text, string("Lorem"), size_t(0))),
make_test<size_t, string, string, size_t>(
"should get the location of a word in the larger string",
22,
make_tuple(long_text, string("dummy"), size_t(0))),
make_test<size_t, string, string, size_t>(
"should get -1 for the location of a word that is not int the string",
-1,
make_tuple(long_text, string("acid"), size_t(0))),
make_test<size_t, string, string, size_t>(
"should should get the location of a subsequent word within the string",
120,
make_tuple(long_text, string("dummy"), size_t(100))),
make_test<size_t, string, string, size_t>(
"should get -1 for the location of a word that is before start location in the string",
-1,
make_tuple(long_text, string("dummy"), size_t(long_text.size()))),
make_test<size_t, string, string, size_t>(
"should get the location of a word within a simple string",
6,
make_tuple(string("these are words"), string("are"), size_t(0))),
make_test<size_t, string, string, size_t>(
"should get the first location of a word within the string",
4,
make_tuple(string("one two one two"), string("two"), size_t(0))),
make_test<size_t, string, string, size_t>(
"should get the second location of a word within the string",
12,
make_tuple(string("one two one two"), string("two"), size_t(5))),
})
));
}
//string get_substring(const string& text, int32_t var1, int32_t var2);
TestResults test_get_substring() {
return TestResults();
return execute_suite<string, string, size_t, size_t>(make_test_suite(
"SBF::get_substring",
get_substring,
vector<TestTuple<string, string, size_t, size_t>>({
make_test<string, string, size_t, size_t>(
"should get an empty string if start is too big",
"",
make_tuple(string("asdf"), size_t(6), size_t(2))),
make_test<string, string, size_t, size_t>(
"should get an empty string if count is 0",
"",
make_tuple(string("asdf"), size_t(0), size_t(0))),
make_test<string, string, size_t, size_t>(
"should get a string that starts at 0",
"as",
make_tuple(string("asdf"), size_t(0), size_t(2))),
make_test<string, string, size_t, size_t>(
"should get the whole string",
"asdf",
make_tuple(string("asdf"), size_t(0), size_t(4))),
make_test<string, string, size_t, size_t>(
"should get a partial string if count is too big",
"asdf",
make_tuple(string("asdf"), size_t(0), size_t(8))),
make_test<string, string, size_t, size_t>(
"should get a substring",
"234",
make_tuple(string("1234567890"), size_t(1), size_t(3))),
make_test<string, string, size_t, size_t>(
"should get a word",
"Paris",
make_tuple(string("Where is Paris?"), size_t(9), size_t(5))),
})
));
}
//string left(const string& text, size_t length);
TestResults test_left() {
return TestResults();
return execute_suite<string, string, size_t>(make_test_suite(
"SBF::left",
SBF::left,
vector<TestTuple<string, string, size_t>>({
make_test<string, string, size_t>(
"should get a substring",
"Micro",
make_tuple(string("Microsoft QBasic"), size_t(5))),
make_test<string, string, size_t>(
"should get the whole string if length is equal to text.size()",
"Microsoft QBasic",
make_tuple(string("Microsoft QBasic"), size_t(16))),
make_test<string, string, size_t>(
"should get the whole string if length is greater than text.size()",
"Microsoft QBasic",
make_tuple(string("Microsoft QBasic"), size_t(20))),
make_test<string, string, size_t>(
"should get an empty string if length is 0",
"",
make_tuple(string("Microsoft QBasic"), size_t(0))),
make_test<string, string, size_t>(
"should get an empty string if text is empty",
"",
make_tuple(string(""), size_t(1))),
})
));
}
//string make_fit_l(const string& text, size_t length, char paddCh);
TestResults test_make_fit_l() {
return TestResults();
return execute_suite<string, string, int32_t, char>(make_test_suite(
"SBF::make_fit_l",
make_fit_l,
vector<TestTuple<string, string, int32_t, char>>({
make_test<string, string, int32_t, char>(
"should truncate a string that is too long",
"Micro",
make_tuple(string("Microsoft QBasic"), 5, 'A')),
make_test<string, string, int32_t, char>(
"should pad a string that is too short",
"MicroAAAAA",
make_tuple(string("Micro"), 10, 'A')),
make_test<string, string, int32_t, char>(
"should return a string that is perfectly sized",
"Micro",
make_tuple(string("Micro"), 5, 'A')),
make_test<string, string, int32_t, char>(
"should pad the string with spaces if padCh is the null character",
"Micro ",
make_tuple(string("Micro"), 10, '\0')),
make_test<string, string, int32_t, char>(
"should return a padded string even if text is empty",
"ZZZZZZZZZZ",
make_tuple(string(""), 10, 'Z')),
})
));
}
//string string_dollar(size_t length, char ch);
TestResults test_string_dollar() {
return TestResults();
return execute_suite<string, size_t, char>(make_test_suite(
"SBF::string_dollar",
string_dollar,
vector<TestTuple<string, size_t, char>>({
make_test<string, size_t, char>(
"should make a string",
"YYYYY",
make_tuple(size_t(5), 'Y')),
make_test<string, size_t, char>(
"should make a string of spaces if ch is the null character",
" ",
make_tuple(size_t(5), '\0')),
make_test<string, size_t, char>(
"should make an empty string if length is 0",
"",
make_tuple(size_t(0), 'C')),
})
));
}
//string word_wrap(const string& text, int maxWidth);
TestResults test_word_wrap() {
return TestResults();
return execute_suite<string, string, int32_t>(make_test_suite(
"SBF::word_wrap",
word_wrap,
vector<TestTuple<string, string, int32_t>>({
make_test<string, string, int32_t>(
"should return the string if it is shorter than max_width",
"0123_",
make_tuple(string("0123"), 5)),
make_test<string, string, int32_t>(
"should return the string if its length is equal to max_width",
"01234",
make_tuple(string("01234"), 5)),
make_test<string, string, int32_t>(
"should wrap a string to two lines if it has no whitespace",
"01234\n5____",
make_tuple(string("012345"), 5)),
make_test<string, string, int32_t>(
"should wrap a string to three lines if it has no whitespace",
"01234\n56789\n0____",
make_tuple(string("01234567890"), 5)),
make_test<string, string, int32_t>(
"should wrap a string with even spacing",
"01 23\n45 67\n89 01",
make_tuple(string("01 23 45 67 89 01"), 5)),
make_test<string, string, int32_t>(
"should collapse whitespace to a single space",
"01 34\n67 90\n23 56\n89___",
make_tuple(string("01 34 67 90 23 56 89 "), 5)),
// TODO: Treat newlines and tabs in text as spaces.
})
));
}
} // End namespace Test::Utils