Unit Tests - fixing broken tests

- The split tests are all broken after a recent change.  They need
  to be extended to accommodate the new split_minimal functions.
This commit is contained in:
Paul Beckingham 2009-11-29 22:41:55 -05:00
parent 8c5508de4b
commit b246fae889
3 changed files with 65 additions and 8 deletions

View file

@ -104,12 +104,19 @@ int main (int argc, char** argv)
t.is (items.size (), (size_t) 1, "split 'a' '-' -> 1 item"); t.is (items.size (), (size_t) 1, "split 'a' '-' -> 1 item");
t.is (items[0], "a", "split 'a' '-' -> 'a'"); t.is (items[0], "a", "split 'a' '-' -> 'a'");
split (items, unsplit, '-');
t.is (items.size (), (size_t) 1, "split 'a' '-' -> 1 item");
t.is (items[0], "a", "split 'a' '-' -> 'a'");
unsplit = "-"; unsplit = "-";
split (items, unsplit, '-'); split (items, unsplit, '-');
t.is (items.size (), (size_t) 2, "split '-' '-' -> '' ''"); t.is (items.size (), (size_t) 2, "split '-' '-' -> '' ''");
t.is (items[0], "", "split '-' '-' -> [0] ''"); t.is (items[0], "", "split '-' '-' -> [0] ''");
t.is (items[1], "", "split '-' '-' -> [1] ''"); t.is (items[1], "", "split '-' '-' -> [1] ''");
split (items, unsplit, '-');
t.is (items.size (), (size_t) 1, "split '-' '-' -> '-'");
unsplit = "-a-bc-def"; unsplit = "-a-bc-def";
split (items, unsplit, '-'); split (items, unsplit, '-');
t.is (items.size (), (size_t) 4, "split '-a-bc-def' '-' -> '' 'a' 'bc' 'def'"); t.is (items.size (), (size_t) 4, "split '-a-bc-def' '-' -> '' 'a' 'bc' 'def'");
@ -118,11 +125,21 @@ int main (int argc, char** argv)
t.is (items[2], "bc", "split '-a-bc-def' '-' -> [2] 'bc'"); t.is (items[2], "bc", "split '-a-bc-def' '-' -> [2] 'bc'");
t.is (items[3], "def", "split '-a-bc-def' '-' -> [3] 'def'"); t.is (items[3], "def", "split '-a-bc-def' '-' -> [3] 'def'");
split (items, unsplit, '-');
t.is (items.size (), (size_t) 3, "split '-a-bc-def' '-' -> 'a' 'bc' 'def'");
t.is (items[0], "a", "split '-a-bc-def' '-' -> [1] 'a'");
t.is (items[1], "bc", "split '-a-bc-def' '-' -> [2] 'bc'");
t.is (items[2], "def", "split '-a-bc-def' '-' -> [3] 'def'");
// void split (std::vector<std::string>& results, const std::string& input, const std::string& delimiter) // void split (std::vector<std::string>& results, const std::string& input, const std::string& delimiter)
unsplit = ""; unsplit = "";
split (items, unsplit, "--"); split (items, unsplit, "--");
t.is (items.size (), (size_t) 0, "split '' '--' -> 0 items"); t.is (items.size (), (size_t) 0, "split '' '--' -> 0 items");
split (items, unsplit, "--");
t.is (items.size (), (size_t) 1, "split '' '--' -> 1");
t.is (items[0], "", "split '' '--' -> [1] ''");
unsplit = "a"; unsplit = "a";
split (items, unsplit, "--"); split (items, unsplit, "--");
t.is (items.size (), (size_t) 1, "split 'a' '--' -> 1 item"); t.is (items.size (), (size_t) 1, "split 'a' '--' -> 1 item");

View file

@ -57,15 +57,33 @@ void wrapText (
void split ( void split (
std::vector<std::string>& results, std::vector<std::string>& results,
const std::string& input, const std::string& input,
const char delimiter, const char delimiter)
bool nontrivial /* = true */)
{ {
results.clear (); results.clear ();
std::string::size_type start = 0; std::string::size_type start = 0;
std::string::size_type i; std::string::size_type i;
while ((i = input.find (delimiter, start)) != std::string::npos) while ((i = input.find (delimiter, start)) != std::string::npos)
{ {
if (!nontrivial || i != start) results.push_back (input.substr (start, i - start));
start = i + 1;
}
if (input.length ())
results.push_back (input.substr (start, std::string::npos));
}
////////////////////////////////////////////////////////////////////////////////
void split_minimal (
std::vector<std::string>& results,
const std::string& input,
const char delimiter)
{
results.clear ();
std::string::size_type start = 0;
std::string::size_type i;
while ((i = input.find (delimiter, start)) != std::string::npos)
{
if (i != start)
results.push_back (input.substr (start, i - start)); results.push_back (input.substr (start, i - start));
start = i + 1; start = i + 1;
} }
@ -78,8 +96,7 @@ void split (
void split ( void split (
std::vector<std::string>& results, std::vector<std::string>& results,
const std::string& input, const std::string& input,
const std::string& delimiter, const std::string& delimiter)
bool nontrivial /* = true */)
{ {
results.clear (); results.clear ();
std::string::size_type length = delimiter.length (); std::string::size_type length = delimiter.length ();
@ -88,7 +105,28 @@ void split (
std::string::size_type i; std::string::size_type i;
while ((i = input.find (delimiter, start)) != std::string::npos) while ((i = input.find (delimiter, start)) != std::string::npos)
{ {
if (!nontrivial || i != start) results.push_back (input.substr (start, i - start));
start = i + length;
}
if (input.length ())
results.push_back (input.substr (start, std::string::npos));
}
////////////////////////////////////////////////////////////////////////////////
void split_minimal (
std::vector<std::string>& results,
const std::string& input,
const std::string& delimiter)
{
results.clear ();
std::string::size_type length = delimiter.length ();
std::string::size_type start = 0;
std::string::size_type i;
while ((i = input.find (delimiter, start)) != std::string::npos)
{
if (i != start)
results.push_back (input.substr (start, i - start)); results.push_back (input.substr (start, i - start));
start = i + length; start = i + length;
} }

View file

@ -38,8 +38,10 @@ std::string trimRight (const std::string& in, const std::string& t = " ");
std::string trim (const std::string& in, const std::string& t = " "); std::string trim (const std::string& in, const std::string& t = " ");
std::string unquoteText (const std::string&); std::string unquoteText (const std::string&);
void extractLine (std::string&, std::string&, int); void extractLine (std::string&, std::string&, int);
void split (std::vector<std::string>&, const std::string&, const char, bool nontrivial = true); void split (std::vector<std::string>&, const std::string&, const char);
void split (std::vector<std::string>&, const std::string&, const std::string&, bool nontrivial = true); void split (std::vector<std::string>&, const std::string&, const std::string&);
void split_minimal (std::vector<std::string>&, const std::string&, const char);
void split_minimal (std::vector<std::string>&, const std::string&, const std::string&);
void join (std::string&, const std::string&, const std::vector<std::string>&); void join (std::string&, const std::string&, const std::vector<std::string>&);
std::string commify (const std::string&); std::string commify (const std::string&);
std::string lowerCase (const std::string&); std::string lowerCase (const std::string&);